文章目錄
- 一:Doker網絡通信
- 二:Docker Compose容器編排
- 2.1 Docker Compose 容器編排概述
- 2.2 Docker Compose環境準備
- 2.3 查看Docker Compose文件結構
- 2.4 Docker Compose配置常用字段
- 2.5 compose 常用操作命令
- 2.6 Compose命令說明
- 2.7 演示使用Docker-compose創建nginx
- 2.7.1 下載compose
- 2.7.1 創建nginx
- 三:Docker consul容器服務更新與發現
- 四:演示構建自動發現的Docker服務架構
- 五:實現容器服務自動加入nginx集羣
- 5.1 安裝gliderlabs/registrator gliderlabs/registrator
- 5.2 在192.168.247.142節點即操作
- 5.3 測試服務發現功能是否正常
- 5.4 驗證後端http服務是否註冊到consul節點
- 5.5 測試網頁是否開啓
- 5.6 查看對應容器的日誌文件
- 5.7 再次查看狀態
- 六 consul-template實現容器自動加入Nginx集羣
- 6.1 在consul節點安裝consul-template,準備模板文件
- 6.2 consul節點編譯安裝nginx
- 6.3 配置nginx然後啓動
- 6.4 準備工作完畢,啓動template,指定template模板文件及生成路徑
- 6.5 然後打開另一個終端查看生成配置文件
- 6.6 在客戶端測試並結合nginx後端容器節點logs驗證
- 七:consul-template測試加入新容器是否會被發現
docker網絡原理
docker Compose容器編排
構建自動發現的Docker服務架構
實現容器服務自動加入nginx集羣
一:Doker網絡通信
1.1Docker單機網絡拓撲圖
1.2 端口映射
- 端口映射機制將容器內的服務提供給外部網絡訪問
- 可隨機或者指定映射端口
docker run -d -P httpd:centos
docker run -d -p 60000:80 httpd:centos
1.3 容器互聯
- 在源容器和接收容器間建立一條網絡通信隧道
- 使用docker run --link 選項實現容器間互連通信
實現容器互聯命令
docker run -d -P --name web1 httpd:centos
docker run -d -P --name web2 --link web1:web1 httpd:centos
docker exec -it web2 /bin/bash
ping web1
二:Docker Compose容器編排
2.1 Docker Compose 容器編排概述
-
Docker Compose的前身是Fig,它是一個定義及運行多個Docker容器的工具
-
使用Docker Compose 不再需要使用shell腳本來啓動容器
-
Docker Compose非常適合組合使用多個容器進行開發的場景
備註:淺析Dockerfile與Compose之間區別
compose不僅可以給構建鏡像,還可以自動啓動
dockerfile只提供鏡像
dockerfile一次只能執行一個鏡像
compose可以執行多個,他的文件結尾時yaml
2.2 Docker Compose環境準備
[root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# docker-compose-v
2.3 查看Docker Compose文件結構
[root@localhost compose_lnmp]# vim docker-compose.yml
YAML是一種標記語言很直觀的數據序列化格式
文件格式及編寫注意事項
- 不支持製表符tab鍵縮進,需要使用空格縮進
- 通常開頭縮進2個空格
- 字符後縮進1個空格,如:冒號、,逗號、-橫槓
- 用#號註釋
- 如果包含特殊字符用單引號引起來
- 布爾值必須用引號括起來
2.4 Docker Compose配置常用字段
序號 | 字段 | 描述 |
---|---|---|
1 | build dockerfile context | 指定Dockerfile文件名構建鏡像上下文路徑 |
2 | image | 指定鏡像 |
3 | command | 執行命令,覆蓋默認命令 |
4 | container name | 指定容器名稱,由於容器名稱是唯一的,如果自定自定義名稱,則無法scale |
5 | deploy | 指定部署和運行服務相關配置,只能在Swam模式使用 |
6 | environment | 添加環境變量 |
7 | network | 加入網絡 |
8 | ports | 暴露容器端口,與-p相同,注意端口不能低於60 |
9 | volumes | 掛載宿主機路徑或命令卷 |
10 | restart | 重啓策略,默認no,always,no-failure,unless-stopped |
11 | hostname | 容器主機名 |
2.5 compose 常用操作命令
序號 | 字段 | 描述 |
---|---|---|
1 | build | 重新構建服務 |
2 | ps | 列出容器 |
3 | up | 創建和啓動容器 |
4 | exec | 在容器裏面執行命令 |
5 | scale | 指定一個服務容器啓動數量,可以理解爲副本數,一次性創建容器的個數 |
6 | top | 顯示容器進程 |
7 | logs | 查看容器輸出 |
8 | down | 刪除容器、網絡、數據卷和鏡像 |
9 | stop、start、restart | 停止、啓動、重啓服務 |
2.6 Compose命令說明
- 基本的使用格式
docker-compose [選項] [命令] [ARGS]
- docker-compose選項
–version 打印版本並退出
–verbose 輸出更多調試信息
-f ,–file FILE 使用特定的compose模板文件,默認爲docker-compose.yml
-p , --project-name NAME 指定項目名稱,默認使用目錄名稱
2.7 演示使用Docker-compose創建nginx
首先要記得先部署好環境
所有主機都安裝docker環境(內容爲docker基礎)
yum install compose -y
具體可見之前博客
2.7.1 下載compose
[root@ct ~]# curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 638 100 638 0 0 657 0 --:--:-- --:--:-- --:--:-- 657
100 10.3M 100 10.3M 0 0 1245k 0 0:00:08 0:00:08 --:--:-- 1893k
[root@ct ~]# chmod +x /usr/local/bin/docker-compose
2.7.1 創建nginx
[root@ct ~]# mkdir /root/compose_nginx
[root@ct ~]# cd /root/compose_nginx/
[root@ct compose_nginx]#
[root@ct compose_nginx]# mkdir nginx
[root@ct compose_nginx]#
[root@ct compose_nginx]# cp /abc/LNMP-C7/LNMP-C7/nginx-1.12.2.tar.gz /root/compose_nginx/nginx/
[root@ct compose_nginx]# vim /root/compose_nginx/nginx/Dockerfile
FROM centos:7
MAINTAINER to finsh nginx
RUN yum -y update
RUN yum -y install gcc gcc-c++ pcre* make cmake zlib-devel openssh* net-tools lsof telnet passwd vim
ADD nginx-1.12.2.tar.gz /usr/local/src
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/src/nginx-1.12.2
RUN (./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module)
RUN make && make install
ENV PATH /usr/local/nginx/sbin/:$PATH
#RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
#指關閉守護進程啓動
CMD ["nginx"]
[root@ct compose_nginx]# mkdir /root/compose_nginx/wwwroot
[root@ct compose_nginx]# echo "this is gsy" > /root/compose_nginx/wwwroot/index.html
[root@ct compose_nginx]# vim /root/compose_nginx/docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80
- 1217:443
networks:
- cluster
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
cluster:
[root@ct compose_nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@ct compose_nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
systemctl new a2c3616f15d1 34 hours ago 717MB
[root@ct compose_nginx]# tree /root/compose_nginx/
/root/compose_nginx/
├── docker-compose.yml
├── nginx
│ ├── Dockerfile
│ └── nginx-1.12.2.tar.gz
└── wwwroot
└── index.html
2 directories, 4 files
[root@ct compose_nginx]# docker-compose -f docker-compose.yml up -d
Successfully built 1935a8940906
Successfully tagged compose_nginx_nginx:latest
WARNING: Image for service nginx was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating compose_nginx_nginx_1 ... done
[root@ct compose_nginx]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
compose_nginx_nginx_1 nginx Up 0.0.0.0:1217->443/tcp, 0.0.0.0:1216->80/tcp
[root@ct compose_nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a91757d1dabf compose_nginx_nginx "nginx" 29 seconds ago Up 28 seconds 0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp compose_nginx_nginx_1
[root@ct compose_nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_nginx_nginx latest e3abfd076454 37 seconds ago 726MB
centos 7 5e35e350aded 5 months ago 203MB
Warning翻譯如下
警告:爲服務nginx構建的映像,因爲它還不存在。要重建此映像,您必須使用“docker-compose build”或“docker-compose up—build”。
創建compose_nginx_nginx_1……完成
2.7.2 驗證docker-compose創建的nginx
三:Docker consul容器服務更新與發現
consul可以算是微服務中的內容
3.1 容器服務更新發現拓撲圖
consul template 相當於配置文件模板
consul server會根據這個進行更新
regisrator註冊機制
當後面增加了一個容器時,容器會註冊registrator,
registrator會發現多了一個容器,便會通知consul server要更新
consul server使用consul template自動更新
3.2 Consul概述
Consul時HashiCorp公司推出的開源工具,用於實現分佈式系統的服務發現與配置
Consul的特性
- Consul支持健康檢查,允許存儲鍵值對
- 一致性協議採用Raft算法用來保證服務的高可用
- 成員管理和罅隙廣播採用GOSSIP協議,支持ACL訪問控制
方便部署,與Docker等輕量級容器可無縫配合
四:演示構建自動發現的Docker服務架構
4.1 建立Consul服務注意事項
- 每個提供服務的節點上都要部署和運行Consul的agent
- Consul agent有兩種運行模式
- Server
- Client
- Server和Clinet只是Consul集羣層面的羣分,與搭建在Cluster之上的應用服務無關
4.2 主節點建立Consul服務,即consul agent ——server
先介紹下環境
192.168.247.20 Docker-ce、Compose 3、Consul、Consul-template
192.168.247.142 Docker-ce、registrator
consul使用go語言編寫的
[root@ct compose_nginx]# mkdir /root/consul
[root@ct compose_nginx]# cd /root/consul/
[root@ct consul]# ls
[root@ct consul]#
[root@ct consul]# cp /abc/consul_0.9.2_linux_amd64.zip /root/consul/
[root@ct consul]# ls
consul_0.9.2_linux_amd64.zip
[root@ct consul]# unzip consul_0.9.2_linux_amd64.zip
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@ct consul]# mv consul /usr/bin/
[root@ct consul]# consul agent \
> -server \
> -bootstrap \ //前端框架
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.247.20 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log&
[1] 3339
[root@ct consul]# jobs
[1]+ Running consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.247.20 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &
[root@ct consul]#
4.3 查看集羣信息
[root@ct consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.247.20:8301 alive server 0.9.2 2 dc1
[root@ct consul]# consul info | grep leader
leader = true
leader_addr = 192.168.247.20:8300
備註:查看consul info
[root@ct consul]# consul info
agent:
check_monitors = 0
check_ttls = 0
checks = 0
services = 0
build:
prerelease =
revision = 75ca2ca
version = 0.9.2
consul:
bootstrap = true
known_datacenters = 1
leader = true
leader_addr = 192.168.247.20:8300
server = true
raft:
applied_index = 10
commit_index = 10
fsm_pending = 0
last_contact = 0
last_log_index = 10
last_log_term = 2
last_snapshot_index = 0
last_snapshot_term = 0
latest_configuration = [{Suffrage:Voter ID:192.168.247.20:8300 Address:192.168.247.20:8300}]
latest_configuration_index = 1
num_peers = 0
protocol_version = 2
protocol_version_max = 3
protocol_version_min = 0
snapshot_version_max = 1
snapshot_version_min = 0
state = Leader
term = 2
runtime:
arch = amd64
cpu_count = 4
goroutines = 61
max_procs = 4
os = linux
version = go1.8.3
serf_lan:
coordinate_resets = 0
encrypted = false
event_queue = 1
event_time = 2
failed = 0
health_score = 0
intent_queue = 0
left = 0
member_time = 1
members = 1
query_queue = 0
query_time = 1
serf_wan:
coordinate_resets = 0
encrypted = false
event_queue = 0
event_time = 1
failed = 0
health_score = 0
intent_queue = 0
left = 0
member_time = 1
members = 1
query_queue = 0
query_time = 1
4.4 通過http api獲取集羣信息
curl 127.0.0.1:8500/v1/status/peers //查看集羣server成員
curl 127.0.0.1:8500/v1/status/leaders //查看集羣Raf leader
curl 127.0.0.1:8500/v1/catalog/services //查看註冊的所有服務
curl 127.0.0.1:8500/v1/catalog/nginx //查看nginx服務的信息
curl 127.0.0.1:8500/v1/catalog/nodes //集羣節點詳細信息
[root@ct consul]# curl 127.0.0.1:8500/v1/status/peers //查看集羣server成員
["192.168.247.20:8300"][root@ct consul]#
[root@ct consul]# curl 127.0.0.1:8500/v1/status/leaders //查看集羣Raf leader
[root@ct consul]# curl 127.0.0.1:8500/v1/catalog/services //查看註冊的所有服務
{"consul":[]}[root@ct consul]#
[root@ct consul]# curl 127.0.0.1:8500/v1/catalog/nginx //查看nginx服務的信息
[root@ct consul]# curl 127.0.0.1:8500/v1/catalog/nodes //集羣節點詳細信息
[{"ID":"3b8f315c-cb49-0a15-9869-a846f5d2d6fd","Node":"consul-server01","Address":"192.168.247.20","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.247.20","wan":"192.168.247.20"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}][root@ct consul]#
五:實現容器服務自動加入nginx集羣
5.1 安裝gliderlabs/registrator gliderlabs/registrator
- 檢查容器運行狀態
- 自動註冊和註銷docker容器的服務到服務配置中心
- 目前支持consul、Etcd和skyDNS2
5.2 在192.168.247.142節點即操作
[root@localhost ~]# docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.247.142 \
consul://192.168.247.20:8500
查看狀態
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gliderlabs/registrator latest 3b59190c6c80 4 years ago 23.8MB
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0fdcf13a88bf gliderlabs/registrator:latest "/bin/registrator -i…" 49 seconds ago Restarting (1) 20 seconds ago registrator
[root@localhost ~]#
5.3 測試服務發現功能是否正常
在registrator節點處創建容器
-h 指定容器的主機名
[root@localhost ~]# docker run -itd -p 83:80 --name test-01 -h test01 nginx
[root@localhost ~]# docker run -itd -p 84:80 --name test-02 -h test02 nginx
[root@localhost ~]# docker run -itd -p 85:80 --name test-03 -h test03 httpd
[root@localhost ~]# docker run -itd -p 86:80 --name test-04 -h test04 httpd
查看容器狀態
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db4b5d76c22e httpd "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:86->80/tcp test-04
5794680892c4 httpd "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:85->80/tcp test-03
3db53dacc006 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:84->80/tcp test-02
d7ef2151f770 nginx "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 0.0.0.0:83->80/tcp test-01
0fdcf13a88bf gliderlabs/registrator:latest "/bin/registrator -i…" 8 minutes ago Restarting (1) Less than a second ago registrator
此時到consul節點,查看8500端口已被打開(之前並沒有出現)
[root@ct consul]# netstat -natp | grep 8500
tcp6 0 0 :::8500 :::* LISTEN 3339/consul
You have new mail in /var/spool/mail/root
5.4 驗證後端http服務是否註冊到consul節點
需要關閉防火牆或者開放8500端口,關閉核心防護
#systemctl stop firewalld
[root@ct consul]# firewall-cmd --get-active-zones
public
interfaces: eth0 eth1
[root@ct consul]# firewall-cmd --zone=public --add-port=8500/tcp
#此方法是臨時修改,如果永久修改,需要--permanent然後firewall-cmd --reload
success
[root@ct consul]# setenforce ?
setenforce: SELinux is disabled
5.5 測試網頁是否開啓
5.6 查看對應容器的日誌文件
發現訪問IP是物理網卡的網關
[root@localhost ~]# docker logs -f test-01
192.168.247.1 - - [22/Apr/2020:06:44:07 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36" "-"
2020/04/22 06:44:07 [error] 6#6: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.247.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.247.142:83", referrer: "http://192.168.247.142:83/"
192.168.247.1 - - [22/Apr/2020:06:44:07 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.247.142:83/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36" "-"
5.7 再次查看狀態
[root@ct ~]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}[root@ct ~]#
六 consul-template實現容器自動加入Nginx集羣
- 是基於consul的自動替換配置文件的應用
- 可以查詢consul中的服務目錄、key、key-values等
- 特別適合動態創建配置文件
consul-template是一個守護進程,用來實時查詢consul集羣信息,並更新文件系統上任意數量的指定模板,生成配置文件;更新完成以後,可以選擇運行shell命令執行更新操作,重新加載nginx
可以查詢consul中的服務目錄、key、key、value等
這種強大的抽象功能和查詢語言模板功能讓consul-template特別適合動態的創建配置文件
例如:創建apache/nginx proxy balacers、haproxy backends
6.1 在consul節點安裝consul-template,準備模板文件
備註:將腳本文件移動到/usr/bin下
[root@ct ~]# cp /abc/consul-template_0.19.3_linux_amd64.zip .
[root@ct ~]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@ct ~]# ls
anaconda-ks.cfg compose_nginx consul consul-template consul-template_0.19.3_linux_amd64.zip
[root@ct ~]# mv consul-template /usr/bin/
- 準備template nginx模板文件
備註:
此模板用於nginx反向代理模板
nginx.ctmpl跟nginx沒有直接關係,
consul是docker的一種自動管理機制
nginx.ctmpl中的參數以變量的形式寫入
[root@ct nginx-1.12.2]# mkdir /var/log/nginx/
[root@ct ~]# vim /root/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 88;
server_name ct 192.168.247.20;
access_log /var/log/nginx/gsy.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
6.2 consul節點編譯安裝nginx
[root@ct consul]# yum install gcc gcc-c++ make expat* pcre* perl* zlib* -y
[root@ct consul]# cp /abc/LNMP-C7/LNMP-C7/nginx-1.12.2.tar.gz .
[root@ct consul]# tar xf nginx-1.12.2.tar.gz
[root@ct consul]# ls
consul_0.9.2_linux_amd64.zip nginx-1.12.2 nginx-1.12.2.tar.gz nginx.ctmpl
[root@ct nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@ct nginx-1.12.2]# make && make install
6.3 配置nginx然後啓動
[root@ct nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost/
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
include vhost/*.conf; //添加虛擬主機配置文件路徑
[root@ct nginx-1.12.2]# /usr/local/nginx/sbin/nginx
[root@ct nginx-1.12.2]# netstat -natp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 77583/nginx: master
[root@ct consul]# firewall-cmd --zone=public --add-port=80/tcp
success
6.4 準備工作完畢,啓動template,指定template模板文件及生成路徑
指定模板路徑,/root/consul/nginx.ctmpl,生成到/usr/locla/nginx/conf/vhost/gsy.conf,然後重載nginx -s reload
[root@ct conf.d]# consul-template -consul-addr 192.168.247.20:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/gsy.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
輸入這段指定後便會進入監控狀態
6.5 然後打開另一個終端查看生成配置文件
獲取到IP,是依靠consul功能
[root@ct nginx]# cd -
/usr/local/nginx/conf/vhost
[root@ct vhost]# ls
gsy.conf
[root@ct vhost]# vim gsy.conf
upstream http_backend {
server 192.168.247.142:83;
server 192.168.247.142:84;
}
server {
listen 88;
server_name ct 192.168.247.20;
access_log /var/log/nginx/gsy.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
6.6 在客戶端測試並結合nginx後端容器節點logs驗證
- 如果能訪問且後端容器節點logs互爲輪詢說明服務已自動發現及更新配置文件完畢
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db4b5d76c22e httpd "httpd-foreground" 23 hours ago Up 23 hours 0.0.0.0:86->80/tcp test-04
5794680892c4 httpd "httpd-foreground" 23 hours ago Up 23 hours 0.0.0.0:85->80/tcp test-03
3db53dacc006 nginx "nginx -g 'daemon of…" 24 hours ago Up 24 hours 0.0.0.0:84->80/tcp test-02
d7ef2151f770 nginx "nginx -g 'daemon of…" 24 hours ago Up 24 hours 0.0.0.0:83->80/tcp test-01
0fdcf13a88bf gliderlabs/registrator:latest "/bin/registrator -i…" 24 hours ago Up 23 hours registrator
[root@localhost ~]# docker logs -f test-01
[root@localhost ~]# docker logs -f test-02 //新開一個終端
在consul節點開放88端口
[root@ct consul]# firewall-cmd --zone=public --add-port=88/tcp
success
訪問一次
再訪問一次
多次下來可以發現到是輪詢訪問後方docker容器的,若想驗證更加明顯,可以在兩個容器內添加不同的首頁
七:consul-template測試加入新容器是否會被發現
當前容器數量(已刪掉之前的httpd容器)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3db53dacc006 nginx "nginx -g 'daemon of…" 24 hours ago Up 24 hours 0.0.0.0:84->80/tcp test-02
d7ef2151f770 nginx "nginx -g 'daemon of…" 24 hours ago Up 24 hours 0.0.0.0:83->80/tcp test-01
0fdcf13a88bf gliderlabs/registrator:latest "/bin/registrator -i…" 24 hours ago Up 24 hours registrator
添加容器
[root@localhost ~]# docker run -itd --name test-03 -p 86:80 -h test03 nginx
c1fee05a5f45734b32816f2acd9717b61c833ed7d9a9fe6f638ff08e0b5466f4
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1fee05a5f45 nginx "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds 0.0.0.0:86->80/tcp test-03
3db53dacc006 nginx "nginx -g 'daemon of…" 24 hours ago Up 24 hours 0.0.0.0:84->80/tcp test-02
d7ef2151f770 nginx "nginx -g 'daemon of…" 24 hours ago Up 24 hours 0.0.0.0:83->80/tcp test-01
0fdcf13a88bf gliderlabs/registrator:latest "/bin/registrator -i…" 24 hours ago Up 24 hours registrator
consul-template監控到變化
查看consul節點指定的額外配置文件,發現地址池多了一個IP
upstream http_backend {
server 192.168.247.142:83;
server 192.168.247.142:84;
server 192.168.247.142:86;
}
server {
listen 88;
server_name ct 192.168.247.20;
access_log /var/log/nginx/gsy.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
驗證IP是否可用
刷新了三次,test-03日誌出現消息,證實可用