Docker container - Consul deployment

Table of contents

1. Overview of Consul

2. Characteristics of Consul

3. Consul usage scenarios

4. Build consul cluster

1. Server deployment (192.168.127.130)

2. Client deployment (192.168.127.140)

3. Configure the template to automatically update

 4. Test access to proxy server


1. Overview of Consul

template template (update)
registrator (automatic discovery)
Every time the backend builds a container, it will register with the registrator and control consul to complete the update operation. Consul will trigger the consul template template for hot update. Core
mechanism: consul: automatic discovery, automatic update, Provide services for containers (addition, deletion, life cycle)

2. Characteristics of Consul

  1. Supports health checks and allows storage of key-value pairs
  2. Based on Golong language, highly portable
  3. Support ACL access control

3. Consul usage scenarios

Consul's application scenarios include service discovery, service isolation, and service configuration:

  1. In the service discovery scenario, consul serves as the registration center. After the service address is registered in consul, it can be queried using the dns and http interfaces provided by consul. Consul supports health check.
  2. In the service isolation scenario, Consul supports setting access policies on a service basis, supporting both classic and emerging platforms, TLS certificate distribution, and service-to-service encryption.
  3. In the service configuration scenario, consul provides key-value data storage function and can quickly notify changes. With the help of Consul, configuration sharing can be achieved. Services that need to read configuration can read accurate configuration information from Consul.
  4. Consul can help system managers understand the internal system architecture of complex systems more clearly. Operation and maintenance personnel can regard Consul as a monitoring software or an asset (resource) management system.

4. Build consul cluster

 

Create Consul service

The Consul agent must be deployed and run on each node that provides services.

Consul agent has two operating modes:
Server
Client

Server and Client are only distinctions at the Consul cluster level and have nothing to do with application services built on Cluster.

1. Server deployment (192.168.127.130)

mkdir /root/consul
cd consul
rz consul_0.9.2_linux_amd64.zip

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.127.130 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &

View cluster server members curl 127.0.0.1:8500/v1/status/peers
Cluster Raf leader curl 127.0.0.1:8500/v1/status/leader
All services registered curl 127.0.0.1:8500/v1/catalog/services
View nginx service information curl 127.0.0.1:8500/v1/catalog/nginx
Cluster node details curl 127.0.0.1:8500/v1/catalog/nodes

2. Client deployment (192.168.127.140)

容器服务自动加入nginx集群
1、安装Gliderlabs/Registrator Gliderlabs/Registrator
可检查容器运行状态自动注册,还可注销docker容器的服务 到服务配置中心
目前支持Consul、Etcd和SkyDNS2

在192.168.127.140节点上,执行以下操作

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

systemctl restart docker
docker run -itd -p:81:80 --name test-01 -h test01 nginx
docker run -itd -p:82:80 --name test-02 -h test02 nginx
docker run -itd -p:83:80 --name test-03 -h test03 httpd
docker run -itd -p:84:80 --name test-04 -h test04 httpd

运行两个nginx容器,两个apache容器,以测试服务发现功能

3. Configure the template to automatically update

cd consul/
vim nginx.ctmpl

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

server {
 listen 100;
 server_name localhost 192.168.127.130;
 access_log /var/log/nginx/lic.com-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;
  }     
}

yum -y install gcc pcre-devel zlib-devel
rz nginx-1.12.0.tar.gz
tar zxvf nginx-1.12.0.tar.gz -C /opt
cd /opt/nginx-1.12.10

./configure --prefix=/usr/local/nginx

make && make install

vim /usr/local/nginx/conf/nginx.conf
//19行添加  include vhost/*.conf;

cd /usr/local/nginx/conf/
mkdir vhost
mkdir /var/log/nginx

/usr/local/nginx/sbin/nginx

cd /opt
rz consul-template_0.19.3_linux_amd64.zip

unzip consul-template_0.19.3_linux_amd64.zip

mv consul-template /usr/bin
consul-template -consul-addr 192.168.127.130:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/lic.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info

 Open another terminal

 

 

 4. Test access to proxy server

Is it possible to complete proxy access polling

The browser accesses 192.168.127.130:100 and refreshes the log multiple times.

 

Guess you like

Origin blog.csdn.net/weixin_71429844/article/details/127479825