docker compose
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiNx8FesU2cfdGLwczX0xiRGZkRGZ0Xy9GbvNGL2EzXlpXazxSP9cHWElHc10UcMVjQykVNEdVW1EUaMVTQClGVF5UMR9Fd4VGdsATNfd3bkFGazxycykFaKdkYzZUbapXNXlleSdVY2pESa9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLyczN4AzM1MjM0ETOwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
docker compose容器編排
docker compose配置常用字段
關于docker compose && consul!!docker進階~~docker compose
docker compose常用指令
關于docker compose && consul!!docker進階~~docker compose
關于compose的部署
[[email protected] ~]# cp -p docker-compose /usr/local/bin/
[[email protected] ~]# chmod +x /usr/local/bin/docker-compose
[[email protected] compose_nginx]# vim docker-compose.yml
version: '3' //與docker版本相關
services:
nginx:
hostname: nginx //容器主機名
build:
context: ./nginx //指定建構鏡像的路徑
dockerfile: Dockerfile //Dockerfile檔案
ports: //暴露容器端口
- 1216:80
- 1217:443
networks: //加入網絡
- cluster
volumes: //挂載點
- ./wwwroot:/usr/local/nginx/html
networks:
cluster:
[[email protected] compose_nginx]# mkdir nginx
FROM centos:7
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && yum clean all
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
RUN ./configure --prefix=/usr/local/nginx &> /dev/null
RUN make &> /dev/null
RUN make install &> /dev/null
RUN rm -rf /mnt/nginx-1.12.2
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[[email protected] compose_nginx]# tree
.
├── docker-compose.yml
├── nginx
│ ├── Dockerfile
│ └── nginx-1.12.2.tar.gz
└── wwwroot
└── index.html
[[email protected] compose_nginx]# docker-compose -f docker-compose.yml up -d
[[email protected] compose_nginx]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------------------------------------------------
compose_nginx_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:1217->443/tcp,:::1217->443/tcp, 0.0.0.0:1216->80/tcp,:::1216->80/tcp
部署consul
consul的工作流程
在consul伺服器上
[[email protected] ~]# mkdir /root/consul
[[email protected] ~]# cp consul_0.9.2_linux_amd64.zip /root/consul
[[email protected] ~]# cd /root/consul/
[[email protected] consul]# ls
consul_0.9.2_linux_amd64.zip
[[email protected] consul]# unzip consul_0.9.2_linux_amd64.zip
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[[email protected] consul]# mv consul /usr/bin/
[[email protected] consul]# consul agent \
> -server \ //服務端
> -bootstrap \ //前端架構
> -ui \ //可被通路的web界面
> -data-dir=/var/lib/consul-data \
> -bind=192.168.1.101 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[[email protected] consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.1.101:8301 alive server 0.9.2 2 dc1
[[email protected] consul]# consul info | grep leader
leader = true
leader_addr = 192.168.1.101:8300
[[email protected] consul]# curl 127.0.0.1:8500/v1/status/peers //檢視叢集server成員
["192.168.1.101:8300"]
[[email protected] consul]# curl 127.0.0.1:8500/v1/status/leader //叢集Raf leader
"192.168.1.101:8300"
[[email protected] consul]# curl 127.0.0.1:8500/v1/catalog/services //注冊的所有服務
{"consul":[]}
[[email protected] consul]# curl 127.0.0.1:8500/v1/catalog/nginx //檢視nginx服務資訊
[[email protected] consul]# curl 127.0.0.1:8500/v1/catalog/nodes //叢集節點詳細資訊
[{"ID":"4c354aca-4b09-9ea0-9741-6d4e6298affc","Node":"consul-server01","Address":"192.168.1.101","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.1.101","wan":"192.168.1.101"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}]
容器服務自動加入consul叢集
[[email protected] ~]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> gliderlabs/registrator:latest \
> -ip=192.168.1.4 \
> consul://192.168.1.101:8500
[[email protected] ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
c1f71bfc180e2e785908853eee16f21724ff30c234414ed90131eb9355afa7ff
[[email protected] ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
fbb25e0d26941dbed3ee97a71936b8d4255b132c3b8d130cd8972ce20bd06e5c
[[email protected] ~]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
77fdb711e6064c675c746ef3ed23b3890505edd73441e94b8cad02f019806efc
[[email protected] ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
666d1c4405ba5af95c05f12d0aedfc64a1cf428a3e0a0214e686e07c32b998e3
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
666d1c4405ba httpd "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:89->80/tcp, :::89->80/tcp test-04
77fdb711e606 httpd "httpd-foreground" 19 seconds ago Up 18 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp test-03
fbb25e0d2694 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:84->80/tcp, :::84->80/tcp test-02
c1f71bfc180e nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:83->80/tcp, :::83->80/tcp test-01
[[email protected] consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}
upstream http_backend {
{{range service "nginx"}} //過濾nginx伺服器
server {{.Address}}:{{.Port}}; //此處引用的變量會指向後端的位址和端口(動态變化)
{{end}}
}
server {
listen 83;
server_name localhost 192.168.1.101; //反向代理的IP位址(前端展示web的ip)
access_log /var/log/nginx/test.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;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; //轉發位址
proxy_pass http://http_backend;
}
}
[[email protected] ~]# yum install -y gcc pcre-devel zlib-devel
[[email protected] ~]# tar zxf nginx-1.12.2.tar.gz -C /opt/
[[email protected] ~]# cd /opt/nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[[email protected] nginx-1.12.2]# make && make install
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; //添加虛拟主機目錄
default_type application/octet-stream;
[[email protected] ~]# mkdir /usr/local/nginx/conf/vhost
[[email protected] ~]# mkdir /var/log/nginx
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[[email protected] ~]# mv consul-template /usr/bin/
[[email protected] ~]# consul-template -consul-addr 192.168.1.101:8500 \
> -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info
2021/09/11 10:04:41.326881 [INFO] consul-template v0.19.3 (ebf2d3d)
2021/09/11 10:04:41.326899 [INFO] (runner) creating new runner (dry: false, once: false)
2021/09/11 10:04:41.327148 [INFO] (runner) creating watcher
2021/09/11 10:04:41.327324 [INFO] (runner) starting
2021/09/11 10:04:41.327341 [INFO] (runner) initiating run
2021/09/11 10:04:41.329281 [INFO] (runner) initiating run
2021/09/11 10:04:41.330282 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/09/11 10:04:41.330309 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/09/11 10:04:41.330399 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
[[email protected] vhost]# cat /usr/local/nginx/conf/vhost/test.conf
upstream http_backend {
server 192.168.1.4:83;
server 192.168.1.4:84;
}
server {
listen 83;
server_name localhost 192.168.1.101;
access_log /var/log/nginx/test.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;
}
}
[[email protected] ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx
0d3041dd7cd9c99b06c536dbeea19d7d8bb108be6a06269a45d35cc5f5c104da
2021/09/11 10:08:43.298860 [INFO] (runner) initiating run
2021/09/11 10:08:43.300198 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/09/11 10:08:43.300227 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/09/11 10:08:43.300263 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
[[email protected] vhost]# cat /usr/local/nginx/conf/vhost/test.conf
upstream http_backend {
server 192.168.1.4:83;
server 192.168.1.4:84;
server 192.168.1.4:85;
}
server {
listen 83;
server_name localhost 192.168.1.101;
access_log /var/log/nginx/test.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;
}
}
[[email protected] ~]# docker logs -f test-01
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/09/11 17:23:03 [notice] 1#1: using the "epoll" event method
2021/09/11 17:23:03 [notice] 1#1: nginx/1.21.1
2021/09/11 17:23:03 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/09/11 17:23:03 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2021/09/11 17:23:03 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/09/11 17:23:03 [notice] 1#1: start worker processes
2021/09/11 17:23:03 [notice] 1#1: start worker process 32
2021/09/11 17:23:03 [notice] 1#1: start worker process 33
2021/09/11 17:23:03 [notice] 1#1: start worker process 34
2021/09/11 17:23:03 [notice] 1#1: start worker process 35
[[email protected] ~]# docker logs -f test-02
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/09/11 17:23:17 [notice] 1#1: using the "epoll" event method
2021/09/11 17:23:17 [notice] 1#1: nginx/1.21.1
2021/09/11 17:23:17 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/09/11 17:23:17 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2021/09/11 17:23:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/09/11 17:23:17 [notice] 1#1: start worker processes
2021/09/11 17:23:17 [notice] 1#1: start worker process 32
2021/09/11 17:23:17 [notice] 1#1: start worker process 33
2021/09/11 17:23:17 [notice] 1#1: start worker process 34
2021/09/11 17:23:17 [notice] 1#1: start worker process 35
[[email protected] ~]# docker logs -f test-05
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/09/11 18:08:43 [notice] 1#1: using the "epoll" event method
2021/09/11 18:08:43 [notice] 1#1: nginx/1.21.1
2021/09/11 18:08:43 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/09/11 18:08:43 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2021/09/11 18:08:43 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/09/11 18:08:43 [notice] 1#1: start worker processes
2021/09/11 18:08:43 [notice] 1#1: start worker process 31
2021/09/11 18:08:43 [notice] 1#1: start worker process 32
2021/09/11 18:08:43 [notice] 1#1: start worker process 33
2021/09/11 18:08:43 [notice] 1#1: start worker process 34