六、 部署 k8s Cluster(下)
上节我们[通过 kubeadm 在 k8s-master 上部署了 Kubernetes,本节安装 Pod 网络并添加 k8s-node1 和 k8s-node2,完成集群部署。
(一)安装 Pod 网络
要让 Kubernetes Cluster 能够工作,必须安装 Pod 网络,否则 Pod 之间无法通信。Kubernetes 支持多种网络方案,这里我们先使用 flannel,后面还会讨论 Canal。
执行如下命令部署 flannel:
root@cuiyongchao:~# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
root@cuiyongchao:~#
(二)添加 k8s-node1 和 k8s-node2
在 k8s-node1 和 k8s-node2 上分别执行如下命令,将其注册到 Cluster 中:
关闭swap:
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
kubeadm join --token wjxawc.qsjz0lp4m9ihdcy0 10.0.0.41:6443 --discovery-token-unsafe-skip-ca-verification
这里的 --token
来自前面 kubeadm init
输出的第 ⑨ 步提示,如果当时没有记录下来可以通过 kubeadm token list
查看。
root@k8s-master:~# kubeadm token list
TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
0872ga.bg0rbp0jvp2omjem 23h 2020-11-02T11:22:37Z authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
kubeadm join
执行如下:
root@k8s-node-02:~# kubeadm join --token 0872ga.bg0rbp0jvp2omjem 10.0.0.41:6443 --discovery-token-unsafe-skip-ca-verification
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
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.
root@k8s-node-02:~#
根据提示,我们可以通过 kubectl get nodes
查看节点的状态。
root@k8s-master:~# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 17m v1.19.3
k8s-node-01 Ready <none> 76s v1.19.3
k8s-node-02 Ready <none> 94s v1.19.3
目前所有节点都是 NotReady
,这是因为每个节点都需要启动若干组件,这些组件都是在 Pod 中运行,需要首先从 google 下载镜像,我们可以通过如下命令查看 Pod 的状态:
root@k8s-master:~# kubectl get pod --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-6d56c8448f-9grxk 1/1 Running 0 17m
kube-system coredns-6d56c8448f-mrx55 1/1 Running 0 17m
kube-system etcd-k8s-master 1/1 Running 0 17m
kube-system kube-apiserver-k8s-master 1/1 Running 0 17m
kube-system kube-controller-manager-k8s-master 1/1 Running 0 17m
kube-system kube-flannel-ds-cqwqv 1/1 Running 0 12m
kube-system kube-flannel-ds-nh2qg 1/1 Running 0 104s
kube-system kube-flannel-ds-wbrs6 1/1 Running 0 86s
kube-system kube-proxy-45prh 1/1 Running 0 17m
kube-system kube-proxy-g2c4x 1/1 Running 0 104s
kube-system kube-proxy-jwvg5 1/1 Running 0 86s
kube-system kube-scheduler-k8s-master 1/1 Running 0 17m
root@k8s-master:~#
Pending
、ContainerCreating
、ImagePullBackOff
都表明 Pod 没有就绪,Running
才是就绪状态。我们可以通过 kubectl describe pod <Pod Name>
查看 Pod 具体情况,比如:
为了节省篇幅,这里只截取命令输出的最后部分,可以看到在下载 image 时失败,如果网络质量不好,这种情况是很常见的。我们可以耐心等待,因为 Kubernetes 会重试,我们也可以自己手工执行 docker pull
去下载这个镜像。
等待一段时间,image 都成功下载后,所有 Pod 会处于 Running
状态。
这时,所有的节点都已经 Ready
,Kubernetes Cluster 创建成功,一切准备就绪。