前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Keepalived实现简单的GrayLog高可用

使用Keepalived实现简单的GrayLog高可用

作者头像
yuanfan2012
发布2023-06-23 10:33:43
2220
发布2023-06-23 10:33:43
举报
文章被收录于专栏:WalkingCloud

如下图所示,使用Keepalived实现简单的GrayLog高可用

(图片点击放大查看)

Node1:192.168.31.64

Node2:192.168.31.66

虚拟VIP——192.168.31.65

具体步骤如下

1、yum安装keepalived

代码语言:javascript
复制
yum -y install keepalived

(图片点击放大查看)

2、配置keepalived.conf

代码语言:javascript
复制
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"
}

(图片点击放大查看)

(图片点击放大查看)

备节点修改如下位置

代码语言:javascript
复制
scp /etc/keepalived/keepalived.conf root@192.168.31.66:/etc/keepalived/keepalived.conf
代码语言:javascript
复制
vim /etc/keepalived/keepalived.conf
state BACKUP
priority 80

(图片点击放大查看)

3、编写notify脚本

代码语言:javascript
复制
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

(图片点击放大查看)

代码语言:javascript
复制
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

代码语言:javascript
复制
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

(图片点击放大查看)

代码语言:javascript
复制
chmod a+x /etc/keepalived/*.sh
scp /etc/keepalived/*.sh root@192.168.31.66:/etc/keepalived/

(图片点击放大查看)

5、启动keepalived

代码语言:javascript
复制
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会漂移到主节点

(图片点击放大查看)

本文参考如下链接完成

代码语言:javascript
复制
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
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WalkingCloud 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、yum安装keepalived
  • 2、配置keepalived.conf
  • 3、编写notify脚本
  • 4、编写track_script
  • 5、启动keepalived
  • 6、Keepalived高可用测试验证
  • 本文参考如下链接完成
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档