
环境
(server1主)(node1副)
关闭防火墙 (两台主机同样 *** 作)
[root@server1 ~]# systemctl disable --now firewalld Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@server1 ~]# vim /etc/selinux/config ....... SELINUX=disabled ....... [root@server1 ~]# reboot
安装keepalived
[root@server1 ~]# yum -y install keepalived
查看生成的文件
[root@server1 ~]# rpm -ql keepalived /etc/keepalived //配置目录 /etc/keepalived/keepalived.conf //主配置文件 /etc/sysconfig/keepalived /usr/bin/genhash ...... /usr/lib/systemd/system/keepalived.service //服务控制文件 ......
在node1上也安装keepalived
[root@node1 ~]# yum -y install keepalived在主备机上安装httpd
在server1和node1上安装httpd
[root@server1 ~]# yum -y install httpd [root@node1 ~]# yum -y install httpd #开启自启httpd [root@server1 ~]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. [root@server1 ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 128 *:80 *:* [root@node1 ~]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. [root@node1 ~]# [root@node1 ~]# [root@node1 ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:22 [::]:* #在node1上添加 [root@node1 ~]# cd /var/www/html/ [root@node1 html]# ls [root@node1 html]# vim index.html [root@node1 html]# cat index.html web2在浏览器用IP访问(确保可以正常访问) keepalived配置 配置主keepalived
复制一个副本
[root@server1 keepalived]# cp keepalived.conf{,-bak}
[root@server1 keepalived]# ls
keepalived.conf keepalived.conf-bak
[root@server1 keepalived]# > keepalived.conf #清空文件
[root@server1 keepalived]# vi keepalived.conf
[root@server1 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface ens160 #服务器的网卡名
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass ksy
}
virtual_ipaddress {
192.168.244.250 #服务器网段
}
}
virtual_server 192.168.244.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.244.144 80 { #server1 ip
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.244.145 80 { #node2 IP
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#开机自启并启动服务
[root@server1 ~]# systemctl enable --now keepalived
#查看服务状态
[root@server1 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availabili>
Loaded: loaded (/usr/lib/systemd/system/keepaliv>
Active: active (running) since Thu 2021-10-21 17>
Main PID: 175522 (keepalived)
Tasks: 3 (limit: 11294)
Memory: 3.1M
CGroup: /system.slice/keepalived.service
├─175522 /usr/sbin/keepalived -D
├─175523 /usr/sbin/keepalived -D
└─175524 /usr/sbin/keepalived -D
Oct 21 17:56:58 server1 Keepalived_vrrp[175524]: (V>
Oct 21 17:56:59 server1 Keepalived_vrrp[175524]: (V>
Oct 21 17:57:00 server1 Keepalived_vrrp[175524]: (V>
Oct 21 17:57:01 server1 Keepalived_vrrp[175524]: (V>
Oct 21 17:57:02 server1 Keepalived_vrrp[175524]: (V>
Oct 21 17:57:03 server1 Keepalived_vrrp[175524]: (V>
Oct 21 17:57:04 server1 Keepalived_vrrp[175524]: (V>
Oct 21 17:57:05 server1 Keepalived_vrrp[175524]: (V>
Oct 21 17:57:06 server1 Keepalived_vrrp[175524]: (V>
lines 1-20/21 94%
#查看IP
[root@server1 ~]# ip a
1: lo: 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: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:be:41:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.244.144/24 brd 192.168.244.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.244.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::c8bb:96e4:534:b9f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
配置备keepalived
[root@node1 ~]# cd /etc/keepalived/
[root@node1 keepalived]# ls
keepalived.conf
[root@node1 keepalived]# cp keepalived.conf{,bak}
[root@node1 keepalived]# ls
keepalived.conf keepalived.confbak
[root@node1 keepalived]# > keepalived.conf
[root@node1 keepalived]# vi keepalived.conf
[root@node1 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass sky
}
virtual_ipaddress {
192.168.244.250
}
}
virtual_server 192.168.244.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.244.144 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.244.145 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#开机自启
[root@node1 ~]# systemctl enable --now keepalived
[root@node1 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availabili>
Loaded: loaded (/usr/lib/systemd/system/keepaliv>
Active: active (running) since Thu 2021-10-21 17>
Main PID: 171184 (keepalived)
Tasks: 3 (limit: 11301)
Memory: 3.2M
CGroup: /system.slice/keepalived.service
├─171184 /usr/sbin/keepalived -D
├─171185 /usr/sbin/keepalived -D
└─171186 /usr/sbin/keepalived -D
Oct 21 18:00:02 node1 Keepalived_vrrp[171186]: (VI_>
Oct 21 18:00:03 node1 Keepalived_vrrp[171186]: (VI_>
Oct 21 18:00:04 node1 Keepalived_vrrp[171186]: (VI_>
Oct 21 18:00:05 node1 Keepalived_vrrp[171186]: (VI_>
Oct 21 18:00:06 node1 Keepalived_vrrp[171186]: (VI_>
Oct 21 18:00:07 node1 Keepalived_vrrp[171186]: (VI_>
Oct 21 18:00:08 node1 Keepalived_vrrp[171186]: (VI_>
Oct 21 18:00:09 node1 Keepalived_vrrp[171186]: (VI_>
Oct 21 18:00:10 node1 Keepalived_vrrp[171186]: (VI_>
lines 1-20/21 94%
[root@node1 ~]# ip a
1: lo: 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: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:d7:44:97 brd ff:ff:ff:ff:ff:ff
inet 192.168.244.145/24 brd 192.168.244.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.244.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::1225:31da:302b:c596/64 scope link noprefixroute
valid_lft forever preferred_lft forever
让keepalived监控httpd负载均衡机
keepalived通过脚本来监控nginx负载均衡机的状态
在server1上编写脚本
[root@server1 ~]# mkdir /scripts
[root@server1 ~]# cd /scripts/
[root@server1 scripts]# vi check_n.sh
[root@server1 scripts]# cat check_n.sh
#!/bin/bash
nginx_status=$(ps -ef|grep -Ev "grep|[root@node1 ~]# mkdir /scripts
[root@node1 ~]# cd /scripts/
[root@node1 scripts]# vi notify.sh
[root@node1 scripts]# cat notify.sh
#!/bin/bash
VIP=
sendmail (){
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" 1611639655@qq.com
}
case "" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|[root@server1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script nginx_check {
script "/scripts/check_n.sh"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass ksy
}
virtual_ipaddress {
192.168.244.250
}
track_script {
nginx_check
}
notify_server1 "/scripts/notify.sh server1 192.168.244.250"
notify_node1 "/scripts/notify.sh node1 192.168.244.250"
}
virtual_server 192.168.244.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.244.144 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.244.145 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
"|grep 'bhttpb'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|[root@node1 ~]# vi /etc/keepalived/keepalived.conf
[root@node1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass sky
}
virtual_ipaddress {
192.168.244.250
}
notify_server1 "/scripts/notify.sh server1 192.168.244.250"
notify_node1 "/scripts/notify.sh node1 192.168.244.250"
}
virtual_server 192.168.244.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.244.144 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.244.145 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
"|grep 'bhttpb'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage: master|backup VIP"
;;
esac
[root@node1 scripts]# chmod +x notify.sh
[root@node1 scripts]# ll
total 4
-rwxr-xr-x 1 root root 662 Oct 21 18:11 notify.sh
"|grep 'bnginxb'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl stop keepalived
fi
[root@server1 scripts]# ls
check_n.sh
[root@server1 scripts]# chmod +x check_n.sh
[root@server1 scripts]# ll
total 4
-rwxr-xr-x 1 root root 142 Oct 21 18:04 check_n.sh
[root@server1 scripts]# vi notify.sh
[root@server1 scripts]# cat notify.sh
#!/bin/bash
VIP=
sendmail (){
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" 1611639655@qq.com
}
case "" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|"|grep 'bhttpb'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|"|grep 'bhttpb'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage: master|backup VIP"
;;
esac
[root@server1 scripts]# chmod +x notify.sh
[root@server1 scripts]# ll
total 12
-rw-r--r-- 1 root root 662 Oct 21 18:06 1
-rwxr-xr-x 1 root root 142 Oct 21 18:04 check_n.sh
-rwxr-xr-x 1 root root 662 Oct 21 18:06 notify.sh
在node2上编写脚本
配置keepalived加入监控脚本的配置配置主keepalived
配置备keepalived
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)