docker-compose 的使用和负载均衡的初探

前言

a. 本文主要为 Docker的视频教程 笔记。

b. 环境为 CentOS 7.0 云服务器

c. 上一篇:Docker 私有仓库

1. 简介和安装

docker-compose 是单机版的容器编排工具,Kubernetes 是可以跨服务器的联机版容器编排工具。

使用 yum install docker-compose 命令安装,提示:

Error: Package: python3-setuptools-39.2.0-10.el7.noarch (base)
Requires: /usr/bin/python3
Available: python3-3.6.8-13.el7.i686 (base)
Not found
Available: python34u-3.4.8-1.ius.centos7.x86_64 (ius)
Not found
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest

可以看出 docker-compose 依赖于 python3,而本机没有 python3(只有 python2,使用 python 命令默认会输出 python2 的相关内容),因此使用先 yum install python3 命令安装。之后再次安装 docker-compose 成功。

2. 基本使用

A. 首先编写 docker-compose 的 yaml 脚本

version: '3'

services:
    db:
      image: mysql:5.7
      volumes:
          - db_data:/var/lib/mysql
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: somewordpress
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress

    wordpress:
      depends_on:
        - db
      image: wordpress:latest
      volumes:
        - web_data:/var/www/html
      ports:
        - "80:80"
      restart: always
      environment:
        WORDPRESS_DB_HOST: db
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:
    web_data:

文件的开头要使用 version 指定版本。其中 services 定义要启动的容器, depends_on 表示本容器依赖的其它容器,注意 volumes 中定义的卷不要省略

B. 命令

docker-compose up 创建并启动全部容器
docker-compose up -d 创建并后台启动全部容器

docker-compose down 停止并删除全部容器
docker-compose restart 重启全部容器

docker-compose start 启动容器
docker-compose stop 停止容器

这些操作都必须在 docker-compose.yml 目录下进行

如执行 docker-compose up 后:

[root@VM_0_2_centos my_wordpress]# docker-compose up
Creating network "mywordpress_default" with the default driver
Creating volume "mywordpress_db_data" with default driver
Creating volume "mywordpress_web_data" with default driver
Pulling wordpress (wordpress:latest)...
latest: Pulling from library/wordpress
8559a31e96f4: Pull complete
e0276193a084: Pull complete
eb2d00c10344: Pull complete
……
Digest: sha256:6f609ebf8518069516df36f0ab504735f8e563c1e303c37eba4902e732fcc6c6
Creating mywordpress_db_1 ... done
Creating mywordpress_db_1 ...
Creating mywordpress_wordpress_1 ... done
Attaching to mywordpress_db_1, mywordpress_wordpress_1
db_1 | 2020-07-20 08:21:19+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.30-1debian10 started.
wordpress_1 | WordPress not found in /var/www/html - copying now...
db_1 | 2020-07-20 08:21:19+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1 | 2020-07-20 08:21:19+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.30-1debian10 started.
db_1 | 2020-07-20 08:21:20+00:00 [Note] [Entrypoint]: Initializing database files
db_1 | 2020-07-20T08:21:20.202983Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
wordpress_1 | Complete! WordPress has been successfully copied to /var/www/html

由于我本机没有 wordpress 镜像,会自动下载,之后按照 yaml 脚本启动镜像,前台启动会输出日志

docker容器如何做负载均衡_python

docker-compose 会自动以当前目录的名字加数字来命名容器。由于此时我的目录名为 mywordpress,启动的容器名为 mywordpress_wordpress_1 和 mywordpress_db_1

[root@VM_0_2_centos my_wordpress]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c5bf42783c8 wordpress:latest "docker-entrypoint.s…" 11 minutes ago Up 39 seconds 0.0.0.0:80->80/tcp mywordpress_wordpress_1
32fc54cde2ea mysql:5.7 "docker-entrypoint.s…" 11 minutes ago Up 39 seconds 3306/tcp, 33060/tcp mywordpress_db_1

3. 容器的单机编排

启动后,使用命令:

docker-compose scale <SERVICES_NAME>=<NUMBER>

动态调节容器的数量,如:

[root@VM_0_2_centos my_wordpress]# docker-compose scale wordpress=3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
WARNING: The "wordpress" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Starting mywordpress_wordpress_1 ... done
Creating mywordpress_wordpress_2 ... error
Creating mywordpress_wordpress_3 ... error

ERROR: for mywordpress_wordpress_3 Cannot start service wordpress: driver failed programming external connectivity on endpoint mywordpress_wordpress_3 (c891a22ed5380646b7da331d1036560ea896f77646a6c51e1a499355507203ea): Bind for 0.0.0.0:80 failed: port is already allocated

ERROR: for mywordpress_wordpress_2 Cannot start service wordpress: driver failed programming external connectivity on endpoint mywordpress_wordpress_2 (4026c1996cdefb65c1fc2e5ea7523bbdb6812d42515be7191be53aeaf09d3c9f): Bind for 0.0.0.0:80 failed: port is already allocated
ERROR: Cannot start service wordpress: driver failed programming external connectivity on endpoint mywordpress_wordpress_3 (c891a22ed5380646b7da331d1036560ea896f77646a6c51e1a499355507203ea): Bind for 0.0.0.0:80 failed: port is already allocated

这是因为按照原 docker-compose.yml 的写法,端口被固定分配,因此动态调节创建容器时端口冲突了。对于要动态编排的容器,不应显式指定映射的端口,即省略对应的本机端口,以随机映射端口

