在Ubuntu 22.04上安装Kubernetes时出现错误:无法创建新的DNS服务

67次阅读
没有评论

问题描述

在尝试在Ubuntu 22.04机器上安装Kubernetes时,使用以下命令:

sudo kubeadm init --skip-phases=addon/kube-proxy --pod-network-cidr=10.244.0.0/16 --v=5

但是得到了以下结果:

I1213 17:00:05.712274  521447 initconfiguration.go:116] detected and using CRI socket: unix:///var/run/containerd/containerd.sock
I1213 17:00:05.712654  521447 interface.go:432] Looking for default routes with IPv4 addresses
I1213 17:00:05.712662  521447 interface.go:437] Default route transits interface "wlp2s0"
I1213 17:00:05.713113  521447 interface.go:209] Interface wlp2s0 is up
I1213 17:00:05.713174  521447 interface.go:257] Interface "wlp2s0" has 2 addresses :[192.168.1.2/24 fe80::7a46:dd7e:145:d5e2/64].
I1213 17:00:05.713208  521447 interface.go:224] Checking addr  192.168.1.2/24.
I1213 17:00:05.713214  521447 interface.go:231] IP found 192.168.1.2
I1213 17:00:05.713240  521447 interface.go:263] Found valid IPv4 address 192.168.1.2 for interface "wlp2s0".
I1213 17:00:05.713244  521447 interface.go:443] Found active IP 192.168.1.2 
I1213 17:00:05.713260  521447 kubelet.go:196] the value of KubeletConfiguration.cgroupDriver is empty; setting it to "systemd"
I1213 17:00:05.716792  521447 version.go:187] fetching Kubernetes version from URL: https://dl.k8s.io/release/stable-1.txt
[init] Using Kubernetes version: v1.26.0
[preflight] Running pre-flight checks
I1213 17:00:05.945722  521447 checks.go:568] validating Kubernetes and kubeadm version
I1213 17:00:05.945753  521447 checks.go:168] validating if the firewall is enabled and active
I1213 17:00:05.955106  521447 checks.go:203] validating availability of port 6443
I1213 17:00:05.955356  521447 checks.go:203] validating availability of port 10259
I1213 17:00:05.955425  521447 checks.go:203] validating availability of port 10257
I1213 17:00:05.955500  521447 checks.go:280] validating the existence of file /etc/kubernetes/manifests/kube-apiserver.yaml
I1213 17:00:05.955513  521447 checks.go:280] validating the existence of file /etc/kubernetes/manifests/kube-controller-manager.yaml
I1213 17:00:05.955536  521447 checks.go:280] validating the existence of file /etc/kubernetes/manifests/kube-scheduler.yaml
I1213 17:00:05.955548  521447 checks.go:280] validating the existence of file /etc/kubernetes/manifests/etcd.yaml
I1213 17:00:05.955559  521447 checks.go:430] validating if the connectivity type is via proxy or direct
I1213 17:00:05.955586  521447 checks.go:469] validating http connectivity to first IP address in the CIDR
I1213 17:00:05.955597  521447 checks.go:469] validating http connectivity to first IP address in the CIDR
I1213 17:00:05.955605  521447 checks.go:104] validating the container runtime
I1213 17:00:05.980010  521447 checks.go:329] validating the contents of file /proc/sys/net/bridge/bridge-nf-call-iptables
I1213 17:00:05.980082  521447 checks.go:329] validating the contents of file /proc/sys/net/ipv4/ip_forward
I1213 17:00:05.980166  521447 checks.go:644] validating whether swap is enabled or not
I1213 17:00:05.980235  521447 checks.go:370] validating the presence of executable crictl
I1213 17:00:05.980269  521447 checks.go:370] validating the presence of executable conntrack
I1213 17:00:05.980297  521447 checks.go:370] validating the presence of executable ip
I1213 17:00:05.980350  521447 checks.go:370] validating the presence of executable iptables
I1213 17:00:05.980379  521447 checks.go:370] validating the presence of executable mount
I1213 17:00:05.980403  521447 checks.go:370] validating the presence of executable nsenter
I1213 17:00:05.980429  521447 checks.go:370] validating the presence of executable ebtables
I1213 17:00:05.980451  521447 checks.go:370] validating the presence of executable ethtool
I1213 17:00:05.980472  521447 checks.go:370] validating the presence of executable socat
I1213 17:00:05.980511  521447 checks.go:370] validating the presence of executable tc
I1213 17:00:05.980531  521447 checks.go:370] validating the presence of executable touch
I1213 17:00:05.980571  521447 checks.go:516] running all checks
I1213 17:00:05.992438  521447 checks.go:401] checking whether the given node name is valid and reachable using net.LookupHost
I1213 17:00:05.992475  521447 checks.go:610] validating kubelet version
I1213 17:00:06.045271  521447 checks.go:130] validating if the "kubelet" service is enabled and active
I1213 17:00:06.056454  521447 checks.go:203] validating availability of port 10250
I1213 17:00:06.056548  521447 checks.go:203] validating availability of port 2379
I1213 17:00:06.056582  521447 checks.go:203] validating availability of port 2380
I1213 17:00:06.056614  521447 checks.go:243] validating the existence and emptiness of directory /var/lib/etcd
[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'
I1213 17:00:06.056717  521447 checks.go:832] using image pull policy: IfNotPresent
I1213 17:00:06.080185  521447 checks.go:841] image exists: registry.k8s.io/kube-apiserver:v1.26.0
I1213 17:00:06.102194  521447 checks.go:841] image exists: registry.k8s.io/kube-controller-manager:v1.26.0
I1213 17:00:06.124531  521447 checks.go:841] image exists: registry.k8s.io/kube-scheduler:v1.26.0
I1213 17:00:06.147256  521447 checks.go:841] image exists: registry.k8s.io/kube-proxy:v1.26.0
I1213 17:00:06.170320  521447 checks.go:841] image exists: registry.k8s.io/pause:3.9
I1213 17:00:06.192792  521447 checks.go:841] image exists: registry.k8s.io/etcd:3.5.6-0
I1213 17:00:06.215664  521447 checks.go:841] image exists: registry.k8s.io/coredns/coredns:v1.9.3
[certs] Using certificateDir folder "/etc/kubernetes/pki"
I1213 17:00:06.215728  521447 certs.go:112] creating a new certificate authority for ca
[certs] Generating "ca" certificate and key
I1213 17:00:06.416819  521447 certs.go:519] validating certificate period for ca certificate
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [a kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.1.2]
[certs] Generating "apiserver-kubelet-client" certificate and key
I1213 17:00:06.890208  521447 certs.go:112] creating a new certificate authority for front-proxy-ca
[certs] Generating "front-proxy-ca" certificate and key
I1213 17:00:07.193396  521447 certs.go:519] validating certificate period for front-proxy-ca certificate
[certs] Generating "front-proxy-client" certificate and key
I1213 17:00:07.287405  521447 certs.go:112] creating a new certificate authority for etcd-ca
[certs] Generating "etcd/ca" certificate and key
I1213 17:00:07.443845  521447 certs.go:519] validating certificate period for etcd/ca certificate
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [a localhost] and IPs [192.168.1.2 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [a localhost] and IPs [192.168.1.2 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
I1213 17:00:08.693250  521447 certs.go:78] creating new public/private key files for signing service account users
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
I1213 17:00:08.903454  521447 kubeconfig.go:103] creating kubeconfig file for admin.conf
[kubeconfig] Writing "admin.conf" kubeconfig file
I1213 17:00:09.084882  521447 kubeconfig.go:103] creating kubeconfig file for kubelet.conf
[kubeconfig] Writing "kubelet.conf" kubeconfig file
I1213 17:00:09.323962  521447 kubeconfig.go:103] creating kubeconfig file for controller-manager.conf
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
I1213 17:00:09.400161  521447 kubeconfig.go:103] creating kubeconfig file for scheduler.conf
[kubeconfig] Writing "scheduler.conf" kubeconfig file
I1213 17:00:09.573083  521447 kubelet.go:67] Stopping the kubelet
[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
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
I1213 17:00:10.068134  521447 manifests.go:99] [control-plane] getting StaticPodSpecs
I1213 17:00:10.068404  521447 certs.go:519] validating certificate period for CA certificate
I1213 17:00:10.068561  521447 manifests.go:125] [control-plane] adding volume "ca-certs" for component "kube-apiserver"
I1213 17:00:10.068569  521447 manifests.go:125] [control-plane] adding volume "etc-ca-certificates" for component "kube-apiserver"
I1213 17:00:10.068575  521447 manifests.go:125] [control-plane] adding volume "etc-pki" for component "kube-apiserver"
I1213 17:00:10.068580  521447 manifests.go:125] [control-plane] adding volume "k8s-certs" for component "kube-apiserver"
I1213 17:00:10.068603  521447 manifests.go:125] [control-plane] adding volume "usr-local-share-ca-certificates" for component "kube-apiserver"
I1213 17:00:10.068624  521447 manifests.go:125] [control-plane] adding volume "usr-share-ca-certificates" for component "kube-apiserver"
I1213 17:00:10.070556  521447 manifests.go:154] [control-plane] wrote static Pod manifest for component "kube-apiserver" to "/etc/kubernetes/manifests/kube-apiserver.yaml"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
I1213 17:00:10.070588  521447 manifests.go:99] [control-plane] getting StaticPodSpecs
I1213 17:00:10.070825  521447 manifests.go:125] [control-plane] adding volume "ca-certs" for component "kube-controller-manager"
I1213 17:00:10.070835  521447 manifests.go:125] [control-plane] adding volume "etc-ca-certificates" for component "kube-controller-manager"
I1213 17:00:10.070847  521447 manifests.go:125] [control-plane] adding volume "etc-pki" for component "kube-controller-manager"
I1213 17:00:10.070859  521447 manifests.go:125] [control-plane] adding volume "flexvolume-dir" for component "kube-controller-manager"
I1213 17:00:10.070870  521447 manifests.go:125] [control-plane] adding volume "k8s-certs" for component "kube-controller-manager"
I1213 17:00:10.070901  521447 manifests.go:125] [control-plane] adding volume "kubeconfig" for component "kube-controller-manager"
I1213 17:00:10.070926  521447 manifests.go:125] [control-plane] adding volume "usr-local-share-ca-certificates" for component "kube-controller-manager"
I1213 17:00:10.070939  521447 manifests.go:125] [control-plane] adding volume "usr-share-ca-certificates" for component "kube-controller-manager"
I1213 17:00:10.071767  521447 manifests.go:154] [control-plane] wrote static Pod manifest for component "kube-controller-manager" to "/etc/kubernetes/manifests/kube-controller-manager.yaml"
[control-plane] Creating static Pod manifest for "kube-scheduler"
I1213 17:00:10.071795  521447 manifests.go:99] [control-plane] getting StaticPodSpecs
I1213 17:00:10.072094  521447 manifests.go:125] [control-plane] adding volume "kubeconfig" for component "kube-scheduler"
I1213 17:00:10.072475  521447 manifests.go:154] [control-plane] wrote static Pod manifest for component "kube-scheduler" to "/etc/kubernetes/manifests/kube-scheduler.yaml"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
I1213 17:00:10.074184  521447 local.go:65] [etcd] wrote Static Pod manifest for a local etcd member to "/etc/kubernetes/manifests/etcd.yaml"
I1213 17:00:10.074198  521447 waitcontrolplane.go:83] [wait-control-plane] Waiting for the API server to be healthy
[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 13.001692 seconds
I1213 17:00:23.077009  521447 uploadconfig.go:111] [upload-config] Uploading the kubeadm ClusterConfiguration to a ConfigMap
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
I1213 17:00:23.179127  521447 uploadconfig.go:125] [upload-config] Uploading the kubelet component config to a ConfigMap
[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubelets in the cluster
I1213 17:00:23.348491  521447 uploadconfig.go:130] [upload-config] Preserving the CRISocket information for the control-plane node
I1213 17:00:23.348515  521447 patchnode.go:31] [patchnode] Uploading the CRI Socket information "unix:///var/run/containerd/containerd.sock" to the Node API object "a" as an annotation
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node a as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node a as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: 9jiz6o.t21o06tt700624bj
[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
I1213 17:00:24.848283  521447 clusterinfo.go:47] [bootstrap-token] loading admin kubeconfig
I1213 17:00:24.849139  521447 clusterinfo.go:58] [bootstrap-token] copying the cluster from admin.conf to the bootstrap kubeconfig
I1213 17:00:24.849419  521447 clusterinfo.go:70] [bootstrap-token] creating/updating ConfigMap in kube-public namespace
I1213 17:00:24.871820  521447 clusterinfo.go:84] creating the RBAC rules for exposing the cluster-info ConfigMap in the kube-public namespace
I1213 17:00:25.049727  521447 kubeletfinalize.go:90] [kubelet-finalize] Assuming that kubelet client certificate rotation is enabled: found "/var/lib/kubelet/pki/kubelet-client-current.pem"
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
I1213 17:00:25.050942  521447 kubeletfinalize.go:134] [kubelet-finalize] Restarting the kubelet to enable client certificate rotation
I1213 17:00:27.099127  521447 with_retry.go:241] Got a Retry-After 1s response for attempt 1 to https://192.168.1.2:6443/api/v1/namespaces/kube-system/services/kube-dns?timeout=10s
Put "https://192.168.1.2:6443/api/v1/namespaces/kube-system/services/kube-dns?timeout=10s": dial tcp 192.168.1.2:6443: connect: connection refused
unable to create/update the DNS service
k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns.createDNSService    cmd/kubeadm/app/phases/addons/dns/dns.go:273
k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns.createCoreDNSAddon    cmd/kubeadm/app/phases/addons/dns/dns.go:255
k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns.coreDNSAddon    cmd/kubeadm/app/phases/addons/dns/dns.go:157
k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns.EnsureDNSAddon    cmd/kubeadm/app/phases/addons/dns/dns.go:102
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/init.runCoreDNSAddon    cmd/kubeadm/app/cmd/phases/init/addons.go:112
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow.(*Runner).Run.func1    cmd/kubeadm/app/cmd/phases/workflow/runner.go:259
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow.(*Runner).visitAll    cmd/kubeadm/app/cmd/phases/workflow/runner.go:446
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow.(*Runner).Run    cmd/kubeadm/app/cmd/phases/workflow/runner.go:232
k8s.io/kubernetes/cmd/kubeadm/app/cmd/newCmdInit.func1    cmd/kubeadm/app/cmd/init.go:112
github.com/spf13/cobra.(*Command).execute    vendor/github.com/spf13/cobra/command.go:916
github.com/spf13/cobra.(*Command).ExecuteC    vendor/github.com/spf13/cobra/command.go:1040
github.com/spf13/cobra.(*Command).Execute    vendor/github.com/spf13/cobra/command.go:968
k8s.io/kubernetes/cmd/kubeadm/app.Run    cmd/kubeadm/app/kubeadm.go:50
main.main    cmd/kubeadm/kubeadm.go:25
runtime.main    /usr/local/go/src/runtime/proc.go:250
runtime.goexit    /usr/local/go/src/runtime/asm_amd64.s:1594
error execution phase addon/coredns
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow.(*Runner).Run.func1    cmd/kubeadm/app/cmd/phases/workflow/runner.go:260
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow.(*Runner).visitAll    cmd/kubeadm/app/cmd/phases/workflow/runner.go:446
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow.(*Runner).Run    cmd/kubeadm/app/cmd/phases/workflow/runner.go:232
k8s.io/kubernetes/cmd/kubeadm/app/cmd/newCmdInit.func1    cmd/kubeadm/app/cmd/init.go:112
github.com/spf13/cobra.(*Command).execute    vendor/github.com/spf13/cobra/command.go:916
github.com/spf13/cobra.(*Command).ExecuteC    vendor/github.com/spf13/cobra/command.go:1040
github.com/spf13/cobra.(*Command).Execute    vendor/github.com/spf13/cobra/command.go:968
k8s.io/kubernetes/cmd/kubeadm/app.Run    cmd/kubeadm/app/kubeadm.go:50
main.main    cmd/kubeadm/kubeadm.go:25
runtime.main    /usr/local/go/src/runtime/proc.go:250
runtime.goexit    /usr/local/go/src/runtime/asm_amd64.s:1594

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

