容器編排docker-compose


一、compose簡介

   編排功能是復雜系統是否具有靈活可操作性的關鍵。特別在docker應用場景中,編排意味着用戶可以靈活的對各種容器資源實現定義和管理。compose作為docker官方編排工具,它可以讓用戶通過編寫一個簡單的模板文件,快速的創建和管理基於docker容器的應用集群。

    compose項目是docker官方的開源項目,負責實現對基於docker容器的多應用服務的快速編排。其代碼目前在https://github.com/docker/compose上開源。

    compose允許用戶通過一個單獨的docker-compose.yml模板文件來定義一組相關聯的應用容器為一個服務棧。

    compose幾個概念

1. 任務:一個容器被稱為一個任務。任務擁有獨一無二的ID,在同一個服務中的多個任務序號依次遞增。

2. 服務:某個相同應用鏡像的容器副本集合,一個服務可以橫向擴展為多個容器實例。

3. 服務棧:由多個服務組成,相互配合完成特定業務,如web應用服務、數據庫服務共同構成web服務棧,一般由一個docker-compose.yml文件定義。

    compose的默認管理對象是服務棧,通過子命令對棧中的多個服務進行便捷的生命周期管理。compose項目由python編寫,實現上調用了docker服務提供的API來對容器進行管理。因此,只要所操作的平台支持docker API,就可以在其上利用compose來進行編排管理。

二、安裝docker-compose

1. 安裝compose之前,要先安裝docker引擎。compose可以通過Python的pip工具進行安裝。

[root@web01 ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 664 100 664 0 0 4337 0 --:--:-- --:--:-- --:--:-- 4368

[root@web01 ~]# yum install -y python-pip

[root@web01 ~]# pip install --upgrade pip

[root@web01 ~]# pip -V
pip 20.2.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)

[root@web01 ~]# pip install  -U  docker-compose

如果執行過程中,報錯1:

ERROR: Cannot uninstall 'requests'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

解決辦法:

[root@web01 ~]# pip install --ignore-installed requests

報:2:

ERROR: After October 2020 you may experience errors when installing or updating packages. This is because pip will change the way that it resolves dependency conflicts.

We recommend you use --use-feature=2020-resolver to test your packages with the new resolver before it becomes the default.

jsonschema 3.2.0 requires six>=1.11.0, but you'll have six 1.9.0 which is incompatible.

解決辦法:

[root@web02 ~]# pip install six --user -U 

2. 檢查是否安裝成功

[root@web01 ~]# docker-compose -v
/usr/lib/python2.7/site-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
from cryptography.hazmat.backends import default_backend
docker-compose version 1.26.2, build unknown

三、compose模板文件

模板文件是使用compose的核心,涉及的指令關鍵字比較多。默認的模板文件名稱為docker-compose.yml,格式為yaml格式,目前最新的版本為v3.

[root@web01 ~]# mkdir /docker-compose

[root@web01 ~]# cd /docker-compose/

[root@web01 docker-compose]# vi docker-compose.yml


注:提前pull下mysql和wordpress兩款鏡像

[root@web01 docker-compose]#  docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wordpress latest 420b971d0f8b 9 days ago 546MB
mysql 5.7 ef08065b0a30 10 days ago 448MB

四、啟動docker-compose

[root@web01 docker-compose]# docker-compose up

1. 查看相關信息

[root@web01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7fcbdbe8f07 wordpress:latest "docker-entrypoint.s…" 39 minutes ago Up 3 minutes 0.0.0.0:32770->80/tcp docker-compose_wordpress_1
d9bc706dc5a7 mysql:5.7 "docker-entrypoint.s…" 39 minutes ago Up 6 minutes 3306/tcp, 33060/tcp docker-compose_db_1

[root@web01 ~]# docker volume ls
DRIVER VOLUME NAME
local docker-compose_db_data
local docker-compose_web_data

[root@web01 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
406aa541b3b3 bridge bridge local
181f52e786c7 docker-compose_default bridge local
1f756a7d8624 host host local
a358bdb73e1b none null local

2. 動態擴展wordpress節點數

[root@web01 docker-compose]# docker-compose scale wordpress=3
/usr/lib/python2.7/site-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
from cryptography.hazmat.backends import default_backend
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting docker-compose_wordpress_1 ... done
Creating docker-compose_wordpress_2 ... done
Creating docker-compose_wordpress_3 ... done

[root@web01 docker-compose]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1d8755ab2f0 wordpress:latest "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:32771->80/tcp docker-compose_wordpress_3
629fdb7cae28 wordpress:latest "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:32772->80/tcp docker-compose_wordpress_2
c7fcbdbe8f07 wordpress:latest "docker-entrypoint.s…" 43 minutes ago Up 7 minutes 0.0.0.0:32770->80/tcp docker-compose_wordpress_1
d9bc706dc5a7 mysql:5.7 "docker-entrypoint.s…" 43 minutes ago Up 10 minutes 3306/tcp, 33060/tcp docker-compose_db_1

3. 配置反向代理,訪問wordpress

[root@web02 ~]# yum install nginx -y

[root@web02 ~]# vim /etc/nginx/nginx.conf

[root@web02 ~]# cat /etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream wordpress{
server 192.168.0.208:32770;
server 192.168.0.208:32771;
server 192.168.0.208:32772;
}
server {
listen 80;
server_name localhost;

location / {
proxy_pass http://wordpress;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}
}
[root@web02 ~]# systemctl start nginx

[root@web02 ~]# netstat -lntup

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1210/master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11939/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1342/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1210/master
tcp6 0 0 :::22 :::* LISTEN 1342/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 700/dhclient
udp 0 0 127.0.0.1:323 0.0.0.0:* 642/chronyd
udp6 0 0 ::1:323 :::* 642/chronyd

4. 訪問

[root@web02 ~]# curl -v 192.168.0.184
* About to connect() to 192.168.0.184 port 80 (#0)
* Trying 192.168.0.184...
* Connected to 192.168.0.184 (192.168.0.184) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.0.184
> Accept: */*
>
< HTTP/1.1 302 Found
< Server: nginx/1.16.1
< Date: Sun, 20 Sep 2020 10:15:59 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 0
< Connection: keep-alive
< X-Powered-By: PHP/7.4.10
< Expires: Wed, 11 Jan 1984 05:00:00 GMT
< Cache-Control: no-cache, must-revalidate, max-age=0
< X-Redirect-By: WordPress
< Location: http://192.168.0.184/wp-admin/install.php
<
* Connection #0 to host 192.168.0.184 left intact

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM