介绍:通过单个服务器部署的伪集群,借助docker的自定义网络,定义多个ip来模拟多台服务器。
一. 双机热备模式
- 节点信息
docker-compose.yml:docker编排配置文件,主要指定服务,指定打包当前服务部署所使用的镜像的文件(dockerfile),指定网络。
Dockerfile:就是docker-compose文件中制定的需要打包镜像的镜像文件。
entrypoint.sh:容器中要执行的启动脚本。
index-xx.html:这个是Nginx里的挂载的默认打开地址,这里只是为了可观测性配置。
keepalived-xx.conf:keepalived的配置文件,监听nginx健康状态,主节点挂掉自动用上备份。
haproxy.conf:提供一个虚拟的路径,统一的去接收用户的请求。
conf: nginx配置文件。
log: nginx日志文件。
二. 搭建流程
- 编写Dockerfile制作nginx-keepalived的镜像
FROM nginx:1.19.2-alpine
RUN apk update && apk upgrade
RUN apk add --no-cache bash curl ipvsadm iproute2 openrc keepalived && rm -f /var/cache/apk/* /tmp/*
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]
- 创建启动脚本entrypoint.sh
#!/bin/sh
/usr/sbin/keepalived -n -l -D -f /etc/keepalived/keepalived.conf --dont-fork --log-console &
nginx -g "daemon off;"
- 创建docker-compose.yml文件编排容器
version: '3'
services:
# 主服务
nginx_master:
build:
context: ./
# 指定打镜像的镜像文件
dockerfile: ./Dockerfile
# 镜像挂载
restart: always
volumes:
- ./keepalived-master.conf:/etc/keepalived/keepalived.conf
- /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- /opt/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- /opt/nginx/html/index-master.html:/usr/share/nginx/html/index.html
- /opt/nginx/data:/opt/data
- /opt/nginx/logs:/var/log/nginx
# 容器网络
networks:
static-network:
ipv4_address: 172.21.128.2
cap_add:
- NET_ADMIN
# 辅服务(以下配置参考主服务)
nginx_slave:
build:
context: ./
dockerfile: ./Dockerfile
restart: always
volumes:
- ./keepalived-slave.conf:/etc/keepalived/keepalived.conf
- /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- /opt/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- /opt/nginx/html/index-slave.html:/usr/share/nginx/html/index.html
- /opt/nginx/data:/opt/data
- /opt/nginx/logs:/var/log/nginx
networks:
static-network:
ipv4_address: 172.21.128.3
cap_add:
- NET_ADMIN
# 代理,为了解决容器IP映射到本机
proxy:
image: haproxy:1.7-alpine
# 代理,监听的端口,外界端口是8000。映射到haproxy的6301端口上
ports:
- 8000:6301
volumes:
- ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
networks:
- static-network
# 指定网络
networks:
static-network:
ipam:
config:
- subnet: 172.21.0.0/16
- 创建keepalived配置文件
keepalived-master.conf
# nx的健康状态
vrrp_script chk_nginx {
script "pidof nginx"
interval 2
}
vrrp_instance VI_1 {
# master负责接收用户的请求,当master宕掉后才会有slave接收
state MASTER
# 容器内部的网卡名称
interface eth0
# 虚拟路由ID
virtual_router_id 33
# 优先级,很重要
priority 200
advert_int 1
unicast_src_ip 172.21.128.2
unicast_peer {
172.21.128.3
}
authentication {
auth_type PASS
auth_pass letmein
}
# 虚拟IP地址,很重要
virtual_ipaddress {
172.21.128.4/24 dev eth0
}
track_script {
chk_nginx
}
}
keepalived-slave.conf
vrrp_script chk_nginx {
script "pidof nginx"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 33
priority 100
advert_int 1
unicast_src_ip 172.21.128.3
unicast_peer {
172.21.128.2
}
authentication {
auth_type PASS
auth_pass letmein
}
virtual_ipaddress {
172.21.128.4/24 dev eth0
}
track_script {
chk_nginx
}
}
- haproxy.cfg
global
log 127.0.0.1 local0
maxconn 4096
daemon
nbproc 4
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
listen web1
bind :8080
mode http
server app1 172.21.128.4:8080
listen web2
bind :8002
mode http
server app2 172.21.128.4:8002
listen web3
bind :8003
mode http
server app3 172.21.128.4:8003
listen web4
bind :8004
mode http
server app4 172.21.128.4:8004
- 创建index.html验证文件
index-master.html
<h1>this is nginx-master</h1>
index-slave.html
<h1>this is nginx-slave</h1>
访问index页面http://172.16.7.108:8000
模拟master宕机docker pause 088
再次访问http://172.16.7.108:8000
已经切换到从节点
重新运行主节点docker unpause 088