Detail docker compose, consul!!

Keywords: Docker docker compose consul

Docker Compose, predecessor of Fig, is a tool for defining and running multiple Docker containers.
Using Docker Compose no longer requires shell I scripts to start containers
Docker Compose is ideal for scenarios where multiple containers are used together for development.

Consul is an open source tool from HashiCor that enables service discovery and configuration of distributed systems, seamlessly matching lightweight containers such as Docker.

Features of Consul
Supports health checks, allowing storage of key-value pairs
Based on Golong language, strong portability
Supporting ACL access control

Docker Consul Container Service Update and Discovery

Container Service Update and Discovery Topology

Docker Compose container arrangement
YAML is an intuitive data serialization format for markup languages
File format and writing considerations
tab key indentation is not supported, space indentation is required
Separate spaces are usually indented at the beginning
Compress a space after a character, such as a colon, comma, or bar (-)
Note with #
Separate quotation marks if special characters are included
Boolean values must be enclosed in quotation marks

docker compose configuration common fields

Common docker compose commands

Compose Deployment

Environment Deployment All Hosts Install docker environment (content is docker base)

yum install docker-ce -y

Download compose (upload docker_compose)

curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
cp -p docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose

mkdir /root/compose_nginx
 


Edit docker-compose.yml file

vim /root/compose_nginx/docker-compose.yml
version: '3'
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 1216:80
      - 1217:443
    networks:
      - cluster
    volumes:
      - ./wwwroot:/usr/local/nginx/html
networks:
  cluster:

Build and Start

[root@docker compose_nginx]#  docker-compose -f docker-compose.yml up -d

See

[root@docker compose_nginx]# docker-compose ps

consul deployment

Server: 192.168.80.4 Docker-ce, Compose 3,Consul, Consul-template
 Server: 192.168.80.3 Docker-ce,registrator

Template template (update)
registrator (automatic discovery)
Each container built by the back-end registers with the registrator, controls the consul to complete the update operation, and triggers the consul template to make a hot update
Core Mechanisms: consul: Automatic discovery, automatic updating, serving containers (add, delete, life cycle)

[root@docker ~]# mkdir /root/consul
[root@docker ~]# cd consul/
[root@docker consul]# ls
consul_0.9.2_linux_amd64.zip     //Need to upload
[root@docker consul]# unzip consul_0.9.2_linux_amd64.zip  
[root@docker consul]# mv consul  /usr/bin/
[root@docker consul]# consul agent \
-server \		Server
-bootstrap \	Front End Frame
-ui \		Accessible web Interface
-data-dir=/var/lib/consul-data \
-bind=192.168.80.4 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &

View cluster information

[root@docker consul]# consul members

[root@consul consul]# consul info | grep leader


Getting cluster information through httpd api

curl 127.0.0.1:8500/v1/status/peers   //View Cluster server Members
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
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/nodes//cluster node details


Container services automatically join consul clusters

  1. Install Gliderlabs/Registrator Gliderlabs/Registrator
    You can check the automatic registration of container status and unregister the service of the docker container to the Service Configuration Center.
    Consul, Etcd, and SkyDNS2 are currently supported.
    At node 192.168.8.3, do the following:
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.80.3 \
consul://192.168.80.4:8500


2.Test if service discovery is working

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

  1. Verify that http and nginx services are registered with consul
    Browser Input http://192.168.80.4:8500"Click NODES" and then "consurl-server01" to see five services.


##View service on consul server

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

  1. Install consul-template
    Consul-Template is a daemon that queries the Consul cluster information in real time.
    And update any number of specified templates on the file system to generate a configuration file.
    You can choose to run the shell command to perform the update and reload Nginx.Consul-Template
    You can query the service directory, Key, Key-values, and so on in Consul.
    This powerful abstraction and query language template makes Consul-Template particularly suitable for dynamic configuration file creation.
    For example, create Apache/Nginx Proxy Balancers, Haproxy Backends
[root@docker consul]# vim /root/consul/nginx.ctmpl
upstream http_backend {
  {{range service "nginx"}}
   server {{.Address}}:{{.Port}};   #The variable referenced here points to the back-end address and port (dynamic change)
   {{end}}
}

server {
  listen 83;
  server_name localhost 192.168.80.4;		#IP address of the reverse proxy (IP of the NG service shown on the front end)
  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;		#Real IP Backend
    proxy_set_header Client-IP $remote_addr;	
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;	#Forward Address
    proxy_pass http://http_backend;
  }
}

6. Compile and install nginx

yum install gcc pcre-devel zlib-devel -y
tar zxvf nginx-1.12.0.tar.gz  -C /opt
[root@docker opt]# cd nginx-1.12.2/ 
./configure --prefix=/usr/local/nginx
make && make install
  1. Configure nginx
vim /usr/local/nginx/conf/nginx.conf
http {
     include       mime.types;		#Default
     include  vhost/*.conf;         #Add Virtual Host Directory (consul dynamically generated configuration files will be placed here)
     default_type  application/octet-stream;

Create Virtual Host Directory

mkdir /usr/local/nginx/conf/vhost

Create log file directory

mkdir /var/log/nginx

start nginx

usr/local/nginx/sbin/nginx
  1. Configure and start the template

Upload the consul-template_0.19.3_linux_amd64.zip package to the / root directory

unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin/

Associate subprofile operations in nginx virtual directories
c

onsul-template -consul-addr 192.168.80.4:8500  \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/my.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info

Open another terminal to view the build profile

Add a nginx container node
Add a nginx container node to test service discovery and configuration updates
//Register with the registrator server
[root@server ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx

Automatically update on consul server

View on nginx server:

View the logs of two nginx containers and request normal polling on each container node

docker logs -f test-01
docker logs -f test-02


Posted by skippy111 on Wed, 08 Sep 2021 15:14:09 -0700