Docker Compose container arrangement

This chapter summarizes the points of knowledge:

Docker Compose container arrangement
Building an Autodiscover Docker Service Architecture
Implement container services to automatically join the Nginx cluster

**Docker Compose Container Layout Function**
Docker compose, predecessor of Fig, is a tool for defining and running multiple docker containers
 Using Docker Compose no longer requires shell scripts to start containers
 Docker Compose is ideal for combining scenarios developed with multiple containers
 Can perform operations on multiple containers
**Docker Compose container arrangement**
YAML is an intuitive data serialization format for markup languages
 File format and writing considerations
 Table tab 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 horizontal bar
 Comment with #.
Use single quotation marks if special characters are included
 Boolean values must be enclosed in quotation marks

Docker Compose Configuration Common Fields

Buildockerfile context Specifies Dockerfile filename Build Mirror Context Path
 image Specifies Mirror
 command executes commands, overriding default commands
 The container name specifies the container name, because the container name is unique, you cannot scale if you specify a custom name
 deploy specifies deployment and run service-related configurations and can only be used in swarm mode
 Environment adds environment variables
 networks Join Network
 Ports expose container ports, but ports cannot be less than 60
 volumes mounts a host or command volume, just like a data volume
 Restart restart policy
 hostname Container Host Name

Common Docer Compose commands

build rebuilds services
 ps List Containers
 up Create and Start Containers
 exec executes commands in containers
 scale specifies the number of service container startups
 top Displays Container Processes
 logs View Container Output
 down Delete Containers, Networks, Data Volumes and Mirrors
 stop/start/restart Stop/start/restart services

Compose command description

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

Consul

Consul is an open source tool introduced by HashCorp to implement services and configurations for distributed systems
 Features of Consul
 Consul supports health checks, allowing storage of key-value pairs
 Consistency protocol uses Raft algorithm to ensure high availability of services
 Membership management and message broadcasting use GOSSIP protocol, support ACL access control list, seamlessly work with docker

Building an Autodiscover Docker Service Architecture

Establish Consul Service

Deploy Consul's agent on each node that provides services
 Consul agent has two modes of operation
Server
Client
 Server and Client are just differences at the Conusl cluster level, not related to the application services built on top of Cluster

consul cluster architecture, found docker containers registered in nginx, monitors the status of node servers.
nginx in consul server servers acts as a reverse proxy to poll for access to a container in the server pool.When a user accesses the proxy port, he or she can access several containers in the back, and our ports are mapped twice.Configuration files can be modified uniformly on consul servers

-------------------------------------------------------------------- docker-compose container arrangement---------------------------------------------------

#The shared directory of the host is the nginx package, and we mount it under / opt / directory
mount.cifs //192.168.100.25/LNMP /opt/
#Create container catalog
mkdir /root/compose_ngin
#Create nginx container image
cd compose_nginx/

mkdir nginx

cd nginx/

cd /opt/

cp nginx-1.12.0.tar.gz /root/compose_nginx/nginx/

cd /root/compose_nginx/nginx/

#Add nginx startup script
vim run.sh 
#!/bin/bash
/usr/local/nginx/sbin/nginx

[root@localhost nginx]# vim Dockerfile
FROM centos:7
MAINTAINER this is nginx <chen>
RUN yum -y update
RUN yum -y install wget pcre-devel zlib-devel make zlib gcc gcc-c++ openssl-devel net-tools
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
WORKDIR /root/nginx
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]

Write docker-compose orchestration to install nginx

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

[root@localhost compose_nginx]# mount.cifs //192.168.100.25/compose /mnt/
Password for root@//192.168.100.25/compose:   
[root@localhost compose_nginx]# cd /mnt/
[root@localhost mnt]# ls
consul_0.9.2_linux_amd64.zip  consul-template_0.19.3_linux_amd64.zip  docker-compose
[root@localhost mnt]# cp -p docker-compose /usr/local/bin/
[root@localhost mnt]# chmod +x /usr/local/bin/docker-compose 

#Turn on routing forwarding
[root@localhost compose_nginx]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1[root@localhost compose_nginx]# sysctl -p
net.ipv4.ip_forward = 1

Opening nginx with a layout tool

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

[root@localhost compose_nginx]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                                         NAMES
66d7cee5692d        compose_nginx_nginx   "/run.sh"           11 seconds ago      Up 9 seconds        0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp   compose_nginx_nginx_1

We made a data volume We wrote a default home page in

[root@localhost compose_nginx]# ls
docker-compose.yml  nginx  wwwroot
[root@localhost compose_nginx]# cd wwwroot/
[root@localhost wwwroot]# vim index.html
<h1>this is chen web ! ! !</h1>

------------------------------------------------------------------------------------------------ consul deployment------------------------------------------------------------------------------------------------------------------------------------------

Automatic discovery registers multiple docker container nodes. To be managed by consul, you need to register. An agent that satisfies the search criteria submits a registration request, consul server automatically discovers to process the registration request, monitors the status of the container, template

