keepalived+nginx负载安装
环境:centos7.6
keepalived工具可以生成虚拟浮动ip,绑定到网卡上。
172.21.210.19 master
172.21.210.20 backup
172.21.210.30 vip(浮动ip)
1、两个节点安装keepalived
yum -y install keepalived
2、确定要绑定的网卡(这儿使用eth0)
[root@tool-19 ~]# ip a 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:16:3e:c5:ac:f5 brd ff:ff:ff:ff:ff:ff inet 172.21.210.19/25 brd 172.21.210.127 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::f816:3eff:fec5:acf5/64 scope link valid_lft forever preferred_lft forever
3、master配置
[root@tool-19 ~]# more /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id kp01 } vrrp_instance VI_1 { state MASTER #master interface eth0 #网卡名称 virtual_router_id 51 # 虚拟路由标识,这个标识是一个数字,并且同一个vrrp实例使用唯一的标识,即同一个vrrp_instance下,MASTER和BACKUP必须是一致的 priority 151 # 定义优先级,数字越大,优先级越高,在一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级 advert_int 1 # 设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.21.210.30/25 #浮动ip } }
3、backup节点配置
[root@harbor-20 ~]# more /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id kp02 } vrrp_instance VI_1 { state BACKUP #backup interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.21.210.30/25 } }
4、启动服务
systemctl enable keepalived && systemctl start keepalived && systemctl status keepalived
5、进行验证
[root@harbor-20 ~]# ip a 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:16:3e:c0:74:1e brd ff:ff:ff:ff:ff:ff inet 172.21.210.20/25 brd 172.21.210.127 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 172.21.210.30/25 scope global secondary eth0 valid_lft forever preferred_lft forever 现在在20上,重启20的keeplived服务,可以看到IP自动跳转到19 systemctl restart keepalived
6、配置nginx,两边配置一样服务,每次vip跳转到该服务器都是用一样的nginx,nginx具体的安装可以参考:https://www.cnblogs.com/wukc/p/13236699.html
做一个决定,并不难,难的是付诸行动,并且坚持到底。