Docker container: Docker consul's container service update and discovery

Table of contents

1. Docker consul

1. What is service registration and discovery

2. What is consul

3. Consul deployment

① Purpose of the experiment

②Experimental environment and topology

③consul configuration

④registrator backend configuration

⑤ Test to find out whether the function is normal

4. consul-template deployment

① Prepare the template nginx template file

② Compile and install nginx

③Install template

5. Verify the template-nginx load result

6. Test whether the microservice change consul-template takes effect

①Add nginx microservice container

②Delete the nginx microservice container


1. Docker consul

1. What is service registration and discovery

Service registration and discovery are indispensable and important components in the microservice architecture. At first, the services were single-node, which did not guarantee high availability and did not consider the pressure bearing of the service. The calls between services were simply accessed through the interface. Until the distributed architecture of multiple nodes appeared later, the initial solution was to balance the load on the service front-end, so that the front-end must know the network locations of all back-end services and configure them in the configuration file. There will be several problems here:
● If you need to call the backend service AN, you need to configure the network locations of N services, which is very troublesome to configure
● Changes in the network location of the backend service require changing the configuration of each caller

Since there are these problems, service registration and discovery are to solve these problems. The back-end service AN can register its current network location to the service discovery module, and the service discovery will be recorded in the form of KV. K is generally the service name, and V is IP:PORT. The service discovery module conducts health checks regularly, and polls to see if these back-end services can be accessed. When the front-end calls the back-end service AN, it runs to the service discovery module to ask their network locations, and then calls their services. In this way, the above problems can be solved. The front end does not need to record the network locations of these back-end services at all, and the front-end and back-end are completely decoupled!


2. What is consul

consul is a service management software developed by google open source using go language. Supports multi-data centers, distributed high availability, service discovery and configuration sharing. The Raft algorithm is used 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 solution, no longer need to rely on other tools (such as ZooKeeper, etc.). Service deployment is simple, with only one executable binary package. Each node needs to run agent, which has two modes of operation: server and client. The official recommendation of each data center is that 3 or 5 server nodes are required to ensure data security and ensure that the election of the server-leader can be performed correctly.

In client mode, all services registered to the current node will be forwarded to the server node, and the information itself will not be persisted.
In the server mode, the function is similar to the client mode, the only difference is that it will persist all the information to the local, so that the information can be retained in case of failure.
The server-leader is the boss of all server nodes. It is different from other server nodes in that it needs to be responsible for synchronizing registered information to other server nodes, and is also responsible for the health monitoring of each node.

Some key features provided by consul:
service registration and discovery: consul makes service registration and service discovery easy through the DNS or HTTP interface, and some external services, such as those provided by saas, can also be registered in the same way.
Health check: Health check enables consul to quickly alert the operation in the cluster. Integration with service discovery prevents service forwarding to failed services.
Key/Value Storage: A system for storing dynamic configurations. Provides a simple HTTP interface that can be operated anywhere.
Multi-datacenter: Support any number of regions without complicated configuration.

Installing consul is used for service registration, that is, some information of the container itself is registered in consul, and other programs can obtain registered service information through consul, which is service registration and discovery.

3. Consul deployment

① Purpose of the experiment

Automatically discover and register the server (backend server) through consul deployment, and check the running status of the container. Automatic registration can also cancel the service of the docker container to the service configuration center

②Experimental environment and topology

server function server ip server deployer
consul-server 192.168.10.23 Run consul service, nginx service, consul-template daemon process
registrator server 192.168.10.13 Run the registrator container, run the nginx container

③consul configuration

cd /opt
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/local/bin/
#将consul压缩包放入opt,解压后移动到/usr/local/bin目录可全局使用consul命令
consul agent \
#设置代理,在后台启动 consul 服务端
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.30.11 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
#-server: 以server身份启动。默认是client。
#-bootstrap :用来控制一个server是否在bootstrap模式,在一个数据中心中只能有一个server处于bootstrap模式,当一个server处于 bootstrap模式时,可以自己选举为 server-leader。
#-bootstrap-expect=2 :集群要求的最少server数量,当低于这个数量,集群即失效。
#-ui :指定开启 UI 界面,这样可以通过 http://localhost:8500/ui 这样的地址访问 consul 自带的 web UI 界面。
#-data-dir :指定数据存储目录。
#-bind :指定用来在集群内部的通讯地址,集群内的所有节点到此地址都必须是可达的,默认是0.0.0.0。
#-client :指定 consul 绑定在哪个 client 地址上,这个地址提供 HTTP、DNS、RPC 等服务,默认是 127.0.0.1。
#-node :节点在集群中的名称,在一个集群中必须是唯一的,默认是该节点的主机名。
#-datacenter :指定数据中心名称,默认是dc1。
netstat -natp | grep consul
#查看consul是否启动成功,会监听5个端口
8300:replication、leader farwarding的端口
8301:lan cossip的端口
8302:wan gossip的端口
8500:web ui界面的端口
8600:使用dns协议查看节点信息的端口
consul members
#查看集群信息
consul operator raft list-peers
#查看集群状态
consul info | grep leader
#查看leader信息
curl 127.0.0.1:8500/v1/status/peers
#查看集群server成员
curl 127.0.0.1:8500/v1/status/leader
#集群 server-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
#集群节点详细信息

