从零开始的 Kubernetes 学习笔记(四)
后知后觉 暂无评论

使用 Kubernetes 对容器进行编排成为微服务时代的技术风向标。

更新记录

2022-08

2020-08


创建 Kubernetes 集群

关于 IPVS 模式,具体可以参考官方文档

简单理解就是网络性能比 iptables 更高,更适合大型集群。性能更高的同时,还支持多种负载均衡算法:

检查

部署集群前先检查当前 IPVS 规则是否为空

sudo ipvsadm -ln

生成初始化配置文件

制作 kubeadm 配置

kubeadm config print init-defaults --component-configs \
KubeProxyConfiguration | tee kubeadm-config.yaml

生成集群 Token(复制备用)

kubeadm token generate

然后简单说一下配置文件修改哪些部分,无须修改部分以 (---) 省略。

---
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef  ## 这里改为刚才生成的复制备用 Token
---
localAPIEndpoint:
  advertiseAddress: 1.2.3.4       ## 这里改为主节点的地址,如果配置高可用集群,这里改为VIP或者负载均衡IP
  bindPort: 6443
nodeRegistration:
  criSocket: unix:///var/run/containerd/containerd.sock
  imagePullPolicy: IfNotPresent
  name: node                      ## 这里改为主节点的 hostname
  taints: null
---
kubernetesVersion: 1.27.0         ## 这里默认只会生成主版本的第一个版本,比如 1.22.0,小版本需要手动指定
---
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
  podSubnet: 10.244.0.0/16        ## 模板里没有规定 Pod 子网,添加此行
---
ipvs:
  excludeCIDRs: null
  minSyncPeriod: 0s
  scheduler: ""
  strictARP: false                ## 严格 ARP 模式,默认 false,如果使用 MetalLB 等负载均衡器的话改为 true
  syncPeriod: 0s
  tcpFinTimeout: 0s
  tcpTimeout: 0s
  udpTimeout: 0s
kind: KubeProxyConfiguration
metricsBindAddress: ""
mode: ""                          ## 网络模式,默认为空,修改网络模式为 "ipvs"

配置文件修改后,运行测试命令验证其是否存在语法错误

sudo kubeadm init --config kubeadm-config.yaml --dry-run

无报错即可

初始化集群

配置验证无误后即可正式初始化集群

sudo kubeadm init --config kubeadm-config.yaml

详细过程可参考

# kubeadm init
[init] Using Kubernetes version: v1.18.8
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [debian kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.16.16.221]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [debian localhost] and IPs [172.16.16.221 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [debian localhost] and IPs [172.16.16.221 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
W0818 22:49:49.636154    2987 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[control-plane] Creating static Pod manifest for "kube-scheduler"
W0818 22:49:49.639931    2987 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 27.511628 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.18" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node debian as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node debian as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: zb5t92.ggq885b0bw1ni7m8
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

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/

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

kubeadm join 172.16.16.221:6443 --token zb5t92.ggq885b0bw1ni7m8 \
    --discovery-token-ca-cert-hash sha256:0e49919c783ef0b917819e04ee0777fdc7b35ba8c1de6535315f8c63281155f2

提示信息的最后需要重点关注:

## 这部分是需要在主节点上执行的命令,为了后续可以使用 kubectl
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
## 这部分是其他节点加入集群的命令,复制备用,需要先为集群配置网络插件再添加节点
kubeadm join 172.16.16.221:6443 --token zb5t92.ggq885b0bw1ni7m8 \
    --discovery-token-ca-cert-hash sha256:0e49919c783ef0b917819e04ee0777fdc7b35ba8c1de6535315f8c63281155f2

应用网络模型

Kubernetes 本身没有网络实现,集群的网络需要使用网络插件才能实现互联互通,Kubernetes 支持多种网络模型,常用的有 flannel, Calicoweavecilium

它们之间的区别和性能对比可以查看文章 The Ultimate Guide To Using Calico, Flannel, Weave and Cilium

flannel

部署很简单,一行命令即可

kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml

Calico

参考文档,先安装 operator。

kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/tigera-operator.yaml

然后下载配置文件

curl https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/custom-resources.yaml -O

修改 #13 的 spec.calicoNetwork.ipPools.cidr 为集群 Pod 子网网段,本文的缺省配置 10.244.0.0/16,然后应用配置。

kubectl create -f custom-resources.yaml

稍等片刻,等待镜像拉取并初始化集群网络,然后使用 calicoctl 对集群进行检测(kubectl 插件模式)

curl -L https://github.com/projectcalico/calico/releases/download/v3.26.1/calicoctl-linux-amd64 -o kubectl-calico
sudo install -m 755 kubectl-calico /usr/local/bin/kubectl-calico

然后执行检测

kubectl calico get nodes

检测集群网络状态

DATASTORE_TYPE=kubernetes KUBECONFIG=~/.kube/config sudo kubectl calico node status

返回信息如下:

Calico process is running.

IPv4 BGP status
+---------------+-------------------+-------+----------+-------------+
| PEER ADDRESS  |     PEER TYPE     | STATE |  SINCE   |    INFO     |
+---------------+-------------------+-------+----------+-------------+
| 172.16.16.222 | node-to-node mesh | up    | 02:47:38 | Established |
| 172.16.16.223 | node-to-node mesh | up    | 02:47:52 | Established |
| 172.16.16.224 | node-to-node mesh | up    | 02:47:53 | Established |
+---------------+-------------------+-------+----------+-------------+

IPv6 BGP status
No IPv6 peers found.

添加工作节点

在其他的节点上执行备份的加入集群命令

sudo kubeadm join 172.16.16.221:6443 --token zb5t92.ggq885b0bw1ni7m8 \
    --discovery-token-ca-cert-hash sha256:0e49919c783ef0b917819e04ee0777fdc7b35ba8c1de6535315f8c63281155f2

然后获取集群节点信息

$ kubectl get node
NAME   STATUS   ROLES    AGE    VERSION
k8s1   Ready    master   63m    v1.18.8
k8s2   Ready    <none>   62m    v1.18.8
k8s3   Ready    <none>   61m    v1.18.8

修改节点身份

将 s2、 s3 节点身份改为 Node 工作节点

$ kubectl label nodes k8s2 node-role.kubernetes.io/node=worker
node/k8s2 labeled
$ kubectl get nodes
NAME   STATUS     ROLES    AGE   VERSION
k8s1   NotReady   master   64m   v1.18.8
k8s2   NotReady   node     65m   v1.18.8
k8s3   NotReady   <none>   66m   v1.18.8

应用控制面板(可选)

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.6.0/aio/deploy/recommended.yaml

重置集群节点

重装集群或者卸载需要将集群重置后拆分再进行。

注意:master 节点初始化前需要移除全部 node 才能正常初始化。

常见故障

故障排除

查看 kubelet 日志对故障排除很有帮助

sudo journalctl -f -u kubelet

localhost:8080 was refused

如果看到以下报错

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

是因为没有按照 kubeadm init 的指导步骤操作导致的,需要操作:

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

node flannel not have CIDR IPs

如果 flannel 虚拟网卡没有地址,类似于下部信息:

# ip a
4: flannel.1: <BROADCAST,MULTICAST> mtu 1450 qdisc noop state DOWN group default 
    link/ether 5e:88:cf:b0:81:ea brd ff:ff:ff:ff:ff:ff

可能是因为初始化集群时没有指定 CIDR 导致的,重新初始化集群并添加参数 --pod-network-cidr=10.244.0.0/16

且 Node 节点日志会出现大量类似日志

Aug 19 03:53:17 k8s2 kubelet[32203]: E0819 03:53:17.698669   32203 pod_workers.go:191] Error syncing pod 310713fc-e1b5-451a-9816-bfba8a40c985 ("kube-flannel-ds-amd64-2bvqs_kube-system(310713fc-e1b5-451a-9816-bfba8a40c985)"), skipping: failed to "StartContainer" for "kube-flannel" with CrashLoopBackOff: "back-off 5m0s restarting failed container=kube-flannel pod=kube-flannel-ds-amd64-2bvqs_kube-system(310713fc-e1b5-451a-9816-bfba8a40c985)"
Aug 19 03:36:21 k8s2 kubelet[32203]: E0819 03:36:21.438073   32203 kubelet.go:2188] Container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized

