Kubernetes API 服务器无法注册主节点

Kubernetes API 服务器无法注册主节点

我尝试使用 kubeadm 创建 Kubernetes 集群。我启动了 Ubuntu 18.04 服务器,安装了 docker(确保 docker.service 正在运行),安装了 kubeadm kubelet 和 kubectl。

以下是我执行的步骤:

sudo apt-get update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu `lsb_release -cs` test"
sudo apt update
sudo apt install docker-ce
sudo systemctl enable docker
sudo systemctl start docker

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt-get install kubeadm kubelet kubectl -y
sudo apt-mark hold kubeadm kubelet kubectl 
kubeadm version
swapoff –a

另外,为了配置 Docker cgroup 驱动程序,我编辑了/etc/systemd/system/kubelet.service.d/10-kubeadm.conf。在文件中,我添加Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=systemd"并注释掉了Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"

/etc/systemd/system/kubelet.service.d/10-kubeadm.conf以供参考:

# Note: This dropin only works with kubeadm and kubelet v1.11+
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
#Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=systemd"
# This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
EnvironmentFile=-/etc/default/kubelet
ExecStart=
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS

此后我运行:systemctl daemon-reload并且systemctl restart kubelet。kubelet.service 运行良好。

接下来我运行sudo kubeadm init --pod-network-cidr=10.244.0.0/16,结果出现了以下错误:

root@ip-172-31-1-238:/home/ubuntu# kubeadm init --pod-network-cidr=10.244.0.0/16
[init] 使用 Kubernetes 版本:v1.23.1
[preflight] 运行飞行前检查
[preflight] 提取设置 Kubernetes 集群所需的镜像
[preflight] 这可能需要一两分钟,具体取决于您的互联网连接速度
[preflight] 您也可以预先使用“kubeadm config images pull”执行此操作
[certs] 使用 certificateDir 文件夹“/etc/kubernetes/pki”
[certs] 生成“ca”证书和密钥
[certs] 生成“apiserver”证书和密钥
[certs] apiserver 服务证书已为 DNS 名称签名 [ip-172-31-1-238 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] 和 IP [10.96.0.1 172.31.1.238]
[certs] 生成“apiserver-kubelet-client”证书和密钥
[certs] 生成“front-proxy-ca”证书和密钥
[certs] 生成“front-proxy-client”证书和密钥
[certs] 生成“etcd/ca”证书和密钥
[certs] 生成“etcd/server”证书和密钥
[certs] etcd/server 服务证书已为 DNS 名称 [ip-172-31-1-238 localhost] 和 IP [172.31.1.238 127.0.0.1 ::1] 签名
[certs] 生成“etcd/peer”证书和密钥
[certs] etcd/peer 服务证书已为 DNS 名称 [ip-172-31-1-238 localhost] 和 IP 签名[172.31.1.238 127.0.0.1 ::1]
[certs] 生成“etcd/healthcheck-client”证书和密钥
[certs] 生成“apiserver-etcd-client”证书和密钥
[certs] 生成“sa”密钥和公钥
[kubeconfig] 使用 kubeconfig 文件夹“/etc/kubernetes”
[kubeconfig] 写入“admin.conf”kubeconfig 文件
[kubeconfig] 写入“kubelet.conf”kubeconfig 文件
[kubeconfig] 写入“controller-manager.conf”kubeconfig 文件
[kubeconfig] 写入“scheduler.conf”kubeconfig 文件
[kubelet-start] 将带有标志的 kubelet 环境文件写入文件“/var/lib/kubelet/kubeadm-flags.env”
[kubelet-start] 将 kubelet 配置写入文件“/var/lib/kubelet/config.yaml”
[kubelet-start] 启动 kubelet
[control-plane] 使用清单文件夹“/etc/kubernetes/manifests”
[control-plane] 为“kube-apiserver”创建静态 Pod 清单
[control-plane] 为“kube-controller-manager”创建静态 Pod 清单
[control-plane] 为“kube-scheduler”创建静态 Pod 清单
[etcd] 在“/etc/kubernetes/manifests”中为本地 etcd 创建静态 Pod 清单
[wait-control-plane] 等待 kubelet 从目录“/etc/kubernetes/manifests”将控制平面作为静态 Pod 启动。这可能需要最多 4 分钟
[kubelet-check] 初始超时已过 40 秒。

    Unfortunately, an error has occurred:  
            timed out waiting for the condition  

    This error is likely caused by:  
            - The kubelet is not running  
            - The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)  

    If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:  
            - 'systemctl status kubelet'  
            - 'journalctl -xeu kubelet'  

    Additionally, a control plane component may have crashed or exited when started by the container runtime.  
    To troubleshoot, list all containers using your preferred container runtimes CLI.  

    Here is one example how you may list all Kubernetes containers running in docker:  
            - 'docker ps -a | grep kube | grep -v pause'  
             Once you have found the failing container, you can inspect its logs with:  
            - 'docker logs CONTAINERID'  

