9、Keepalived安装配置

全部文章导航

点此查看K8S、Jenkins持续集成环境搭建全部文章

KeepAlived安装使用

1、安装

# 查看可安装版本
sudo yum list keepalived --showduplicates | tail -n 20
# 安装
sudo yum install -y keepalived-2.1.5-6.el8

2、配置示例

需要在110.103、110.104机器安装配置keepalived。当110.104主机宕机或者110.104的nginx服务停止时,vip自动漂移到110.103,由110.103继续提供服务。

示例图

image-20211013110738546

修改配置信息
# 配置文件
vi /etc/keepalived/keepalived.conf
:.,$d # 删除所有行内容
keepalived.conf主配置信息-nginx(192.168.110.104)
! Configuration File for keepalived

global_defs {
   notification_email {
   }
   router_id LB_DEVEL_4
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script nginx_check {
  script "/bin/bash -c 'if [[ $(netstat -nlp | grep 8008 | wc -l ) > 0 ]]; then exit 0; else exit 1; fi'"
  interval 2
  weight -20
}

vrrp_instance nginx-vip {
  state MASTER #表示当前实例nginx-vip的角色状态这个状态只能有MASTER和BACKUP两种状态,并且需要大写这些字符ASTER为正式工作的状态,BACKUP为备用的状态
  priority 100 # 节点优先级,MASTER要比BACKUP高 
  interface ens33                #vip绑定网卡
  virtual_router_id 61 #虚拟路由的ID号
  advert_int 1 # MASTER与BACKUP节点间同步检查的时间间隔,单位为秒 
  authentication {
    auth_type PASS
    auth_pass f123456
  }
  unicast_src_ip 192.168.110.104   #当前机器地址
  unicast_peer {
    192.168.110.103                #peer中其它地址
  }

  virtual_ipaddress {
    192.168.110.180           #vip地址               
  }

  track_script {
    nginx_check
  }
}
keepalived.conf从配置信息-nginx(192.168.110.103)
! Configuration File for keepalived

global_defs {
   notification_email {
   }
   router_id LB_DEVEL_1
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script nginx_check {
  script "/bin/bash -c 'if [[ $(netstat -nlp | grep 8008 | wc -l ) > 0 ]]; then exit 0; else exit 1; fi'"
  interval 2
  weight -20
}

vrrp_instance nginx-vip {
  state BACKUP #表示当前实例nginx-vip的角色状态这个状态只能有MASTER和BACKUP两种状态,并且需要大写这些字符ASTER为正式工作的状态,BACKUP为备用的状态
  priority 90  # 节点优先级,MASTER要比BACKUP高 
  interface ens33                #vip绑定网卡
  virtual_router_id 61  #虚拟路由的ID号
  advert_int 1 # MASTER与BACKUP节点间同步检查的时间间隔,单位为秒 
  authentication {
    auth_type PASS
    auth_pass f123456
  }
  unicast_src_ip 192.168.110.103   #当前机器地址
  unicast_peer {
    192.168.110.104                #peer中其它地址
  }

  virtual_ipaddress {
    192.168.110.180           #vip地址               
  }

  track_script {
    nginx_check
  }
}

3、开机启动、启动

 # 启动keepalived
 sudo systemctl start keepalived
 # 加入开机启动keepalived
 sudo systemctl enable keepalived
 # 查看keepalived状态
 sudo systemctl status keepalived

4、验证

访问测试:
# 通过110.180访问k8s服务
http://192.168.110.180:8008/ele-admin/
高可用测试
# 停掉Mater(110.104)的Nginx
sudo systemctl stop nginx
# 通过110.180访问k8s服务,如果可以正常
http://192.168.110.180:8008/ele-admin/
# BackUp执行'ip a',查看虚拟ip是否漂移到BackUp(110.103)
ip a
# 启动Mater(110.104)的Nginx,执行'ip a',查看虚拟ip是否漂移到Master(110.104)
sudo systemctl start nginx
ip a 
posted @ 2021-10-15 09:01  江边飞鸟  阅读(368)  评论(0编辑  收藏  举报