一、概述

kubeadm 是 kubernetes 的集群安装工具,能够快速安装 kubernetes 集群,安装 kubernetes 主要是安装它的各个镜像,而 kubeadm 已经为我们集成好了运行 kubernetes 所需的基本镜像。但由于国内的网络原因,在搭建环境时,无法拉取到这些镜像。此时我们只需要修改为阿里云提供的镜像服务即可解决该问题。

二、创建并修改配置

在Kubernetes-Master机器上操作。

  1. # 在local目录下创建级联创建cluster目录
  2. mkdir -p /usr/local/kubernetes/cluster
  3. # 切换到cluster目录下
  4. cd /usr/local/kubernetes/cluster
  5. # 导出配置文件到cluster目录下
  6. kubeadm config print init-defaults --kubeconfig ClusterConfiguration > kubeadm.yml

三、修改 kubeadm.yml 配置文件

apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  # 修改为主节点(Kubernetes-Master机器)的IP地址
  advertiseAddress: 192.168.87.110
  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
# 国内不能访问 Google,修改为阿里云
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
# 修改安装的版本号,我们下载的kubeadm是1.23.6版本的,所以需要修改版本为1.23.6
kubernetesVersion: 1.23.6
networking:
  dnsDomain: cluster.local
  # 配置 POD 所在网段为我们虚拟机不重叠的网段(这里用的是 Flannel 默认网段)
  podSubnet: "10.244.0.0/16"
  serviceSubnet: 10.96.0.0/12
scheduler: {}

四、查看安装所需要的镜像

kubeadm config images list --config kubeadm.yml

# 输出如下:
registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.6
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.6
registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.6
registry.aliyuncs.com/google_containers/kube-proxy:v1.23.6
registry.aliyuncs.com/google_containers/pause:3.6
registry.aliyuncs.com/google_containers/etcd:3.5.1-0
registry.aliyuncs.com/google_containers/coredns:v1.8.6

4.1 镜像说明

  • kube-apiserver:作为整个Kubernetes集群的入口
  • kube-controller-manager:控制管理器
  • kube-scheduler:调度器
  • kube-proxy:代理器
  • pause:暂停器
  • etcd:kv数据库,Kubernetes用来做注册中心使用。
  • coredns:核心DNS

    五、拉取所需的镜像

    ```shell kubeadm config images pull —config kubeadm.yml

输出如下:

[config/images] Pulled registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.6 [config/images] Pulled registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.6 [config/images] Pulled registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.6 [config/images] Pulled registry.aliyuncs.com/google_containers/kube-proxy:v1.23.6 [config/images] Pulled registry.aliyuncs.com/google_containers/pause:3.6 [config/images] Pulled registry.aliyuncs.com/google_containers/etcd:3.5.1-0 [config/images] Pulled registry.aliyuncs.com/google_containers/coredns:v1.8.6

<a name="Ly9X9"></a>
# 六、安装主节点(Kubernetes-Master)
执行以下命令初始化主节点,该命令指定了初始化时需要使用的配置文件,其中添加 --experimental-upload-certs 参数可以在后续执行加入节点时自动分发证书文件。追加的 tee kubeadm-init.log 用以输出日志。
> **注意:**如果安装 kubernetes 版本和下载的镜像版本不统一则会出现 `timed out waiting for the condition` 错误。中途失败或是想修改配置可以使用 `kubeadm reset` 命令重置配置,再做初始化操作即可。

```shell
kubeadm init --config=kubeadm.yml --upload-certs | tee kubeadm-init.log

# 输出如下:
[init] Using Kubernetes version: v1.23.6
[preflight] Running pre-flight checks
        [WARNING Hostname]: hostname "node" could not be reached
        [WARNING Hostname]: hostname "node": lookup node on 127.0.0.53:53: server misbehaving
