当前位置: 首页 > article >正文

Kubernetes学习指南:保姆级实操手册05——配置集群HA负载均衡

五、Kubernetes学习指南:保姆级实操手册05——配置集群HA负载均衡

简介: Keepalived 提供 VRRP 实现,并允许您配置 Linux 机器使负载均衡,预防单点故障。HAProxy 提供可靠、高性能的负载均衡,能与 Keepalived 完美配合

1、配置Keepalive

官方文档提供了两种运行方式(此案例使用选项1):

  • 选项1:在操作系统上运行服务
  • 选项2:将服务作为静态pod运行

参考文档:[https://github.com/kubernetes/kubeadm/blob/main/docs/ha-considerations.md#options-for-software-load-balancing]

1.1、安装keepalived组件

:三台master节点上安装

yum install -y keepalived
1.2、配置keepalived
### 在k8s-master01上设置:
[root@k8s-m01 ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.back
[root@k8s-master01 keepalived]# 

cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {
    router_id k8s-master01
}
vrrp_script check_apiserver {
  script "/etc/keepalived/check_apiserver.sh"
  interval 3
  weight -2
  fall 10
  rise 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens192
    virtual_router_id 51
    priority 100
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.255.210.99
    }
    track_script {
        check_apiserver
    }
}

EOF

### 在k8s-master02上设置:
$ mkdir /etc/keepalived 

[root@k8s-master02 keepalived]# cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {
    router_id k8s-master02
}
vrrp_script check_apiserver {
  script "/etc/keepalived/check_apiserver.sh"
  interval 3
  weight -2
  fall 10
  rise 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 99
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.255.210.99
    }
    track_script {
        check_apiserver
    }
}

EOF


### 在k8s-master03上设置:
$ mkdir /etc/keepalived

[root@k8s-master03 keepalived]# cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {
    router_id k8s-master03
}
vrrp_script check_apiserver {
  script "/etc/keepalived/check_apiserver.sh"
  interval 3
  weight -2
  fall 10
  rise 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 98
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.255.210.99
    }
    track_script {
        check_apiserver
    }
}

EOF

扩展:参数说明

参数说明:
    router_id:
        节点ip,master每个节点配置自己的IP
    mcast_src_ip:
        节点IP,master每个节点配置自己的IP
    virtual_ipaddress:
        虚拟IP,即VIP。
    interface:
        指定接口的名称。
    virtual_router_id:
        有效值为0-255,可以理解为一个组ID,只有相同的ID才被确认为一个组。
        如果每个keepalived实例修改的ID不一致,则会出现各自有一个VIP的现象。    
1.3、编写健康检查脚本
[root@k8s-master01 keepalived]# cat > /etc/keepalived/check_apiserver.sh   <<EOF
#!/bin/sh  

errorExit() {  
    echo "*** $*" 1>&2  
    exit 1  
}  

curl --silent --max-time 2 --insecure https://localhost:16443/ -o /dev/null || errorExit "Error GET https://localhost:16443/"  
if ip addr | grep -q 10.255.210.99; then  
curl --silent --max-time 2 --insecure https://10.255.210.99:16443/ -o /dev/null || errorExit "Error GET https://10.255.210.99:16443/"  
fi  

EOF

chmod +x  /etc/keepalived/check_apiserver.sh

scp /etc/keepalived/check_apiserver.sh root@k8s-master02:/etc/keepalived/
scp /etc/keepalived/check_apiserver.sh root@k8s-master03:/etc/keepalived/
1.4、启动Keepalived
systemctl enable keepalived --now; systemctl restart keepalived.service ;systemctl status keepalived.service
1.5、测试keepalived
ip a   
#查看VIP在那个节点   

systemctl stop keepalived.service   
#VIP所在节点停止服务,观察是否飘移VIP  

 systemctl restart keepalived.service   
#重启服务后,VIP将迁回

2、配置Haproxy

2.1、安装Haproxy
yum install -y haproxy
2.2、配置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 5s
  timeout client  50s
  timeout server  50s
  timeout http-request 15s
  timeout http-keep-alive 15s

frontend monitor-in
  bind *:33305
  mode http
  option httplog
  monitor-uri /monitor

listen stats
  bind    *:8006
  mode    http
  stats   enable
  stats   hide-version
  stats   uri       /stats
  stats   refresh   30s
  stats   realm     Haproxy\ Statistics
  stats   auth      admin:admin

frontend k8s-master
  bind 0.0.0.0:16443
  bind 127.0.0.1:16443
  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-master01 10.255.210.1:6443  check
  server k8s-master02 10.255.210.2:6443  check
  server k8s-master03 10.255.210.3:6443  check
2.3、启动haproxy
systemctl restart haproxy.service;systemctl status haproxy.service
2.4、查看端口
 ss -alnupt |grep 16443
tcp    LISTEN     0      2000      *:16443                 *:*                   users:(("haproxy",pid=53056,fd=6))
tcp    LISTEN     0      2000   127.0.0.1:16443                 *:*                   users:(("haproxy",pid=53056,fd=7))
[root@k8s-master02 ~]# ss -alnupt |grep 6443
tcp    LISTEN     0      2000      *:16443                 *:*                   users:(("haproxy",pid=53056,fd=6))
tcp    LISTEN     0      2000   127.0.0.1:16443                 *:*                   users:(("haproxy",pid=53056,fd=7))

http://www.kler.cn/a/291147.html

相关文章:

  • 《全面解析 QT 各版本:特性、应用与选择策略》
  • 【STM32 Modbus编程】-作为主设备写入多个线圈和寄存器
  • Kubernetes(K8s)学习笔记
  • AlipayHK支付宝HK接入-商户收款(PHP)
  • Java并发编程框架之综合案例——在线聊天室(二)
  • JS设计模式之中介者模式
  • 智能巡检机器人创新设计的关键技术
  • Xml 映射文件中常见的标签
  • python(进阶2)实现自动化注册和登录
  • 长芯微国产LS0102电平转换器/电平移位器P2P替代TXS0102
  • 支付宝线上小程序打开异常
  • VUE3+FLASK+TYPESCRIPT(实习接触,学习并自主实现)
  • 硬件-电源常识
  • Python作为客户端连接websocket
  • 10.2 TCP IP模型、IP协议、IPv4、子网掩码
  • SpringBoot3 项目部署
  • 【计算机毕业设计】微信小程序的美甲店铺座位预约系统
  • 数据结构(6.4_6)——拓扑排序
  • SDIO驱动开发
  • 支持萝卜快跑:AI能否颠覆出租车与外卖行业?
  • 大模型研发全揭秘:数据决定模型成败!如何确保数据采集不踩坑?
  • http、https、https原理
  • UI自动化测试 —— 下拉选择框弹出框滚动条操作实践!
  • armv8 memory model概述
  • 【Redis】缓存(上)
  • 红黑树总结(RbTree)——C++版