关于docker compose && consul!!docker进阶~~

docker compose

 docker compose容器编排

 docker compose配置常用字段

 

docker compose常用命令

 

关于compose的部署

[root@node1 ~]# cp -p docker-compose /usr/local/bin/
[root@node1 ~]# chmod +x /usr/local/bin/docker-compose 

[root@node1 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:

 

[root@node1 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;"]

 

[root@node1 compose_nginx]# tree
.
├── docker-compose.yml
├── nginx
│   ├── Dockerfile
│   └── nginx-1.12.2.tar.gz
└── wwwroot
    └── index.html

 

[root@node1 compose_nginx]# docker-compose -f docker-compose.yml up -d
[root@node1 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服务器上

[root@server ~]# mkdir /root/consul
[root@server ~]# cp consul_0.9.2_linux_amd64.zip /root/consul
[root@server ~]# cd /root/consul/
[root@server consul]# ls
consul_0.9.2_linux_amd64.zip
[root@server consul]# unzip consul_0.9.2_linux_amd64.zip 
Archive:  consul_0.9.2_linux_amd64.zip
  inflating: consul                  
[root@server consul]# mv consul /usr/bin/
[root@server 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 &

[root@server consul]# consul members
Node             Address             Status  Type    Build  Protocol  DC
consul-server01  192.168.1.101:8301  alive   server  0.9.2  2         dc1
[root@server consul]# consul info | grep leader
	leader = true
	leader_addr = 192.168.1.101:8300

 

[root@server consul]# curl 127.0.0.1:8500/v1/status/peers	//查看集群server成员
["192.168.1.101:8300"]
[root@server consul]# curl 127.0.0.1:8500/v1/status/leader	//集群Raf leader
"192.168.1.101:8300"
[root@server consul]# curl 127.0.0.1:8500/v1/catalog/services	//注册的所有服务
{"consul":[]}
[root@server consul]# curl 127.0.0.1:8500/v1/catalog/nginx		//查看nginx服务信息
[root@server 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集群

[root@apache ~]# 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

 

[root@apache ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
c1f71bfc180e2e785908853eee16f21724ff30c234414ed90131eb9355afa7ff
[root@apache ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
fbb25e0d26941dbed3ee97a71936b8d4255b132c3b8d130cd8972ce20bd06e5c
[root@apache ~]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
77fdb711e6064c675c746ef3ed23b3890505edd73441e94b8cad02f019806efc
[root@apache ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
666d1c4405ba5af95c05f12d0aedfc64a1cf428a3e0a0214e686e07c32b998e3
[root@apache ~]# 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

 

[root@server 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;
  }
}

[root@server ~]# yum install -y gcc pcre-devel zlib-devel 
[root@server ~]# tar zxf nginx-1.12.2.tar.gz -C /opt/
[root@server ~]# cd /opt/nginx-1.12.2/
[root@server nginx-1.12.2]# ./configure  --prefix=/usr/local/nginx
[root@server nginx-1.12.2]# make && make install

 

[root@server ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    include vhost/*.conf;		//添加虚拟主机目录
    default_type  application/octet-stream;

 

[root@server ~]# mkdir /usr/local/nginx/conf/vhost
[root@server ~]# mkdir /var/log/nginx
[root@server ~]# systemctl start nginx

 

[root@server ~]# unzip consul-template_0.19.3_linux_amd64.zip 
Archive:  consul-template_0.19.3_linux_amd64.zip
  inflating: consul-template         
[root@server ~]# mv consul-template /usr/bin/
[root@server ~]# 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

 

[root@server 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;
  }
}

 

[root@apache ~]# 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
[root@server 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;
  }
}

 

[root@apache ~]# 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
[root@apache ~]# 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
[root@apache ~]# 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

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值