1. 程式人生 > 其它 >keepalived 高可用配置

keepalived 高可用配置

安裝

#ubuntu
apt-get install keepalived
#centos
yum install keepalived

master 節點配置,以nginx為例

cat > /etc/keepalived/keepalived.conf <<EOF
# master node
vrrp_script chk_nginx {
  script "killall -0 nginx"  #檢查nginx程序
  interval 2 #每隔兩秒一次
  weight 2 #檢查ok的權重
}
vrrp_instance VI_1 {
  interface bond0 # 設定對應的網絡卡,需要修改
  state MASTER
  virtual_router_id 100 # 一個keepalived組需要一個單獨的ID,主從一致,需要修改
  priority 101 #master的必須大於slave的值
  virtual_ipaddress {
    x.x.x.x # vip,需要修改
  }
  track_script {
    chk_nginx #上面檢測的配置
  }
}
EOF

配置slave節點

cat > /etc/keepalived/keepalived.conf <<EOF
# master node
vrrp_script chk_nginx {
  script "killall -0 nginx"  #檢查nginx程序
  interval 2 #每隔兩秒一次
  weight 2 #檢查ok的權重
}
vrrp_instance VI_1 {
  interface bond0 # 設定對應的網絡卡,需要修改
  state SLAVE
  virtual_router_id 100 # 一個keepalived組需要一個單獨的ID,主從一致,需要修改
  priority 100 #master的必須大於slave的值
  virtual_ipaddress {
    x.x.x.x # vip,需要修改
  }
  track_script {
    chk_nginx #上面檢測的配置
  }
}
EOF

啟動keepalived
檢查是否ok ip a檢視