Docker Compose 容器编排 + Consul 集群

网友投稿 262 2022-10-30

Docker Compose 容器编排 + Consul 集群

一、Docker Compose 容器编排

1、Docker Compose 前身是Fig,是一个定义及运行多个 Docker 容器的工具;2、使用 Docker Compose 不再需要使用 shell脚本来启动容器;3、Docker Compose 非常适合组合使用多个容器进行开发的场景。4、Docker Compose 的文件结构:

-----vim docker-compose.yml

二、Docker Compose 配置:

二、编排实例:

(1)先优化好网络:

vim /etc/sysctl.conf net.ipv4.ip_forward=1 ##文件末尾加入 sysctl -p ##修改生效 systemctl restart network ##重启网络服务

(2)将下载好的 docker-compose 复制到 /usr/bin/ 目录下:

cp -p docker-compose /usr/bin/

(3)创建工作目录(将nginx软件包拷贝到nginx目录下):

[root@localhost ~]# mkdir compose_nginx [root@localhost ~]# cd compose_nginx [root@localhost compose_nginx]# mkdir nginx [root@localhost compose_nginx]# cd nginx [root@localhost nginx]# vim Dockerfile FROM centos:7 RUN yum -y update RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make RUN useradd -M -s /sbin/nologin 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-&& make && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 EXPOSE 443 RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf ADD run.sh /run.sh RUN chmod 755 /run.sh CMD ["/run.sh"] [root@localhost nginx]# vim run.sh #!/bin/bash /usr/local/nginx/sbin/nginx

(4)创建编辑 yml 文件:

vim /root/compose_nginx/docker-compose.yml version: '3' services: nginx: hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - 1216:80 - 1217:443 networks: - abc volumes: - ./wwwroot:/usr/local/nginx/html networks: abc:

(5)此时,可以用 tree 命令,查看一下根目录下的文件结构:

[root@localhost compose_nginx]# tree ./ ##查看树形图 ./ ├── docker-compose.yml ##创建模板脚本 ├── nginx │ ├── Dockerfile ##创建容器脚本 │ ├── nginx-1.12.0.tar.gz ##源码包 │ └── run.sh ##服务脚本 └── ##站点

(6)执行开启:

docker-compose -f docker-compose.yml up -d

[root@localhost compose_nginx]# cd wwwroot/ [root@localhost vim index.html this is nginx new web

三、Docker concul 容器服务更新与发现:

consul 支持健康检查,允许存储键值对;一致性协议采用 Raft 算法,用来保证服务的高可用;成员管理和消息广播采用 GOSSIP 协议,支持 ACL 访问控制;

3、方便部署,与 Docker 等轻量级容器可无缝配合。(2)建立 Consul 服务:1、每个提高服务的节点上都需要部署和运行 consul 的 agent2、Consul agent 两种运行模式:

server;client;

3、server 与 client 只是 consul 群集层面的区分,与搭建在 cluster 之上的应用服务无关。

四、搭建 consul 集群:

[root@localhost vhost]# docker pull nginx //下载一个nginx镜像 [root@localhost vhost]# docker create -it nginx:latest /bin/bash //创建一个容器 be7904151f5d6cb110aba1aaa637dffeb8100c4d8761a1492e4b008dcd57d313 [root@localhost vhost]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES be7904151f5d nginx:latest "/bin/bash" 3 seconds ago Created xenodochial_black

(1)配置 consul 服务器:

[root@localhost ~]# mkdir consul [root@localhost abc1]# cp consul_0.9.2_linux_amd64.zip /root/consul [root@localhost abc1]# cd /root/consul [root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip ##解压 [root@localhost consul]# mv consul /usr/bin/ ##便于系统识别 建立 Consul 服务: consul agent \ -server \ -bootstrap \ -ui \ -data-dir=/var/lib/consul-data \ -bind=192.168.220.131 \ -client=0.0.0.0 \ -node=consul-server01 &> /var/log/consul.log &

[root@localhost consul]# consul members [root@localhost consul]# consul info | grep leader

