从零开始的 Kubernetes 学习笔记(四)

后知后觉 暂无评论

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

更新记录

2022-08

2020-08

配置内核模块

初始化集群需要使用内核模块 br_netfilter ,检查当前是否存在模块

lsmod | grep br_netfilter

若命令输出为空,则需要手动加载模块

sudo modprobe br_netfilter

配置下模块自动加载

echo "br_netfilter" | sudo tee /etc/modules-load.d/br_netfilter.conf

配置下系统参数

cat <<EOF | sudo tee /etc/sysctl.d/88-kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.ipv4.tcp_fastopen = 3
EOF
sudo sysctl --system

初始化 Kubernetes 集群

初始化集群

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

详细过程可参考

# kubeadm init
W0818 22:49:38.623515    2987 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[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.5]
[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.5 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.5 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.5:6443 --token zb5t92.ggq885b0bw1ni7m8 \
    --discovery-token-ca-cert-hash sha256:0e49919c783ef0b917819e04ee0777fdc7b35ba8c1de6535315f8c63281155f2

应用网络模型

kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
注意需要先应用网络模型再将 Node 添加到集群,否则会出现网络错误。

应用控制面板

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

添加工作节点

在其他的节点上执行 kubeadm init 后的提示命令

kubeadm join 172.16.16.5: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=node
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

初始化节点

查看问题

查看 kubelet 日志,一般用于网络故障排除

# 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"

附录

相关链接

参考链接

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