我是靠谱客的博主 复杂流沙,这篇文章主要介绍笔记-单台虚拟机使用docker-compose搭建nginx(三节点)+keepalived(三节点)+Haproxy(单节点)文章简介目录结构镜像制作使用docker-compose部署运行docker-compose测试结尾,现在分享给大家,希望可以做个参考。

文章简介

记录自己如何使用Docker-compose在单台宿主机的情况下搭建Nginx(三节点)+keepalived(三节点)+Haproxy(单节点),希望对大家有帮助,如配置有问题或有可优化的地方请发评论,各位大佬们多多指教

目录结构

目录中stream配置文件为备用,以后想用再说。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@localhost nginx]# tree . ├── check_nginx.sh ├── conf.d │ ├── haproxy │ │ └── haproxy.cfg │ ├── nginx │ │ └── nginx.conf │ └── stream │ └── server.conf ├── docker-compose.yml ├── init │ └── nginx_index │ ├── master │ │ └── index-master.html │ └── slave │ ├── index-slave-1.html │ └── index-slave-2.html └── keepalive ├── master │ └── keepalived-master.conf └── slave ├── keepalived-slave-1.conf └── keepalived-slave-2.conf 11 directories, 11 files

镜像制作

制作nginx+keepalived镜像

编写Dockerfile