[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'
[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 [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local node] and IPs [10.96.0.1 192.168.87.110]
[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 [localhost node] and IPs [192.168.87.110 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost node] and IPs [192.168.87.110 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
[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"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[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 13.504328 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.23" in namespace kube-system with the configuration for the kubelets in the cluster
NOTE: The "kubelet-config-1.23" naming of the kubelet ConfigMap is deprecated. Once the UnversionedKubeletConfigMap feature gate graduates to Beta the default name will become just "kubelet-config". Kubeadm upgrade will handle this transition transparently.
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
4faf43baff8b82faca7aefb9129f7c24617e13c8fd78cc11cfa56a4651208f13
[mark-control-plane] Marking the node node as control-plane by adding the labels: [node-role.kubernetes.io/master(deprecated) node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node node as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: abcdef.0123456789abcdef
[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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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 192.168.87.110:6443 --token abcdef.0123456789abcdef \
        --discovery-token-ca-cert-hash sha256:d624934ab01d476115495b12884c48d63a844e0a66f78b97e95ed6232d35716f

七、配置 kubectl

# root账户执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

# 非root账户执行
sudo chown $(id -u):$(id -g) $HOME/.kube/config

八、验证主节点是否安装成功

kubectl get node

# 输出如下:
NAME   STATUS     ROLES                  AGE   VERSION
node   NotReady   control-plane,master   81s   v1.23.6

九、安装从节点(Kubernetes-Node-01、Kubernetes-Node-02)

将 Node 节点加入到集群中很简单,只需要在 Node 服务器上安装 kubeadmkubectlkubelet 三个工具,然后使用 kubeadm join 命令加入即可。kubeadm join 命令在刚刚安装主节点 kubernetes-master的时候,在输出日志信息的最后即可找到,然后将其复制粘贴到其他节点运行即可。

# 这个就是刚刚安装Kubernetes-Master主节点时生成的Token,目前已经存放在/usr/local/kubernetes/cluster路径下的kubeadm-init.log日志文件内了
kubeadm join 192.168.87.110:6443 --token abcdef.0123456789abcdef \
        --discovery-token-ca-cert-hash sha256:d624934ab01d476115495b12884c48d63a844e0a66f78b97e95ed6232d35716f

# 输出如下:
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
W0519 13:14:49.911834    4952 utils.go:69] The recommended value for "resolvConf" in "KubeletConfiguration" is: /run/systemd/resolve/resolv.conf; the provided value is: /run/systemd/resolve/resolv.conf
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

十、验证安装从节点是否成功

返回到Kubernetes-Master主节点的机器上,查看是否安装成功!

注意:如果 Node 节点加入 Master 时配置有问题可以在 Node 节点上使用kubeadm reset重置配置再使用kubeadm join命令重新加入即可。希望在 master 节点删除 node ,可以使用kubeadm delete nodes <NAME>删除。

kubectl get node

# 输出如下:
NAME                 STATUS     ROLES                  AGE     VERSION
kubernetes-node-01   NotReady   <none>                 63s     v1.23.6
kubernetes-node-02   NotReady   <none>                 23s     v1.23.6
node                 NotReady   control-plane,master   3m30s   v1.23.6

十一、查看 Pod 状态

coredns 尚未运行,此时我们还需要安装网络插件。

kubectl get pod -n kube-system -o wide

# 输出如下:
NAME                           READY   STATUS              RESTARTS   AGE     IP               NODE                 NOMINATED NODE   READINESS GATES
coredns-6d8c4cb4d-5wqwt        0/1     Pending             0          3m33s   <none>           <none>               <none>           <none>
coredns-6d8c4cb4d-cdllc        0/1     Pending             0          3m33s   <none>           <none>               <none>           <none>
etcd-node                      1/1     Running             0          3m46s   192.168.87.110   node                 <none>           <none>
kube-apiserver-node            1/1     Running             0          3m48s   192.168.87.110   node                 <none>           <none>
kube-controller-manager-node   1/1     Running             0          3m46s   192.168.87.110   node                 <none>           <none>
kube-proxy-7cdrk               1/1     Running             0          84s     192.168.87.120   kubernetes-node-01   <none>           <none>
kube-proxy-dmtmb               1/1     Running             0          3m33s   192.168.87.110   node                 <none>           <none>
kube-proxy-fvxq2               0/1     ContainerCreating   0          44s     192.168.87.121   kubernetes-node-02   <none>           <none>
kube-scheduler-node            1/1     Running             0          3m46s   192.168.87.110   node                 <none>           <none>

附:扩展阅读

kubeadm init 的执行过程

  • init:指定版本进行初始化操作
  • preflight:初始化前的检查和下载所需要的 Docker 镜像文件
  • kubelet-start:生成 kubelet 的配置文件 var/lib/kubelet/config.yaml,没有这个文件 kubelet 无法启动,所以初始化之前的 kubelet 实际上启动不会成功
  • certificates:生成 Kubernetes 使用的证书,存放在 /etc/kubernetes/pki 目录中
  • kubeconfig:生成 KubeConfig 文件,存放在 /etc/kubernetes 目录中,组件之间通信需要使用对应文件
  • control-plane:使用 /etc/kubernetes/manifest 目录下的 YAML 文件,安装 Master 组件
  • etcd:使用 /etc/kubernetes/manifest/etcd.yaml 安装 Etcd 服务
  • wait-control-plane:等待 control-plan 部署的 Master 组件启动
  • apiclient:检查 Master 组件服务状态。
  • uploadconfig:更新配置
  • kubelet:使用 configMap 配置 kubelet
  • patchnode:更新 CNI 信息到 Node 上,通过注释的方式记录
  • mark-control-plane:为当前节点打标签,打了角色 Master,和不可调度标签,这样默认就不会使用 Master 节点来运行 Pod
  • bootstrap-token:生成 token 记录下来,后边使用 kubeadm join 往集群中添加节点时会用到
  • addons:安装附加组件 CoreDNS 和 kube-proxy