Kubeadm设置clusterName

使用Kubeadm可以快速的创建一个k8s集群,但是默认情况下Kubeadm创建的集群名称都是kubernetes(集群名称可以使用命令kubectl config get-clusters查询)。在实际使用中,如果管理多个k8s集群,就需要对集群名称做区分,方便管理维护。

通过网上查找资料,现在可以通过修改kubeadm初始化配置文件,自定义clusterName,操作步骤如下:

配置文件的功能仍然处于 alpha 状态并且在将来的版本中可能会改变,但是一些更加高级的功能只能够通过配置文件设置,比如clusterName

  1. 使用命令kubeadm config print init-defaults > kubeadm-init.yaml获取kubeadm init 的默认 init 配置对象,并将其写入到文件kubeadm-init.yaml中,文件内容如下:
apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 1.2.3.4
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  imagePullPolicy: IfNotPresent
  name: node
  taints: null
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: 1.23.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
scheduler: {}
  1. 对生成的默认配置文件进行修改,这里按照实际需求,对下面的几个配置项进行修改:集群名称clusterName、镜像仓库地址imageRepository、Service网段networking.serviceSubnet、Pod网段networking.podSubnet,最终修改后的文件内容如下:
apiVersion: kubeadm.k8s.io/v1beta3
clusterName: k8s-dev
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
networking:
  serviceSubnet: 10.96.0.0/12
  podSubnet: 10.244.0.0/16
  1. 使用修改后的文件初始化集群kubeadm init --config kubeadm-init.yaml

踩坑

flannel CrashLoopBackOff

问题和原因

集群初始化操作完成之后,部署flannel,过一会就会看到flannel Pod CrashLoopBackOff,查看flannel日志有这么一行Error registering network: failed to acquire lease: node "ubuntu-vm" pod cidr not assigned,很明确告知了flannel CrashLoopBackOff 的原因是未分配Pod网段。

I0503 10:17:21.246851       1 main.go:205] CLI flags config: {etcdEndpoints:http://127.0.0.1:4001,http://127.0.0.1:2379 etcdPrefix:/coreos.com/network etcdKeyfile: etcdCertfile: etcdCAFile: etcdUsername: etcdPassword: version:false kubeSubnetMgr:true kubeApiUrl: kubeAnnotationPrefix:flannel.alpha.coreos.com kubeConfigFile: iface:[] ifaceRegex:[] ipMasq:true subnetFile:/run/flannel/subnet.env publicIP: publicIPv6: subnetLeaseRenewMargin:60 healthzIP:0.0.0.0 healthzPort:0 iptablesResyncSeconds:5 iptablesForwardRules:true netConfPath:/etc/kube-flannel/net-conf.json setNodeNetworkUnavailable:true}
W0503 10:17:21.246945       1 client_config.go:614] Neither --kubeconfig nor --master was specified.  Using the inClusterConfig.  This might not work.
I0503 10:17:21.342762       1 kube.go:378] Starting kube subnet manager
I0503 10:17:21.342913       1 kube.go:120] Waiting 10m0s for node controller to sync
I0503 10:17:22.344507       1 kube.go:127] Node controller sync successful
I0503 10:17:22.344528       1 main.go:225] Created subnet manager: Kubernetes Subnet Manager - ubuntu-vm
I0503 10:17:22.344531       1 main.go:228] Installing signal handlers
I0503 10:17:22.344745       1 main.go:454] Found network config - Backend type: vxlan
I0503 10:17:22.344771       1 match.go:189] Determining IP address of default interface
I0503 10:17:22.344988       1 match.go:242] Using interface with name enp0s5 and address 10.33.33.7
I0503 10:17:22.345003       1 match.go:264] Defaulting external address to interface address (10.33.33.7)
I0503 10:17:22.345054       1 vxlan.go:138] VXLAN config: VNI=1 Port=0 GBP=false Learning=false DirectRouting=false
E0503 10:17:22.345208       1 main.go:317] Error registering network: failed to acquire lease: node "ubuntu-vm" pod cidr not assigned
I0503 10:17:22.345281       1 main.go:434] Stopping shutdownHandler...
W0503 10:17:22.345306       1 reflector.go:436] github.com/flannel-io/flannel/subnet/kube/kube.go:379: watch of *v1.Node ended with: an error on the server ("unable to decode an event from the watch stream: context canceled") has prevented the request from succeeding

解决方法

  1. 集群初始化之前,在配置文件中定义networking.podSubnet: 10.244.0.0/16,再进行初始化集群的操作
  2. 如果集群已经启动通过打补丁的方式配置解决,kubectl patch node $(hostname) -p '{"spec":{"podCIDR":"10.244.0.0/24"}}'(如果集群只是初始化尚未使用,建议kubeadm reset删除集群,重新初始化)

参考:
stackoverflow: Kubernetes cluster name change
GitHub:Ability to configure user and cluster name in AdminKubeConfigFile
Github:https://github.com/kubernetes/kubernetes/pull/60852
Github:Error registering network: failed to acquire lease: node "nodeName" pod cidr not assigned

最后修改:2023 年 02 月 27 日
如果觉得我的文章对你有用,请随意赞赏