运行后systemctl status kubelet.service,kubelet 似乎运行正常。
但是,运行后journalctl -xeu kubelet,我收到以下日志:

kubelet.go:2347] “容器运行时网络未准备好” networkReady="NetworkReady=false 原因:NetworkPluginNotReady 消息:docker:网络插件未准备好:cni 配置未初始化”
kubelet.go:2422] “获取节点时出错” err="未找到节点“ip-172-31-1-238”
kubelet.go:2422] “获取节点时出错” err="未找到节点“ip-172-31-1-238”
controller.go:144] 无法确保租约存在,将在 7 秒后重试,错误:获取“https://172.31.1.238:6443/apis/coordination.k8s.io/v1/namespaces/kube-node-lease/leases/ip-172-31-1-238?timeout=10s”:拨号 tcp 172.31.1.238:6443:连接:连接被拒绝
kubelet.go:2422] “获取节点时出错” err="未找到节点“ip-172-31-1-238”
kubelet.go:2422] “获取节点时出错” err="未找到节点“ip-172-31-1-238”
kubelet_node_status.go:70] “尝试注册节点” node="ip-172-31-1-238"
kubelet_node_status.go:92] “无法向 API 服务器注册节点” err="Post "https://172.31.1.238:6443/api/v1/nodes": 拨号 tcp 172.31.1.238:6443:连接:连接被拒绝” node="ip-172-31-1-238"
kubelet.go:2422]“获取节点时出错”err="未找到节点“ip-172-31-1-238”

版本:
码头工人:Docker 版本 20.10.12,内部版本 e91ed57
Kubeadm:{Major:“1”, Minor:“23”, GitVersion:“v1.23.1”, GitCommit:“86ec240af8cbd1b60bcc4c03c20da9b98005b92e”, GitTreeState:“clean”, BuildDate:“2021-12-16T11:39:51Z”, GoVersion:“go1.17.5”, 编译器:“gc”, 平台:“linux/amd64”}

不确定这是否是 Kube Api 服务器和 Kubelet 之间的连接问题。
有人知道如何解决这个问题吗?

答案1

这里使用的 kubeadm 版本是 1.23.1。Kubernetes 不再直接支持 docker。阅读此处据我了解,您已安装两者,但它们尚未连接。此外,我没有看到您在 docker 安装命令中指定了 containerd.io。请参阅此处。

选项 1:安装 contianerd。请遵循此步骤。 如果问题仍然存在,请在 kubelet 服务中添加以下选项,配置 kubelet 服务以使用 containerd。

--container-runtime=remote --container-runtime-endpoint=unix:///run/containerd/containerd.sock

选项 2:正确安装 docker 并按所述进行配置这里

答案2

我通过查阅有关使用 kubeadm 创建集群的 Kubernetes 官方文档解决了这个问题。以下是我遵循的步骤:

#!/bin/bash

sudo apt update -y && sudo apt upgrade -y
sudo apt-get install -y ca-certificates curl gnupg lsb-release

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get install docker.io -y
systemctl enable docker.service
systemctl start docker.service

echo 1 > /proc/sys/net/ipv4/ip_forward
lsmod | grep br_netfilter
sudo modprobe br_netfilter

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF
sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker

sudo apt-get update -y
sudo apt-get install -y apt-transport-https ca-certificates curl
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update -y
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
sudo hostnamectl set-hostname master-node

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

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

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

这可以像 shell 脚本一样在 Ubuntu 20.04 服务器中运行,然后创建主节点。

测试于kubeadm 版本:1.23.1
测试于kubernetes 版本:1.23.1
容器运行时:Docker

文档参考:

答案3

问题是你没有指定 advertiseAddress。我遇到了同样的问题,花了几个小时才找到它。

相关内容