Docker Compose's container orchestration

一、Docker Compose

(1) Overview of Docker Compose

  • The predecessor of Docker Compose is Fig. It is a tool for defining and running multiple Docker containers.
  • With Compose, there is no need to use shell scripts to start the container, but use YAML files to configure all the services required by the application
  • Then use a command to create and start all services based on the YAML file configuration
  • Docker Compose is very suitable for scenarios where multiple containers are combined for development

(2) 、 YAML

1. YAML is a very intuitive data serialization format with a markup language

2. File format and precautions for writing

  • Tab key indentation is not supported, and space indentation is required
  • Usually indented 2 spaces at the beginning
  • Indent 1 space after the character, such as colon, comma, bar
  • Comment with # sign
  • Use single quotes if it contains special characters
  • Boolean values ​​must be enclosed in quotation marks

(3) Commonly used fields for Docker Compose configuration

Field description
build dockerfile context Specify the Dockerfile file name to build the mirror context path
image Specified mirror
command Execute the command, overwrite the default command
container name Specify the container name, because the container name is unique, if you specify a custom name, you cannot scale
deploy Specify the deployment and operation service related configuration, which can only be used in Swarm mode
environment Add environment variables
networks Join the network
ports Expose the container port, the same as -p, but the port cannot be lower than 60
volumes Mount the host path or command volume
restart Restart policy, default no,always, no-failure,unless-stoped
hostname Container hostname

(4) Common commands of Docker Compose

Field description
build Rebuild the service
ps List containers
up Create and start the container
exec Execute commands in the container
scale Specify the number of service containers to start
top Show container process
logs View container output
down Delete containers, networks, data volumes and mirrors
stop/start/restart Stop/start/restart service

(5), Compose command description

Basic command format

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

docker-compose option

--verbose:输出更多调试信息
--version:打印版本并退出
-f,--file FILE:使用特定的compose模板文件,默认为docker-compose.yml
-p, --project-name NAME:指定项目名称,默认使用目录名称

Second, use docker-compose to build a consul environment

surroundings:

server nickname service
Server 1: 192.168.182.11 Docker-ce 、 Compose 、 Consul 、 Consul-temple
Server 2: 192.168.182.22 Docker-ce、Registrator

demand:

  • Realize the intercommunication between containers and containers under a single-machine network
  • Use Docker Compose to create a container
  • Build Consul service to realize automatic discovery and update

Install consul package

[root@localhost ~]# systemctl start firewalld
[root@localhost ~]# setenforce 0
关闭防火墙,系统自带保护机制
mkdir /root/consul
cd /root/consul/
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/bin/     ##移动到/usr/bin目录相当于可以直接被系统使用

consul agent \   ##构建agent功能
> -server \      ##server模式  
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.182.11 \
> -client=0.0.0.0 \        ##监听客户端所有地址
> -node=consul-server01 &> /var/log/consul.log &

Insert picture description here
View cluster information

consul members
consul info | grep leader

Insert picture description here
Obtain cluster information through httpd api

curl 127.0.0.1:8500/v1/status/peers /查看集群server成员
curl 127.0.0.1:8500/v1/status/leader /集群 Raf leader
curl 127.00.1:8500/v1/catalog/services //注册的所有服务
curl 127.0.0.1:8500/v1/catalog/nginx /查看 nginx 服务信息
curl 127.0.0.1:8500/v1/catalog/nodes //集群节点详细信息

--------------------------------------------The container service automatically joins the nginx cluster --------------------------------------------

1. Install Gliderlabs/Registrator (you can check the running status of the container and register automatically, and you can also cancel the service of the docker container to the service configuration center).
Currently, Consul, Etcd and SkyDNS2 are supported

Start another server for operation 192.168.182.22

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

2. Test whether the service function is normal

docker run -itd -p 83:80 --name test-1 -h test1 nginx
docker run -itd -p 84:80 --name test-2 -h test2 nginx
docker run -itd -p 88:80 --name test-3 -h test3 httpd
docker run -itd -p 89:80 --name test-4 -h test4 httpd

3. Verify that the http and nginx services are registered to the consul
browser, enter http://192.168.182.11:8500, click "NODES", and then click "consul-server", there will be 5 services
Insert picture description here
. View the services on the consul server

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

