教你怎样用Keepalived和HAproxy配置高可用 Kubernetes 集群

打印 上一主题 下一主题

主题 531|帖子 531|积分 1593

本文分享自华为云社区《使用 Keepalived 和 HAproxy 创建高可用 Kubernetes 集群》,作者:江晚正愁余。
高可用 Kubernetes 集群能够确保应用程序在运行时不会出现服务停止,这也是生产的需求之一。为此,有许多方法可供选择以实现高可用。
本教程演示了怎样配置 Keepalived 和 HAproxy 使负载均衡、实现高可用。步骤如下:

  • 准备主机。
  • 配置 Keepalived 和 HAproxy。
  • 使用 KubeKey 创建 Kubernetes 集群,并安装 KubeSphere。
集群架构

示例集群有三个主节点,三个工作节点,两个用于负载均衡的节点,以及一个假造 IP 所在。本示例中的假造 IP 所在也可称为“浮动 IP 所在”。这意味着在节点故障的环境下,该 IP 所在可在节点之间漂移,从而实现高可用。

请留意,在本示例中,Keepalived 和 HAproxy 没有安装在任何主节点上。但您也可以这样做,并同时实现高可用。然而,配置两个用于负载均衡的特定节点(您可以按需增加更多此类节点)会更加安全。这两个节点上只安装 Keepalived 和 HAproxy,以避免与任何 Kubernetes 组件和服务的潜在冲突。
准备主机

IP 所在
主机名
角色
172.16.0.2
lb1
Keepalived & HAproxy
172.16.0.3
lb2
Keepalived & HAproxy
172.16.0.4
master1
master, etcd
172.16.0.5
master2
master, etcd
172.16.0.6
master3
master, etcd
172.16.0.7
worker1
worker
172.16.0.8
worker2
worker
172.16.0.9
worker3
worker
172.16.0.10
 假造 IP 所在
有关更多节点、网络、依靠项等要求的信息,请拜见多节点安装。
配置负载均衡

Keepalived 提供 VRRP 实现,并允许您配置 Linux 机器使负载均衡,预防单点故障。HAProxy 提供可靠、高性能的负载均衡,能与 Keepalived 完美共同。
由于 lb1 和 lb2 上安装了 Keepalived 和 HAproxy,如果其中一个节点故障,假造 IP 所在(即浮动 IP 所在)将主动与另一个节点关联,使集群仍然可以正常运行,从而实现高可用。若有必要,也可以此为目的,添加更多安装 Keepalived 和 HAproxy 的节点。
先运行以下下令安装 Keepalived 和 HAproxy。
  1. yum install keepalived haproxy psmisc -y
复制代码
HAproxy

1.在两台用于负载均衡的机器上运行以下下令以配置 Proxy(两台机器的 Proxy 配置相同):
  1. vi /etc/haproxy/haproxy.cfg
复制代码
2.以下是示例配置,供您参考(请留意 server 字段。请记着 6443 是 apiserver 端口):
  1. global
  2. log /dev/log local0 warning
  3. chroot /var/lib/haproxy
  4. pidfile /var/run/haproxy.pid
  5. maxconn 4000
  6. user haproxy
  7. group haproxy
  8. daemon
  9. stats socket /var/lib/haproxy/stats
  10. defaults
  11. log global
  12. option httplog
  13. option dontlognull
  14. timeout connect 5000
  15. timeout client 50000
  16. timeout server 50000
  17. frontend kube-apiserver
  18. bind *:6443
  19. mode tcp
  20. option tcplog
  21. default_backend kube-apiserver
  22. backend kube-apiserver
  23. mode tcp
  24. option tcplog
  25. option tcp-check
  26. balance roundrobin
  27. default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
  28. server kube-apiserver-1 172.16.0.4:6443 check # Replace the IP address with your own.
  29. server kube-apiserver-2 172.16.0.5:6443 check # Replace the IP address with your own.
  30. server kube-apiserver-3 172.16.0.6:6443 check # Replace the IP address with your own.
复制代码
3.保存文件并运行以下下令以重启 HAproxy。
  1. systemctl restart haproxy
复制代码
4.使 HAproxy 在开机后主动运行:
  1. systemctl enable haproxy
复制代码
5.确保您在另一台机器 (lb2) 上也配置了 HAproxy。
Keepalived

