Centos7 安装部署 Kubernetes(k8s) 高可用集群

Centos7 安装部署 Kubernetes(k8s) 高可用集群
一.系统环境
本文主要基于Kubernetes1.23.1和Linux操作系统CentOS7.4。
| 服务器版本 | docker软件版本 | Kubernetes(k8s)集群版本 | CPU架构 |
|---|---|---|---|
| CentOS Linux release 7.4.1708 (Core) | Docker version 20.10.14 | v1.23.1 | x86_64 |
Kubernetes高可用集群总体架构图:

Kubernetes高可用集群架构图描述:
Kubernetes(k8s)配置文件是放在etcd集群里的,2个Kubernetes master节点都连接到etcd集群,就可以保证Kubernetes的master节点
数据同步,信息对等,我们刚开始是连接到Kubernetes的master1上的,master1出现故障之后,还需要手动切换连接到master2上,我
们可以使用haproxy做负载均衡器,我们连接到haproxy后,haproxy会把请求转发到后端realserver(master1和master2),master1故
障之后,haproxy检测到master1故障,会把请求转发给master2,如果害怕haproxy故障,可以使用Keepalive做haproxy的高可用,我
们连接Keepalive的VIP即可。
由于机器有限,我们本次Kubernetes高可用集群架构图如下:

