Docker部署Consul、Consul Template、Registrator

Environmental preparation

Host Host/ip address Mainly installed software
server 192.168.162.5 Docker-ce 、 Compose 3 、 Consul 、 Consul-template
server 192.168.162.20 Docker-ce、registrator

Consul deployment (192.168.162.5)

关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0


mkdir /root/consul
cd /root/consul

#将consul.0.9.2_linux_amd64.zip上传到/root/consul
unzip consul_0.9.2_linux_amd64.zip

mv consul /usr/bin

consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.162.5 \
-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.162.5:8301  alive   server  0.9.2  2         dc1


[root@localhost consul]#consul info | grep leader
	leader = true
	leader_addr = 192.168.162.5:8300

//通过httpd api获取集群信息
curl 127.0.0.1:8500/v1/status/peers      //查看集群server成员
curl 127.0.0.1:8500/v1/status/leader     //集群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     //集群节点详细信息

Insert picture description here
Insert picture description here

The container service automatically joins the nginx cluster

1) Install Gliderlabs/Registrator Gliderlabs/Registrator
can check the running status of the container and register automatically, and can also cancel the service of the docker container to the service configuration center.
Currently supports Consul, Etcd and SkyDNS2

docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.162.20 \
consul://192.168.162.20:8500

2.测试服务发现功能是否正常
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:99:80 --name test-03 -h test03 httpd
docker run -itd -p:88:80 --name test-04 -h test04 httpd

3.验证http和nginx服务是否注册到consul
浏览器输入http://192.168.162.20:8500,“单击NODES",然后单击"consurl-server01" ,会出现5个服务

Insert picture description here
2), view the service on the consul server.

curl 127.0.0.1:8500/v1/catalog/services

Insert picture description here

Install consul-template

Consul-Template is a daemon process used to query Consul cluster information in real time, update any number of specified templates on the file system, and generate configuration files. After the update is complete, you can choose to run the shell command to perform the update operation and reload Nginx. Consul-Template can query the service catalog, Key, Key-values, etc. in Consul.
This powerful abstract function and query language template can make Consul-Template particularly suitable for dynamically creating configuration files.
For example: create Apache/Nginx Proxy Balancers, Haproxy Backends

Prepare template nginx template file

//在consul服务器上操作
vim /root/consul/nginx.ctmpl

upstream http_backend {
  {
   
   {range service "nginx"}}
   server {
   
   {.Address}}:{
   
   {.Port}};
  {
   
   {end}}
}

server {
  listen 83;
  server_name localhost 192.168.162.5;
  access_log /var/log/nginx/gcc.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;
}
}

Compile and install nginx (operate on consul)

yum install gcc pcre-devel zlib-devel -y

tar zxvf nginx-1.12.0.tar.gz -C /opt

cd //opt/nginx-1.12.0
./configure --prefix=/usr/local/nginx

make && make install

Configure nginx (operate on consul)

vim /usr/local/nginx/conf/nginx.conf

http {
   include  mime.types;
   include  vhost/*.conf;   #添加虚拟主机目录
   default_type application/octet-stream;

//创建虚拟主机目录
mkdir /usr/local/nginx/conf/vhost
//创建日志文件目录
mkdir /var/log/nginx


//启动nginx
/usr/local/nginx/sbin/nginx

Configure and start the template (operate on consul)

cd /root

#上传consul-template_0.19.3_linux_amd64.zip包到/root目录下
unzip consul-template_0.19.3_linux_amd64.zip

mv consul-template /usr/bin/

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

//另外打开-个终端查看生成配置文件
[root@localhost vhost]#cat /usr/local/nginx/conf/vhost/test.conf
upstream http_backend {
  
 server 192.168.162.20:83;

 server 192.168.162.20:84;

  
}

server {
  listen 83;
  server_name localhost 192.168.162.5;
  access_log /var/log/nginx/gcc.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;
}
}

Insert picture description here

Insert picture description here

Add a nginx container node

#增加一个nginx容器节点,测试服务发现及配置更新功能
//在registrator(192.168.162.20)服务端注册
docker run -itd -p:85:80 --name test-05 -h test05 nginx

//在consul服务器监控装填会有提示自动更新
2021/03/26 05:51:59.624677 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
2021/03/26 05:53:09.576538 [INFO] (runner) initiating run
2021/03/26 05:53:09.577487 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/03/26 05:53:09.577505 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/03/26 05:53:09.577534 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload


//查看三台nginx容器日志,请求正常轮询到各个容器节点上
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05

Insert picture description here

Consul multi-node configuration

//添加一台已有docker环境的服务器192.168.162.30/24加入已有的群集中

consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/ib/consul-data \
-bind=192.168.162.30 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.162.5 &> /var/log/consul.log &


-enable-script-checks=true: 设置检查服务为可用
-datacenter: 数据中心名称
-join: 加入到已有的集群中

Guess you like

Origin blog.csdn.net/weixin_51622156/article/details/115286201