2.2 使用Kubeadm搭建Kubernetes集群
本节将演示如何使用Kubeadm初始化一个单主节点+工作节点的Kubernetes集群。
1. 集群信息
使用Kubeadm搭建的集群信息如表2-2所示。
表2-2 Kubeadm集群信息
2. 安装和启动Docker
分别在idc-kubeadmin-master和idc-kubeadmin-worker节点安装和启动Docker。
3. 安装和启动kubelet
按照以下步骤,分别在idc-kubeadmin-master和idc-kubeadmin-worker节点安装和启动kubelet。
关闭防火墙,命令如下。
$ systemctl stop firewalld $ systemctl disable firewalld
关闭Selinux,命令如下。
$ setenforce 0 $ sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
关闭swap,命令如下。
$ swapoff -a $ sed -i '/swap/d' /etc/fstab
修改/etc/sysctl.conf,配置以下参数,命令如下。
net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv6.conf.lo.disable_ipv6 = 1 net.ipv6.conf.all.forwarding = 1
执行如下命令使其生效。
$ sysctl -p
配置kubernetes yum源,命令如下。
$ cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF
卸载旧版本的kubelet,命令如下。
$ yum remove -y kubelet kubeadm kubectl
安装kubelet、kubeadm、kubectl,命令如下。
$ yum install -y kubelet-1.19.4 kubeadm-1.19.4 kubectl-1.19.4
启动kubelet服务,命令如下。
$ systemctl start kubelet $ systemctl enable kubelet
4. 初始化主节点
按照以下步骤,在节点idc-kubeadmin-master上完成主节点的初始化。主节点初始化需要的信息统一配置在kubeadm-config.yaml文件中,MASTER_IP根据实际情况进行配置,其他配置可以保持不变,配置步骤如下。
$ export MASTER_IP=192.168.0.45 $ export SERVICE_SUBNET=10.96.0.0/16 $ export POD_SUBNET=10.100.0.1/16 $ export KUBE_VERSION=v1.19.4 $ cat <<EOF > ./kubeadm-config.yaml apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration kubernetesVersion: ${KUBE_VERSION} controlPlaneEndpoint: "${MASTER_IP}:6443" networking: serviceSubnet: "${SERVICE_SUBNET}" podSubnet: "${POD_SUBNET}" dnsDomain: "cluster.local" EOF
执行kubeadm init命令,完成主节点的初始化。
$ kubeadm init --config=kubeadm-config.yaml --upload-certs .... Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.0.45:6443 --token msdbsj.tb8k2oz17ewuju62 \ --discovery-token-ca-cert-hash sha256:8f56a2c39335622f0eff2c0a7a70fe8ed44c1675dda64f50594a915b2d76ab55
主节点初始化完毕后,将/etc/kubernetes/admin.conf文件配置为kubeconfig,使用kubeconfig在集群中部署网络插件。本示例部署calico网络插件,插件版本为3.13.1。
$ mkdir -p ~/.kube $ cp -i /etc/kubernetes/admin.conf ~/.kube/config
安装Tigera Calico operator和相关的CRD自定义Kubernetes资源,命令如下。
$ kubectl apply -f https://docs.projectcalico.org/manifests/tigera-operator.yaml
编辑custom-resources.yaml文件,将spec.calicoNetwork.ipPools下默认的cidr参数配置为实际值,本示例为10.100.0.1/16。
$ cat <<EOF > custom-resources.yaml apiVersion: operator.tigera.io/v1 kind: Installation metadata: name: default spec: calicoNetwork: ipPools: - blockSize: 26 cidr: 10.100.0.1/16 encapsulation: VXLANCrossSubnet natOutgoing: Enabled nodeSelector: all() EOF
部署custom-resources.yaml并确认calico-system命名空间下所有Pod都运行正常。
$ kubectl apply -f custom-resources.yaml $ kubectl -n calico-system get pods NAME READY STATUS RESTARTS AGE calico-kube-controllers-546d44f5b7-pfqsn 1/1 Running 0 4m7s calico-node-fbc9g 1/1 Running 0 4m7s calico-typha-655f98f7b9-fwfkf 1/1 Running 0 4m7s
至此,我们已经完成了主节点的初始化,可以查看当前集群节点状态是否就绪。
$ kubectl get no NAME STATUS ROLES AGE VERSION idc-kubeadmin-master Ready master 17m v1.19.4
5. 添加工作节点
在使用kubeadm init完成集群初始化时,可以看到日志输出如下内容。
Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.0.45:6443 --token msdbsj.tb8k2oz17ewuju62 \ --discovery-token-ca-cert-hash sha256:8f56a2c39335622f0eff2c0a7a70fe8ed44c1675dda64f50594a915b2d76ab55
根据以上提示,在节点idc-kubeadmin-worker上执行kubeadm join命令可将工作节点添加进集群,命令的执行日志如下所示。
$ kubeadm join 192.168.0.45:6443 --token msdbsj.tb8k2oz17ewuju62 \ --discovery-token-ca-cert-hash sha256:8f56a2c39335622f0eff2c0a7a70fe8ed44c1675dda64f50594a915b2d76ab55 ... This node has joined the cluster: * Certificate signing request was sent to apiserver and a response was received. * The Kubelet was informed of the new secure connection details. Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
成功将工作节点加入集群后,可以在主节点上查看当前集群中所有节点的信息及状态。
$ kubectl get no NAME STATUS ROLES AGE VERSION idc-kubeadmin-master Ready master 48m v1.19.4 idc-kubeadmin-worker Ready <none> 63s v1.19.4