两台机器上必须都安装 Keepalived,但在配置上略有不同。
1.运行以下下令以配置 Keepalived。
  1. vi /etc/keepalived/keepalived.conf
复制代码
2.以下是示例配置 (lb1),供您参考:
  1. global_defs {
  2. notification_email {
  3. }
  4. router_id LVS_DEVEL
  5. vrrp_skip_check_adv_addr
  6. vrrp_garp_interval 0
  7. vrrp_gna_interval 0
  8. }
  9. vrrp_script chk_haproxy {
  10. script "killall -0 haproxy"
  11. interval 2
  12. weight 2
  13. }
  14. vrrp_instance haproxy-vip {
  15. state BACKUP
  16. priority 100
  17. interface eth0 # Network card
  18. virtual_router_id 60
  19. advert_int 1
  20. authentication {
  21. auth_type PASS
  22. auth_pass 1111
  23. }
  24. unicast_src_ip 172.16.0.2 # The IP address of this machine
  25. unicast_peer {
  26. 172.16.0.3 # The IP address of peer machines
  27. }
  28. virtual_ipaddress {
  29. 172.16.0.10/24 # The VIP address
  30. }
  31. track_script {
  32. chk_haproxy
  33. }
  34. }
复制代码
备注

  • 对于 interface 字段,您必须提供自己的网卡信息。您可以在机器上运行 ifconfig 以获取该值。
  • 为 unicast_src_ip 提供的 IP 所在是您当前机器的 IP 所在。对于也安装了 HAproxy 和 Keepalived 举行负载均衡的其他机器,必须在字段 unicast_peer 中输入其 IP 所在。
3.保存文件并运行以下下令以重启 Keepalived。
  1. systemctl restart keepalived
复制代码
4.使 Keepalived 在开机后主动运行:
  1. systemctl enable keepalived
复制代码
5.确保您在另一台机器 (lb2) 上也配置了 Keepalived。
验证高可用

在开始创建 Kubernetes 集群之前,请确保已经测试了高可用。
1.在机器 lb1 上,运行以下下令:
  1. [[email protected] ~]# ip a s
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  9. link/ether 52:54:9e:27:38:c8 brd ff:ff:ff:ff:ff:ff
  10. inet 172.16.0.2/24 brd 172.16.0.255 scope global noprefixroute dynamic eth0
  11. valid_lft 73334sec preferred_lft 73334sec
  12. inet 172.16.0.10/24 scope global secondary eth0 # The VIP address
  13. valid_lft forever preferred_lft forever
  14. inet6 fe80::510e:f96:98b2:af40/64 scope link noprefixroute
  15. valid_lft forever preferred_lft forever
复制代码
2.如上图所示,假造 IP 所在已经成功添加。模仿此节点上的故障:
  1. systemctl stop haproxy
复制代码
3.再次检查浮动 IP 所在,您可以看到该所在在 lb1 上消失了。
  1. [[email protected] ~]# ip a s
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  9. link/ether 52:54:9e:27:38:c8 brd ff:ff:ff:ff:ff:ff
  10. inet 172.16.0.2/24 brd 172.16.0.255 scope global noprefixroute dynamic eth0
  11. valid_lft 72802sec preferred_lft 72802sec
  12. inet6 fe80::510e:f96:98b2:af40/64 scope link noprefixroute
  13. valid_lft forever preferred_lft forever
复制代码
4.理论上讲,若配置成功,该假造 IP 会漂移到另一台机器 (lb2) 上。在 lb2 上运行以下下令,这是预期的输出:
  1. [[email protected] ~]# ip a s
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  9. link/ether 52:54:9e:3f:51:ba brd ff:ff:ff:ff:ff:ff
  10. inet 172.16.0.3/24 brd 172.16.0.255 scope global noprefixroute dynamic eth0
  11. valid_lft 72690sec preferred_lft 72690sec
  12. inet 172.16.0.10/24 scope global secondary eth0 # The VIP address
  13. valid_lft forever preferred_lft forever
  14. inet6 fe80::f67c:bd4f:d6d5:1d9b/64 scope link noprefixroute
  15. valid_lft forever preferred_lft forever
复制代码
5.如上所示,高可用已经配置成功。
使用 KubeKey 创建 Kubernetes 集群

KubeKey 是一款用来创建 Kubernetes 集群的工具,高效而便捷。请按照以下步骤下载 KubeKey。
从 GitHub Release Page 下载 KubeKey 或者直接使用以下下令。
  1. curl -sfL https://get-kk.kubesphere.io | VERSION=v2.0.0 sh -
