Compose orchestration, consul cluster, template for Docker container

docker compose container arrangement

(1) The predecessor Fig of docker compose, which is a tool for defining and running multiple docker containers
(2) Using docker compose eliminates the need to use shell scripts to launch containers
(3) docker compose is well suited for scenarios where multiple containers are used together for development

YAML is an intuitive data serialization format for markup languages

File format and writing considerations:

  • Tab tab key indentation is not supported and space indentation is required
  • Usually two spaces are indented at the beginning
  • Indent a space after a character, such as a colon, comma, or bar
  • Note with well number
  • Use single quotation marks if special characters are included
  • Boolean values must be enclosed in quotation marks

docker compose configuration common fields

field describe
build dockerfile context Specify Dockerfile File Name to Build Mirror Context Path
image Specify Mirror
command Execute commands, overwriting default commands
container name Specify the container name, because the container name is unique, you cannot scale if you specify a custom name
deploy Specify deployment and run service-related configurations, which can only be used in Warm mode
environment Add environment variables
networks Join the network
ports Expose container ports, same as -p, but no less than 60 ports
volumes Mount host path or command volume
restart Restart policy, default no,always, no-failure,unless-stoped
hostname Container host name

Common docker compose commands

field describe
build Rebuild Services
ps List containers
up Create and Start Containers
exec Execute commands inside containers
scale Specify a service container startup number
top Show container processes
logs View Container Output
down Delete containers, networks, data volumes, and mirrors
stop/start/restart Stop/Start/Restart Service

Compoose command description

Basic usage format

docker-compose [options] [COMMAND] [ARGS...]

docker-compose option

--verbose outputs more debug information
 --version Print version and exit
 -f, --file FILE uses a specific compose template file, defaulting to docker-compose.yml
 -p, --project-name NAME specifies the project name, using the directory name by default

1. Install docker containers and compose orchestration tools

[root@localhost ~]# yum install -y \
> yum-utils \    ##Set Source Tool
> device-mapper-persistent-data \    ##Mapping tool
> lvm2   ##Mapping tool
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
##Load Ali Cloud Mirror Source
[root@localhost ~]# yum install docker-ce -y  ##Install docker container
[root@localhost ~]# systemctl stop firewalld.service   ##Close Firewall
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start docker   ##Open docker container
[root@localhost ~]# systemctl enable docker  ##Start-up self-start
[root@localhost ~]# ps aux | grep docker  ##See if the docker process is turned on
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'   ##Mirror Acceleration
> {
>   "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]
> }
> EOF
{
        "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl daemon-reload   ##Overload Daemon
[root@localhost ~]# systemctl restart docker   ##Restart docker service
[root@localhost ~]# mount.cifs //192.168.100.100/LNMP-C7 /mnt/
Password for root@//192.168.100.100/LNMP-C7:  
[root@localhost ~]# cd /mnt/docker/
[root@localhost docker]# cp -p docker-compose /usr/bin/   
##Copy docker-compose to/usr/bin/directory
[root@localhost docker]# docker-compose -v   ##View version information
docker-compose version 1.21.1, build 5a3f1a3

2. Create an instance of Nginx orchestration

[root@localhost docker]# cd ~
[root@localhost ~]# mkdir compose-nginx  ##Create working directory
[root@localhost ~]# cd compose-nginx/
[root@localhost compose-nginx]# mkdir nginx   ##Create directory for Nginx
[root@localhost compose-nginx]# cd nginx/
[root@localhost nginx]# vim run.sh  ##Edit Startup Script
#!/bin/bash
/usr/local/nginx/sbin/nginx   ##Start the Nginx service
[root@localhost nginx]# mkdir /abc
[root@localhost nginx]# mount.cifs //192.168.100.3/LNMP-C7 /abc/
Password for root@//192.168.100.3/LNMP-C7:  
[root@localhost nginx]# cp /abc/nginx-1.12.2.tar.gz ./   ##Copy the compressed package of Nginx to the current directory
[root@localhost nginx]# vim Dockerfile  ##Write container file
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.2.tar.gz /usr/local/src
WORKDIR /usr/local/src
WORKDIR nginx-1.12.2
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]# cd ../
[root@localhost compose-nginx]# vim docker-compose.yml   ##Create compose template script
version: '3'   ##Edition
services:      ##service
    nginx:
        hostname: nginx  ##Container host name
        build:
            context: ./nginx  ##File path to create container
            dockerfile: Dockerfile
        ports:
            - 1216:80   ##Mapping Port
            - 1217:443
        networks:   
            - abc      ##Network name
        volumes:
            - ./wwwroot:/usr/local/nginx/html   ##Data Volume Associated Host Site Directory