这里根据nginx官方提供的镜像来制作nginx+keepalived的镜像

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
FROM nginx ARG TZ="Asia/Shanghai" ENV TZ ${TZ} COPY docker-entrypoint.sh / RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list && apt-get clean && apt-get update && apt-get install --no-install-recommends -y keepalived && apt-get install --no-install-recommends -y net-tools && apt-get install --no-install-recommends -y && apt-get install --no-install-recommends -y curl && apt-get install --no-install-recommends -y procps && apt-get install --no-install-recommends -y wget && apt-get install && apt-get clean &&rm -rf /var/lib/apt/lists/* && chmod +x /docker-entrypoint.sh ENTRYPOINT ["/docker-entrypoint.sh"] CMD ["nginx" , "-g" , "daemon off;"]

docker-entrypoint.sh

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh # vim:sw=4:ts=4:et set -e echo "keepalived start" keepalived -f /etc/keepalived/keepalived.conf echo "keepalived success" if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then exec 3>&1 else exec 3>/dev/null fi if [ "$1" = "nginx" -o "$1" = "nginx-debug" ]; then if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -maxdepth 1 -type f -print -quit 2>/dev/null | read v; then echo >&3 "$0: /docker-entrypoint.d/ is not empty, will attempt to perform configuration" echo >&3 "$0: Looking for shell scripts in /docker-entrypoint.d/" find "/docker-entrypoint.d/" -follow -type f -print | sort -n | while read -r f; do case "$f" in *.sh) if [ -x "$f" ]; then echo >&3 "$0: Launching $f"; "$f" else # warn on shell scripts without exec bit echo >&3 "$0: Ignoring $f, not executable"; fi ;; *) echo >&3 "$0: Ignoring $f";; esac done echo >&3 "$0: Configuration complete; ready for start up" else echo >&3 "$0: No files found in /docker-entrypoint.d/, skipping configuration" fi fi exec "$@"

使用docker build构建镜像

复制代码
1
docker build -t nginx-keepalived:0.0.2 .

拉取Haproxy镜像

复制代码
1
docker pull haproxy

使用docker-compose部署

编写docker-compose.yml文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
version: "3" services: nginx_master: image: nginx-keepalived:0.0.2 container_name: nginx-master restart: always privileged: true volumes: - ./conf.d/stream:/opt/nginx/stream/conf.d - ./conf.d/nginx/nginx.conf:/etc/nginx/nginx.conf - ./init/nginx_index/master/index-master.html:/usr/share/nginx/html/index.html - ./keepalive/master/keepalived-master.conf:/etc/keepalived/keepalived.conf - ./check_nginx.sh:/etc/keepalived/check_nginx.sh networks: nginx: ipv4_address: "20.20.20.10" cap_add: - NET_ADMIN nginx_slave1: image: nginx-keepalived:0.0.2 container_name: nginx-slave1 restart: always privileged: true volumes: - ./conf.d/stream:/opt/nginx/stream/conf.d - ./conf.d/nginx/nginx.conf:/etc/nginx/nginx.conf - ./init/nginx_index/slave/index-slave-1.html:/usr/share/nginx/html/index.html - ./keepalive/slave/keepalived-slave-1.conf:/etc/keepalived/keepalived.conf - ./check_nginx.sh:/etc/keepalived/check_nginx.sh networks: nginx: ipv4_address: "20.20.20.20" cap_add: - NET_ADMIN nginx_slave2: image: nginx-keepalived:0.0.2 container_name: nginx-slave2 restart: always privileged: true volumes: - ./conf.d/stream:/opt/nginx/stream/conf.d - ./conf.d/nginx/nginx.conf:/etc/nginx/nginx.conf - ./init/nginx_index/slave/index-slave-2.html:/usr/share/nginx/html/index.html - ./keepalive/slave/keepalived-slave-2.conf:/etc/keepalived/keepalived.conf - ./check_nginx.sh:/etc/keepalived/check_nginx.sh networks: nginx: ipv4_address: "20.20.20.30" cap_add: - NET_ADMIN proxy: image: haproxy:latest container_name: nginx-proxy restart: always ports: - 80:6301 volumes: - ./conf.d/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg networks: - nginx cap_add: - NET_ADMIN depends_on: - nginx_master - nginx_slave1 - nginx_slave2 networks: nginx: driver: bridge ipam: driver: default config: - subnet: "20.20.0.0/16"

编辑keepalived配置文件

keepalived-master.conf

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
global_defs { router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -20 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 70 priority 200 preempt advert_int 1 unicast_src_ip 20.20.20.10 unicast_peer { 20.20.20.20 20.20.20.30 } authentication { auth_type PASS auth_pass Password@123 } virtual_ipaddress { 20.20.20.40/24 dev eth0 } track_script { chk_nginx } }

keepalived-slave-1.conf

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
global_defs { router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 70 priority 150 nopreempt advert_int 1 unicast_src_ip 20.20.20.20 unicast_peer { 20.20.20.10 20.20.20.30 } authentication { auth_type PASS auth_pass Password@123 } virtual_ipaddress { 20.20.20.40/24 dev eth0 } track_script { chk_nginx } }

keepalived-slave-2.conf

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
global_defs { router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 70 priority 100 nopreempt advert_int 1 unicast_src_ip 20.20.20.30 unicast_peer { 20.20.20.20 20.20.20.10 } authentication { auth_type PASS auth_pass Password@123 } virtual_ipaddress { 20.20.20.40/24 dev eth0 } track_script { chk_nginx } }

编写nginx监测脚本

复制代码
1
2
3
4
5
6
7
8
9
#!/bin/bash A=`ps -C nginx --no-header | wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then killall keepalived fi fi

编辑nginx配置文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http{ server { listen 80; server_name localhost; charset utf-8; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } } }

编辑Haproxy配置文件

此处监听地址keepalived配置文件中配置的vip地址

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
global log 127.0.0.1 local0 maxconn 4096 daemon defaults log 127.0.0.1 local3 mode http option dontlognull option redispatch retries 2 maxconn 2000 balance roundrobin timeout connect 5000ms timeout client 5000ms timeout server 5000ms frontend main bind *:6301 default_backend webserver backend webserver server nginx_master 20.20.20.40:80 check inter 2000 rise 2 fall 5

编写测试页面

编辑测试用index-master.html

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <h1><strong>MASTER</strong></h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>

编辑测试用index-slave-1.html

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <h1><strong>SLAVE-1</strong></h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>

编辑测试用index-slave-2.html

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <h1><strong>SLAVE-2</strong></h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>

运行docker-compose

复制代码
1
docker-compose up -d

运行情况如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost nginx]# docker-compose up -d [+] Running 5/5 ⠿ Network nginx_nginx Created 0.1s ⠿ Container nginx-slave2 Started 1.4s ⠿ Container nginx-master Started 1.3s ⠿ Container nginx-slave1 Started 1.5s ⠿ Container nginx-proxy Started 2.3s [root@localhost nginx]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 14d6fbcd0db3 haproxy:latest "docker-entrypoint.s…" 5 seconds ago Up 2 seconds 0.0.0.0:80->6301/tcp, :::80->6301/tcp nginx-proxy cd5c033ea077 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 6 seconds ago Up 3 seconds 80/tcp nginx-slave2 5a9a24a58169 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 6 seconds ago Up 3 seconds 80/tcp nginx-slave1 d168f89a8f51 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 6 seconds ago Up 3 seconds 80/tcp nginx-master

测试

使用浏览器访问

关闭master节点

复制代码
1
docker stop nginx-master

运行结果如下:

复制代码
1
2
3
4
5
6
7
8
[root@localhost nginx]# docker stop nginx-master nginx-master [root@localhost nginx]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 14d6fbcd0db3 haproxy:latest "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:80->6301/tcp, :::80->6301/tcp nginx-proxy cd5c033ea077 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp nginx-slave2 5a9a24a58169 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp nginx-slave1 d168f89a8f51 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 6 minutes ago Exited (0) 2 seconds ago nginx-master

可以看到nginx-master节点已经处于退出状态

关闭nginx-master后刷新网页

关闭slave1节点

复制代码
1
docker stop nginx-slave1

运行结果如下:

复制代码
1
2
3
4
5
6
7
8
[root@localhost nginx]# docker stop nginx-slave1 nginx-slave1 [root@localhost nginx]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 14d6fbcd0db3 haproxy:latest "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 0.0.0.0:80->6301/tcp, :::80->6301/tcp nginx-proxy cd5c033ea077 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 80/tcp nginx-slave2 5a9a24a58169 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 11 minutes ago Exited (0) 2 seconds ago nginx-slave1 d168f89a8f51 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 11 minutes ago Exited (0) 4 minutes ago nginx-master

可以看到master节点和slave1节点都处于了退出状态

关闭slave1节点后刷新网页

可以看到切换非常的顺畅

打开master节点测试是否会进行一个抢占

复制代码
1
docker start nginx-master

运行效果如下:

复制代码
1
2
3
4
5
6
7
8
[root@localhost nginx]# docker start nginx-master nginx-master [root@localhost nginx]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 14d6fbcd0db3 haproxy:latest "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 0.0.0.0:80->6301/tcp, :::80->6301/tcp nginx-proxy cd5c033ea077 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 80/tcp nginx-slave2 5a9a24a58169 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 14 minutes ago Exited (0) 3 minutes ago nginx-slave1 d168f89a8f51 nginx-keepalived:0.0.2 "/docker-entrypoint.…" 14 minutes ago Up 1 second 80/tcp nginx-master

打开master节点后刷新网页

结尾

感谢大家的耐心观看,第一次发文章排版及顺序会不太好还望大家海涵,如果大家发现什么问题及优化的建议,可以发在评论,祝大家新年快乐,工作顺利

ps:这玩意咋生成目录

最后

以上就是复杂流沙最近收集整理的关于笔记-单台虚拟机使用docker-compose搭建nginx(三节点)+keepalived(三节点)+Haproxy(单节点)文章简介目录结构镜像制作使用docker-compose部署运行docker-compose测试结尾的全部内容,更多相关笔记-单台虚拟机使用docker-compose搭建nginx(三节点)+keepalived(三节点)+Haproxy(单节点)文章简介目录结构镜像制作使用docker-compose部署运行docker-compose测试结尾内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部