部署Docker consul

实验拓扑

在这里插入图片描述

实验目的

实现动态的添加web容器、负载均衡

实验参数

VMware中两台centos虚拟机,需要docker环境
14.0.0.107作为consul服务器
14.0.0.110作为web服务器

实验过程

创建consul集群

[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# mkdir consul
[root@localhost ~]# chmod +x docker-compose 
[root@localhost ~]# mv docker-compose /usr/local/bin/
[root@localhost ~]# cd consul/     ##进入consul目录中,将软件包拷贝到当前目录下
[root@localhost consul]# ls
consul_0.9.2_linux_amd64.zip  consul-template_0.19.3_linux_amd64.zip
[root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip     ##解压consul软件包
Archive:  consul_0.9.2_linux_amd64.zip
  inflating: consul                  
[root@localhost consul]# mv consul /usr/local/bin/    ##移动到指定目录下便于系统识别
[root@localhost consul]# consul agent \       ##设置代理
-server \           ##服务功能
-bootstrap \        ##一个前端框架、选举功能
-ui \               ##web界面
-data-dir=/var/lib/consul-data \    ##agent数据存放的目录
-bind=14.0.0.107 \                  ##绑定本地服务器的IP
-client=0.0.0.0 \                   ##监听所有网段地址,如果后端在一个网段下,也可以只监听那一个网段
-node=consul-server01 &>/var/log/consul.log &   ##在自己本地定义一个名称,并生成日志到指定文件中     
[1] 83863
[root@localhost consul]# jobs
[1]+  运行中               consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=14.0.0.107 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &

查看consul集群状态

[root@localhost consul]# consul members
Node             Address          Status  Type    Build  Protocol  DC
consul-server01  14.0.0.107:8301  alive   server  0.9.2  2         dc1
[root@localhost consul]# consul info     ##查看更加详细的信息
[root@localhost consul]# consul info | grep leader    ##查看谁是主服务器
	leader = true
	leader_addr = 14.0.0.107:8300

通过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    ##集群节点详细信息

在这里插入图片描述

容器服务自动加入集群

  1. 安装 Gliderlabs/Registrator Gliderlabs/Registrator
    可检查容器运行状态自动注册,还可注销docker容器的服务到服务配置中心。
    目前支持Consul、Etcd和SkyDNS2.
    在14.0.0.110节点进行以下操作
[root@localhost ~]# docker run -d \
--name=registrator \        ##定义容器名称
--net=host \                ##定义网络
-v /var/run/docker.sock:/tmp/docker.sock \     ##指定数据卷,存储信息
--restart=always \    
gliderlabs/registrator:latest \                ##定义镜像
-ip=14.0.0.110 \                          ##指定本地地址
consul://14.0.0.107:8500                  ##指定consul管理节点地址、端口
[root@localhost ~]# docker ps -a    ##查看创建的集群状态
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS               NAMES
242f182096e0        gliderlabs/registrator:latest   "/bin/registrator -i…"   2 minutes ago       Up 2 minutes                            registrator
  1. 测试集群的服务发现功能
[root@localhost ~]#  docker run -itd -p 83:80 --name test01 -h test1 nginx     ##添加nginx容器,对外映射到为83端口
2ed3a947f19e8cb35debcd4637740d48eb7abca9374562835d9a13eb0d9a8b94
[root@localhost ~]#  docker run -itd -p 84:80 --name test02 -h test2 nginx    ##添加第二个nginx容器
75aba3331058249e1c544bfe05d160500c634b63868898a69294387f3796667e

在这里插入图片描述
再加入两个apache服务

[root@localhost ~]# docker run -itd -p 85:80 --name test03 -h test3 httpd
e08c25835aa4e31969331d6cb8690140d48ba75102e904d548f5778daafe4539
[root@localhost ~]# docker run -itd -p 86:80 --name test04 -h test4 httpd
539044ae1fddb3896519aa553aee3ba5a9edebc3816812207f9ee91ea5b0f991
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                NAMES
539044ae1fdd        httpd                           "httpd-foreground"       4 minutes ago       Up 4 minutes        0.0.0.0:86->80/tcp   test04
e08c25835aa4        httpd                           "httpd-foreground"       5 minutes ago       Up 5 minutes        0.0.0.0:85->80/tcp   test03
75aba3331058        nginx                           "/docker-entrypoint.…"   10 minutes ago      Up 10 minutes       0.0.0.0:84->80/tcp   test02
2ed3a947f19e        nginx                           "/docker-entrypoint.…"   12 minutes ago      Up 12 minutes       0.0.0.0:83->80/tcp   test01
242f182096e0        gliderlabs/registrator:latest   "/bin/registrator -i…"   23 minutes ago      Up 23 minutes                            registrator

在这里插入图片描述

  1. 在consul服务器上检查状态
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}
  1. 安装consul-template
    Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行shell命令执行更新操作,重新加载 Nginx。Consul-Template可以查询Consul中的服务目录、Key、Key-values等。这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers、Haproxy Backends。

  2. 准备consul-template模板文件
    模板用于nginx反向代理模板,nginx.ctmpl跟nginx没有直接关系,consul是docker的一种自动管理机制,nginx.ctmpl中的参数以变量的形式写入。

[root@localhost consul]# vim nginx.ctmpl
upstream http_backend {
   {{range service "nginx"}}
    server {{.Address}}:{{.Port}};
     {{end}}
}

server {
  listen 88;
  server_name localhost 14.0.0.107;
  access_log /var/log/nginx/tang.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;
  }
}

  1. 手工编译安装nginx