复制代码
首先运行以下下令,以确保您从精确的区域下载 KubeKey。
  1. export KKZONE=cn
复制代码
运行以下下令来下载 KubeKey:
  1. curl -sfL https://get-kk.kubesphere.io | VERSION=v2.0.0 sh -
复制代码
备注
下载 KubeKey 之后,如果您将其转移到访问 Googleapis 受限的新机器上,请务必再次运行 export KKZONE=cn,然后继续执行以下步骤。
备注
通过以上下令,可以下载 KubeKey 的最新版本 (v2.0.0)。您可以更改下令中的版本号来下载特定的版本。
使 kk 成为可执行文件:
  1. chmod +x kk
复制代码
使用默认配置创建一个示例配置文件。此处以 Kubernetes v1.21.5 作为示例。
  1. ./kk create config --with-kubesphere v3.2.1 --with-kubernetes v1.21.5
复制代码
备注

  • 安装 KubeSphere 3.2.1 的建议 Kubernetes 版本:v1.19.x、v1.20.x、v1.21.x 或 v1.22.x(实验性支持)。如果不指定 Kubernetes 版本,KubeKey 将默认安装 Kubernetes v1.21.5。有关受支持的 Kubernetes 版本的更多信息,请拜见支持矩阵。
  • 如果您没有在本步骤的下令中添加标记 --with-kubesphere,那么除非您使用配置文件中的 addons 字段举行安装,或者稍后使用 ./kk create cluster 时再添加该标记,否则 KubeSphere 将不会被摆设。
  • 如果您添加标记 --with-kubesphere 时未指定 KubeSphere 版本,则会安装最新版本的 KubeSphere。
摆设 KubeSphere 和 Kubernetes

运行上述下令后,将创建配置文件 config-sample.yaml。编辑文件以添加机器信息、配置负载均衡器等。
备注
如果自定义文件名,那么文件名可能会有所不同。
config-sample.yaml 示例
  1. ...
  2. spec:
  3. hosts:
  4. - {name: master1, address: 172.16.0.4, internalAddress: 172.16.0.4, user: root, password: Testing123}
  5. - {name: master2, address: 172.16.0.5, internalAddress: 172.16.0.5, user: root, password: Testing123}
  6. - {name: master3, address: 172.16.0.6, internalAddress: 172.16.0.6, user: root, password: Testing123}
  7. - {name: worker1, address: 172.16.0.7, internalAddress: 172.16.0.7, user: root, password: Testing123}
  8. - {name: worker2, address: 172.16.0.8, internalAddress: 172.16.0.8, user: root, password: Testing123}
  9. - {name: worker3, address: 172.16.0.9, internalAddress: 172.16.0.9, user: root, password: Testing123}
  10. roleGroups:
  11. etcd:
  12. - master1
  13. - master2
  14. - master3
  15. master:
  16. - master1
  17. - master2
  18. - master3
  19. worker:
  20. - worker1
  21. - worker2
  22. - worker3
  23. controlPlaneEndpoint:
  24. domain: lb.kubesphere.local
  25. address: 172.16.0.10 # The VIP address
  26. port: 6443
  27. ...
复制代码
备注

  • 请使用您自己的 VIP 所在来更换 controlPlaneEndpoint.address 的值。
  • 有关更多本配置文件中不同参数的信息,请拜见多节点安装。
开始安装

完成配置之后,可以执行以下下令开始安装:
  1. ./kk create cluster -f config-sample.yaml
复制代码
验证安装

1.运行以下下令以检查安装日记。
  1. kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f
复制代码
2.看到以下信息时,表明高可用集群已成功创建。
  1. #####################################################
  2. ### Welcome to KubeSphere! ###
  3. #####################################################
  4. Console: http://172.16.0.4:30880
  5. Account: admin
  6. Password: [email protected]
  7. NOTES:
  8. 1. After you log into the console, please check the
  9. monitoring status of service components in
  10. the "Cluster Management". If any service is not
  11. ready, please wait patiently until all components
  12. are up and running.
  13. 2. Please change the default password after login.
  14. #####################################################
  15. https://kubesphere.io 2020-xx-xx xx:xx:xx
  16. #####################################################
复制代码
 
点击关注,第一时间相识华为云奇怪技术~
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

水军大提督

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表