In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the method of building a Docker Consul cluster environment". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. Overview of Docker consul
CCS update and discovery: first discover and then update. What is found is the change of the container on the back-end node (registrator), and the update is the nginx configuration file (agent).
Registrator: the eyeliner installed by consul in the docker container is used to listen for changes (increase or decrease, or downtime) of the container on the monitoring node. If there is any change, the information will be told and registered on the consul server side (callback and co-program will be used, so its latency and resource consumption will be very small). Once the registration list changes, consul server will tell agent the registration information.
Agent (proxy): used to control the consul template template and use template components to interface with nginx.conf. The template is full of variables. After loading the back end with variables and registering to the consul server side, the server side will tell agent,agent and template to dock, write the template,template to have a mirror, and after the update is completed, it will be recognized by the front-end nginx as a nginx.confession subconfiguration file After consul agent controls the reload, it will recognize the changes in the nginx.conf configuration file, which is equivalent to identifying the backend nodes, and can dynamically adjust its backend resources in the address pool.
Characteristics of Consul
Support health check and allow storage of key-value pairs
Based on Golong language, strong portability
Support for ACL access control
Second, based on nginx and consul to build an automatic discovery that is high availability Docker service architecture
1. Project requirements
Using Docker to combine Consul, Consul template, Registrator and Nginx into a new and scalable service architecture
When adding or removing services in this architecture, there is no need to rewrite any configuration or restart any services, and everything can run normally to realize automatic operation and maintenance.
two。 Environmental preparation
Host IP address needs to install the software master node 192.168.126.11docker-ce, consul, consul-template, nginxnginx192.168.126.12doker-ce
3. Deployment steps
# Docker-ce is installed on both nodes. Remember to turn off the firewall systemctl stop firewalld & & systemctl disable firewalldsetenforce 0 & & sed-I "s/SELINUX=*/SELINUX=disabled/g" / etc/selinux/config
① deploys consul on the primary node
[root@xjj ~] # mkdir / consul [root@xjj ~] # cd / consul/ [root@xjj consul] # rz [root@xjj consul] # lsconsul_0.9.2_linux_ amd64.zip [root @ xjj consul] # unzip consul_0.9.2_linux_amd64.zip-d / usr/bin/Archive: consul_0.9.2_linux_amd64.zip inflating: / usr/bin/consul [root@xjj consul] # consul agent\ >-server\ >-bootstrap\ >-ui\ >-data-dir=/var/lib/consul-data\ >-bind=192.168.126.11\ >-client=0.0.0.0\ >-node=consul-server01 & > / var/log/consul.log & [1] 100683 [root@xjj consul] # jobs-l [1] + 100683 running consul agent-server-bootstrap-ui-data-dir=/var/lib/consul-data-bind=192.168.126.11-client=0.0. 0.0-node=consul-server01 & > / var/log/consul.log & [root@xjj consul] # consul membersNode Address Status Type Build Protocol DCconsul-server01 192.168.126.11 alive server 0.9.2 dc1 [root@xjj consul] # consul info | grep leader leader = true leader_addr = 192.168.126.11 netstat-natp | grep 8500tcp 0 0 127.0.1 TIME_WAIT 34120 127.0.1 TIME_WAIT 8500 TIME_WAIT-tcp6 00: 8500: * LISTEN 100683/consul
Detailed explanation of parameters
[root@xjj consul] # consul agent\ # set proxy >-server\ # Service function >-bootstrap\ # participate in the election of leaders >-ui\ # provide web access interface >-data-dir=/var/lib/consul-data\ # provide a proxy storage data directory >-bind=192.168.126 .16\ # bind local address >-client=0.0.0.0\ # client address (all) >-node=consul-server01 & > / var/log/consul.log & # define the node name Log mixed output to log, and put to the background to run jobs-l # to view the current terminal put into the background work And list the PID number of the process consul members # view cluster information consul info | grep leader # view management information Leader is the leader # Cluster information can be obtained through HTTP API: curl 127.0.0.1:8500/v1/status/peers'/ / View cluster server member 'curl 127.0.0.1:8500/v1/status/leader' / / Cluster Raf 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'
② nginx server connects to consul and creates nginx container service
[root@localhost] # docker run-d\ >-- name=registrator\ >-- net=host\ >-v / var/run/docker.sock:/tmp/docker.sock\ >-- restart=always\ > gliderlabs/registrator:latest\ >-ip=192.168.126.12\ > consul://192.168.126.11:8500-Digest: sha256:6e708681dd52e28f4f39d048ac75376c9a762c44b3d75b2824173f8364e52c10Status: Downloaded newer image for gliderlabs/registrator:latestdb4cb9d6a56ce8b9a2155b1113e48e6c974889cd4cca363b410c116b75be5d59 [root@localhost ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdb4cb9d6a56c gliderlabs/registrator:latest "/ bin/registrator-I..." 5 seconds ago Up 5 seconds registrator# create container Used to test whether the service discovery function is normal: [root@localhost ~] # docker run-itd-p 81:80-- name xjj01-h xcf01 nginx #-h option indicates that the specified host host name [root@localhost ~] # docker run-itd-p 82:80-- name xjj02-h xcf02 nginx [root@localhost ~] # docker run-itd-p 83:80-- name xjj03-h xcf03 httpd [root@localhost ~] # docker run-itd-p 84:80- -name xjj0-h xcf04 httpd [root@localhost ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES8940a9b03dbb httpd "httpd-foreground" 4 seconds ago Up 3 seconds 0.0.0.0 root@localhost 84-> 80/tcp : 84-> 80/tcp xjj09ac4d089eb14 httpd "httpd-foreground" 39 seconds ago Up 38 seconds 0.0.0.0 seconds 83-> 80/tcp,:: 83-> 80/tcp xjj033d626fd61639 nginx "/ docker-entrypoint." 25 minutes ago Created xjj02263aa9deb346 nginx "/ docker-entrypoint." 26 minutes ago Up 26 minutes 0.0.0.0 80/tcp 81-> 80/tcp,:: 81-> 80/tcp xjj01db4cb9d6a56c gliderlabs/registrator:latest "/ bin/registrator-I …" 29 minutes ago Up 29 minutes registrator browser access test: `192.168.126.11: 8500`
[root@xjj consul] # curl 127.0.0.1:8500/v1/catalog/services {"consul": [], "httpd": [], "nginx": []} [root@xjj consul] #
Add consul-template to the ③ consul cluster to automatically join containers
Consul-Template is a daemon, which is 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 query the service directories in Consul, Key, Key-values, etc.
Cd consul/vim nginx.ctmplupstream http_backend {{range service "nginx"}} server {{.Address}}: {{.Port}}; {{end}} server {listen 100; server_name localhost 192.168.126.11; 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-develrz nginx-1.12.2.tar.gztar zxvf nginx-1.12.2.tar.gz-C / optcd / opt/nginx-1.12.10./configure-- prefix=/usr/local/nginxmake & & make install--vim / usr/local/nginx/conf/nginx.conf 19th include vhost/*.conf # 19 Line add Virtual host directory [root@xjj nginx-1.12.2] # mkdir / usr/local/nginx/conf/vhost'// create virtual host directory'[root@xjj nginx-1.12.2] # mkdir / var/log/nginx'// create log file directory'[root@xjj nginx-1.12.2] # / usr/local/nginx/sbin/nginx'/ / launch nginx' [root@xjj nginx-1.12.2] # netstat- Natp | grep nginxtcp 0 0 0.0 0. 0. 0. 0. 0. 0. 0. 0. LISTEN 104348/nginx: maste--# starts template Specify the template template file and generation path: [root@xjj consul] # consul-template-consul-addr 192.168.126.16 template "/ consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/xjj.conf:/usr/local/nginx/sbin/nginx-s reload"-- log-level=info# specify the template path / consul/nginx.ctmpl, generate to / usr/locla/nginx/conf/vhost/xjj.conf, then reload the nginx-s reload, and then define the log level Enter the monitoring status-# Master node opens a new terminal to view the configuration file: [root@xjj ~] # cat / usr/local/nginx/conf/vhost/xjj.conf# to access the Web page in this pool, you have to visit 192.168.126.11 Web 8080, and it is a polling mechanism. If you cannot access it here, you can reload nginx and try upstream http_backend {server 192.168.126.1281. Server 192.168.126.12 server_name localhost 82;} server {listen 8080; server_name localhost 192.168.126.11; access_log / var/log/nginx/xjj.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;}}
④ adds a new nginx container node to test automatic updates
[root@localhost ~] # docker run-itd-p 85:80-- name xjj05-h xcf05 nginx # adds a nginx container node to test the service discovery and configuration update function [root@xjj ~] # cat / usr/local/nginx/conf/vhost/xcf.conf upstream http_backend {server 192.168.126.12 server 82 Server 192.168.126.12 server 85;} server {listen 8080; server_name localhost 192.168.126.11; access_log / var/log/nginx/xcf.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;}}
⑤ tests whether the access proxy server can complete proxy access polling
Http://192.168.126.11:80/docker logs-f xjj01docker logs-f xjj02docker logs-f xjj05
⑥ consul multi-node configuration
# add a server with an existing docker environment to join the existing cluster: consul agent\-server\-bootstrap\-ui\-data-dir=/var/ib/consul-data\-bind=192.168.126.11\-client=0.0.0.0\-node=consul-server02\-enable-script-checks=true\-datacenter=dc1\-join 192.168.126.15 & > / var/log/consul.log &-enable-script-checks=true: set the check server Make it available-datacenter: data center name-join: add to the existing cluster "what is the method of building a Docker Consul cluster environment"? that's it. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.