kubernetes学习(6)---安装haproxy并配置keepalived高可用

在所有master节点(192.168.100.52 192.168.100.53 192.168.100.54)安装hapoxy

yum install haproxy -y

所有master节点(192.168.100.52 192.168.100.53 192.168.100.54)配置haproxy,配置一样

vim /etc/haproxy/haproxy.cfg

内容如下

global
  maxconn  2000
  ulimit-n  16384
  log  127.0.0.1 local0 err
  stats timeout 30s

defaults
  log global
  mode  http
  option  httplog
  timeout connect 5000
  timeout client  50000
  timeout server  50000
  timeout http-request 15s
  timeout http-keep-alive 15s

frontend k8s-master
  bind 0.0.0.0:8443
  bind 127.0.0.1:8443
  mode tcp
  option tcplog
  tcp-request inspect-delay 5s
  default_backend k8s-master

backend k8s-master
  mode tcp
  option tcplog
  option tcp-check
  balance roundrobin
  default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
  server k8s-100-52    192.168.100.52:6443  check
  server k8s-100-53    192.168.100.53:6443  check
  server k8s-100-54    192.168.100.54:6443  check
KEEPNODE = 'k8s-100-52 k8s-100-53 k8s-100-54'
for NODE in $KEEPNODE; do
    ssh $NODE "yum install keepalived -y"
done

在三个master01(192.168.100.52)、master02(192.168.100.53)和 master03(192.168.100.54)上进行文件配置

vim /etc/keepalived/check_apiserver.sh
# 内容如下
#!/bin/bash
err=0
for k in $(seq 1 3)
do
    check_code=$(pgrep haproxy)
    if [[ $check_code == "" ]]; then
        err=$(expr $err + 1)
        sleep 1
        continue
    else
        err=0
        break
    fi
done

if [[ $err != "0" ]]; then
    echo "systemctl stop keepalived"
    /usr/bin/systemctl stop keepalived
    exit 1
else
    exit 0
fi
# 给check_apiserver.sh赋予执行权限
chmod +x /etc/keepalived/check_apiserver.sh

在master01(192.168.100.52)上配置keepalived.conf

vim /etc/keepalived/keepalived.conf
# 内容如下
! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL  #此处注意router_id为负载均衡标识,在局域网内应该是唯一的
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
# 健康检测脚本,必须声明在vrrp_instance节点前
vrrp_script chk_apiserver {
    script "/etc/keepalived/check_apiserver.sh" # 检测apiserver的脚本
    interval 5 # 检测时间间隔
    weight -20 # 如果条件成立,权重-20
    fall 2
    rise 1
}
# vrrp实例设置
vrrp_instance VI_1 {
    state MASTER           # master为主机
    interface eth0         # 网卡跟实际情况一样
    virtual_router_id 251  # 实例1的VRID为251
    priority 100           # 主(192.168.100.52)的优先级为100,从的(192.168.100.53、192.168.100.54)的优先级为99
    advert_int 1
    mcast_src_ip 192.168.100.52
    unicast_src_ip 192.168.100.52
    unicast_peer {
        #对象IP  发送vrrp包给备服务器
        192.168.100.53,
        192.168.100.54
    }
    nopreempt              # 不抢占模式  如果是抢占模式,则在有VIP的虚拟机重启后,会出现ip冲突的
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    track_script {         # 脚本追踪
         chk_apiserver
    }
    virtual_ipaddress {    # 虚拟ip
        192.168.100.57
    }
}

在master02(192.168.100.53)上配置keepalived.conf

vim /etc/keepalived/keepalied.conf
# 内容如下
! Configuration File for keepalived
global_defs {
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script chk_apiserver {
    script "/etc/keepalived/check_apiserver.sh"
    interval 5 
    weight -20
    fall 2
    rise 1
 
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    mcast_src_ip 192.168.100.53
    unicast_src_ip 192.168.100.53
    unicast_peer {
        192.168.100.52,
        192.168.100.54
    }
    virtual_router_id 251
    priority 90
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    virtual_ipaddress {
        192.168.100.57
    }
    track_script {
      chk_apiserver 
    } 
}

在master03(192.168.100.54)上配置keepalived.conf

vim /etc/keepalived/keepalied.conf
# 内容如下
! Configuration File for keepalived
global_defs {
    router_id LVS_DEVEL
    vrrp_skip_check_adv_addr
    vrrp_garp_interval 0
    vrrp_gna_interval 0
}
vrrp_script chk_apiserver {
    script "/etc/keepalived/check_apiserver.sh"
    interval 5
    weight -5
    fall 2  
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    mcast_src_ip 192.168.100.54
    unicast_src_ip 192.168.100.54
    unicast_peer {
        192.168.100.52,
        192.168.100.53
    }
    virtual_router_id 251
    priority 80
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    virtual_ipaddress {
        192.168.100.57
    }
    track_script {
      chk_apiserver 
    } 
}

启动keepalivedhaproxy

# 所有master节点启动haproxy和keepalived
systemctl daemon-reload && \
systemctl enable --now haproxy && \
systemctl enable --now keepalived
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值