Build an automatically discovered Docker architecture (Consul, Consul Template, Registrator)

One, Consul

(1) Introduction to Consul

1. Consul is an open source tool launched by HashiCorp, which is used to realize service discovery and configuration of distributed systems.
2. Features of Consul

  • Support health check, allowing storage of key-value pairs
  • Based on Golong language, strong portability
  • Support ACL access control

3. It can work seamlessly with lightweight containers such as Docker.
4. The commonly used frameworks for service discovery are:

  • zookeeper
  • eureka
  • etcd
  • consul

(2) Docker Consul container service update and discovery

Container Service Update and Discovery Topology Map
Insert picture description here

Second, build the deployment steps of the automatically discovered Docker service architecture

Project requirements:

The company puts forward a new requirement to use Docker to assemble Consul, Consul Template, Registrator and Nginx into a trustworthy and extensible service framework. Services can be added and removed from this framework without rewriting any configuration or Restart any service, everything will run normally

Project steps:

Server IP Install software and services
Server: 192.168.200.40 Docker-ce 、 Compose 3 、 Consul 、 Consul-template
Server: 192.168.200.60 Docker-ce、registrator

(1) Steps to deploy consul (192.168.200.40)

关闭防火墙
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.200.40 \
-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.200.40:8301  alive   server  0.9.2  2         dc1


[root@localhost consul]#consul info | grep leader
	leader = true
	leader_addr = 192.168.200.40: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     //集群节点详细信息

(Two), the container service automatically joins the nginx cluster

1. Underwear Gliderlabs / Registrator Gliderlabs / Registrator

You can check the running status of the container and register automatically, and you can also unregister the service of the docker container to the service configuration center.
Currently supports Consul, Etcd and SkyDNS2

1) On the 192.168.200.60 node, perform the following operations
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.200.60 \
consul://192.168.200.40: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.200.40:8500,“单击NODES",然后单击"consurl-server01" ,会出现5个服务
2), view the service on the consul server.
curl 127.0.0.1:8500/v1/catalog/services
{
    
    "consul":[],"httpd":[], "nginx":[]}

2. 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

3. 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.200.40;
  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;
}
}

4. 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

5. 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

6. 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.200.60:83;
  
   server 192.168.200.60:84;
  
}

server {
    
    
  listen 83;
  server_name localhost 192.168.200.40;
  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;
}
}

(Three), add a nginx container node

#增加一个nginx容器节点,测试服务发现及配置更新功能
//在registrator(192.168.200.60)服务端注册
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

(Four), consul multi-node configuration

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

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


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

Guess you like

Origin blog.csdn.net/Gengchenchen/article/details/115241230