在Kubernetes中,出现这个错误可能是由于DNS服务无法创建或更新导致的。你可以尝试以下解决方案:
1. 确保你的网络连接正常,并且可以访问Kubernetes集群的API服务器。
2. 检查kube-dns服务是否已经正确安装和配置。你可以使用以下命令检查kube-dns的状态:
kubectl get pods -n kube-system -l k8s-app=kube-dns
如果kube-dns的状态为Running,则表示kube-dns已经正确安装和配置。
3. 如果kube-dns的状态不是Running,你可以尝试删除并重新创建kube-dns服务。使用以下命令删除kube-dns服务:
kubectl delete deployment kube-dns -n kube-system
然后,使用以下命令重新创建kube-dns服务:
kubectl apply -f https://github.com/kubernetes/kops/blob/master/addons/kube-dns/v1.18.0.yaml
这将重新创建kube-dns服务并将其配置为与你的Kubernetes集群兼容的版本。
4. 如果上述步骤仍然无法解决问题,你可以尝试使用其他的DNS插件,如CoreDNS。你可以使用以下命令将CoreDNS作为DNS插件安装到你的Kubernetes集群中:
kubectl apply -f https://github.com/coredns/deployment/blob/master/kubernetes/coredns.yaml.sed
这将安装CoreDNS并将其配置为与你的Kubernetes集群兼容的版本。