[root@localhost consul]# yum install gcc gcc-c++ make pcre-devel zlib-devel -y
将软件包拷贝到当前目录下
[root@localhost consul]# tar zxvf nginx-1.12.2.tar.gz -C /opt
[root@localhost consul]# cd /opt
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
include       vhost/*.conf;

在这里插入图片描述

[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost conf]# mkdir /usr/local/nginx/conf/vhost
[root@localhost conf]# mkdir /var/log/nginx
[root@localhost conf]# /usr/local/nginx/sbin/nginx
[root@localhost conf]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      87526/nginx: master 

  1. 配置并启动template
[root@localhost consul]# unzip consul-template_0.19.3_linux_amd64.zip   
[root@localhost consul]# ls
consul_0.9.2_linux_amd64.zip  consul-template_0.19.3_linux_amd64.zip
consul-template               nginx.ctmpl
[root@localhost consul]# mv consul-template /usr/bin/
[root@localhost conf]# consul-template -consul-addr 14.0.0.107:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/hello.conf:/usr/local/nginx/sbin/nginx -s reload"  \
--log-level=info
指定模板路径,/consul/nginx.ctmpl,生成到/usr/locla/nginx/conf/vhost/hello.conf,然后重载nginx -s reload'
接下来会进入监控状态

打开另外一个终端,到指定的vhost目录下查看生成的配置文件

[root@localhost consul]# cd /usr/local/nginx/conf/vhost/
[root@localhost vhost]# ls
hello.conf
[root@localhost vhost]# vim hello.conf 

upstream http_backend {

    server 14.0.0.110:83;

    server 14.0.0.110:84;

}

server {
  listen 88;
  server_name localhost 14.0.0.107;
  access_log /var/log/nginx/tang.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;
  }
}
  1. 查看容器日志文件
    这时候还没有访问,没有产生日志文件
[root@localhost consul]# docker logs -f test01
/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: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: 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: Configuration complete; ready for start up

对网站进行访问
在这里插入图片描述
这时候只访问了一次,只有test01上有日志,test02上面没有日志记录

[root@localhost consul]# docker logs -f test01
/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: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: 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: Configuration complete; ready for start up
14.0.0.1 - - [24/Sep/2020:03:31:11 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36" "-"
2020/09/24 03:31:11 [error] 28#28: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 14.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "14.0.0.110:83", referrer: "http://14.0.0.110:83/"
14.0.0.1 - - [24/Sep/2020:03:31:11 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://14.0.0.110:83/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36" "-"
^C
[root@localhost consul]# docker logs -f test02
/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: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: 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: Configuration complete; ready for start up

Logo

一站式 AI 云服务平台

更多推荐