集群构成

ip地址 VIP 角色
10.0.0.11 10.0.0.20 master
10.0.0.12 10.0.0.20 backup

目录结构

|—— keepalived/   // 根目录
****|—— cmd/
********|—— main.go
********|—— my-server  // 服务执行命令
****|—— script/
********|—— check.sh  // 检测脚本
****|—— log/
********|—— keepalived.log  // keepalived 输出日志

服务编写

  1. /path/to/keepalived/cmd/main.go(master):

     package main
    
     import (
         "fmt"
         "net/http"
     )
    
     func sayHello(w http.ResponseWriter, r *http.Request) {
         fmt.Fprintln(w, "server 1.")
     }
     func main() {
         http.HandleFunc("/", sayHello) // 设置访问的路由
         fmt.Println("server running at: :8080")
         if err := http.ListenAndServe(":8080", nil); err != nil {
             panic(err)
         }
     }
  2. /path/to/keepalived/cmd/main.go(backup):

     package main
    
     import (
         "fmt"
         "net/http"
     )
    
     func sayHello(w http.ResponseWriter, r *http.Request) {
         fmt.Fprintln(w, "server 2.")
     }
     func main() {
         http.HandleFunc("/", sayHello) // 设置访问的路由
         fmt.Println("server running at: :8080")
         if err := http.ListenAndServe(":8080", nil); err != nil {
             panic(err)
         }
     }
  3. 生成可执行文件(master && backup):

    cd /path/to/keepalived/cmd
    go build -o my-server main.go

keepalived 安装及配置

  1. 安装(master && backup):
    yum -y install keepalived

  2. 配置(master):
    vim /etc/keepalived/keepalived.conf

     ! Configuration File for keepalived
     global_defs {
         script_user root
         enable_script_security
     }
     vrrp_script check_my_server {  # 检测服务运行状态脚本
         script "/path/to/keepalived/script/check.sh"  # 检测脚本存放的路径
         interval 5  # 测脚本执行的间隔,即检测脚本每隔 5s 会自动执行一次
         weight 1  # 权重,如果这个脚本检测为真,服务器权重+1
     }
     vrrp_instance VI_1 {
         state MASTER  # 角色是 master
         interface eth1  # 绑定的网卡
         virtual_router_id 51  # id 号必须相同
         priority 2  # 优先级,谁的优先级高谁就是 master
         advert_int 1  # 心跳间隔时间(秒)
         authentication {  # 密码认证
             auth_type PASS
             auth_pass 123456
         }
         virtual_ipaddress {
             10.0.0.20  # 虚拟 IP
         }
         track_script {
             check_my_server
         }
     }
  3. 配置(backup):
    vim /etc/keepalived/keepalived.conf

     ! Configuration File for keepalived
     global_defs {
         script_user root
         enable_script_security
     }
     vrrp_script check_my_server {  # 检测服务运行状态脚本
         script "/path/to/keepalived/script/check.sh"  # 检测脚本存放的路径
         interval 5  # 测脚本执行的间隔,即检测脚本每隔 5s 会自动执行一次
         weight 1  # 权重,如果这个脚本检测为真,服务器权重+1
     }
     vrrp_instance VI_1 {
         state BACKUP  # 角色是 backup
         interface eth1  # 绑定的网卡
         virtual_router_id 51  # id 号必须相同
         priority 1  # 优先级,谁的优先级高谁就是 master
         advert_int 1  # 心跳间隔时间(秒)
         authentication {  # 密码认证
             auth_type PASS
             auth_pass 123456
         }
         virtual_ipaddress {
             10.0.0.20  # 虚拟 IP
         }
         track_script {
             check_my_server
         }
     }

检测脚本配置(master && backup)

  1. 编写检测脚本:
    vim /path/to/keepalived/script/check.sh

     #!/bin/bash
     if [ "$(pgrep my-server | wc -l)" == "0" ]; then
         echo "$(date '+%F %T'):my-server 停止运行,尝试重启" >>/path/to/keepalived/log/keepalived.log
         /path/to/keepalived/cmd/my-server &
         sleep 3
         if [ "$(pgrep my-server | wc -l)" == "0" ]; then
             echo "$(date '+%F %T'):停止运行 keepalived,转换 VIP" >>/path/to/keepalived/log/keepalived.log
             systemctl stop keepalived
         fi
     fi
  2. 给检测脚本添加可执行权限:
    chmod +x /path/to/keepalived/script/check.sh

关闭 selinux

参见:centos7 基本配置-10.关闭selinux

测试

  1. 启动 keepalived(master && backup):
    systemctl start keepalived

  2. 通过 VIP 访问当前在哪台服务器:
    curl 10.0.0.20:8080

  3. 停止 keepalived(master):
    systemctl stop keepalived

  4. 通过 VIP 访问当前在哪台服务器:
    curl 10.0.0.20:8080

文档更新时间: 2024-03-24 15:25   作者:lee