[root@localhost ~]# mkdir consul
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
consul_0.9.2_linux_amd64.zip  consul-template_0.19.3_linux_amd64.zip  docker-compose

[root@localhost mnt]# cp consul_0.9.2_linux_amd64.zip /root/consul/
[root@localhost mnt]# cd /root/consul/
[root@localhost consul]# ls
consul_0.9.2_linux_amd64.zip
[root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip 
Archive:  consul_0.9.2_linux_amd64.zip
  inflating: consul              

[root@localhost consul]# mv consul /usr/bin/

#Use the conusl agent function
[root@localhost consul]# consul agent \
#Specify server functionality
#Participating Leaders
#ui interface
#Parameter Specify Location
#Bind Local Address
#Face all nodes
#Mixed local node names are output to the log and run in the background
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.136.142 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 37957

#View Cluster Information
[root@localhost consul]# consul members
Node             Address               Status  Type    Build  Protocol  DC
consul-server01  192.168.136.142:8301  alive   server  0.9.2  2         dc1
#There's only one, it's the boss
[root@localhost consul]# consul info | grep leader
        leader = true
        leader_addr = 192.168.136.142:8300

Second Node Server (Create Multiple Containers) Container Service automatically joins the nginx cluster

[root@localhost ~]# docker run -d \
> --name=registrator \ #Specify container name
> --net=host \  #Specify Host
> -v /var/run/docker.sock:/tmp/docker.sock \ #Specify data volume host directory, container directory
> --restart=always \ #restart
> gliderlabs/registrator:latest \ #Specify local mirror
> -ip=192.168.136.167 \ #Specify your local address
> consul://192.168.136.142:8500 #Specify the address of consul

#Two nginx service containers
[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
#Two apache service 5 containers
[root@localhost ~]# docker run -itd -p:88:80 --name test-03 -h test02 httpd
[root@localhost ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
#View all containers
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                           COMMAND                  CREATED              STATUS              PORTS                NAMES
00a94fe6fd0c        httpd                           "httpd-foreground"       6 seconds ago        Up 5 seconds        0.0.0.0:89->80/tcp   test-04
b47b89287e43        httpd                           "httpd-foreground"       38 seconds ago       Up 37 seconds       0.0.0.0:88->80/tcp   test-03
9b695e1d8660        nginx                           "nginx -g 'daemon of..."   About a minute ago   Up About a minute   0.0.0.0:84->80/tcp   test-02
3cbf17118dab        nginx                           "nginx -g 'daemon of..."   2 minutes ago        Up 2 minutes        0.0.0.0:83->80/tcp   test-01
055caf398060        gliderlabs/registrator:latest   "/bin/registrator -i..."   3 minutes ago        Up 3 minutes                             registrator

Verify that http and nginx services are registered with consul

consul server

Consul-Templatee Is a daemon for real-time queries Consul Cluster information and update on file system
//Generate a configuration file with any number of specified templates.After the update is complete, you can choose to run the shell command to perform the update operation and reload nginx,Consul-template
//You can query the service directory in Consul, key, key-values, etc.
//This powerful abstraction and query language template make Consul-template particularly suitable for dynamic configuration file creation
[root@localhost ~]# cd /root/
[root@localhost ~]# ls
anaconda-ks.cfg  compose_nginx  initial-setup-ks.cfg  Public Document Template Music
chen             consul         download                  Picture Desktop Video
[root@localhost ~]# cd consul/
[root@localhost consul]# ls
consul_0.9.2_linux_amd64.zip
[root@localhost consul]# 
#Prepare temlate nginx template file

[root@localhost consul]# vim nginx.ctmpl
upstream http_backend {
  {{range service "nginx"}}
   server {{.Address}}:{{.port}};
   {{end}}
}

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

#Install nginx
[root@localhost nginx-1.12.0]# yum install gcc gcc-c++ pcre-devel zlib-devel -y
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx

[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
#There are events, http,if,location, fields in nginx
 19     include     vhost/*.conf;
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/conf/
[root@localhost conf]# mkdir vhost
[root@localhost consul]# cd /var/log/
[root@localhost log]# mkdir /var/log/nginx
[root@localhost log]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
[root@localhost log]# nginx
[root@localhost log]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      107612/nginx: ma
[root@localhost opt]# cd /mnt/
[root@localhost mnt]# cp consul-template_0.19.3_linux_amd64.zip /root/
[root@localhost mnt]# cd /root/

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

#Enable Templates
[root@localhost ~]# consul-template -consul-addr 192.168.136.142:8500 \
> -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info

#coul server is in monitoring state, we open another terminal with the same server

#Validation

#Add a nginx container node
 Add a nginx container node to test service discovery and configuration updates
 //Register with the registrator server
dockers run -itd -p:85:80 --name test-05 -h test05 nginx
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05

Tags: Linux Nginx Docker vim yum

Posted on Mon, 13 Jan 2020 19:18:17 -0500 by Topshed