将文件中 wordpress 中的 ports 部分改为 - "80"。

修改文件后,需要首先使用 docker-compose down 停止并删除所有容器

TIPS:

如果不先启动容器再调节,则会报错:

[root@VM_0_2_centos my_wordpress]# docker-compose scale wordpress=3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Creating mywordpress_wordpress_1 ... error
Creating mywordpress_wordpress_2 ... error
Creating mywordpress_wordpress_3 ... error

ERROR: for mywordpress_wordpress_3 Cannot start service wordpress: network mywordpress_default not found

ERROR: for mywordpress_wordpress_2 Cannot start service wordpress: network mywordpress_default not found

ERROR: for mywordpress_wordpress_1 Cannot start service wordpress: network mywordpress_default not found
ERROR: Cannot start service wordpress: network mywordpress_default not found

启动容器后再进行调节:

[root@VM_0_2_centos my_wordpress]# docker-compose up -d
Creating mywordpress_db_1 ... done
Creating mywordpress_db_1 ...
Creating mywordpress_wordpress_1 ... done
[root@VM_0_2_centos my_wordpress]# docker-compose scale wordpress=3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting mywordpress_wordpress_1 ... done
Creating mywordpress_wordpress_2 ... done
Creating mywordpress_wordpress_3 ... done

需要注意的是,一般程序的容器可以启动多个,但数据库的容器不应启动多个实例

4. 为多个实例添加负载均衡

首先使用 yum install nginx 命令安装负载均衡的“神器” nginx,并使用 systemctl start nginx 命令启动 nginx 服务。

紧接着修改 nginx 的配置文件 /etc/nginx/nginx.conf,添加下图中的红框的部分(注意 upstream 中的地址和端口需根据实际情况填写)

docker容器如何做负载均衡_docker容器如何做负载均衡_02

通过容器命令可以看出此时我的三个容器端口号为 32773,32774,32775:

[root@VM_0_2_centos my_wordpress]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a9172b7eca6 wordpress:latest "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 0.0.0.0:32775->80/tcp mywordpress_wordpress_3
e2d9f0365199 wordpress:latest "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 0.0.0.0:32774->80/tcp mywordpress_wordpress_2
1321805eb5eb wordpress:latest "docker-entrypoint.s…" 36 seconds ago Up 35 seconds 0.0.0.0:32773->80/tcp mywordpress_wordpress_1
32f8809990ee mysql:5.7 "docker-entrypoint.s…" 36 seconds ago Up 36 seconds 3306/tcp, 33060/tcp mywordpress_db_1

主要思路:为了查看访问的网页确实进行了负载均衡,使用的方法是使用 phpinfo() 函数输出访问的信息和服务器信息。因此需要在服务器路径下新建一个文件。由于在 yaml 文件中,声明了三个容器使用同一个卷,因此需要找到卷的路径。具体有两种方法:

A. 直接查看卷的属性

docker volume inspect <volumn_name>
[root@VM_0_2_centos my_wordpress]# docker volume inspect mywordpress_web_data

[

    {

                 "CreatedAt":         "2020-07-20T16:57:08+08:00",

               "Driver": "local",

               "Labels": {

                       "com.docker.compose.project": "mywordpress",

                       "com.docker.compose.volume": "web_data"

               },

               "Mountpoint": "/var/lib/docker/volumes/mywordpress_web_data/_data",

               "Name": "mywordpress_web_data",

               "Options": null,

               "Scope": "local"

       }

]

B. 查看容器的挂载相关属性

docker inspect <CONTAINER_NAME> | grep "Mount" -A <ROW_NUMBER_TO_SEE>
[root@VM_0_2_centos my_wordpress]# docker inspect mywordpress_wordpress_1 | grep "Mount" -A 10

            "MountLabel": "",

            "ProcessLabel": "",

            "AppArmorProfile": "",

            "ExecIDs": null,

            "HostConfig": {

                "Binds": [

                        "mywordpress_web_data:/var/www/html:rw"

               ],

                "ContainerIDFile": "",

                "LogConfig": {

                   "Type": "json-file",

>--

            "Mounts": [

                {

                        "Type": "volume",

                        "Name": "mywordpress_web_data",

                        "Source": "/var/lib/docker/volumes/mywordpress_web_data/_data",

                        "Destination": "/var/www/html",

                        "Driver": "local",

                        "Mode": "rw",

                        "RW": true,

                        "Propagation": ""

                }

可以看出此时找到了两处,中间用 “--” 分隔

进入目录,并创建 test.php 文件:

<?php phpinfo(); ?>

可以看到两次访问的来源是不同的ip地址:

docker容器如何做负载均衡_docker容器如何做负载均衡_03

docker容器如何做负载均衡_docker_04

由于并没有实际具体研究过负载均衡,上述的操作是照搬视频中老师的做法进行的实验

后记:

个人感觉可以通过上述动态部署 wordpress 的实践一窥微服务的操作方法:利用等价的程序、数据库访问的中间件,动态地添加程序服务器和数据库,以达到动态扩容的目的。在流量大时购入按时收费的服务器,在流量小时维持少量服务器即可,在一定程度上大大增加灵活性,节约成本,还可以减少大量运算对单机性能的依赖。

但是感觉每一种特定的解决方案都只能解决一类的问题,如果服务的流量超过了一定的数量级,利用此种策略未必有效,还需要重新设计结构。

参考:

grep 的用法

https://www.runoob.com/linux/linux-comm-grep.html