如下图所示,使用Keepalived实现简单的GrayLog高可用
(图片点击放大查看)
Node1:192.168.31.64
Node2:192.168.31.66
虚拟VIP——192.168.31.65
具体步骤如下
yum -y install keepalived
(图片点击放大查看)
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
(图片点击放大查看)
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
(图片点击放大查看)
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/
(图片点击放大查看)
firewall-cmd --add-rich-rule='rule protocol value="vrrp" accept' --permanent
firewall-cmd --reload
systemctl enable keepalived
systemctl start keepalived
systemctl status keepalived
(图片点击放大查看)
(图片点击放大查看)
这时Node1为主节点
手动关闭主节点的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
本文分享自 WalkingCloud 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!