Docker Compose container choreography + Consul cluster

1, Docker Compose container orchestration

1. Docker Compose, formerly known as Fig, is a tool for defining and running multiple docker containers;
2. Using Docker Compose no longer requires using shell scripts to start containers;
3. Docker Compose is very suitable for scenarios where multiple containers are combined for development.
4. File structure of Docker Compose:

-----vim docker-compose.yml

YAML is a very intuitive data serialization format of markup language
The following is the document format and preparation Notes:

2, Docker Compose configuration:

(1) Common fields:

(2) Common commands:

(3) Compose command description:

2, Choreography example:

(1) First, optimize the network:

vim /etc/sysctl.conf
net.ipv4.ip_forward=1    ##Add at the end of the file

sysctl -p                  ##Amendment takes effect
systemctl restart network  ##service network restart 

(2) Copy the downloaded docker compose to the directory / usr/bin /

cp  -p  docker-compose  /usr/bin/

(3) Create working directory (copy nginx package to nginx directory):

[root@localhost ~]# mkdir compose_nginx
[root@localhost ~]# cd compose_nginx
[root@localhost compose_nginx]# mkdir nginx
[root@localhost compose_nginx]# cd nginx

[root@localhost nginx]# vim Dockerfile
FROM centos:7
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src
WORKDIR /usr/local/src
WORKDIR nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]