方案2

使用其他DNS插件可能需要修改Kubernetes配置文件,并且可能会导致一些不可预料的问题。在进行任何更改之前,请确保你已经备份了Kubernetes配置文件和数据。
另一种解决方案是尝试使用其他的DNS插件,如CoreDNS。CoreDNS是一个灵活且易于配置的DNS插件,可以替代kube-dns。你可以按照以下步骤来安装和配置CoreDNS:
1. 使用以下命令下载CoreDNS的配置文件:
wget https://github.com/coredns/deployment/blob/master/kubernetes/coredns.yaml.sed
2. 使用文本编辑器打开coredns.yaml.sed文件,并根据你的需求进行必要的修改。你可以修改以下部分:
name字段:指定CoreDNS的名称。
namespace字段:指定CoreDNS所在的命名空间。
image字段:指定CoreDNS的镜像。
args字段:指定CoreDNS的参数。
resources字段:指定CoreDNS的资源限制。
configMap字段:指定CoreDNS的配置文件。
3. 使用以下命令将修改后的配置文件应用到你的Kubernetes集群中:
kubectl apply -f coredns.yaml.sed
这将安装和配置CoreDNS,并将其替代kube-dns作为DNS插件。
4. 使用以下命令检查CoreDNS的状态:
kubectl get pods -n <namespace> -l k8s-app=coredns
如果CoreDNS的状态为Running,则表示CoreDNS已经成功安装和配置。

请注意,使用其他DNS插件可能需要修改Kubernetes配置文件,并且可能会导致一些不可预料的问题。在进行任何更改之前,请确保你已经备份了Kubernetes配置文件和数据。

正文完