Container service update and discovery of docker consumer

Posted by MBDesktop on Mon, 25 Oct 2021 13:50:01 +0200

1, Consul overview

(1) What is service registration and discovery
Service registration and discovery is an indispensable component in microservice architecture. At first, services are single node, which does not guarantee high availability, and does not consider the pressure bearing of services. Calls between services are simply accessed through interfaces.

Until the distributed architecture of multiple nodes appeared later, the initial solution was load balancing at the service front end. In this way, the front end must know the network location of all back-end services and configure them in the configuration file. Here are a few questions:

● if you need to call back-end services A-N, you need to configure the network location of N services, which is very troublesome

● the configuration of each caller needs to be changed when the network location of the back-end service changes

Since there are these problems, service registration and discovery solve these problems. The back-end service A-N can register its current network location with the service discovery module, and the service discovery is recorded in the form of K-V. K is generally the service name, and V is IP:PORT.

The service discovery module conducts regular health checks and polls to see if these back-end services can be accessed. When the front end calls the back-end services A-N, it runs to the service discovery module to ask for their network location, and then calls their services. In this way, the above problems can be solved. The front end does not need to record the network location of these back-end services, and the front end and back-end are completely decoupled!
(2) What is consumer
Consumer is an open source service management software developed by google using go language. Support multi data center, distributed high availability, service discovery and configuration sharing. Raft algorithm is adopted to ensure high availability of services.

Built in service registration and discovery framework, distributed consistency protocol implementation, health check, Key/Value storage, multi data center scheme, and no need to rely on other tools (such as ZooKeeper).

Service deployment is simple, with only one runnable binary package. Each node needs to run agent, which has two running modes: server and client. The official recommendation of each data center requires 3 or 5 server nodes (odd number of servers greater than or equal to 3) to ensure data security and ensure that the server leader election can be carried out correctly.

In the client mode, all services registered with the current node will be forwarded to the server node, and the information itself is not persistent.

In the server mode, the function is similar to the client mode. The only difference is that it will persist all information locally, so that in case of failure, the information can be retained.

Server leader is the boss of all server nodes. Unlike other server nodes, it needs to be responsible for synchronizing the registered information to other server nodes, as well as monitoring the health of each node.
Some key features of consumer:

Service registration and discovery: consumer makes service registration and discovery easy through DNS or HTTP interfaces. Some external services, such as those provided by saas, can also be registered.

Health check: health check enables consumer to quickly alarm the operation in the cluster. Integration with service discovery can prevent services from being forwarded to failed services.

Key/Value storage: a system used to store dynamic configuration. Provides a simple HTTP interface that can be operated anywhere.

Multiple data centers: any number of areas can be supported without complex configuration.

Install consumer is used for service registration, that is, some information of the container itself is registered in consumer. Other programs can obtain the registered service information through consumer, which is service registration and discovery.

2, Consumer deployment

Environmental preparation

The consumer server 192.168.50.33 runs the consumer service, nginx service, and the consumer template daemon

The Registrar server 192.168.50.36 runs the Registrar container and the nginx container
Turn off the firewall first

1. Establish Consul service

mkdir /opt/consul
cd /opt/consul
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/local/bin/ 


Set the agent and start the consumer server in the background

consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.50.33 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &

-server:  with server Start as. The default is client. 
-bootstrap : Used to control a server Is it bootstrap There can only be one mode in a data center server be in bootstrap Mode, when a server be in bootstrap Mode, you can elect to server-leader. 
-bootstrap-expect=2 : Minimum cluster requirements server Quantity. When it is lower than this quantity, the cluster will fail.
-ui : Specify on UI Interface, so that you can http://Use an address like localhost:8500/ui to access the web UI provided by consumer.
-data-dir : Specify the data store directory.
-bind : Specify the communication address within the cluster. All nodes in the cluster must be reachable to this address. The default is 0.0.0.0. 
-client : appoint consul Where is the binding client On the address, this address provides HTTP,DNS,RPC And other services. The default is 127.0.0.1. 
-node : The name of a node in a cluster must be unique in a cluster. The default is the host name of the node.
-datacenter : Specify the data center name. The default is dc1. 
netstat -natp | grep consul

