(亲测)docker+keepalived+nginx搭建集群
目录
- 安装centos 镜像
- 新建2个docker容器
- 在centos上安装所需环境
- 测试
- 相关服务命令
一.安装centos 镜像
docker pull centos
二.新建2个docker镜像
TheEternitydeiMac:~ admin$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 831691599b88 2 weeks ago 215MB
consul latest 197999eb696c 2 months ago 116MB
# 新建2个docker容器
docker run -itd --privileged --name centos_k1 831691599b88 /usr/sbin/init
docker run -itd --privileged --name centos_k2 831691599b88 /usr/sbin/init
三.在两个docker的centos上安装所需环境
0.进入docker容器
docker exec -it centos_k1 /bin/bash
docker exec -it centos_k2 /bin/bash
1.安装keepalived
# 安装keepalived环境依赖
yum install -y gcc openssl-devel popt-devel
# 安装keepalived
yum -y install keepalived
2.安装nginx
# 使用下面命令安装nginx
yum -y install nginx
3.安装网络包和vim
# 安装网络包(需要使用ifconfig和ping命令)
yum -y install net-tools
# 安装vim
yum -y install vim
4.修改/etc/keepalived/keepalived.conf文件
Master keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
zhangh0725@gmail.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_nginx
{
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.0.100
}
track_script {
chk_nginx
}
}
Slave keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
zhangh0725@gmail.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_nginx{
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.0.100
}
track_script{
chk_nginx
}
}
备注:
-
interface为网卡名称,通过ifconfig命令查看网卡名称
# 通过ifconfig查看网卡名称 [root@3ef3ca3b354c keepalived]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 10253 bytes 8974220 (8.5 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6444 bytes 355067 (346.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 672 bytes 49422 (48.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 672 bytes 49422 (48.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-
virtual_router_id为组队标识。 同为一个vip服务的keepalived的virtual_router_id要保持相同
-
priority 为权值,同一个virtual_router_id下那个priority大那个就是master,其它为backup
-
vrrp_script为需要运行的脚本,interval为间隔执行时间,单位为秒.
-
master和slave配置文件中state和priority两个参数的调整,其中master节点的priority值一定要比backup大才行!
5./etc/keepalived/check_nginx.sh脚本文件(2个docker容器都要部署)
#! /bin/bash
A=`ps -ef | grep nginx | grep -v 'grep'|grep -v 'check_nginx' |grep -v '/bin'| wc -l`
if [ $A -eq 0 ];then
systemctl start nginx
systemctl restart keepalived
sleep 2
if [ `ps -ef | grep nginx | grep -v 'grep'|grep -v 'check_nginx' |grep -v '/bin'| wc -l` -eq 0 ];then
systemctl stop keepalived
fi
fi
详细解释
#! /bin/bash
# 查找nginx进程,排除带grep的,排除带check_nginx的,排除带/bin的.如果排除之后没有的话,值为0,有的话则大于0
A=`ps -ef | grep nginx | grep -v 'grep'|grep -v 'check_nginx' |grep -v '/bin'| wc -l`
if [ $A -eq 0 ];then
# 如果nginx进程不存在,则重启nginx,重启keepalived
systemctl start nginx
systemctl restart keepalived
# 休眠2秒
sleep 2
# 再次查看nginx的进程
if [ `ps -ef | grep nginx | grep -v 'grep'|grep -v 'check_nginx' |grep -v '/bin'| wc -l` -eq 0 ];then
# 如果nginx进程不存在,则杀死keepalived进程,此时vip地址会漂移到另外一个keepalived上
systemctl stop keepalived
fi
fi
再对check_nginx.sh赋于执行权限:
chmod +x check_nginx.sh
注:keepalived是通过检测keepalived进程是否存在判断服务器是否宕机,如果keepalived进程在但是nginx进程不在了那么keepalived是不会做主备切换,所以我们需要写个脚本来监控nginx进程是否存在,如果nginx不存在就将keepalived进程杀掉。
在主nginx上需要编写nginx进程检测脚本(check_nginx.sh),判断nginx进程是否存在,如果nginx不存在就将keepalived进程杀掉,并将vip漂移到备份机器上
6.修改两个nginx的配置文件,区分主备
主nginx配置文件
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
return 200 "Hello Nginx Master!";
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
备nginx配置文件
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
return 200 "Hello Nginx Slave!";
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
主备nginx配置文件的差别
location / {
return 200 "Hello Nginx Master!";
}
location / {
return 200 "Hello Nginx Slave!";
}
7.分别在2个容器上先启动nginx,后启动keepalived
# 启动nginx
systemctl start nginx
# 启动keepalived
systemctl start keepalived
# 设置开机自启
systemctl enable keepalived
8.查看启动状态
主keepalived
[root@3ef3ca3b354c keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-07-03 02:44:12 UTC; 1h 25min ago
Process: 9942 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 9943 (keepalived)
Tasks: 2 (limit: 12530)
Memory: 2.5M
CGroup: /docker/3ef3ca3b354c445b262f497f7fe03be6783e68ef050de6a3d4eee301475d22e6/system.slice/keepalived.service
├─9943 /usr/sbin/keepalived -D
└─9944 /usr/sbin/keepalived -D
Jul 03 02:44:15 3ef3ca3b354c Keepalived_vrrp[9944]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:15 3ef3ca3b354c Keepalived_vrrp[9944]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:15 3ef3ca3b354c Keepalived_vrrp[9944]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:15 3ef3ca3b354c Keepalived_vrrp[9944]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:20 3ef3ca3b354c Keepalived_vrrp[9944]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:20 3ef3ca3b354c Keepalived_vrrp[9944]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.17.0.100
Jul 03 02:44:20 3ef3ca3b354c Keepalived_vrrp[9944]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:20 3ef3ca3b354c Keepalived_vrrp[9944]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:20 3ef3ca3b354c Keepalived_vrrp[9944]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:20 3ef3ca3b354c Keepalived_vrrp[9944]: Sending gratuitous ARP on eth0 for 172.17.0.100
备keepalived
[root@9ef3dc2ac227 keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-07-03 02:30:18 UTC; 1h 39min ago
Process: 16158 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 16159 (keepalived)
Tasks: 2 (limit: 12530)
Memory: 2.7M
CGroup: /docker/9ef3dc2ac227b967785c7eb6599e1fde1ab40ed8fd269defab0e223e280a870b/system.slice/keepalived.service
├─16159 /usr/sbin/keepalived -D
└─16160 /usr/sbin/keepalived -D
Jul 03 02:44:11 9ef3dc2ac227 Keepalived_vrrp[16160]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:11 9ef3dc2ac227 Keepalived_vrrp[16160]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.17.0.100
Jul 03 02:44:11 9ef3dc2ac227 Keepalived_vrrp[16160]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:11 9ef3dc2ac227 Keepalived_vrrp[16160]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:11 9ef3dc2ac227 Keepalived_vrrp[16160]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:11 9ef3dc2ac227 Keepalived_vrrp[16160]: Sending gratuitous ARP on eth0 for 172.17.0.100
Jul 03 02:44:15 9ef3dc2ac227 Keepalived_vrrp[16160]: (VI_1) Master received advert from 172.17.0.2 with higher priority 100, ours 90
Jul 03 02:44:15 9ef3dc2ac227 Keepalived_vrrp[16160]: (VI_1) Entering BACKUP STATE
Jul 03 02:44:15 9ef3dc2ac227 Keepalived_vrrp[16160]: (VI_1) removing VIPs.
Jul 03 02:44:15 9ef3dc2ac227 Keepalived_vrrp[16160]: (VI_1) removing iptable drop rule
四.测试
1.正常测试
主docker,master上vip172.17.0.100漂移在master的keepalived上
[root@3ef3ca3b354c keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000
link/tunnel6 :: brd ::
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
inet 172.17.0.100/32 scope global eth0
valid_lft forever preferred_lft forever
备docker,slave
[root@9ef3dc2ac227 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000
link/tunnel6 :: brd ::
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
主测试
[root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Master!
备测试
[root@9ef3dc2ac227 keepalived]# curl 172.17.0.100
Hello Nginx Master!
都是访问的主keepalived上的nginx
2.停止主上面的nginx
主.请求是返回的备上面nginx的信息,vip(172.17.0.100)也漂移了
[root@3ef3ca3b354c keepalived]# systemctl stop nginx
[root@3ef3ca3b354c keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000
link/tunnel6 :: brd ::
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@3ef3ca3b354c keepalived]# curl 172.17.0.100
curl: (7) Failed to connect to 172.17.0.100 port 80: Connection refused
[root@3ef3ca3b354c keepalived]# curl 172.17.0.100
^[[A
^[[A
Hello Nginx Slave![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Slave![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Slave![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Slave![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Slave![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Slave![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Slave![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Slave![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Slave![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Master![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Master![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Master![root@3ef3ca3b354c keepalived]# curl 172.17.0.100
备.请求返回的是备上面nginx的信息,VIP(172.17.0.100)漂移到备上面了
[root@9ef3dc2ac227 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000
link/tunnel6 :: brd ::
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
inet 172.17.0.100/32 scope global eth0
valid_lft forever preferred_lft forever
[root@9ef3dc2ac227 keepalived]# curl 172.17.0.100
Hello Nginx Slave!
检测脚本执行之后,master上的nginx重新启动,keepalived也重新启动,master重新获得VIP(172.17.0.100)
[root@3ef3ca3b354c keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000
link/tunnel6 :: brd ::
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
inet 172.17.0.100/32 scope global eth0
valid_lft forever preferred_lft forever
[root@3ef3ca3b354c keepalived]# curl 172.17.0.100
Hello Nginx Master!
slave的VIP(172.17.0.100)重新回到了master上面
[root@9ef3dc2ac227 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000
link/tunnel6 :: brd ::
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@9ef3dc2ac227 keepalived]# curl 172.17.0.100
Hello Nginx Master!
可以看到,当master容器中的keepalived服务开启后,请求返回的数据会再次转到master中。
五.相关服务命令
Keepalived
systemctl daemon-reload 重新加载
systemctl enable keepalived 设置开机自动启动
systemctl disable keepalived 取消开机自动启动
systemctl start keepalived 启动
systemctl restart keepalived 重启
systemctl stop keepalived 停止
systemctl status keepalived 查看服务状态
nginx
systemctl start nginx 启动nginx
systemctl stop nginx 停止nginx
systemctl restart nginx 重启nginx