All node status are NotReady

若发现集群全部处于 NotReady 状态,如下:

root@k8s1:~# kubectl get nodes
NAME   STATUS     ROLES    AGE   VERSION
k8s1   NotReady   master   65m   v1.18.8
k8s2   NotReady   <none>   61m   v1.18.8
k8s3   NotReady   <none>   60m   v1.18.8

可能是因为没有先部署网络模型就添加了节点,需要删除节点后,重新部署网络模型后重新添加节点。

already has an IP address different

如果已经部署了网络模型却依然全部节点提示 NotReady 则需要查日志,若出现类似如下报错:

  Warning  FailedCreatePodSandBox  15m                    kubelet, k8s1      Failed to create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "d8113436aa24e2c32d07cb15b7997d66709456892cbe62625710a655f91814be" network for pod "coredns-66bff467f8-fvczt": networkPlugin cni failed to set up pod "coredns-66bff467f8-fvczt_kube-system" network: failed to set bridge addr: "cni0" already has an IP address different from 10.244.0.1/24

查看当前的 cni 虚拟网卡

1731: cni0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default qlen 1000
    link/ether 7a:71:cf:43:aa:17 brd ff:ff:ff:ff:ff:ff
    inet 10.10.0.1/24 brd 10.244.0.255 scope global cni0
       valid_lft forever preferred_lft forever
    inet6 fe80::7871:cfff:fe43:aa17/64 scope link 
       valid_lft forever preferred_lft forever

可以发现这是因为初始化集群时的参数与当前虚拟网卡的 CIDR 不符导致的,注意初始化集群时需要删除旧的 CIDR

ip link delete cni0

然后重新启用节点即可。

Trace[456471162]: [30.001441123s] [30.001441123s] END
E0820 05:33:15.672472       1 reflector.go:153] pkg/mod/k8s.io/client-go@v0.17.2/tools/cache/reflector.go:105: Failed to list *v1.Service: Get https://10.96.0.1:443/api/v1/services?limit=500&resourceVersion=0: dial tcp 10.96.0.1:443: i/o timeout
[INFO] plugin/ready: Still waiting on: "kubernetes"
[INFO] plugin/ready: Still waiting on: "kubernetes"

附录

相关链接

参考链接

本文撰写于一年前,如出现图片失效或有任何问题,请在下方留言。博主看到后将及时修正,谢谢!
禁用 / 当前已拒绝评论,仅可查看「历史评论」。