目录
2.1.6 docker-compose restart :
2.3.2 docker-compose up --build :
3.1.1 服务名称(service1_name/service2_name 等):
4 利用容器编排完成haproxy和nginx负载均衡架构实施
1 Docker Compose 概述
Docker Compose 是一个用于定义和运行多容器 Docker 应用程序的工具。
其是官方的一个开源项目,托管到github上
![](https://i-blog.csdnimg.cn/direct/60c6ad5b04234245b8b979a0311a1199.png)
1.1 主要功能
1. 定义服务:
- 使用 YAML 格式的配置文件来定义一组相关的容器服务。每个服务可以指定镜像、端口映射、环境变量、存储卷等参数。
- 例如,可以在配置文件中定义一个 Web 服务和一个数据库服务,以及它们之间的连接关系。
2. 一键启动和停止:
- 通过一个简单的命令,可以启动或停止整个应用程序所包含的所有容器。这大大简化了多容器 应用的部署和管理过程。
- 例如,使用 docker-compose up 命令可以启动配置文件中定义的所有服务,使用 docker compose down 命令可以停止并删除这些服务。
3. 服务编排:
- 可以定义容器之间的依赖关系,确保服务按照正确的顺序启动和停止。例如,可以指定数据库 服务必须在 Web 服务之前启动。
- 支持网络配置,使不同服务的容器可以相互通信。可以定义一个自定义的网络,将所有相关的 容器连接到这个网络上。
4. 环境变量管理:
- 可以在配置文件中定义环境变量,并在容器启动时传递给容器。这使得在不同环境(如开发、 测试和生产环境)中使用不同的配置变得更加容易。
- 例如,可以定义一个数据库连接字符串的环境变量,在不同环境中可以设置不同的值。
1.2 工作原理
1. 读取配置文件:
Docker Compose 读取 YAML 配置文件,解析其中定义的服务和参数。
2. 创建容器:
- 根据配置文件中的定义,Docker Compose 调用 Docker 引擎创建相应的容器。它会下载所需 的镜像(如果本地没有),并设置容器的各种参数。
3. 管理容器生命周期:
- Docker Compose 监控容器的状态,并在需要时启动、停止、重启容器。
- 它还可以处理容器的故障恢复,例如自动重启失败的容器。
1.3 Docker Compose 中的管理层
1. 服务 (service) 一个应用的容器,实际上可以包括若干运行相同镜像的容器实例
2. 项目 (project) 由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中 定义
3. 容器(container)容器是服务的具体实例,每个服务可以有一个或多个容器。容器是基于服务定义 的镜像创建的运行实例
2 Docker Compose 的常用命令参数
[root@node-3 ~]# mkdir test
[root@node-3 test]# vim shuyan.yml
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: shuyan
以下是一些 Docker Compose 常用命令
2.1 服务管理
2.1.1 docker-compose up :
- 启动配置文件中定义的所有服务。
- 可以使用 -d 参数在后台启动服务。
- 可以使用-f 来指定yml文件
- 例如: docker-compose up -d 。
[root@node-3 test]# docker compose -f /root/test/shuyan.yml up -d
[+] Running 2/2
✔ Container test-db-1 Started 0.6s
✔ Container test-web-1 Started
[root@node-3 test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
079e08d735e0 mysql:5.7 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 3306/tcp, 33060/tcp test-db-1
70f7e8854800 nginx:latest "/docker-entrypoint.…" 47 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp test-web-1
也可以使用默认名称docker-compose up --不需要指定路径
[root@node-3 ~]# docker compose -f test/docker-compose.yml up -d
[+] Running 3/3
✔ Network test_default Created 0.1s
✔ Container test-web-1 Started 0.9s
✔ Container test-db-1 Started
2.1.2 docker-compose down :
- 停止并删除配置文件中定义的所有服务以及相关的网络和存储卷。
[root@node-3 test]# docker compose -f /root/test/shuyan.yml down
[+] Running 3/3
✔ Container test-web-1 Removed 0.4s
✔ Container test-db-1 Removed 1.5s
✔ Network test_default Removed 0.2s
[root@node-3 test]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2.1.3 docker-compose start :
[root@node-3 test]# docker compose -f /root/test/shuyan.yml up -d
[+] Running 3/3
✔ Network test_default Created 0.1s
✔ Container test-web-1 Started 0.7s
✔ Container test-db-1 Started 0.6s
[root@node-3 test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7a1a9d11dd5d nginx:latest "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp test-web-1
7274f6be5dab mysql:5.7 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 3306/tcp, 33060/tcp test-db-1
2.1.4 docker-compose stop :
- 停止正在运行的服务
[root@node-3 test]# docker compose -f /root/test/shuyan.yml stop
[+] Stopping 2/2
✔ Container test-web-1 Stopped 0.4s
✔ Container test-db-1 Stopped 1.9s
2.1.5 docker-compose start :
- 启动已经存在的服务,但不会创建新的服务。
[root@node-3 test]# docker compose -f /root/test/shuyan.yml start
[+] Running 2/2
✔ Container test-web-1 Started 0.5s
✔ Container test-db-1 Started 0.6s
2.1.6 docker-compose restart :
- 重启服务。
[root@node-3 test]# docker compose -f /root/test/shuyan.yml restart
[+] Restarting 2/2
✔ Container test-db-1 Started 2.0s
✔ Container test-web-1 Started
2.2 服务状态查看
2.2.1 docker-compose ps :
列出正在运行的服务以及它们的状态,包括容器 ID、名称、端口映射等信息。
[root@node-3 test]# docker compose -f /root/test/shuyan.yml ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
test-db-1 mysql:5.7 "docker-entrypoint.s…" db About an hour ago Up About an hour 3306/tcp, 33060/tcp
test-web-1 nginx:latest "/docker-entrypoint.…" web About an hour ago Up About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp
[root@node-3 test]# docker compose -f /root/test/shuyan.yml ps db
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
test-db-1 mysql:5.7 "docker-entrypoint.s…" db About an hour ago Up About an hour 3306/tcp, 33060/tcp
[root@node-3 test]# docker compose -f /root/test/shuyan.yml ps web
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
test-web-1 nginx:latest "/docker-entrypoint.…" web About an hour ago Up About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp
2.2.2 docker-compose logs :
[root@node-3 test]# docker compose -f shuyan.yml logs web
[root@node-3 test]# docker compose -f shuyan.yml logs db
2.3 构建和重新构建服务
将之前的给删掉
[root@node-3 test]# docker compose -f shuyan.yml down
[+] Running 3/3
✔ Container test-db-1 Removed 1.3s
✔ Container test-web-1 Removed 0.3s
✔ Network test_default Removed
2.3.1 docker-compose build :
- 构建配置文件中定义的服务的镜像。可以指定服务名称来只构建特定的服务。
# 创建存放Dockerfile文件的目录
[root@node-3 ~]# mkdir docker
# 创建Dockerfile
[root@node-3 ~]# vim docker/Dockerfile
FROM nginx:latest
RUN echo this is docker contain > /usr/share/nginx/html/index.html
CMD ["nginx","-g","daemon off;"]
# 创建nginx.yml 来对容器进行编排
[root@node-3 ~]# vim test/nginx.yml
services:
nginx_web:
image: nginx_v1
build:
context: /root/docker
dockerfile: Dockerfile
container_name: nginx-1
ports:
- "80:80"
~]# docker compose -f test/nginx.yml build nginx_web #构建build 中的nginx_web
~]# docker compose -f test/nginx.yml build # 构建yml文件中的所有服务
[root@node-3 ~]# docker compose -f test/nginx.yml build nginx_web
[+] Building 0.1s (7/7) FINISHED docker:default
=> [nginx_web internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 182B 0.0s
=> [nginx_web internal] load metadata for docker.io/library/nginx:latest 0.0s
=> [nginx_web internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [nginx_web 1/2] FROM docker.io/library/nginx:latest 0.0s
=> CACHED [nginx_web 2/2] RUN echo this is docker contain > /usr/share/nginx/html/index.ht 0.0s
=> [nginx_web] exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:7600d8405fde409386a09f6e7989a3e0e29f57e8cef87296ede8dc6ba3b2996 0.0s
=> => naming to docker.io/library/nginx_v1 0.0s
=> [nginx_web] resolving provenance for metadata file 0.0s
[root@node-3 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_v1 latest 7600d8405fde 27 minutes ago 188MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
phpmyadmin latest 2b39e17532a1 2 weeks ago 562MB
ubuntu latest edbfe74c41f8 4 weeks ago 78.1MB
mysql 5.7 5107333e08a8 8 months ago 501MB
busybox latest 65ad0d468eb1 15 months ago 4.26MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
构建并启动
第一种构建方式,提前在Dockerfile中写好
# 创建存放Dockerfile文件的目录
[root@node-3 ~]# mkdir docker
# 创建Dockerfile
################## 第一种构建方式
[root@node-3 ~]# vim docker/Dockerfile
FROM nginx:latest
RUN echo this is docker contain > /usr/share/nginx/html/index.html
CMD ["nginx","-g","daemon off;"]
# 创建nginx.yml 来对容器进行编排
[root@node-3 ~]# vim test/nginx.yml
services:
nginx_web:
image: nginx_v1
build:
context: /root/docker
dockerfile: Dockerfile
container_name: nginx-1
ports:
- "80:80"
[root@node-3 test]# docker compose -f nginx.yml up -d
[+] Running 1/1 #会去仓库拉去镜像
! nginx_web Warning pull access denied for nginx_v1, repository does ... 6.2s
[+] Building 0.1s (7/7) FINISHED docker:default
=> [nginx_web internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 215B 0.0s
=> [nginx_web internal] load metadata for docker.io/library/nginx:latest 0.0s
=> [nginx_web internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [nginx_web 1/2] FROM docker.io/library/nginx:latest 0.0s
=> CACHED [nginx_web 2/2] RUN echo this is docker contain > /usr/share/nginx/html/index.ht 0.0s
=> [nginx_web] exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:ce37b64a2aacc61505d78a3ef89f805a22a8ddc92ddd5449effb3dc79a91f7c 0.0s
=> => naming to docker.io/library/nginx_v1 0.0s
=> [nginx_web] resolving provenance for metadata file 0.0s
[+] Running 2/2
✔ Network test_default Created 0.1s
✔ Container nginx-1 Started
[root@node-3 ~]# curl localhost
this is docker contain
[root@node-3 ~]# docker rm -f nginx-1
[root@node-3 ~]# docker rmi nginx_v1:latest
第二种方式
[root@node-3 ~]# docker compose -f test/nginx.yml up -d
[+] Running 1/1
! nginx_web Warning pull access denied for nginx_v1, repository does ... 11.1s
[+] Building 0.1s (7/7) FINISHED docker:default
=> [nginx_web internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 182B 0.0s
=> [nginx_web internal] load metadata for docker.io/library/nginx:latest 0.0s
=> [nginx_web internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [nginx_web 1/2] FROM docker.io/library/nginx:latest 0.0s
=> CACHED [nginx_web 2/2] RUN echo this is docker contain > /usr/share/nginx/html/index.ht 0.0s
=> [nginx_web] exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:7600d8405fde409386a09f6e7989a3e0e29f57e8cef87296ede8dc6ba3b2996 0.0s
=> => naming to docker.io/library/nginx_v1 0.0s
=> [nginx_web] resolving provenance for metadata file 0.0s
[+] Running 1/1
✔ Container nginx-1 Started 0.4s
[root@node-3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c95467a0ada9 nginx_v1 "/docker-entrypoint.…" 16 seconds ago Up 16 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-1
[root@node-3 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_v1 latest 7600d8405fde 11 minutes ago 188MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
phpmyadmin latest 2b39e17532a1 2 weeks ago 562MB
ubuntu latest edbfe74c41f8 4 weeks ago 78.1MB
mysql 5.7 5107333e08a8 8 months ago 501MB
busybox latest 65ad0d468eb1 15 months ago 4.26MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
[root@node-3 ~]# curl localhost
this is docker contain
[root@node-3 ~]# docker rm -f nginx-1
[root@node-3 ~]# docker rmi nginx_v1:latest
无论是哪种方式使用启动都会先去仓库拉取镜像
2.3.2 docker-compose up --build :
- 启动服务并在启动前重新构建镜像
[root@node-3 ~]# docker compose -f test/nginx.yml up --build -d
[+] Building 0.1s (7/7) FINISHED docker:default
=> [nginx_web internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 182B 0.0s
=> [nginx_web internal] load metadata for docker.io/library/nginx:latest 0.0s
=> [nginx_web internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [nginx_web 1/2] FROM docker.io/library/nginx:latest 0.0s
=> CACHED [nginx_web 2/2] RUN echo this is docker contain > /usr/share/nginx/html/index.ht 0.0s
=> [nginx_web] exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:7600d8405fde409386a09f6e7989a3e0e29f57e8cef87296ede8dc6ba3b2996 0.0s
=> => naming to docker.io/library/nginx_v1 0.0s
=> [nginx_web] resolving provenance for metadata file 0.0s
[+] Running 1/1
✔ Container nginx-1 Started 0.4s
[root@node-3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3e0975a7593 nginx_v1 "/docker-entrypoint.…" 32 seconds ago Up 10 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-1
[root@node-3 ~]# curl localhost
this is docker contain
2.4 其他操作
2.4.1 docker-compose exec :
- 在正在运行的服务容器中执行命令。
[root@node-3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99d93d3ef475 nginx_v1 "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-1
[root@node-3 ~]# docker exec -it nginx-1 sh
# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
2.4.2 docker-compose pull :
拉取配置文件中定义的服务所使用的镜像。
services:
busybox_v1:
image: busybox
command: ["/bin/sh","-c","sleep 3000"]
restart: always
container_name: busybox1
[root@node-3 ~]# docker compose -f test/busybox.yml pull
[+] Pulling 2/2
✔ busybox_v1 Pulled 6.5s
✔ 3d1a87f2317d Pull complete 1.5s
3 Docker Compose 的yml文件
Docker Compose 的 YAML 文件用于定义和配置多容器应用程序的各个服务。以下是一个基本的 Docker Compose YAML 文件结构及内容解释:
3.1 服务(services)
3.1.1 服务名称(service1_name/service2_name 等):
- 每个服务在配置文件中都有一个唯一的名称,用于在命令行和其他部分引用该服务。
services:
web:
# 服务1的配置
mysql:
# 服务2的配置
3.1.2 镜像(image):
- 指定服务所使用的 Docker 镜像名称和标签。例如, image: nginx:latest 表示使用 nginx 镜像的最新版本
services:
web:
images:nginx
mysql:
images:mysql:5.7
3.1.3 端口映射(ports):
将容器内部的端口映射到主机的端口,以便外部可以访问容器内的服务。例如, - "8080:80" 表示将主机的 8080 端口映射到容器内部的 80 端口。
services:
web:
image: nginx
container_name: game #指定容器名称
restart: always #docekr容器自动启动
expose:
- 1234 #指定容器暴露那些端口,些端口仅对链接的服务可见,不会映射到主机的端口
ports:
- "80:80"
3.1.4 环境变量(environment):
- 为容器设置环境变量,可以在容器内部的应用程序中使用。例如, VAR1: value1 设置环境变 量 VAR1 的值为 value1
services:
web:
images:mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: shuyan
3.1.5 存储卷(volumes):
- 将主机上的目录或文件挂载到容器中,以实现数据持久化或共享。例如, - /host/data:/container/data 将主机上的 /host/data 目录挂载到容器内的 /container/data 路径。
services:
test:
image: busybox
command: ["/bin/sh","-c","sleep 3000"]
restart: always
container_name: busybox1
volumes:
- /etc/passwd:/tmp/passwd:ro #只读挂在本地文件到指定位置
3.1.6 网络(networks):
- 将服务连接到特定的网络,以便不同服务的容器可以相互通信
services:
web:
image: nginx
container_name: webserver
network_mode: bridge #使用本机自带bridge网络
services:
busybox_v1:
image: busybox
container_name: busybox_01
command: ["/bin/sh","-c","sleep 10000000"]
#network_mode: mynet2
networks:
- mynet1 # 引用自定义的mynet1桥接网络
- mynet2 # 引用自定义的mynet2桥接网络
networks:
mynet1:
driver: bridge
mynet2:
driver: bridge
3.2 网络(networks)
- 定义 Docker Compose 应用程序中使用的网络。可以自定义网络名称和驱动程序等属性。
- 默认情况下docker compose 在执行时会自动建立网路
[root@node-3 ~]# vim test/busybox.yml
services:
busybox_v1:
image: busybox
command: ["/bin/sh","-c","sleep 3000"]
restart: always
container_name: busybox
network_mode: default
busybox_v2:
image: busybox
command: ["/bin/sh","-c","sleep 3000"]
restart: always
container_name: busybox1
networks:
- mynet1
- mynet2
networks:
mynet1:
driver: bridge # 使用自定义桥接网络
mynet2:
driver: bridge
ipam: # 自定义网络
config:
- subnet: 192.168.0.0/24
gateway: 192.168.0.2
default:
external: true
name: bridge
# default 网络:
# external: true 表示使用一个已经存在的外部网络。
# name: bridge 指定这个外部网络的名字为 bridge。
启动容器
[root@node-3 ~]# docker compose -f test/busybox.yml up -d
[+] Running 4/4
✔ Network test_mynet2 Created 0.2s
✔ Network test_mynet1 Created 0.2s
✔ Container busybox Started 1.5s
✔ Container busybox1 Started 1.5s
[root@node-3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c834b2f17f40 busybox "/bin/sh -c 'sleep 3…" 5 seconds ago Up 4 seconds busybox1
41e5d5c145b0 busybox "/bin/sh -c 'sleep 3…" 5 seconds ago Up 4 seconds busybox
进入容器查看
[root@node-3 ~]# docker compose -f test/busybox.yml exec -it busybox_v1 sh
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:19 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2422 (2.3 KiB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # exit
[root@node-3 ~]# docker compose -f test/busybox.yml exec -it busybox_v2 sh
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:13:00:02
inet addr:172.19.0.2 Bcast:172.19.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:40 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5626 (5.4 KiB) TX bytes:0 (0.0 B)
eth1 Link encap:Ethernet HWaddr 02:42:C0:A8:00:01
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:40 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5628 (5.4 KiB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
3.3 存储卷(volumes)
- 定义 Docker Compose 应用程序中使用的存储卷。可以自定义卷名称和存储位置等属性。
停掉之前的yml
[root@node-3 ~]# docker compose -f test/busybox.yml down
[root@node-3 ~]# vim /root/test/volume.yml
services:
busybox_v1:
image: busybox
command: ["/bin/sh","-c","sleep 30000"]
restart: always
container_name: busybox1
volumes:
- data:/test # 默认逻辑卷,路径在 /var/lib/docker/volumes
- /root:/backup:rw # 指定路径挂载
# 定义逻辑卷的名称
volumes:
data:
name: shuyan
启动容器并在挂载点建立文件查看效果
[root@node-3 ~]# docker compose -f test/volume.yml up -d
[+] Running 2/2
✔ Volume "shuyan" Created 0.0s
✔ Container busybox1 Started 0.5s
[root@node-3 ~]# ls /var/lib/docker/volumes/
backingFsBlockDev metadata.db shuyan shuyanvol1
[root@node-3 ~]# cd /var/lib/docker/volumes/shuyan/_data/
[root@node-3 _data]# touch shuyanfile{1..10}
进入容器查看效果
[root@node-3 _data]# docker compose -f /root/test/volume.yml exec -it busybox_v1 sh
/ # ls
backup bin dev etc home lib lib64 proc root sys test tmp usr var
/ # ls backup/
anaconda-ks.cfg script ubuntu-latest.tar.gz ?? ?? ??
cpu.cfs_quota_us~ shuyan uncontain.sh ?? ??
docker test ?? ?? ??
/ # ls test/
shuyanfile1 shuyanfile10 shuyanfile2 shuyanfile3 shuyanfile4 shuyanfile5 shuyanfile6 shuyanfile7 shuyanfile8 shuyanfile9
4 利用容器编排完成haproxy和nginx负载均衡架构实施
4.1 实验的思路:
- 挂载特定目录到haproxy的配置文件目录与NGINX网站发布目录
- 需要挂载的配置文件目录可以直接覆盖
- 数据文件比如说NGINX的html 中需要复制挂载使用 docker managed 方式挂载
- 配置文件如haproxy.cfg可以直接使用bind mount 将原有的文件进行覆盖
- 仅暴露haproxy的端口,NGINX的端口不做暴露,这样可以直接访问自己的地址访问到haproxy再由haproxy进行容器之间的数据交换
4.2 准备haproxy的配置文件
[root@node-3 ~]# mkdir haproxy/
[root@node-3 haproxy]# cd haproxy
[root@node-3 haproxy]# yum install haproxy --downloaddir /root --downloadonly
# 将haproxy所有文件安装到本目录下
[root@node-3 haproxy]# rpm2cpio haproxy-2.4.22-3.el9_3.x86_64.rpm | cpio -idmv
[root@node-3 haproxy]# ls
etc haproxy-2.4.22-3.el9_3.x86_64.rpm usr var
[root@node-3 haproxy]# cd etc/haproxy/
[root@node-3 haproxy]# ls
conf.d haproxy.cfg
[root@node-3 haproxy]# mkdir -p /docker/conf/
[root@node-3 haproxy]# cp haproxy.cfg /docker/conf/
[root@node-3 ~]# vim /docker/conf/haproxy.cfg
listen webcluster
bind *:80
balance roundrobin
server web1 webserver1:80 check inter 3 fall 3 rise 5
server web2 webserver2:80 check inter 3 fall 3 rise 5
4.3 编写 yml 文件
[root@node-3 ~]# vim test/haproxy.yml
services:
web1:
image: nginx:latest
container_name: webserver1 # 容器主机名需要写对,与haproxy里面写得一样。不然无法通讯
restart: always
networks:
- mynet1
expose:
- 80
volumes:
- data_web1:/usr/share/nginx/html
web2:
image: nginx:latest
container_name: webserver2
restart: always
networks:
- mynet1
expose:
- 80 # expose 在这里只是一个标识符,它仅仅标记了容器内部监听的端口,并不会直接影响容器的实际运行或端口映射
volumes:
- data_web2:/usr/share/nginx/html
# 引用定义了的数据卷
haproxy:
image: haproxy:2.3
container_name: haproxy
restart: always
networks:
- mynet1
- mynet2
volumes:
- /docker/conf/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
ports:
- 80:80
# 定义网络组,因为在一台机子上为了省事,直接使用自定义桥接
# 假如说使用的是不同主机之间的容器通讯,需要使用 macvlan
networks:
mynet1:
driver: bridge
mynet2:
driver: bridge
volumes:
data_web1:
name: nginx_v1
data_web2:
name: nginx_v2
4.4 启动容器
[root@node-3 ~]# docker compose -f test/haproxy.yml up -d
[+] Running 5/5
✔ Network test_mynet1 Created 0.1s
✔ Network test_mynet2 Created 0.1s
✔ Container webserver2 Started 2.0s
✔ Container haproxy Started 2.0s
✔ Container webserver1 Started
4.5 查看容器服务是否起来
[root@node-3 ~]# docker compose -f test/haproxy.yml ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
haproxy haproxy:2.3 "docker-entrypoint.s…" haproxy 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp
webserver1 nginx:latest "/docker-entrypoint.…" web1 8 minutes ago Up 8 minutes 80/tcp
webserver2 nginx:latest "/docker-entrypoint.…" web2 8 minutes ago Up 8 minutes 80/tcp
4.6 修改默认挂载点的数据文件
[root@node-3 ~]# echo this is web1 > /var/lib/docker/volumes/nginx_v1/_data/index.html
[root@node-3 ~]# echo this is web2 > /var/lib/docker/volumes/nginx_v2/_data/index.html
4.7 实现效果
尝试curl自己
[root@node-3 ~]# curl 192.168.239.30:80
this is web2
[root@node-3 ~]# curl 192.168.239.30:80
this is web1
[root@node-3 ~]# curl 192.168.239.30:80
this is web2
[root@node-3 ~]# curl 192.168.239.30:80
this is web1