networks:     ##External Declaration Network
    abc:
[root@localhost compose-nginx]# docker-compose -f docker-compose.yml up -d
##Execute compose script to open, -d daemon
[root@localhost compose-nginx]# cd wwwroot/
[root@localhost wwwroot]# vim index.html
this is test!!!   ##Edit Home Page Content
[root@localhost wwwroot]# yum install tree -y
[root@localhost wwwroot]# tree ~   ##View the entire required content structure
/root
├── anaconda-ks.cfg
├── compose-nginx
│   ├── docker-compose.yml   ##Layout Template
│   ├── nginx
│   │   ├── Dockerfile   ##Container scripts
│   │   ├── nginx-1.12.2.tar.gz    ##Installation package
│   │   └── run.sh  ##Start Service Script
│   └── wwwroot
│       └── index.html   ##Home

3. View Web Pages with Browser

Overview of consul

consul is an open source tool for service discovery and configuration of distributed systems

Characteristic of consul:

(1) consul supports health checks, allowing storage of key-value pairs
 (2) Consistency protocol uses Raft algorithm to ensure high availability of services
 (3) Membership management and message broadcasting use GOSSIP protocol to support ACL access control

Easy to deploy and seamless fit with lightweight containers like docker

Set up consul service

  • Deploy and run consul and agent on each node that provides services
  • consul agent has two modes of operation: server, client
  • The distinction between server and client knowledge consul cluster level is independent of the application services built on top of the cluster

Architecture Topology Diagram

Experimental environment

Consul server 192.168.13.128 Docker-ce, Compose 3, Consul, Consul-template
 client customer node 192.168.13.129 Docker-ce, registrator

1. Configure consul server

