[部署zabbix_agent请参考zabbix_agent的配置流程文章](()
在客户端上安装zabbix_agentd
[root@Server ~]# scp zabbix-5.4.4.tar.gz 192.168.129.135:/usr/src
root@192.168.129.135’s password:
zabbix-5.4.4.tar.gz 100% 20MB 101.2MB/s 00:00
[root@agent ~]# tar xf zabbix-5.4.4.tar.gz
//创建用户
[root@agent ~]# cd zabbix-5.4.4
[root@agent zabbix-5.4.4]# useradd -r -M -s /sbin/nologin zabbix
[root@agent ~]# yum -y install vim wget gcc gcc-c++ make pcre-devel openssl openssl-devel
[root@agent zabbix-5.4.4]# ./configure --enable-agent
[root@agent zabbix-5.4.4]# make install
在客户端配置文件中配置服务端的地址和主机名
[root@agent ~]# vim /usr/local/etc/zabbix_agentd.conf
……
Server=192.168.129.250 #113行
ServerActive=192.168.129.250 #154行
Hostname=zhLbLUid #165行 生产环境建议使用随机生成的字符串
……
//随机生成的字符串
[root@Server ~]#tr -dc A-Za-z < /dev/urandom| head -c 8 |xargs
zhLbLUid
启动agent
[root@agent ~]# zabbix_agentd
[root@agent ~]# ss -anlt
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 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]😗
[](()keepalived
[部署keepalived请参考 keepalived实现httpd负载均衡机高可用 文章](()
在主备机上分别安装httpd
- 在master上安装httpd
[root@master ~]# yum -y install httpd
[root@master ~]# echo “This is MASTER” > /var/www/html/index.html
[root@master ~]# systemctl restart httpd
[root@master ~]# systemctl enable --now httpd
[root@master ~]# curl 192.168.129.134
This is MASTER
- 在slave 上安装httpd
[root@slave ~]# yum -y install httpd
[root@slave ~]# echo “This is BACKUP” > /var/www/html/index.html
[root@slave ~]# systemctl restart httpd
[root@slave ~]# systemctl enable --now httpd
[root@slave ~]# curl 192.168.129.135
This is BACKUP
配置备keepalived
[root@anget ~]# dnf -y install keepalived
[root@anget ~]# cp -r /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.brk
[root@anget ~]# ls /etc/keepalived/
keepalived.conf keepalived.conf.brk
[root@anget ~]# vim /etc/keepalived/keepalived.conf
[root@anget ~]# 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 12345678
}
virtual_ipaddress {
192.168.129.144
}
}
virtual_server 192.168.129.144 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.129.135 80 {
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.129.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@anget ~]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@master ~]# yum -y install keepalived
[root@master ~]# cp -r /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.brk
[root@master ~]# ls /etc/keepalived/
keepalived.conf keepalived.conf.brk
[root@master ~]# vi /etc/keepalived/keepalived.conf
[root@master ~]# cat /etc/keepalived/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 12345678
}
virtual_ipaddress {
192.168.129.144
}
}
virtual_server 192.168.129.144 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.129.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.129.135 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master ~]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
查看vip在哪里
[root@anget ~]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:aa:87:a6 brd ff:ff:ff:ff:ff:ff
inet 192.168.129.132/24 brd 192.168.129.255 scope global dynamic noprefixroute ens160
valid_lft 1518sec preferred_lft 1518sec
inet 192.168.129.135/24 brd 192.168.129.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::830b:1a3a:1104:10b5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@master ~]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:04:e2:3a brd ff:ff:ff:ff:ff:ff
inet 192.168.129.134/24 brd 192.168.129.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.129.144/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::5f9d:bb13:b9cf:e4a2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
修改内核参数,开启监听VIP功能
此步可做可不做,该功能可用于仅监听VIP的时候
[root@master ~]# echo ‘net.ipv4.ip_nonlocal_bind = 1’ >>/etc/sysctl.conf
[root@master ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@master ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1
[root@anget ~]# echo ‘net.ipv4.ip_nonlocal_bind = 1’ >>/etc/sysctl.conf
[root@anget ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@anget ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1
编写监控keppalived脚本
[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# vim check_n.sh
[root@master scripts]# cat check_n.sh
#!/bin/bash
httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)
if [ $httpd_status -lt 1 ];then
systemctl stop keepalived
fi
[root@master scripts]# chmod +x check_n.sh
[root@master scripts]# ll
总用量 4
-rwxr-xr-x 1 root root 142 10月 23 00:41 check_n.sh
[root@master scripts]# vim notify.sh
[root@master scripts]# cat notify.sh
#!/bin/bash
VIP=$2
sendmail (){
subject=“${VIP}'s server keepalived state is translate”
content=“date +'%F %T'
: hostname
’s state change to master”
echo c o n t e n t ∣ m a i l − s " content | mail -s " content∣mail−s"subject" 2464146612@qq.com
}
case “$1” in
master)
httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)
if [ $httpd_status -lt 1 ];then
systemctl start httpd
fi
sendmail
;;
backup)
httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)
if [ $httpd_status -gt 0 ];then
systemctl stop httpd
fi
;;
*)
echo “Usage:$0 master|backup VIP”
;;
esac
[root@master scripts]# chmod +x notify.sh
[root@master scripts]# ll
总用量 8
-rwxr-xr-x 1 root root 142 10月 23 00:41 check_n.sh
-rwxr-xr-x 1 root root 662 10月 23 00:43 notify.sh
[root@anget ~]# mkdir /scripts
[root@anget ~]# cd /scripts/
[root@anget scripts]# vim notify.sh
[root@anget scripts]# cat notify.sh
#!/bin/bash
VIP=$2
sendmail (){
subject=“${VIP}'s server keepalived state is translate”
content=“date +'%F %T'
: hostname
’s state change to master”
echo c o n t e n t ∣ m a i l − s " content | mail -s " content∣mail−s"subject" 2464146612@qq.com
}
case “$1” in
master)
httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)
if [ $httpd_status -lt 1 ];then
systemctl start httpd
fi
sendmail
;;
backup)
httpd_status=$(ps -ef|grep -Ev “grep|$0”|grep ‘\bhttpd\b’|wc -l)
if [ $httpd_status -gt 0 ];then
systemctl stop httpd
fi
;;
*)
echo “Usage:$0 master|backup VIP”
;;
esac
[root@anget scripts]# chmod +x notify.sh
[root@anget scripts]# ll
总用量 4
-rwxr-xr-x 1 root root 663 10月 23 00:44 notify.sh
配置keepalived加入监控脚本的配置
- 配置主keepalived
[root@master ~]# vim /etc/keepalived/keepalived.conf
[root@master ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script httpd_check {
script “/scripts/check_n.sh”
interval 1
weight -20 #权重减20
}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 12345678
}
virtual_ipaddress {
192.168.129.144
}
}
virtual_server 192.168.129.144 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.129.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.129.135 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master ~]# systemctl restart keepalived
配置备keepalived
backup无需检测httpd是否正常,当升级为MASTER时启动httpd,当降级为BACKUP时关闭
[root@anget ~]# vim /etc/keepalived/keepalived.conf