原始高可用和负载均衡是 keepalived+heaproxy 或者 keepalived+nginx
kube-vip
这个是简化原始的方案,kube-vip 包含了负载均衡和高可用,降低了运维成本。
keepalived 架构:
kube-vip 架构:
前期准备工作跟 kubeadm 部署 k8s 高可用集群步骤一样,直接略过,这里只说 kube-vip 的安装过程。kubeadm 安装 k8s 可以看https://www.cnblogs.com/lfl17718347843/p/13417304.html
1、在第一个控制节点执行以下命令
[root@master1 ~]# docker run --network host --rm plndr/kube-vip:0.2.1 manifest pod --interface ens33 --vip 192.168.200.16 --arp --leaderElection --startAsLeader | sudo tee /etc/kubernetes/manifests/vip.yaml
备注:把 ens33 换成自己的服务器网卡名,vip 根据自己环境修改
2、继续在刚才的节点执行以下命令
[root@master1 ~]# cat kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.168.200.3 # 本机IP
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: master1 # 本主机名
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: "192.168.200.16:6443" # kube-vip 的虚拟ip
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers # 镜像仓库源要根据自己实际情况修改
kind: ClusterConfiguration
kubernetesVersion: v1.18.2 # k8s版本
networking:
dnsDomain: cluster.local
podSubnet: "10.244.0.0/16"
serviceSubnet: 10.96.0.0/12
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
featureGates:
SupportIPVSProxyMode: true
mode: ipvs
3、查看第一个控制节点 kube-vip 状态
[root@master1 ~]# kubectl get all -n kube-system
NAME READY STATUS RESTARTS AGE
pod/coredns-7ff77c879f-4tlg6 1/1 Running 0 19m
pod/coredns-7ff77c879f-tvhwv 1/1 Running 0 19m
pod/etcd-master1 1/1 Running 0 19m
pod/etcd-master2 1/1 Running 0 7m15s
pod/etcd-master3 1/1 Running 0 4m48s
pod/kube-apiserver-master1 1/1 Running 0 19m
pod/kube-apiserver-master2 1/1 Running 0 7m15s
pod/kube-apiserver-master3 1/1 Running 1 4m54s
pod/kube-controller-manager-master1 1/1 Running 1 19m
pod/kube-controller-manager-master2 1/1 Running 0 7m16s
pod/kube-controller-manager-master3 1/1 Running 0 4m55s
pod/kube-flannel-ds-c9wlq 1/1 Running 0 5m6s
pod/kube-flannel-ds-njlnp 1/1 Running 0 5m6s
pod/kube-flannel-ds-sgcv8 1/1 Running 0 5m6s
pod/kube-flannel-ds-wnn6l 1/1 Running 0 5m6s
pod/kube-proxy-2kcbn 1/1 Running 0 19m
pod/kube-proxy-gm824 1/1 Running 0 6m6s
pod/kube-proxy-p22x7 1/1 Running 0 7m17s
pod/kube-proxy-qxrvg 1/1 Running 0 5m49s
pod/kube-scheduler-master1 1/1 Running 1 19m
pod/kube-scheduler-master2 1/1 Running 0 7m16s
pod/kube-scheduler-master3 1/1 Running 0 5m9s
pod/kube-vip-master1 1/1 Running 1 19m # 1
4、在其它几个控制节点执行第一步的命令
[root@master1 ~]# docker run --network host --rm plndr/kube-vip:0.2.1 manifest pod --interface ens33 --vip 192.168.200.16 --arp --leaderElection --startAsLeader | sudo tee /etc/kubernetes/manifests/vip.yaml
5、在查看 kube-vip 节点状态
[root@master1 ~]# kubectl get po --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-7ff77c879f-4tlg6 1/1 Running 0 33m
kube-system coredns-7ff77c879f-tvhwv 1/1 Running 0 33m
kube-system etcd-master1 1/1 Running 0 33m
kube-system etcd-master2 1/1 Running 0 21m
kube-system etcd-master3 1/1 Running 0 18m
kube-system kube-apiserver-master1 1/1 Running 0 33m
kube-system kube-apiserver-master2 1/1 Running 0 21m
kube-system kube-apiserver-master3 1/1 Running 1 19m
kube-system kube-controller-manager-master1 1/1 Running 1 33m
kube-system kube-controller-manager-master2 1/1 Running 0 21m
kube-system kube-controller-manager-master3 1/1 Running 0 19m
kube-system kube-flannel-ds-c9wlq 1/1 Running 0 19m
kube-system kube-flannel-ds-njlnp 1/1 Running 0 19m
kube-system kube-flannel-ds-sgcv8 1/1 Running 0 19m
kube-system kube-flannel-ds-wnn6l 1/1 Running 0 19m
kube-system kube-proxy-2kcbn 1/1 Running 0 33m
kube-system kube-proxy-gm824 1/1 Running 0 20m
kube-system kube-proxy-p22x7 1/1 Running 0 21m
kube-system kube-proxy-qxrvg 1/1 Running 0 19m
kube-system kube-scheduler-master1 1/1 Running 1 33m
kube-system kube-scheduler-master2 1/1 Running 0 21m
kube-system kube-scheduler-master3 1/1 Running 0 19m
kube-system kube-vip-master1 1/1 Running 1 33m # 1
kube-system kube-vip-master2 1/1 Running 0 93s # 2
kube-system kube-vip-master3 1/1 Running 0 36s # 3
6、随意查看 kube-vip 节点日志
[root@master2 ~]# kubectl logs -f kube-vip-master1 -n kube-system
time="2022-04-17T07:54:25Z" level=info msg="Beginning cluster membership, namespace [kube-system], lock name [plunder-lock], id [master1]"
I0417 07:54:25.399808 1 leaderelection.go:242] attempting to acquire leader lease kube-system/plunder-lock...
I0417 07:54:25.453744 1 leaderelection.go:252] successfully acquired lease kube-system/plunder-lock
time="2022-04-17T07:54:25Z" level=info msg="This node is starting with leadership of the cluster"
time="2022-04-17T07:54:25Z" level=info msg="Node [master1] is assuming leadership of the cluster" # master1 是 leader
time="2022-04-17T07:54:25Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:28Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:31Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:34Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:37Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:40Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:43Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:46Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:49Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:52Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:55Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:54:58Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
time="2022-04-17T07:55:01Z" level=info msg="Broadcasting ARP update for 192.168.200.16 (00:0c:29:26:6c:ad) via ens33"
说明:只是替换 keepalived+haproxy,其它部署步骤都一样。