如下图所示,使用Keepalived实现简单的GrayLog高可用
(图片点击放大查看)
Node1:192.168.31.64
Node2:192.168.31.66
虚拟VIP——192.168.31.65
具体步骤如下
1、yum安装keepalived
yum -y install keepalived
(图片点击放大查看)
2、配置keepalived.conf
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf-default echo -n | tee /etc/keepalived/keepalived.conf vim /etc/keepalived/keepalived.conf global_defs { # Keepalived process identifier router_id graylog } # Script to check whether graylog is running or not vrrp_script check_graylog { script "/etc/keepalived/check_graylog.sh" interval 3 weight 50 fall 2 } # Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover vrrp_instance VI_Graylog { state MASTER interface ens33 virtual_router_id 80 priority 110 # The virtual ip address shared between the two GrayLog Web Server which will float virtual_ipaddress { 192.168.31.65/24 dev ens33 } track_script { check_graylog } authentication { auth_type PASS auth_pass Keepalived2023 } notify_master "/etc/keepalived/master.sh" notify_backup "/etc/keepalived/backup.sh" notify_fault "/etc/keepalived/fault.sh" }
(图片点击放大查看)
(图片点击放大查看)
备节点修改如下位置
scp /etc/keepalived/keepalived.conf root@192.168.31.66:/etc/keepalived/keepalived.conf
vim /etc/keepalived/keepalived.conf state BACKUP priority 80
(图片点击放大查看)
3、编写notify脚本
vim /etc/keepalived/master.sh 添加如下行 #!/bin/bash LOGFILE=/var/log/keepalived-state.log date >> $LOGFILE echo "[MASTER]" >> $LOGFILE webhook_url="https://oapi.dingtalk.com/robot/send?access_token=4f851XXXXXXXXcaf25e6eabad63f88aXX" NodeIP=`(hostname -I)` set_payload_file(){ cat > /opt/payload_result.json << \EOF { "msgtype": "actionCard", "actionCard": { "title":"当前节点已切换成Master节点", "text":" ##### 当前节点已切换成Master节点 \n > ##### <font color=#67C23A> 【Master节点IP】</font> :<font color=#FF0000> templateIP </font> \n " } } EOF } set_payload_file sed -i "s^templateIP^$NodeIP^g" /opt/payload_result.json response=$(curl -sS -H "Content-Type: application/json" -X POST -d @/opt/payload_result.json "${webhook_url}") if [ $? -eq 0 ]; then echo "Alert sent successfully" else echo "Failed to send alert: ${response}" fi
(图片点击放大查看)
vim /etc/keepalived/backup.sh 添加如下行 #!/bin/bash LOGFILE=/var/log/keepalived-state.log date >> $LOGFILE echo "[BACKUP]" >> $LOGFILE vim /etc/keepalived/fault.sh 添加如下行 #!/bin/bash LOGFILE=/var/log/keepalived-state.log date >> $LOGFILE echo "[FAULT]" >> $LOGFILE
(图片点击放大查看)
4、编写track_script
vim /etc/keepalived/check_graylog.sh 添加如下行 #!/bin/bash Graylog_PID=`ps -ef | grep '/bin/graylog-server' | grep -v grep | awk '{print $2}'` if [ -z $Graylog_PID ]; then exit 1 else exit 0 fi
(图片点击放大查看)
chmod a+x /etc/keepalived/*.sh scp /etc/keepalived/*.sh root@192.168.31.66:/etc/keepalived/
(图片点击放大查看)
5、启动keepalived
firewall-cmd --add-rich-rule='rule protocol value="vrrp" accept' --permanent firewall-cmd --reload systemctl enable keepalived systemctl start keepalived systemctl status keepalived
(图片点击放大查看)
(图片点击放大查看)
这时Node1为主节点
6、Keepalived高可用测试验证
手动关闭主节点的graylog-server服务
这时虚拟VIP会漂移到备节点
(图片点击放大查看)
再启动主节点的graylog-server服务
这时Node1为切回Master 这时虚拟VIP会漂移到主节点
(图片点击放大查看)
本文参考如下链接完成
https://linuxhandbook.com/load-balancing-setup/ https://www.linuxtechi.com/setup-highly-available-nginx-keepalived-linux/ https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/load_balancer_administration/keepalived_install_example1#doc-wrapper https://www.redhat.com/sysadmin/advanced-keepalived https://www.howtoforge.com/how-to-setup-highly-available-nginx-with-keepalived-on-centos-8/ https://www.atlantic.net/dedicated-server-hosting/how-to-vrrp-keepalived-configuration/ https://tobru.ch/keepalived-check-and-notify-scripts/ https://docs.oracle.com/en/operating-systems/oracle-linux/6/admin/section_hxz_zdw_pr.html