④registrator backend configuration

安装 Gliderlabs/Registrator,Gliderlabs/Registrator 可检查容器运行状态自动注册,还可注销 docker 容器的服务到服务配置中心。目前支持 Consul、Etcd和SkyDNS2。
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
--ip=192.168.30.12 \
consul://192.168.30.11:8500
#--net=host :把运行的docker容器设定为host网络模式。
#-v /var/run/docker.sock:/tmp/docker.sock :把宿主机的Docker守护进程(Docker daemon)默认监听的Unix域套接字挂载到容器中。
#--restart=always :设置在容器退出时总是重启容器。
#--ip :刚才把network指定了host模式,所以我们指定ip为宿主机的ip。
#consul :指定consul服务器的IP和端口。
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:88:80 --name test-03 -h test03 httpd
docker run -itd -p:89:80 --name test-04 -h test04 httpd
#创建2个nginx容器,2个httpd容器测试发现功能是否正常

⑤ Test to find out whether the function is normal

In the browser, enter http://192.168.10.23:8500, "click NODES" on the Web page, and then click "consurl-server01", 5 services will appear 

4. consul-template deployment

① Prepare the template nginx template file

#consul服务:即192.168.30.11上执行
vim /opt/consul/nginx.ctmpl
upstream http_backend {
  {
   
   {range service "nginx"}}
   server {
   
   {.Address}}:{
   
   {.Port}};
   {
   
   {end}}
}
server {
    listen 8000;
    server_name localhost 192.168.30.11;
    access_log /var/log/nginx/kgc.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;
    }
}
#准备模板文件,upstream模块中的全为变量自动获取微服务地址和端口,server定义是监听本机8000端口指定日志文件和主页类型

② Compile and install nginx

#consul服务:即192.168.30.11上执行
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin nginx
#安装依赖环境和编译工具,并创建nginx用户
cd /opt/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxvf nginx-1.12.0.tar.gz 
#下载nginx压缩包到/opt目录下并解压
cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make -j4 && make install
#进入解压后的nginx文件夹镜像编译安装
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#将nginx命令软连接到/usr/local/sbin/下可以进行全局使用
vim /usr/local/nginx/conf/nginx.conf
#编辑nginx主配置文件在http模块中指定子配置文件路径,如下
http {
     include       mime.types;
     include  vhost/*.conf;       				
#添加虚拟主机目录即子配置文件路径
     default_type  application/octet-stream;
mkdir /usr/local/nginx/conf/vhost 
#创建虚拟主机目录即nginx子配置文件目录
mkdir /var/log/nginx
#创建日志文件目录
nginx
#启动nginx

③Install template

#consul服务:即192.168.30.11上执行
cd /opt/
unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/local/bin/
#将 consul-template包上传到/opt目录下并解压,将解压后的 consul-template移动到/usr/local/bin下可进行全局使用
consul-template --consul-addr 192.168.30.11:8500 \
--template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
#在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程,注意前台

#重新打开一个窗口查看是否根据模板生成了新的子配置文件
cat /usr/local/nginx/conf/vhost/kgc.conf

5. Verify the template-nginx load result

#服务端:即192.168.30.12
docker ps 
#查看启动的容器信息
docker exec -it 6ac6f22d5b9d bash
echo "this is test1 web" > /usr/share/nginx/html/index.html
exit
docker exec -it 1be8511f8525 bash
echo "this is test2 web" > /usr/share/nginx/html/index.html
exit
#分别进入2个微服务容器即2个nginx和2个http,更换主页内容
浏览器访问:http://192.168.30.11:8000/并不断刷新,会有2种结果是更换后的nginx主页内容

6. Test whether the microservice change consul-template takes effect

①Add nginx microservice container

#服务端:即192.168.30.12
docker run -itd -p:85:80 --name test-05 -h test05 nginx
#新增查看是否生效

②Delete the nginx microservice container

docker stop test-05
#停止新增的test-05的nginx容器查看是生效

Guess you like

Origin blog.csdn.net/weixin_67287151/article/details/130365991