Nginx Keepalived双机主备配置
2021-02-05 本文已影响0人
小P聊技术
1 双机主备原理
双机主备,是为了处理当Nginx主服务器宕机之后,会启动备用服务器进行服务,从而实现框架的高可用。
在这里插入图片描述2 主机配置
2.1 查看网卡名称
inet 192.168.51.6/24 brd 192.168.51.255 scope global ens33
ens33
就是网卡名称
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:26:f8:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.51.6/24 brd 192.168.51.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fe26:f857/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:49:a9:d2:94 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
2.2 修改配置文件
[root@localhost ~]# cd /etc/keepalived
[root@localhost keepalived]# vi keepalived.conf
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_5
} v
rrp_instance VI_1 {
# 表示状态是MASTER主机还是备用机BACKUP
state MASTER
# 该实例绑定的网卡
interface ens33
# 保证主备节点一致即可
virtual_router_id 51
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 100
# 主备之间同步检查时间间隔,单位秒
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
192.168.51.101
}
}
2.3 启动服务
[root@localhost ~]# cd /usr/local/keepalived/sbin/
[root@localhost sbin]# ./keepalived
2.4 查看运行
[root@localhost sbin]# ps -ef | grep keepalived
root 10384 1 0 16:11 ? 00:00:00 ./keepalived
root 10385 10384 0 16:11 ? 00:00:00 ./keepalived
root 10387 3963 0 16:12 pts/1 00:00:00 grep --color=auto keepalived
[root@localhost sbin]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:37:75:55 brd ff:ff:ff:ff:ff:ff
inet 192.168.51.5/24 brd 192.168.51.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.51.101/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fe37:7555/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:99:71:38:eb brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
3 备用机配置
3.1 查看网卡名称
inet 192.168.51.6/24 brd 192.168.51.255 scope global ens33
ens33
就是网卡名称
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:26:f8:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.51.6/24 brd 192.168.51.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fe26:f857/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:49:a9:d2:94 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
3.2 修改配置文件
[root@localhost ~]# cd /etc/keepalived
[root@localhost keepalived]# vi keepalived.conf
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_6
} v
rrp_instance VI_1 {
# 表示状态是MASTER主机还是备用机BACKUP
state BACKUP
# 该实例绑定的网卡
interface ens33
# 保证主备节点一致即可
virtual_router_id 51
# 权重低于MASTER
priority 80
# 主备之间同步检查时间间隔,单位秒
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
# 注意:主备两台的vip都是一样的,绑定到同一个vip
192.168.51.101
}
}
3.3 启动服务
[root@localhost ~]# cd /usr/local/keepalived/sbin/
[root@localhost sbin]# ./keepalived
3.4 查看运行
[root@localhost etc]# ps -ef | grep keepalived
root 9964 1 0 16:14 ? 00:00:00 ./keepalived
root 9965 9964 0 16:14 ? 00:00:00 ./keepalived
root 10011 3463 0 16:31 pts/1 00:00:00 grep --color=auto keepalived
[root@localhost etc]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:26:f8:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.51.6/24 brd 192.168.51.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fe26:f857/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:49:a9:d2:94 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
4 注册服务
4.1 拷贝文件
[root@localhost etc]# cd /opt/module/software/keepalived-2.0.18/keepalived/etc/
[root@localhost etc]# ll
总用量 44
drwxrwxr-x. 2 ts rvm 86 2月 1 15:31 init
drwxrwxr-x. 2 ts rvm 135 2月 1 15:31 init.d
drwxrwxr-x. 2 ts rvm 29 8月 16 2018 keepalived
-rw-r--r--. 1 root root 19576 2月 1 15:31 Makefile
-rw-rw-r--. 1 ts rvm 181 8月 16 2018 Makefile.am
-rw-rw-r--. 1 ts rvm 18377 7月 26 2019 Makefile.in
drwxrwxr-x. 2 ts rvm 24 8月 16 2018 openrc
drwxrwxr-x. 2 ts rvm 24 8月 16 2018 sysconfig
[root@localhost etc]# cp init.d/keepalived /etc/init.d/
[root@localhost etc]# cp sysconfig/keepalived /etc/sysconfig/
cp:是否覆盖"/etc/sysconfig/keepalived"? y
4.2 载入配置
[root@localhost etc]# systemctl daemon-reload
4.2 常用命令
-
启动
[root@localhost ~]# systemctl start keepalived
-
停止
[root@localhost ~]# systemctl stop keepalived
-
重启
[root@localhost ~]# systemctl restart keepalived
5 功能测试
5.1 虚拟VIP访问
在这里插入图片描述5.2 主机访问
在这里插入图片描述5.3 备用机访问
在这里插入图片描述5.4 主机宕机
1 主机
停止 keepalived
服务
[root@localhost etc]# systemctl stop keepalived
在这里插入图片描述
2 备用机
查看 ip信息
[root@localhost etc]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:26:f8:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.51.6/24 brd 192.168.51.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.51.101/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fe26:f857/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:49:a9:d2:94 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet 192.168.51.101/32 scope global ens33
切换成功
6 相关信息
- 博文不易,辛苦各位猿友点个关注和赞,感谢