[root@consul ~]# mkdir consul
[root@consul ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/
Password for root@//192.168.100.3/LNMP-C7:  
[root@consul ~]# cd /mnt/docker/                         
[root@consul docker]# cp consul_0.9.2_linux_amd64.zip /root/consul/
[root@consul docker]# cd /root/consul/
[root@consul consul]# unzip consul_0.9.2_linux_amd64.zip  ##Unzip consul
Archive:  consul_0.9.2_linux_amd64.zip
    inflating: consul                  
[root@consul consul]# mv consul /usr/bin/   ##Easy system identification
[root@consul consul]# consul agent \   ##agent
> -server \   ##Provide server
> -bootstrap \
> -ui \   ##web access interface
> -data-dir=/var/lib/consul-data \   ##Data Store Directory
> -bind=192.168.13.128 \   ##Local Address
> -client=0.0.0.0 \   ##All Nodes
> -node=consul-server01 &> /var/log/consul.log &   ##Specify local node name
[root@consul consul]# consul members   ##View Cluster Information
Node             Address              Status  Type    Build  Protocol  DC
consul-server01  192.168.13.128:8301  alive   server  0.9.2  2         dc1
[root@consul consul]# consul info | grep leader  ##View leader
                leader = true
                leader_addr = 192.168.13.128:8300
[root@consul consul]# systemctl stop firewalld.service   ##Close Firewall
[root@consul consul]# setenforce 0

##Getting cluster information 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 registered services
[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

2. Configure client Node Server

[root@client ~]# docker run -d \
> --name=registrator \   ##Install registrator auto-registration 
> --net=host \   ##Specify Network
> -v /var/run/docker.sock:/tmp/docker.sock \  ##Specify Data Volume
> --restart=always \    
> gliderlabs/registrator:latest \   ##image
> -ip=192.168.13.129 \    ##Local Address
> consul://192.168.13.128:8500 ##consul server address

3. Testing the discovery function on the node server

[root@client ~]# docker run -itd -p 83:80 --name test-01 -h test01 nginx
[root@client ~]# docker run -itd -p 84:80 --name test-02 -h test02 nginx
[root@client ~]# docker run -itd -p 88:80 --name test-03 -h test03 httpd
[root@client ~]# docker run -itd -p 89:80 --name test-04 -h test04 httpd
[root@client ~]# systemctl stop firewalld.service ##Close Firewall
[root@client ~]# setenforce 0

4. Verify that http and Nginx services are registered with consul

Overview of consul-template

Consul-based auto-replacement profile application;
You can query the service directory in Consul: Key, Key-values, etc.
Especially suitable for dynamic configuration file creation;
Is a daemon that queries consul cluster information in real time;

1. Configure template files on consul server

[root@consul consul]# vim /root/consul/nginx.ctmpl  
upstream http_backend {           ##Declare Backend Server Pool
        {{range service "nginx"}}
         server {{.Address}}:{{.Port}};
         {{end}}
}

server {
             listen 1216;   ##Proxy Port
             server_name localhost 192.168.13.128;   ##Local Address
             access_log /var/log/nginx/kgc.cn-access.log;
             index index.html index.php;
             location / {
                    proxy_set_header HOST $host;    ##Header Information
                    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; ##Jump back-end server pool
                    }
}

2. Install Configuration Nginx on consul server

[root@consul consul]# cd /mnt/
[root@consul mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/   ##Unzip Nginx
[root@consul mnt]# cd /opt/nginx-1.12.2
[root@consul nginx-1.12.2]# yum install gcc gcc-c++ pcre-devel zlib-devel -y ##Install Components
[root@consul nginx-1.12.2]# ./configure --prefix=/usr/local/nginx  ##To configure
[root@consul nginx-1.12.2]# make && make install
[root@consul nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf  ##Modify the Nginx configuration file
 17 http {
 18     include       mime.types;
 19     include     vhost/*.conf;   ##Specify Virtual Host Directory
 [root@consul nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost  ##Create Virtual Host Directory
[root@consul nginx-1.12.2]# mkdir /var/log/nginx   ##Create log file directory in template
[root@consul nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  ##Create Soft Connections
[root@consul nginx-1.12.2]# nginx   ##Open the Nginx service
[root@consul nginx-1.12.2]# netstat -natp | grep nginx

3. Configure and start templeta on consul server

[root@consul nginx-1.12.2]# cd /mnt/docker/
[root@consul docker]# cp consul-template_0.19.3_linux_amd64.zip /root/
[root@consul docker]# cd /root/
[root@consul ~]# unzip consul-template_0.19.3_linux_amd64.zip  ##Install templeta
Archive:  consul-template_0.19.3_linux_amd64.zip
    inflating: consul-template         
[root@consul ~]# mv consul-template /usr/bin
[root@consul ~]# consul-template -consul-addr 192.168.13.128:8500 \  ##Specify consul local address
> -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
##Specify path to template
> --log-level=info   ##log level
[root@consul ~]# cd /usr/local/nginx/conf/vhost/
[root@consul vhost]# ls
kgc.conf   ##Automatically generate profile templates
upstream http_backend {

         server 192.168.13.129:83;   ##Automatically generate back-end container service address and port number

         server 192.168.13.129:84;

}

server {
             listen 1216;
             server_name localhost 192.168.13.128;
             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;
                    }
}

View with browser

Visit address 128 can also be viewed on the back-end node server through docker logs-f test-01

4. Create a nginx container node on the client side to detect service discovery and configuration updates. Do not close the firewall

[root@localhost ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx
##Continue browser access to 128 servers
[root@client ~]# docker logs -f test-05
192.168.13.128 - - [09/Jan/2020:11:05:39 +0000]
##Load balancing is achieved by polling or 128 service-side access nodes

Tags: Linux Nginx Docker yum vim

Posted on Tue, 04 Feb 2020 18:23:26 -0800 by PupChow