搭建 Kubernetes 高可用集群

使用 3 台阿里云服务器(k8s-master0, k8s-master1, k8s-master2)作为 master 节点搭建高可用集群,负载均衡用的是阿里云 SLB ,需要注意的是由于阿里云负载均衡不支持后端服务器自己转发给自己,所以 master 节点的 control-plane-endpoint 不能走负载均衡。

先在 k8s-master0 上安装好 k8s ,安装步骤见 Ubuntu 安装 k8s 三驾马车 kubelet kubeadm kubectl ,然后打快照创建阿里云 ecs 镜像。

确定 control-plane-endpoint 主机名,这里假设是 k8s-api ,在 k8s-master0 的 hosts 中添加 k8s-api 的解析。

10.0.1.81       k8s-api

在 k8s-master0 上创建集群,

kubeadm init \
    --control-plane-endpoint "k8s-api:6443" --upload-certs \
    --image-repository registry.aliyuncs.com/google_containers \
    --pod-network-cidr=192.168.0.0/16 \
    --v=6

创建成功后会出现下面的提示

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of the control-plane node running the following command on each as root:

  kubeadm join k8s-api:6443 --token ****** \
    --discovery-token-ca-cert-hash ****** \
    --control-plane --certificate-key ******

Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join k8s-api:6443 --token ****** \
    --discovery-token-ca-cert-hash ******

用之前创建的阿里云 ecs 镜像创建2台服务器 k8s-master1 与 k8s-master2 作为另外的 master 节点,并在这2台服务器的 hosts 中将 k8s-api 解析到 k8s-master0 的 IP 地址。

然后分别登录 k8s-master1 与 k8s-master2 用前面得到的加入 control-plane node 的 kubeadm join 命令将这 2 台服务器作为 master 加入集群。

接着登录这 3 台 master 节点分别运行下面的命令:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

如果不执行上面的命令,运行 kubectl get nodes 会出现下面的错误提示:

The connection to the server localhost:8080 was refused - did you specify the right host or port?

再接着登录 k8s-master1 与 k8s-master2 在 hosts 中将 k8s-api 解析到本机的 IP 地址。

现在这 3 台 master 节点都加入了集群,但通过 kubectl get pods 查看会发现 3 个节点都处于 NotReady 状态,这是由于没有部署 CNI 网络插件。

接下来的一步就是部署网络插件,比如这里我们用 calico 网络,

kubectl apply -f calico.yaml

当 calico 网络部署成功后,3 台 master 就都进入 Running 状态,master 节点的部署就完成了。

接下来就是部署 worker 节点。

Worker 节点都通过阿里云负载均衡访问 master 节点上的 api-server ,所以先要创建阿里云私网负载均衡,添加针对 6443 端口 的 tcp 四层转发,并挂载 3 台 master 节点服务器。

接下来就是继续用之前创建的阿里云 ecs 镜像创建 worker node 服务器,并在每台服务器的 hosts 中将 k8s-api 解析到阿里负载均衡的 IP 地址,然后用之前生成的加入 worker 节点的 kubeadm join k8s-api:6443 命令将这些服务器加入集群。

这样高可用 k8s 集群就搭建好了,可以部署应用了。需要注意的是现在用了 3 台 master ,根据 Raft 一致性算法,只有当其中至少 2 台 master 正常时,集群才能正常工作。

猜你喜欢

转载自www.cnblogs.com/dudu/p/12168433.html