Kubernetes高可用集群架构:k8sbalancemaster1作为master1节点,k8sbalancemaster2作为master2节点,k8sbalanceworker1作为worker节点,k8sbalanceetcd1作为etcd服务器,k8sbalanceetcd2作为etcd服务器,k8sbalancehaproxy1作为HAProxy服务器。
| 服务器 | 操作系统版本 | CPU架构 | 进程 | 功能描述 |
|---|---|---|---|---|
| k8sbalancehaproxy1/192.168.110.134 | CentOS Linux release 7.4.1708 (Core) | x86_64 | haproxy | HAProxy负载均衡器 |
| k8sbalanceetcd1/192.168.110.135 | CentOS Linux release 7.4.1708 (Core) | x86_64 | etcd | etcd服务器 |
| k8sbalanceetcd2/192.168.110.136 | CentOS Linux release 7.4.1708 (Core) | x86_64 | etcd | etcd服务器 |
| k8sbalancemaster1/192.168.110.137 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kube-apiserver,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico | k8s master1节点 |
| k8sbalancemaster2/192.168.110.138 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kube-apiserver,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico | k8s master2节点 |
| k8sbalanceworker1/192.168.110.139 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kubelet,kube-proxy,calico | k8s worker节点 |
二.前言
Kubernetes是一个开源的容器编排平台,用于自动化地部署、扩展和管理容器化应用程序。在生产环境中,为了确保集群的高可用性,我们需要使用多个Master节点来实现冗余和故障切换。
关于Kubernetes(k8s)单master集群的安装部署,可以查看博客《Centos7 安装部署Kubernetes(k8s)集群》https://www.cnblogs.com/renshengdezheli/p/16686769.html。
三.Kubernetes(k8s)高可用简介
Kubernetes高可用集群由多个Master节点组成,每个Master节点都能够处理用户请求并执行相关操作。当任一Master节点发生故障时,其他节点可以接管其职责,从而保证整个集群的稳定运行。
本文将使用以下主要步骤来实现Kubernetes高可用集群:
准备环境:安装所需的软件包和依赖项。
配置负载均衡器:设置负载均衡器以实现流量的分发和故障切换。
配置etcd集群:etcd集群存储k8s的配置文件和集群信息。
初始化Master节点:选择一台Master节点,并进行初始化设置。
添加额外的Master节点:将其他Master节点加入到集群中。
添加worker节点:将worker节点加入到集群中。
部署CNI网络插件calico:calico用于节点间的通信和配置网络策略。
测试Kubernetes(k8s)的master节点数据同步。
测试Kubernetes(k8s)集群的高可用。
四.配置机器基本环境
本次Kubernetes高可用集群共6台机器,我们先把6台机器都配置好基本环境,后面部署应用才会顺风顺水。
先配置各个节点的基本环境,6个节点都要同时设置,在此以k8sbalancehaproxy1节点作为示例。
首先设置主机名。
[root@localhost ~]# vim /etc/hostname[root@localhost ~]# cat /etc/hostnamek8sbalancehaproxy1
查看默认的IP地址。
[root@localhost ~]# ifconfigens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.110.134 netmask 255.255.255.0 broadcast 192.168.110.255 inet6 fe80::20c:29ff:fe09:7e88 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:09:7e:88 txqueuelen 1000 (Ethernet) RX packets 11476 bytes 4296221 (4.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 9366 bytes 6197543 (5.9 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 4020 bytes 239760 (234.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4020 bytes 239760 (234.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
配置节点IP地址(可选),网卡配置详解,请查看博客《centos 7网卡配置文件详解(ifcfg-ens33)》。
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32TYPE=EthernetBOOTPROTO=staticNAME=ens32DEVICE=ens32ONBOOT=yesDNS1=114.114.114.114IPADDR=192.168.110.134NETMASK=255.255.255.0GATEWAY=192.168.110.2ZONE=trusted
重启网络。
[root@localhost ~]# service network restartRestarting network (via systemctl): [ 确定 ][root@localhost ~]# systemctl restart NetworkManager
重启机器之后,主机名变为k8sbalancehaproxy1。
测试机器是否可以访问网络。
[root@k8sbalancehaproxy1 ~]# ping www.baidu.comPING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=128 time=34.1 ms64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=128 time=34.2 ms64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=128 time=41.9 ms ^C --- www.a.shifen.com ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 34.123/36.765/41.958/3.672 ms
配置IP和主机名映射,其他节点的/etc/hosts文件内容也要一样。
[root@k8sbalancehaproxy1 ~]# vim /etc/hosts[root@k8sbalancehaproxy1 ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.110.134 k8sbalancehaproxy1192.168.110.135 k8sbalanceetcd1192.168.110.136 k8sbalanceetcd2192.168.110.137 k8sbalancemaster1192.168.110.138 k8sbalancemaster2192.168.110.139 k8sbalanceworker1
可以ping通其他5个节点则成功。
[root@k8sbalancehaproxy1 ~]# ping k8sbalancehaproxy1PING k8sbalancehaproxy1 (192.168.110.134) 56(84) bytes of data.64 bytes from k8sbalancehaproxy1 (192.168.110.134): icmp_seq=1 ttl=64 time=0.041 ms64 bytes from k8sbalancehaproxy1 (192.168.110.134): icmp_seq=2 ttl=64 time=0.034 ms64 bytes from k8sbalancehaproxy1 (192.168.110.134): icmp_seq=3 ttl=64 time=0.023 ms ^C --- k8sbalancehaproxy1 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2000ms rtt min/avg/max/mdev = 0.023/0.032/0.041/0.009 ms[root@k8sbalancehaproxy1 ~]# ping k8sbalanceetcd1PING k8sbalanceetcd1 (192.168.110.135) 56(84) bytes of data.64 bytes from k8sbalanceetcd1 (192.168.110.135): icmp_seq=1 ttl=64 time=0.606 ms64 bytes from k8sbalanceetcd1 (192.168.110.135): icmp_seq=2 ttl=64 time=0.438 ms ^C --- k8sbalanceetcd1 ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 0.438/0.522/0.606/0.084 ms[root@k8sbalancehaproxy1 ~]# ping k8sbalanceetcd2[root@k8sbalancehaproxy1 ~]# ping k8sbalancemaster1[root@k8sbalancehaproxy1 ~]# ping k8sbalancemaster2[root@k8sbalancehaproxy1 ~]# ping k8sbalanceworker1
关闭屏保。
[root@k8sbalancehaproxy1 ~]# setterm -blank 0
下载新的yum源。
[root@k8sbalancehaproxy1 ~]# rm -rf /etc/yum.repos.d/* ;wget ftp://ftp.rhce.cc/k8s/* -P /etc/yum.repos.d/--2022-04-09 17:48:37-- ftp://ftp.rhce.cc/k8s/* => “/etc/yum.repos.d/.listing” 正在解析主机 ftp.rhce.cc (ftp.rhce.cc)... 101.37.152.41 正在连接 ftp.rhce.cc (ftp.rhce.cc)|101.37.152.41|:21... 已连接。 正在以 anonymous 登录 ... 登录成功!......100%[=======================================================================================================================================================================>] 276 --.-K/s 用时 0s 2022-04-09 17:48:40 (81.9 MB/s) - “/etc/yum.repos.d/k8s.repo” 已保存 [276]
新的repo文件如下。
[root@k8sbalancehaproxy1 ~]# ls /etc/yum.repos.d/CentOS-Base.repo docker-ce.repo epel.repo k8s.repo
关闭selinux,设置SELINUX=disabled。
[root@k8sbalancehaproxy1 ~]# vim /etc/selinux/config[root@k8sbalancehaproxy1 ~]# cat /etc/selinux/config# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.SELINUX=disabled# SELINUXTYPE= can take one of three two values:# targeted - Targeted processes are protected,# minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection.SELINUXTYPE=targeted [root@k8sbalancehaproxy1 ~]# getenforceDisabled
配置防火墙允许所有数据包通过。
[root@k8sbalancehaproxy1 ~]# firewall-cmd --set-default-zone=trustedWarning: ZONE_ALREADY_SET: trusted success[root@k8sbalancehaproxy1 ~]# firewall-cmd --get-default-zonetrusted
Linux swapoff命令用于关闭系统交换分区(swap area),如果不关闭swap,就会在kubeadm初始化Kubernetes的时候报错:“[ERROR Swap]: running with swap on is not supported. Please disable swap”。
[root@k8sbalancehaproxy1 ~]# swapoff -a ;sed -i '/swap/d' /etc/fstab[root@k8sbalancehaproxy1 ~]# cat /etc/fstab## /etc/fstab# Created by anaconda on Thu Oct 18 23:09:54 2018## Accessible filesystems, by reference, are maintained under '/dev/disk'# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=9875fa5e-2eea-4fcc-a83e-5528c7d0f6a5 / xfs defaults 0 0
五.部署haproxy负载均衡器
haproxy作为负载均衡器,现在部署haproxy。
安装haproxy。
[root@k8sbalancehaproxy1 ~]# yum -y install haproxy [root@k8sbalancehaproxy1 ~]# rpm -qa | grep haproxyhaproxy-1.5.18-9.el7_9.1.x86_64
修改haproxy配置文件/etc/haproxy/haproxy.cfg。
[root@k8sbalancehaproxy1 ~]# ls /etc/haproxy/haproxy.cfg[root@k8sbalancehaproxy1 ~]# vim /etc/haproxy/haproxy.cfg[root@k8sbalancehaproxy1 ~]# tail -10 /etc/haproxy/haproxy.cfg#k8s-masterlb这个名字可以随意起,*:6443表示监听6443端口,当有人通过haproxy访问6443端口#把请求转发给后端的real server(k8s的master节点) weight 1权重都是1listen k8s-masterlb *:6443 mode tcp #balance roundrobin表示轮询调度 balance roundrobin #s1 s2 为k8s的两个master节点IP,weight都为1表示权重一样 server s1 192.168.110.137:6443 weight 1 server s2 192.168.110.138:6443 weight 1
设置haproxy开机自启动,现在启动haproxy。
[root@k8sbalancehaproxy1 ~]# systemctl enable haproxy --now[root@k8sbalancehaproxy1 ~]# systemctl is-active haproxyactive[root@k8sbalancehaproxy1 ~]# systemctl status haproxy● haproxy.service - HAProxy Load Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled) Active: active (running) since 六 2022-04-09 11:33:21 CST; 6h ago Main PID: 958 (haproxy-systemd) CGroup: /system.slice/haproxy.service ├─958 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid ├─963 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds └─964 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
查看端口,已经开始监听6443端口了。
[root@k8sbalancehaproxy1 ~]# netstat -antup | grep 6443tcp 0 0 0.0.0.0:6443 0.0.0.0:* LISTEN 964/haproxy
六.部署etcd集群
此次etcd集群只有两个etcd节点,关于etcd单节点配置,etcd三节点配置,更详细的内容,请查看博客《Kubernetes后台数据库etcd:安装部署etcd集群,数据备份与恢复》。
安装etcd。
[root@k8sbalanceetcd1 ~]# yum -y install etcd [root@k8sbalanceetcd1 ~]# rpm -qa | grep etcdetcd-3.3.11-2.el7.centos.x86_64
修改etcd配置文件/etc/etcd/etcd.conf。
[root@k8sbalanceetcd1 ~]# ls /etc/etcd/etcd.conf[root@k8sbalanceetcd1 ~]# vim /etc/etcd/etcd.conf[root@k8sbalanceetcd1 ~]# cat /etc/etcd/etcd.conf#配置数据目录ETCD_DATA_DIR="/var/lib/etcd/cluster.etcd"ETCD_LISTEN_PEER_URLS="http://192.168.110.135:2380,http://localhost:2380"ETCD_LISTEN_CLIENT_URLS="http://192.168.110.135:2379,http://localhost:2379"ETCD_NAME="etcd135"ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.110.135:2380"ETCD_ADVERTISE_CLIENT_URLS="http://192.168.110.135:2379,http://localhost:2379"##目前是两个节点,所以这里是两个节点的etcdETCD_INITIAL_CLUSTER="etcd135=http://192.168.110.135:2380,etcd136=http://192.168.110.136:2380"##集群tokenETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"##新创建etcd集群的时候ETCD_INITIAL_CLUSTER_STATE="new",往已经存在的etcd集群添加etcd节点时:ETCD_INITIAL_CLUSTER_STATE="existing"ETCD_INITIAL_CLUSTER_STATE="new"
设置etcd开机自启动并现在就启动。
[root@k8sbalanceetcd1 ~]# systemctl enable etcd --now [root@k8sbalanceetcd1 ~]# systemctl status etcd● etcd.service - Etcd Server Loaded: loaded (/usr/lib/systemd/system/etcd.service; enabled; vendor preset: disabled) Active: active (running) since 六 2022-04-09 11:34:09 CST; 6h ago Main PID: 968 (etcd) CGroup: /system.slice/etcd.service └─968 /usr/bin/etcd --name=etcd135 --data-dir=/var/lib/etcd/cluster.etcd --listen-client-urls=http://192.168.110.135:2379,http://localhost:2379
另外一节点也安装etcd。
[root@k8sbalanceetcd2 ~]# yum -y install etcd
修改etcd配置文件。
[root@k8sbalanceetcd2 ~]# vim /etc/etcd/etcd.conf[root@k8sbalanceetcd2 ~]# ls /etc/etcd/etcd.conf[root@k8sbalanceetcd2 ~]# cat /etc/etcd/etcd.conf#配置数据目录ETCD_DATA_DIR="/var/lib/etcd/cluster.etcd"ETCD_LISTEN_PEER_URLS="http://192.168.110.136:2380,http://localhost:2380"ETCD_LISTEN_CLIENT_URLS="http://192.168.110.136:2379,http://localhost:2379"ETCD_NAME="etcd136"ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.110.136:2380"ETCD_ADVERTISE_CLIENT_URLS="http://192.168.110.136:2379,http://localhost:2379"##目前是两个节点,所以这里是两个节点的etcdETCD_INITIAL_CLUSTER="etcd135=http://192.168.110.135:2380,etcd136=http://192.168.110.136:2380"##集群tokenETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"##新创建etcd集群的时候ETCD_INITIAL_CLUSTER_STATE="new",往已经存在的etcd集群添加etcd节点时:ETCD_INITIAL_CLUSTER_STATE="existing"ETCD_INITIAL_CLUSTER_STATE="new"
设置etcd开机自启动,并现在就启动。
[root@k8sbalanceetcd2 ~]# systemctl enable etcd --now [root@k8sbalanceetcd2 ~]# systemctl status etcd
两个etcd节点都启动之后,查看etcd集群成员。
[root@k8sbalanceetcd1 ~]# etcdctl member list36adf18604130cea: name=etcd135 peerURLs=http://192.168.110.135:2380 clientURLs=http://192.168.110.135:2379,http://localhost:2379 isLeader=false 64d719893344455b: name=etcd136 peerURLs=http://192.168.110.136:2380 clientURLs=http://192.168.110.136:2379,http://localhost:2379 isLeader=true
查看集群健康状态。
[root@k8sbalanceetcd1 ~]# etcdctl cluster-healthmember 36adf18604130cea is healthy: got healthy result from http://192.168.110.135:2379 member 64d719893344455b is healthy: got healthy result from http://192.168.110.136:2379 cluster is healthy
查看/下的数据。
[root@k8sbalanceetcd1 ~]# etcdctl ls /
自此,做共享数据的etcd集群配置好了。
七.部署Kubernetes(k8s) master HA
本次Kubernetes(k8s)集群,有2个master节点,1个worker节点。
7.1 配置docker
k8s是容器编排工具,需要容器,所以三个节点同时安装docker。
[root@k8sbalancemaster1 ~]# yum -y install docker-ce
设置docker开机自启动并现在启动docker。
[root@k8sbalancemaster1 ~]# systemctl enable docker --now [root@k8sbalancemaster1 ~]# systemctl status docker
查看docker版本。
[root@k8sbalancemaster1 ~]# docker --versionDocker version 20.10.14, build a224086
配置docker镜像加速器。
[root@k8sbalancemaster1 ~]# cat /etc/docker/daemon.json{"registry-mirrors": ["https://frz7i079.mirror.aliyuncs.com"]}重启docker。
[root@k8sbalancemaster1 ~]# systemctl restart docker [root@k8sbalancemaster1 ~]# systemctl status docker
设置iptables不对bridge的数据进行处理,启用IP路由转发功能。
[root@k8sbalancemaster1 ~]# cat <<EOF> /etc/sysctl.d/k8s.conf > net.bridge.bridge-nf-call-ip6tables = 1 > net.bridge.bridge-nf-call-iptables = 1 > net.ipv4.ip_forward = 1 > EOF
使配置生效。
[root@k8sbalancemaster1 ~]# sysctl -p /etc/sysctl.d/k8s.confnet.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1net.ipv4.ip_forward = 1
7.2 安装kubelet,kubeadm,kubectl
查看可用的kubeadm版本。
[root@k8sbalancemaster1 ~]# yum list --showduplicates kubeadm --disableexcludes=kubernetes | grep 1.23kubeadm.x86_64 1.23.1-0 @kubernetes kubeadm.x86_64 1.23.0-0 kubernetes kubeadm.x86_64 1.23.1-0 kubernetes kubeadm.x86_64 1.23.2-0 kubernetes kubeadm.x86_64 1.23.3-0 kubernetes kubeadm.x86_64 1.23.4-0 kubernetes kubeadm.x86_64 1.23.5-0 kubernetes
此次k8s集群,我们安装k8s 1.23.1版本,三个节点都安装kubelet,kubeadm,kubectl,--disableexcludes=kubernetes 禁掉除了这个之外的别的仓库。
[root@k8sbalancemaster1 ~]# yum -y install kubelet-1.23.1-0 kubeadm-1.23.1-0 kubectl-1.23.1-0 --disableexcludes=kubernetes
设置kubelet开机自启动并现在就启动。
[root@k8sbalancemaster1 ~]# systemctl enable kubelet --now
注意:kubelet现在是启动不了的。
[root@k8sbalancemaster1 ~]# systemctl status kubelet
docker的cgroup driver需要修改为systemd,默认docker的cgroup driver是cgroupfs,k8s 1.23.1版本,如果不加"exec-opts": ["native.cgroupdriver=systemd"]参数,进行kubeadm init初始化的时候会报错如下:failed to run Kubelet: misconfiguration: kubelet cgroup driver: "systemd" is differ 根据报错信息知道,是因为 k8s 和docker 的 cgroup driver 不一致导致的,k8s 的是 systemd ,而 docker 是cgroupfs。
[root@k8sbalancemaster1 ~]# cat /etc/docker/daemon.json {"registry-mirrors": ["https://frz7i079.mirror.aliyuncs.com"],"exec-opts": ["native.cgroupdriver=systemd"]}修改了/etc/docker/daemon.json之后,要重启docker进行生效。
[root@k8sbalancemaster1 ~]# systemctl restart docker
可以查看docker的 Cgroup Driver。
[root@k8sbalancemaster1 ~]# docker info | grep Cgroup Cgroup Driver: systemd Cgroup Version: 1
7.3 kubeadm初始化
在k8sbalancemaster1节点进行kubeadm init初始化,kubeadm初始化命令:kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.23.1 --pod-network-cidr=10.244.0.0/16,本次我们使用的是外部的etcd集群,而不是以容器
Centos7 安装部署 Kubernetes(k8s) 高可用集群
声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。如若本站内容侵犯了原著者的合法权益,可联系本站删除。



