Table of contents
Two, the characteristics of consul
2. The host name is the configuration of the nginx side
3. Execute the following in zwb_docker (the server automatically joins the cosul cluster)
4. Install the nginx service on the zwb_nginx side, compile it manually, and have blogs before
5. Test: Add an nginx container node to test service discovery and configuration update functions
1. Introduction to consul
Consul is an open source tool launched by HashiCorp for service discovery and configuration of distributed systems. Compared with other distributed service registration and discovery solutions, Consul's solution is more "one-stop", with a built-in service registration and discovery framework, distributed consistency protocol implementation, health check, Key/Value storage, and multi-data center solutions. Need to rely on other tools (such as ZooKeeper, etc.). It is also relatively simple to use. Consul is written in Go language, so it is naturally portable (supports Linux, windows and Mac OS X); the installation package contains only one executable file, which is easy to deploy, and can work seamlessly with lightweight containers such as Docker.
When the tomcat4 server joins in (changes), consul components will collect data changes (when consul starts, it will drop a younger brother on the node of the application layer to monitor the work), first, the younger brother first registers 04, And report to consul,
The consul-agent will display the change information on the web page, and then the template template will update the configuration and give it to NGXIN. NGINX updates the configuration through reload.
Two, the characteristics of consul
1. Service Discovery
2. Monitoring and inspection
3. Key/value storage
4. Multiple data centers
5. Based on Raft algorithm
6. Support http and dns protocol interface
Three, consul deployment
1. Experimental environment
host IP | install service |
zwb_docker/192.168.159.68 | docker-ce, registrator (automatic discovery, registered components) |
zwb_nginx/192.168.159.159 | nginx、consul、consul-template |
2. The host name is the configuration of the nginx side
①. Upload the installation package to opt
[root@zwb_nginx opt]# ls
consul_0.9.2_linux_amd64.zip
②, decompression
[root@zwb_nginx opt]# unzip consul_0.9.2_linux_amd64.zip
③. Cut the execution file to /usr/bin
[root@zwb_nginx opt]# mv consul /usr/bin/
④, consul proxy configuration, start to prevent background execution
[root@zwb_nginx opt]# consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.159.159 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
[1] 22356
⑤. View cluster information
[root@zwb_nginx opt]# consul members
view port
Or view it like this:
[root@zwb_nginx opt]# consul info | grep leader
View node information
[root@zwb_nginx opt]# curl 127.0.0.1:8500/v1/catalog/nodes
⑥. Log in to the web page to view
3. Execute the following in zwb_docker (the server automatically joins the cosul cluster)
①. Install Gliderlabs/Registrator, which can detect the running status of the container and automatically register, and can also cancel the service of the docker container to the service configuration center.
[root@zwb_docker harbor]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> -ip=192.168.159.68 \
> consul://192.168.159.159:8500
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.159.68 \
consul://192.168.159.159:8500
②, Test whether the service is normal, run two nginx and two Apache
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
view web page
4. Install the nginx service on the zwb_nginx side, and manually compile it, as in previous blogs.
①. Modify the content of the nginx.conf file
Create the vhost directory:
[root@zwb_nginx conf]# mkdir vhost
②, create a consul directory
[root@zwb_nginx opt]# mkdir consul
create template
[root@zwb_nginx consul]# vim nginx.ctmpl
upstream http_backend {
{
{range service "nginx"}}
server {
{.Address}}:{
{.Port}};
{
{end}}
}
server {
listen 86;
server_name localhost 192.168.159.68;
access_log /var/log/nginx/kgc.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;
}
}
[root@zwb_nginx consul]# nginx # Open nginx service
③. Upload consul-template to the consul directory
[root@zwb_nginx consul]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@zwb_nginx consul]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
④, move position
[root@zwb_nginx consul]# mv consul-template /usr/bin/
⑤. Associate the sub-configuration file operation in the nginx virtual directory
consul-template -consul-addr 192.168.159.159:8500 \
-template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/ceshi.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
[root@zwb_nginx consul]# consul-template -consul-addr 192.168.159.159:8500 -template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/ceshi.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
2022/10/23 17:22:13.717713 [INFO] consul-template v0.19.3 (ebf2d3d)
2022/10/23 17:22:13.717735 [INFO] (runner) creating new runner (dry: false, once: false)
2022/10/23 17:22:13.718242 [INFO] (runner) creating watcher
2022/10/23 17:22:13.718477 [INFO] (runner) starting
2022/10/23 17:22:13.718502 [INFO] (runner) initiating run
2022/10/23 17:22:13.720804 [INFO] (runner) initiating run
2022/10/23 17:22:13.721707 [INFO] (runner) rendered "/opt/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/ceshi.conf"
2022/10/23 17:22:13.721729 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/opt/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/ceshi.conf"
2022/10/23 17:22:13.721789 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
Copy the session, open another interface to view, the ceshi.conf file has been generated
[root@zwb_nginx consul]# cd /usr/local/nginx/conf/vhost/
[root@zwb_nginx vhost]# ls
ceshi.conf
[root@zwb_nginx consul]# cat /usr/local/nginx/conf/vhost/ceshi.conf
upstream http_backend {
server 192.168.159.68:83;
server 192.168.159.68:84;
}
server {
listen 86;
server_name localhost 192.168.159.68;
access_log /var/log/nginx/kgc.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;
}
}
Visit the web page, the jump is complete
5. Test: Add an nginx container node to test service discovery and configuration update functions
Add an nginx to the zwb_docker node
docker run -itd -p 85:80 --name test-05 -h test05 nginx
Automatically incremented.
Delete a service on a node
[root@zwb_docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c4c134478f2 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:85->80/tcp, :::85->80/tcp test-05
7ab78cbf9a7b httpd "httpd-foreground" 22 minutes ago Up 22 minutes 0.0.0.0:89->80/tcp, :::89->80/tcp test-04
18048616c880 httpd "httpd-foreground" 22 minutes ago Up 22 minutes 0.0.0.0:88->80/tcp, :::88->80/tcp test-03
adce82f7031b nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:84->80/tcp, :::84->80/tcp test-02
b8708ba1a234 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:83->80/tcp, :::83->80/tcp test-01
e2dc3187acc5 gliderlabs/registrator:latest "/bin/registrator -i…" 24 minutes ago Up 24 minutes registrator
[root@zwb_docker ~]# docker rm -f test-03
test-03
[root@zwb_docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c4c134478f2 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:85->80/tcp, :::85->80/tcp test-05
7ab78cbf9a7b httpd "httpd-foreground" 23 minutes ago Up 23 minutes 0.0.0.0:89->80/tcp, :::89->80/tcp test-04
adce82f7031b nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:84->80/tcp, :::84->80/tcp test-02
b8708ba1a234 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:83->80/tcp, :::83->80/tcp test-01
e2dc3187acc5 gliderlabs/registrator:latest "/bin/registrator -i…" 24 minutes ago Up 24 minutes registrator
view on the web
Automatically decrease by one.