start-up consul By default, it will listen to 5 ports:
8300: replication,leader farwarding Port
8301: lan cossip Port
8302: wan gossip Port
8500: web ui Interface port
8600: use dns Port for viewing node information by protocol

2. View cluster information

View members status

consul members


View cluster status

consul operator raft list-peers
consul info | grep leader

3. Obtain cluster information through http api

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

3, Registrar server

The container service automatically joins the Nginx cluster

1. Install gliderlabs / Registrar

Gliderlabs/Registrator You can check the running status of the container, register automatically, and log out docker The service of the container to the service configuration center. Current support 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.50.33 \
consul://192.168.50.33:8500


--net=host : Put the running docker Container set to host Network mode.

-v /var/run/docker.sock:/tmp/docker.sock : Put the host Docker Daemon(Docker daemon)Default listening Unix Mount the domain socket into the container.

--restart=always : Set to always restart the container when it exits.

--ip : Just put network Specified host Mode, so we specify ip For the host ip. 

consul : appoint consul Server IP And ports.

2. Test whether the service discovery function is normal

On the monitored node server, after the service of nginx container is started and port mapping is done, the mapped information will be written to the docker.sock file of the host

The Registrar automatic discovery module will monitor the docker.sock of the host machine and discover the nginx service.

The registrar will write the information to the automatic registration module of consumer and display it through 8500 web ui

docker run -itd -p:83:80 --name web2 -h test2 nginx
docker run -itd -p:84:80 --name web3 -h test3 httpd




In the browser, enter http://192.168.50.33:8500 , click "NODES" in the Web page, and then click "consorl-server01", and several services will appear.

//Use curl test to connect to the consumer server

curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}

4, Consumer template

Consul template is an application based on consul to automatically replace configuration files== Consul template is a daemon 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 completed, you can choose to run the shell command to perform the update operation and reload Nginx.

The consult template can query the service directory, Key, Key values, etc. in consult. This powerful abstraction function and query language template can make Consul template particularly suitable for dynamic configuration file creation. For example, create Apache/Nginx Proxy Balancers, Haproxy Backends, etc.

1. Prepare template nginx template file

Operate on the consumer server

vim /opt/consul/nginx.ctmpl  
#Define a simple template for nginx upstream
 
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
 
#Define a server, listen to port 8000, and reverse proxy to upstream
server {
listen 8000;
server_name localhost 192.168.50.33;
access_log /var/log/nginx/ly-access.log;    #Modify log path
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;
}
}

2. Compile and install nginx

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


3. Configure nginx

vim /usr/local/nginx/conf/nginx.conf
......
http {
include mime.types;
include vhost/*.conf; #Add virtual host directory
default_type application/octet-stream;  


Create virtual host directory

mkdir /usr/local/nginx/conf/vhost

Create log file directory

	
mkdir /usr/local/nginx/conf/vhost

start nginx

4. Configure and start the template

unzip consul-template_0.19.3_linux_amd64.zip -d /opt/
cd /opt/
mv consul-template /usr/local/bin/


Start the template service in the foreground. After starting, do not press ctrl+c to stop the consumption template process.

consul-template --consul-addr 192.168.50.33:8500 \
--template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/ly.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info 

In addition, open a terminal to view the generated configuration file

5. Access template nginx

Operate on the Registrar server and add a web page test file

Web test http://192.168.50.33:83/ http://192.168.50.33:82/

6. Add an nginx container node

Add an nginx container node to test the service discovery and configuration update functions.


View the nginx container log and request normal polling to each container node

5, Consumer multi node

Add a server 192.168.229.70/24 with an existing docker environment to join the existing cluster

consul agent \
-server \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.50.36 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.50.33 &> /var/log/consul.log &

-enable-script-checks=true : Set check service to available
-datacenter : Data center name
-join : Join an existing cluster

View cluster members and status

consul members
consul operator raft list-peers

Topics: Docker Container consul