[root@localhost consul]# curl 127.0.0.1:8500/v1/status/peers ##查看群集server成员 [root@localhost consul]# curl 127.0.0.1:8500/v1/status/leaders ##群集中 Raf leader [root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/services ##注册的所有服务 [root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/nodes ##群集节点详细信息 [root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/nginx ##查看 nginx 服务信息

(4)让容器服务自动加入 nginx 群集:

配置 192.168.220.140 节点: docker run -d \ --name=registrator \ --net=host \ -v /var/run/docker.sock:/tmp/docker.sock \ --restart=always \ gliderlabs/registrator:latest \ -ip=192.168.220.140 \ consul://192.168.220.131:8500

创建两个容器,分别为test-01和test02,指定端口号为83和84: [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

是基于 Consul 的自动替换配置文件的应用;可以查询 Consul 中的服务目录:Key、Key-values等;特别适合动态的创建配置文件;是一个守护进程,用于实时查询 consul 集群信息;

2、准备 template nginx 模板文件:

//在 consul 服务器上操作 创建一个模板文件: vim /root/consul/nginx.ctmpl upstream { {{range service "nginx"}} server {{.Address}}:{{.Port}}; {{end}} } server { listen 1216; server_name localhost 192.168.220.131; 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; proxy_set_header Client-IP $remote_addr; proxy_set_header X-Fprwarded-For $proxy_add_x_forwarded_for; proxy_pass http://http_backend; } }

yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y tar zxvf nginx-1.12.0.tar.gz -C /opt/ ./configure --prefix=/usr/local/nginx make && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

4、配置 nginx :

[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf 在 模板添加虚拟主机目录: { include mime.types; include vhost/*.conf; ##添加虚拟主机目录 default_type application/octet-stream; }

//创建虚拟主机目录: [root@localhost nginx-1.12.0]# mkdir /usr/local/nginx/conf/vhost //创建日志文件目录: [root@localhost nginx-1.12.0]# mkdir /var/log/nginx //启动 nginx [root@localhost nginx-1.12.0]# /usr/local/nginx/sbin/nginx [root@localhost nginx-1.12.0]# netstat -natp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 65476/nginx: master

(8)配置并启动 template:

1、解压、并复制到 /bin目录下,方便直接使用: [root@localhost abc]# unzip consul-template_0.19.3_linux_amd64.zip [root@localhost abc]# mv consul-template /usr/bin/

consul-template -consul-addr 192.168.220.131:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info

[root@localhost ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx bdc51a5c59e68c032e7466494fcb0212bae48fe939325845e00abb4840d0b48e

[root@localhost ~]# docker logs -f test-01 192.168.220.131 - - [03/Jan/2020:15:01:57 +0000] "GET / HTTP/1.0" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" 192.168.220.131 - - [03/Jan/2020:15:02:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" 192.168.220.131 - - [03/Jan/2020:15:02:20 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" [root@localhost ~]# docker logs -f test-02 192.168.220.131 - - [03/Jan/2020:15:02:18 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" 192.168.220.131 - - [03/Jan/2020:15:02:21 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" 192.168.220.131 - - [03/Jan/2020:15:02:24 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" 192.168.220.131 - - [03/Jan/2020:15:02:28 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" [root@localhost ~]# docker logs -f test-05 192.168.220.131 - - [03/Jan/2020:15:01:57 +0000] "GET /favicon.ico HTTP/1.0" 404 153 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" 192.168.220.131 - - [03/Jan/2020:15:02:19 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" 192.168.220.131 - - [03/Jan/2020:15:02:22 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-" 192.168.220.131 - - [03/Jan/2020:15:02:26 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"

可以,看出都是来自192.168.220.131的访问,都会被以轮询的方式发送给后台 docker 进行处理,实现了负载均衡。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:RabbitMQ 延迟队列实现订单支付结果异步阶梯性通知(实例代码)
下一篇:基于12位串行ADC与PC之间的接口电路设计
相关文章

 发表评论

暂时没有评论,来抢沙发吧~