iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 6
0
DevOps

Cloud Native 開發維運一條龍系列 第 6

Day 6. 容器化的 Reverse Proxy 部署與設置 - 以 Nginx 為例

今天就來小試身手,用 Ansible 部署 Nginx,在 Cloud Native 的開發中,平台(Cloud Provider)也會有 Load Balancer ,像是 AWS 的 NLB、ALB,或是早些年的 ELB。這裡的 Nginx 的作用,是在平台提供的 LB 後面,作 App Layer 的導流與 Reverse Proxy,見下圖。


圖片來源

我們會把每一個微服務要用到的 docker 獨立出來寫作一個 Yaml 檔,這樣的好處是,當你在堡壘機(bastion)上,要作目標環境(e.g. staging, production)作操作時,操作的彈性比較大,一個簡單的部署範例如下

$ cat roles/tasks/run_nginx.yaml 
- name: File | Remove Directories
  file:
    path: '{{ item }}'
    state: absent
    owner: centos
    group: centos
  with_items:
    - "{{ prd_dir }}/nginx/cert"
    - "{{ prd_dir }}/nginx/conf"
    - "{{ prd_dir }}/nginx/html"
    - "{{ prd_dir }}/nginx/log"
- name: File | Create Directories
  file:
    path: '{{ item }}'
    state: directory
    owner: centos
    group: centos
  with_items:
    - "{{ prd_dir }}/nginx/cert"
    - "{{ prd_dir }}/nginx/conf"
    - "{{ prd_dir }}/nginx/html"
    - "{{ prd_dir }}/nginx/log"
- name: Copy | Copy Cert File to Remote
  copy:
    src: "{{ dev_dir }}/files/cert.d/{{ item }}"
    dest: "{{ prd_dir }}/nginx/cert"
  with_items:
    - fullchain.pem
    - privkey.pem
    - ssl-dhparam.pem
- name: Copy | Copy Config File to Remote
  copy:
    src: "{{ dev_dir }}/files/conf.d/nginx.conf"
    dest: "{{ prd_dir }}/nginx/conf"
- name: Copy | Copy Maintenance HTML to Remote
  copy:
    src: "{{ dev_dir }}/files/html.d/maintenance.html"
    dest: "{{ prd_dir }}/nginx/html"
- name: Docker | Start nginx.mytodos
  docker_container:
    networks_cli_compatible: yes
    name: nginx.mytodos
    state: started
    domainname: nginx.mytodos
    image:  nginx:1.14.2-alpine
    networks:
      - name: mynet
        ipv4_address: 172.20.0.2
    ports:
      - '443:443' # port_on_host:port_on_docker
    restart: true
    volumes:
      - /usr/share/zoneinfo/UTC:/etc/localtime:ro
      - /sys/fs/cgroup:/sys/fs/cgroup:ro
      - "{{ prd_dir }}/nginx/cert:/etc/nginx/cert.d:ro"
      - "{{ prd_dir }}/nginx/conf/:/etc/nginx/conf.d:ro"
      - "{{ prd_dir }}/nginx/html:/var/www/html:ro"
      - "{{ prd_dir }}/nginx/log:/var/log/nginx"

上一篇
Day 5. 用 Ansible 控制一切
下一篇
Day 7. 容器化的 Database 部署與管理 - 以 PostgreSQL 為例
系列文
Cloud Native 開發維運一條龍18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言