本章結構
1.Docker網絡原理
2.Docker Compose容器編排(yml)
3.構建自動發現的Docker服務架構(羣集)
4.實現容器服務自動加入Nginx集羣
Docker網絡通信
Docker單機網絡拓撲圖:
端口映射:
1.端口映射機制將容器內的服務提供給外部網絡訪問
2.可隨機或指定映射端口範圍
docker run -d -P httpd:centos
docker run -d -p 49888:80 httpd:centos(兩個端口號)
容器互聯
1.在源容器和接收容器間建立一條網絡通信隧道
2.使用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容器編排
1.Docker Compose的前身是Fig,它是一個定義及運行多個Docker容器的工具
2.使用Docker Compose不再需要使用shell腳本來啓動容器
3.Docker Compose非常適合組合使用多個容器進行開發的場景
4.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`
5.Docker Compose文件結構
[root@localhost compose_ Inmp]# vim docker-compose.yml
6.YAML是一種標記語言很直觀的數據序列化格式
7.文件格式及編寫注意事項
1.不支持製表符tab鍵縮進,需要使用空格縮進
2.通常開頭縮進2個空格
3.字符後縮進1個空格,如冒號、逗號、橫槓
4.用井號註釋
5.如果包含特殊字符用單引號引起來
6.布爾值必須用引號括起來
Docker Compse配置常用字段:
字段 | 描述 |
---|---|
build dockerfile context | 指定Dockerfile文件名構建鏡像上下文路徑 |
image | 指定鏡像 |
command | 執行命令,覆蓋默認命令 |
container name | 指定容器名稱,由於容器名稱時唯一的,如果指定自定義名稱,則無法scale |
deploy | 指定部署和運行服務相關配置,只能在Swarm模式使用 |
environment | 添加環境變量 |
networks | 加入網絡 |
ports | 暴露容器端口,與-p相同,但端口不能低於60 |
volumes | 掛載宿主機路徑或命令卷 |
restart | 重啓策略,默認no,always,no -failure,unless-stoped |
hostname | 容器主機名 |
Docker Compse常用命令:
字段 | 描述 |
---|---|
build | 重新構建服務 |
ps | 列出容器 |
up | 創建和啓動容器 |
exec | 在容器裏面執行命令 |
scale | 指定一個服務容器啓動數量 |
top | 顯示容器進程 |
logs | 查看容器輸出 |
down | 刪除容器/網絡/數據卷和鏡像 |
stop/start/restart | 停止/啓動/重啓服務 |
Compose命令說明
基本的使用格式
docker-compose [options] [COMMAND] [ARGS...]
docker-compose選項
--verbose輸出更多調試信息
--version打印版本並退出
-f, --file FILE使用特定的compose模板文件,默認爲docker-compose.yml
-p, --project-name NAME指定項目名稱,默認使用目錄名稱
Docker consul容器服務更新與發現
容器服務更新與發現拓撲圖:
Demo:docker-compose部署
先安裝Docker引擎
[root@localhost ~]# yum install yum-utils device-mapper-persistent-data lvm2 -y
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum install -y docker-ce
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start docker.service
[root@localhost ~]# systemctl enable docker.service
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://w1ogxqvl.mirror.aliyuncs.com"]
}
EOF
[root@localhost ~]# echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.cnf
[root@localhost ~]# sysctl -p
[root@localhost ~]# service network restart
Restarting network (via systemctl): [ 確定 ]
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker pull centos:7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 5e35e350aded 6 weeks ago 203MB
查看版本操作:
[root@localhost ~]# mkdir /aaa
[root@localhost ~]# mount.cifs //192.168.10.189/rpm /aaa
Password for root@//192.168.10.189/rpm:
[root@localhost ~]# cd /aaa/compose/
[root@localhost compose]# ls
consul_0.9.2_linux_amd64.zip docker-compose
consul-template_0.19.3_linux_amd64.zip
[root@localhost compose]# cp -p docker-compose /usr/bin/
[root@localhost compose]# docker-compose -v #查看版本
docker-compose version 1.21.1, build 5a3f1a3
編排:
[root@localhost ~]# mkdir /root/compse_nginx
[root@localhost ~]# cd compse_nginx/
[root@localhost compse_nginx]# mkdir nginx #創建nginx工作目錄
[root@localhost compse_nginx]# cd nginx/
[root@localhost nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
[root@localhost nginx]# cd /aaa/tomcat/
[root@localhost tomcat]# ls
nginx-1.12.0.tar.gz nginx-1.6.0.tar.gz
[root@localhost tomcat]# cp nginx-1.12.0.tar.gz /root/compse_nginx/nginx/
[root@localhost tomcat]# cd /root/compse_nginx/nginx/
[root@localhost nginx]# vim Dockerfile
#基於基礎鏡像
FROM centos:7
#用戶信息
MAINTAINER this is nginx image <zhou>
#添加環境包
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#下載nginx軟件包
ADD nginx-1.12.0.tar.gz /usr/local/src
WORKDIR /usr/local/src
#指定工作目錄
WORKDIR nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
#指定http和https端口
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
:
#添加宿主機中run.sh到容器中
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
[root@localhost nginx]# ls
Dockerfile nginx-1.12.0.tar.gz run.sh
[root@localhost nginx]# cd ..
[root@localhost compse_nginx]# yum install tree -y
#安裝進程樹
[root@localhost compse_nginx]# tree ./ #查看進程樹
./
└── nginx #在nginx目錄下有以下內容
├── Dockerfile
├── nginx-1.12.0.tar.gz
└── run.sh
1 directory, 3 files
`但是還缺少compose編排工具腳本,還有站點(站點不需要手動創建,在compose編排工具中設定)`
[root@localhost compse_nginx]# vim docker-compose.yml
[root@localhost compse_nginx]# docker-compose -f docker-compose.yml up -d
......此處省略多行
Successfully built d331cacd6adc
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
Consul
Consul是HashiCorp公司推出的開源工具,用於實現分佈式系統的服務發現與配置
Consul的特性:
1.Consul支持健康檢查,允許存儲鍵值對
2.一致性協議採用Raft算法,用來保證服務的高可用
3.成員管理和消息廣播採用GOSSIP協議,支持ACL訪問控制
方便部署,與Docker等輕量級容器可無縫配合
構建自動發現的Docker服務架構
建立Consul服務
1.每個提供服務的節點上都要部署和運行Consul的agent
2.Consul agent有兩種運行模式
Server
Client
3.Server和Client只是Consul集羣層面的區分,與搭建在Cluster之上的應用服務無關
其中的命令:
-v 數據卷
-d 放在後臺執行
-i 標準性輸入
-t 分配僞終端
作用:做編排時需要修改配置文件,實現多配置文件同時修改
擴展性的文件放在vhost中,節偶的方式
Demo:consul羣集+template
CentOS 7-4:192.168.18.148做爲consul服務端:安裝Docker-ce,Compose3,consul,consul-template
CentOS 7-5:192.168.18.145做爲客戶端:安裝Docker-ce,registrator
CentOS 7-4上的操作:
[root@localhost ~]# mkdir consul
[root@localhost ~]# cd /aaa/docker/
[root@localhost docker]# ls
consul_0.9.2_linux_amd64.zip kibana-4.3.1-linux-x64.tar.gz
consul-template_0.19.3_linux_amd64.zip marathon-0.15.2.tgz
docker-compose mesos-0.25.0.tar.gz
harbor-offline-installer-v1.2.2.tgz zookeeper-3.4.6.tar.gz
[root@localhost docker]# cp consul_0.9.2_linux_amd64.zip /root/consul/
[root@localhost docker]# cd /root/consul/
[root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@localhost consul]# ls
consul consul_0.9.2_linux_amd64.zip
[root@localhost consul]# mv consul /usr/bin/
[root@localhost consul]# consul agent \ #使用代理功能
-server \ #提供server功能
-bootstrap \ #參與選舉
-ui \ #提供web訪問界面
-data-dir=/var/lib/consul-data \ #參數存儲位置飯在/var/lib/consul-data目錄下
-bind=192.168.18.148 \ #綁定本地地址
-client-0.0.0.0 \ #面對所有節點終端
-node=consul-server01 &> /var/log/consul.log & #混合輸出到目錄下個,運行放在後臺
[root@localhost consul]# jobs
[1]+ 運行中 consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.18.148 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &
[root@localhost consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.18.148:8301 alive server 0.9.2 2 dc1
#此處可以看到本地提供出來的端口爲8301
[root@localhost consul]# consul info | grep leader
leader = true
leader_addr = 192.168.18.148:8300
#此時leader爲自己本地端148
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/nodes #查看節點信息
[{"ID":"d14ca8a7-0fca-9a6b-09d1-2b62e425e4ac","Node":"consul-server01","Address":"192.168.18.148","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.18.148","wan":"192.168.18.148"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}]
CentOS 7-5上實現容器服務自動加入nginx羣集
`先安裝Docker引擎`
[root@localhost ~]# yum install yum-utils device-mapper-persistent-data lvm2 -y
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum install -y docker-ce
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start docker.service
[root@localhost ~]# systemctl enable docker.service
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://w1ogxqvl.mirror.aliyuncs.com"]
}
EOF
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker pull centos:7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 5e35e350aded 6 weeks ago 203MB
[root@localhost ~]# docker run -d \
--name=registrator \ #指定名稱
--net=host \ #指定主機網絡
-v /var/run/docker.sock:/temp/docker.sock \ #數據卷
--restart=always \ #重啓功能
gliderlabs/registrator:latest #指定鏡像加載
-ip=192.168.18.145 \ #指定本地的IP地址
consul://192.168.18.148:8500
`此時會去自動下載鏡像`
測試服務發現功能是否正常
[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 ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f65b37b2fb7 nginx "nginx -g 'daemon of…" 15 seconds ago Up 14 seconds 0.0.0.0:84->80/tcp test-02
62997fe8c92a nginx "nginx -g 'daemon of…" 17 seconds ago Up 16 seconds 0.0.0.0:83->80/tcp test-01
#此時會有兩個nginx容器處於Up狀態
[root@localhost ~]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
[root@localhost ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78945855fdaa httpd "httpd-foreground" 16 seconds ago Up 15 seconds 0.0.0.0:89->80/tcp test-04
1d02aa8ee0a0 httpd "httpd-foreground" 18 seconds ago Up 17 seconds 0.0.0.0:88->80/tcp test-03
7f65b37b2fb7 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:84->80/tcp test-02
62997fe8c92a nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:83->80/tcp test-01
#此時含兩個httpd容器,一共有四個容器處於Up狀態
驗證http和nginx服務是否註冊到consul
在宿主機的瀏覽器中輸入:192.168.18.148:8500,點擊NODES,再點擊consul-server01,此時會出現5個服務
驗證兩個Nginx容器是否能被訪問
分別在宿主機的瀏覽器訪問192.168.118.145的83和84端口
[root@localhost consul]# cd /root/consul/
[root@localhost consul]# vim nginx.ctmpl #nginx配置文件的模板
upstream http_backend { #服務器池的名稱
{{range service "nginx"}}
server {{.Address}:{{.Port}}; #調用變量:服務器節點的地址和端口
{{end}}
}
server {
listen 83;
server_name localhost 192.168.18.148; #本機的地址
access_log /var/log/nginx/kgc.cn-access.log;
index index.html index.php;
location / { #請求頭部信息的設定
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr; #後端服務器IP
proxy_set_header Client-IP $remote_addr; #client的IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #forward跳轉地址
proxy_pass http://http_backend; #請求跳轉給http_backend服務器池
}
}
[root@localhost consul]# cd /aaa/tomcat/
[root@localhost tomcat]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@localhost tomcat]# cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]# yum install gcc gcc-c++ pcre-devel zlib-devel -y
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
`在18行下插入以下內容`
include vhost/*.conf;
#此時還沒有這個文件,需要去創建
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/conf/
[root@localhost conf]# mkdir vhost #創建虛擬機目錄
[root@localhost conf]# ls
fastcgi.conf mime.types uwsgi_params
fastcgi.conf.default mime.types.default uwsgi_params.default
fastcgi_params nginx.conf vhost
`此時就有vhost文件了`
[root@localhost conf]# cd /var/log/
[root@localhost log]# mkdir nginx #創建日誌文件目錄
[root@localhost log]# ls
btmp messages tuned
btmp-20200102 messages-20200102 vmware-vgauthsvc.log.0
chrony nginx vmware-vmsvc.log
[root@localhost log]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#建立軟鏈接
[root@localhost log]# nginx #啓動nginx
[root@localhost log]# netstat -ntap | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29839/nginx: master
7-4上配置並啓用template
[root@localhost log]# cd /aaa/docker/
[root@localhost docker]# cp consul-template_0.19.3_linux_amd64.zip /root/
[root@localhost docker]# cd /root/
[root@localhost ~]# ls
anaconda-ks.cfg consul-template_0.19.3_linux_amd64.zip 模板 文檔 桌面
compose_nginx initial-setup-ks.cfg 視頻 下載
consul 公共 圖片 音樂
[root@localhost ~]# unzip consul-template_0.19.3_linux_amd64.zip #解壓
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@localhost ~]# mv consul-template /usr/bin/
`啓用模板`
[root@localhost ~]# consul-template -consul-addr 192.168.18.145:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
2020/01/03 11:56:31.069518 [INFO] consul-template v0.19.3 (ebf2d3d)
2020/01/03 11:56:31.069587 [INFO] (runner) creating new runner (dry: false, once: false)
2020/01/03 11:56:31.071008 [INFO] (runner) creating watcher
2020/01/03 11:56:31.072647 [INFO] (runner) starting
2020/01/03 11:56:31.072679 [INFO] (runner) initiating run
2020/01/03 11:56:31.073409 [ERR] (cli) /root/consul/nginx.ctmpl: parse: template: :3: unexpected "}" in operand