Insert picture description here
4. Install consul-template

Consul-Template is a daemon process used to query Consul cluster information in real time, update any number of specified templates on the file system, and generate configuration files. After the update is complete, you can choose to run the shell command to perform the update operation and reload Nginx. Consul-Template can query the service catalog, Key, Key-values, etc. in Consul.
This powerful abstract function and query language template can make Consul-Template particularly suitable for dynamically creating configuration files.
For example: create Apache/Nginx Proxy Balancers, Haproxy Backends

5. Prepare Template nginx template file

##Operate on consul

//在consul服务器上操作
vim /root/consul/nginx.ctmpl

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

server {
    
    
  listen 83;
  server_name localhost 192.168.200.40;
  access_log /var/log/nginx/gcc.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;
}
}

The following is the comment

vim /root/consul/nginx.ctmpl

upstream http_backend {
    
    					#群集模块模板
    {
    
    {
    
    range service "nginx"}}				#指定nginx服务项
     server {
    
    {
    
    .Address}}:{
    
    {
    
    .Port}};			#指向真实服务器的地址和端口“{
    
    {
    
    .Address}}” 此参数会根据consul群集的参数自动设置上去
     {
    
    {
    
    end}}						#结束语句(以上为轮询请求方式)
}

server {
    
    
       listen 88;					#对外提供的端口,可自行设置,只要不起冲突
       server_name localhost 192.168.126.10;		#本地反向代理的节点地址
       access_log /var/log/nginx/kgc.cn-access.log;	#访问日志文件目录(需手动创建)
       index index.html index.php;			#指定访问的index 首页类型
       location / {
    
    
          proxy_set_header HOST $host;			#反向代理的请求头部信息
          proxy_set_header X-Real-IP $remote_addr;	#真实服务器IP
          proxy_set_header Client-IP $remote_addr; 	#客户IP
          proxy_set_header X-Fprwarded-For $proxy_add_x_forwarded_for;	#Forward转发地址
          proxy_pass http://http_backend;		#反向代理指向upstream地址池
                 }
       }

6, compile and install nginx

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

Put the nginx compressed package in the /opt/ directory

tar zxvf nginx-1.12.0.tar.gz -C /opt
cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx
make && make install

7, configure nginx

vim /usr/local/nginx/conf/nginx.conf

Add under http module

http {
    
    
   include  mime.types;
   include  vhost/*.conf;   #添加虚拟主机目录
   default_type application/octet-stream;

Insert picture description here
// Create virtual host vhost file directory

mkdir /usr/local/nginx/conf/vhost

//Create log file directory

mkdir /var/log/nginx

//Start nginx

/usr/local/nginx/sbin/nginx

8. 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/

Enable template

consul-template -consul-addr 192.168.182.11:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
参数详解
-consul-addr 192.168.226.132:8500:指向consul服务端节点

-template "/root/consul/nginx.ctmpl:指定模板路径

:/usr/local/nginx/conf/vhost/test.conf:指定文件生成到vhost中并命名为test.conf

:/usr/local/nginx/sbin/nginx -s reload:不中断服务reload重载nginx服务让配置文件生效

–log-level=info:产生日志,级别为info(日志生成位置在nginx.ctml中定义“access_log”)
//另外打开-个终端查看生成配置文件
[root@localhost vhost]#cat /usr/local/nginx/conf/vhost/test.conf
upstream http_backend {
    
    
  
   server 192.168.182.11:83;
  
   server 192.168.182.11:84;
  
}

server {
    
    
  listen 83;
  server_name localhost 192.168.182.11;
  access_log /var/log/nginx/gcc.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;
}
}

Add a nginx container node, test server discovery and configuration update functions

//在registrator(192.168.182.11)服务端注册
docker run -itd -p 85:80 --name test-5 -h test5 nginx

Insert picture description here

Consul multi-node configuration

//添加一台已有docker环境的服务器192.168.182.33/24加入已有的群集中

consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/ib/consul-data \
-bind=192.168.182.33 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.182.11 &> /var/log/consul.log &


-enable-script-checks=true: 设置检查服务为可用
-datacenter: 数据中心名称
-join: 加入到已有的集群中

Guess you like

Origin blog.csdn.net/panrenjun/article/details/115290274