[root@localhost nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx

(4) To create an edit 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:
      - abc
    volumes:
      - ./wwwroot:/usr/local/nginx/html
networks:
  abc:

(5) At this time, you can use the tree command to view the file structure under the root directory:

[root@localhost compose_nginx]# tree ./    ##View tree
./
├── docker-compose.yml        ##Create template script
├── nginx
│   ├── Dockerfile            ##Create container script
│   ├── nginx-1.12.0.tar.gz   ##Source package
│   └── run.sh                ##Service script
└── wwwroot                   ##site

(6) Execute on:

docker-compose -f docker-compose.yml up -d

Check whether the mirror and container are normal and successfully created:

(7) We can create a web page in the site directory, and use the browser to access whether it can display normally:

[root@localhost compose_nginx]# cd wwwroot/
[root@localhost wwwroot]# vim index.html
<h1>this is nginx new web</h1>

Browser access (IP address is local address): http://192.168.220.131:1216/

3, Docker concul container service update and discovery:


(1)Consul:
1. Consul is an open-source tool launched by HashCorp to realize service discovery and configuration of distributed system;
2. Features of Consul:

Consumer supports health checks and allows key value pairs to be stored;
The consistency protocol uses the Raft algorithm to ensure the high availability of services;
GOSSIP protocol is adopted for member management and message broadcast, which supports ACL access control;

3. It is easy to deploy and can work seamlessly with lightweight containers such as Docker.
(2) Establish Consul service:
1. Every node that improves the service needs to deploy and run the agent of consumer
2. There are two operation modes of Consul agent:

server;
client;

3. server and client are just the differences at the cluster level, not the application services built on the cluster.

4, Build a consumer cluster:


Environment preparation: the first server downloads a nginx image for experiment use:

[root@localhost vhost]# docker pull nginx / / download an nginx image

[root@localhost vhost]# docker create -it nginx:latest /bin/bash / / create a container
be7904151f5d6cb110aba1aaa637dffeb8100c4d8761a1492e4b008dcd57d313

[root@localhost vhost]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
be7904151f5d        nginx:latest        "/bin/bash"         3 seconds ago       Created                                 xenodochial_black

Structure thinking chart:

Deployment process:

(1) To configure the consumer server:

[root@localhost ~]# mkdir consul
[root@localhost abc1]# cp consul_0.9.2_linux_amd64.zip /root/consul
[root@localhost abc1]# cd /root/consul

[root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip     ##decompression
[root@localhost consul]# mv consul /usr/bin/                    ##Easy system identification

//Establish Consul service:
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.220.131 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &


(2) To view cluster information:

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


(3) Cluster information can be obtained through httpd api:

[root@localhost consul]# curl 127.0.0.1:8500/v1/status/peers                 ##View cluster server members
[root@localhost consul]# curl 127.0.0.1:8500/v1/status/leaders               ##Raf leader in cluster
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/services             ##All services registered
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/nodes                ##Cluster node details
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/nginx                ##View nginx service information

(4) Let container service automatically join nginx cluster:

Configuration 192.168.220.140 Node:

docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.220.140 \
consul://192.168.220.131:8500


(5) Test whether the service and function are normal:

Create two containers, test-01 and test02, and specify port numbers 83 and 84:

[root@localhost ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
[root@localhost ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx


(6) Verify that both http and nginx services are registered with consumer:
• browser access: 192.168.220.131:8500
• click "NODES" -- > consurl-server01, the two services just created will appear

(7) To automatically join the container service into the Nginx cluster:
1,consul-template:

It is the application of Consul based automatic replacement configuration file;
You can query the service directory in Consul: Key, Key values, etc;
It is especially suitable for creating configuration files dynamically;
Is a daemons for real-time query of consumer cluster information;

2. To prepare the template nginx template file:

//Operate on the consumer server
//Create a template file:
vim /root/consul/nginx.ctmpl

upstream http_backend {
    {{range service "nginx"}}
     server {{.Address}}:{{.Port}};
     {{end}}
}

server {
       listen 1216;
       server_name localhost 192.168.220.131;
       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-Fprwarded-For $proxy_add_x_forwarded_for;
          proxy_pass http://http_backend;
                 }
       }


3. Compile and install a nginx service:

yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y

tar zxvf nginx-1.12.0.tar.gz -C /opt/

./configure --prefix=/usr/local/nginx

make && make install

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

4. To configure nginx:

[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf

//To add a virtual host directory in the http template:
http {
     include   mime.types;
     include vhost/*.conf;   ##Add virtual host directory 
     default_type application/octet-stream;
}

//To create a virtual host Directory:
[root@localhost nginx-1.12.0]# mkdir /usr/local/nginx/conf/vhost

//Create log file directory:
[root@localhost nginx-1.12.0]# mkdir /var/log/nginx

//Start nginx
[root@localhost nginx-1.12.0]# /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.12.0]# netstat -natp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      65476/nginx: master 

(8) Configure and start template:

1,Extract and copy to /bin Directory, easy to use:
[root@localhost abc]# unzip consul-template_0.19.3_linux_amd64.zip 

[root@localhost abc]# mv consul-template /usr/bin/


2. Start:

consul-template -consul-addr 192.168.220.131:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info


At this time, we can open another terminal to view the profile generated according to the template:
cat /usr/local/nginx/conf/vhost/kgc.conf

(9) In order to test the automatic update effect, we can create a nginx container node on the Registrar server to detect the service discovery and configure the update function:

[root@localhost ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx
bdc51a5c59e68c032e7466494fcb0212bae48fe939325845e00abb4840d0b48e

At this time, you will be prompted to update automatically in the consumer server monitoring to view the configuration file:

(10) In order to show the polling processing request, you can use the logs command to view the logs of three nginx containers, which will show the access from the same IP address:
Visit nginx homepage several times: 192.168.220.131:1216

[root@localhost ~]# docker logs -f test-01
192.168.220.131 - - [03/Jan/2020:15:01:57 +0000] "GET / HTTP/1.0" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"
192.168.220.131 - - [03/Jan/2020:15:02:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"
192.168.220.131 - - [03/Jan/2020:15:02:20 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"

[root@localhost ~]# docker logs -f test-02
192.168.220.131 - - [03/Jan/2020:15:02:18 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"
192.168.220.131 - - [03/Jan/2020:15:02:21 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"
192.168.220.131 - - [03/Jan/2020:15:02:24 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"
192.168.220.131 - - [03/Jan/2020:15:02:28 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"

[root@localhost ~]# docker logs -f test-05
192.168.220.131 - - [03/Jan/2020:15:01:57 +0000] "GET /favicon.ico HTTP/1.0" 404 153 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"
192.168.220.131 - - [03/Jan/2020:15:02:19 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"
192.168.220.131 - - [03/Jan/2020:15:02:22 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"
192.168.220.131 - - [03/Jan/2020:15:02:26 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"

It can be seen that all the accesses from 192.168.220.131 will be sent to the background docker for processing in the form of polling, realizing load balancing.

Tags: Linux Nginx Docker Firefox

Posted on Sun, 12 Jan 2020 21:26:18 -0500 by willfitch