Docker Native Arrangement Tool - compose+consul Cluster+template

Docker Native Layout Tool

1. docker-compose

concept

The Compose default management object is the project, which manages the life cycle of a set of containers in the project through subcommands.

The Compose project is implemented by Python, which calls the API provided by the Docker service to manage containers.

Configuration steps

1. Install Docker-ce engine

Simpler, no more verbose, go directly to the shell script

#!/bin/bash
#Docker Engine Deployment
#Install Dependent Packages
yum install yum-utils device-mapper-persistent-data lvm2 -y

#Set Ali Cloud Mirror Source
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

#Install Docker-ce
yum install -y docker-ce

#Turn off firewalls and enhanced security features
systemctl stop firewalld.service
setenforce 0

#Start Docker and set to Start-Up Self-Start
systemctl start docker.service
systemctl enable docker.service

#network optimization
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
service network restart 
systemctl restart docker

2. Deploy docker-compose

#Download and install compose
curl -L https://github.com/docker/compose/releases/download/1.25.1-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

#Title for easy invocation
chmod +x /usr/local/bin/docker-compose

#Build compose site
mkdir /root/docker_compose

3. Examples of compose exercises

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, since it is unique
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 log files for container output
down Delete containers, networks, data volumes, and mirrors
stop/start/restart Stop/Start/Restart Service

Collection Deployment nginx+Tomcat

#Create compose file
vim /root/docker-compose/docker-compose.yml
#compose version number, 1, 2, 3
version: '3.3'
#Service Name
services:
  nginx:
#host name
    hostname: nginx
#Directory, dockerfile script name required for mirroring
    build:
      context: ./nginx
      dockerfile: Dockerfile
#Map Host Port
    ports:
     - 1216:80
     - 1200:443
#Workgroup: One name for the same cluster
    networks:
     - test
#Catalogs for creating datasets
    volumes:
     - ./wwwroot:/usr/local/nginx/html
  tomcat:
    hostname: tomcat
    build:
      context: ./tomcat
      dockerfile: Dockerfile
    ports:
     - 888:8080
    networks:
     - test
networks:
  test:

#Services that require a new mirror need to be placed on the compose worksite
[root@localhost docker_compose]# ls
docker-compose.yml  nginx  tomcat

#Build Containers
"-f"  Appoint compose file
docker-compose -f docker-compose.yml up -d

2. docker-consul cluster+template

Experimental preparation

Name role IP Address Pre-installed environment
centos7-1 master 192.168.142.66 Docker-ce,Compose 3,Consul,Consul-template
centos7-2 slave1 192.168.142.77 Docker-ce,registrator
centos7-min slave2 192.168.142.136 Docker-ce,registrator

Doker-consul cluster experimental steps

master side

//Install Consul
[root@localhost ~]# mkdir consul
[root@localhost ~]# cd consul/
[root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip
[root@localhost consul]# ls
consul  consul_0.9.2_linux_amd64.zip

Background boot

//Move files for easy identification
[root@localhost consul]# mv consul /usr/bin/

[root@localhost consul]# consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.142.66 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &

[root@localhost consul]# jobs
[1]+  Running               consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.142.66 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &

"Agent" uses agent proxy functionality

'-server'provides server functionality

"-bootstrap" participates in the election as leader

"-ui" provides user interface

'-data-dir'parameter storage location

'-bind'binding address

'-node'defines the node name

Check Cluster

//View Cluster Information
[root@localhost consul]# consul members
Node             Address              Status  Type    Build  Protocol  DC
consul-server01  192.168.142.66:8301  alive   server  0.9.2  2         dc1

//Filter leader information
[root@localhost consul]# consul info | grep leader
        leader = true
        leader_addr = 192.168.142.66:8300

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

curl 127.0.0.1:8500/v1/catalog/nodes //Cluster Node Details

Automatically join containers in node servers

//Install registrator
//This tool checks the status of the container for automatic registration and also unregisters the services of the docker container
[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.142.77 \
consul://192.168.142.66:8500

Test if the function is working

[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
[root@localhost ~]# docker run -itd -p:85:80 --name test-03 -h test03 httpd
[root@localhost ~]# docker run -itd -p:86:80 --name test-04 -h test04 httpd

Verify that the cluster was successfully established

Enter http://192.168.142.66:8500 in your browser, click NODES, and then click consurl-seerver01 to see five services (graphical interface)

Create template to proxy

Compile and install nginx manually

Nothing to say. Load.Version: nginx1.12.0

Install consul-template

Templates are daemons that query consul cluster information in real time and generate configuration files using template s in real time.

[root@localhost ~]# unzip consul-template_0.19.3_linux_amd64.zip
//Enable template command
[root@localhost ~]# mv consul-template /usr/bin/

Write nginx profile template

[root@localhost consul]# Template for VIM nginx.ctmpl //nginx configuration file
upstream http_backend {         //Name of server pool
  {{range service "nginx"}}
  server {{.Address}:{{.Port}};     //Invoke variables: address and port of the server node
  {{end}}
}

server {
  listen 110;
  server_name localhost 192.168.142.66;     //master End Address
  access_log /var/log/nginx/kgc.cn-access.log;
  index index.html index.php;
  location / {          //Setup of Request Header Information
    proxy_set_header HOST $host;
    proxy_set_header X-Real-IP $remote_addr;    //Backend Server IP
    proxy_set_header Client-IP $remote_addr;    //client's IP
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    //forward Jump Address
    proxy_pass http://Http_backend; //Request to jump to http_backend server pool
  }
}

Configure

//Change nginx profile
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
//Insert the following under line 18
include     vhost/\*.conf;                    //Add include to facilitate nginx recognition template generated profiles

//This file is not available yet and needs to be created
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/conf/
[root@localhost conf]# mkdir vhost //Create virtual machine directory

//Enable template template (will enter monitoring mode when enabled)
[root@localhost ~]# consul-template -consul-addr 192.168.142.66:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info

"-consul-addr" #Specify consul end address

"-template" #The rear left to right are: template file path, generated profile name, overloaded nginx

"--log-level" #Level of writing to log

Because templates are built on consul clusters, the nginx containers in slave s can be directly identified when templates are started

When a new nginx container is added later, a new container is added to the configuration file automatically after run ning directly

Keywords: Linux Docker Nginx curl Tomcat

Added by myleow on Thu, 16 Jan 2020 01:13:00 +0200