实验机器清单
环境准备

以下操作,如果没有特殊说明, 指在对应的服务器上执行

一、设置yum源

所有服务器都执行
因为我使用的环境不能科学上网,所以配置阿里云的yum源

  1. [root@k8s-5-146 ~]# cat <<EOF > /etc/yum.repos.d/kubernetes.repo
  2. > [kubernetes]
  3. > name=Kubernetes
  4. > baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  5. > enabled=1
  6. > gpgcheck=0
  7. > repo_gpgcheck=0
  8. > gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
  9. > http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  10. > EOF

二、安装 kubeadm、kubelet、kubectl

所有服务器都执行

由于我的环境中docker版本是 20.10.6

  1. # --disableexcludes 禁掉除了kubernetes之外的别的仓库
  2. [root@k8s-5-146 ~]# yum install -y kubelet-1.19.9 kubeadm-1.19.9 kubectl-1.19.9 --disableexcludes=kubernetes
  3. [root@k8s-5-147 ~]# kubeadm version
  4. kubeadm version: &version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.9", GitCommit:"9dd794e454ac32d97cde41ae10be801ae98f75df", GitTreeState:"clean", BuildDate:"2021-03-18T01:07:09Z", GoVersion:"go1.15.8", Compiler:"gc", Platform:"linux/amd64"}
  5. ## 设置开机启动
  6. [root@k8s-5-146 ~]# systemctl enable --now kubelet

三、初始化集群

  1. #导出默认的初始化配置文件
  2. [root@k8s-5-146 ~]# mkdir -p /opt/k8s
  3. [root@k8s-5-146 ~]# cd /opt/k8s
  4. [root@k8s-5-146 k8s]# kubeadm config print init-defaults > kubeadm.yaml
  5. [root@k8s-5-146 k8s]# vim kubeadm.yaml
  6. apiVersion: kubeadm.k8s.io/v1beta2
  7. bootstrapTokens:
  8. - groups:
  9. - system:bootstrappers:kubeadm:default-node-token
  10. token: abcdef.0123456789abcdef
  11. ttl: 24h0m0s
  12. usages:
  13. - signing
  14. - authentication
  15. kind: InitConfiguration
  16. localAPIEndpoint:
  17. advertiseAddress: 192.168.5.146
  18. bindPort: 6443
  19. nodeRegistration:
  20. criSocket: /var/run/dockershim.sock
  21. name: k8s-5-146.k8s.host
  22. taints:
  23. - effect: NoSchedule
  24. key: node-role.kubernetes.io/master
  25. ---
  26. apiServer:
  27. timeoutForControlPlane: 4m0s
  28. apiVersion: kubeadm.k8s.io/v1beta2
  29. certificatesDir: /etc/kubernetes/pki
  30. clusterName: kubernetes
  31. controllerManager: {}
  32. dns:
  33. type: CoreDNS
  34. etcd:
  35. local:
  36. dataDir: /var/lib/etcd
  37. imageRepository: registry.aliyuncs.com/google_containers #将原来默认的镜像仓库修改为阿里云的
  38. kind: ClusterConfiguration
  39. kubernetesVersion: v1.19.0
  40. networking:
  41. dnsDomain: cluster.local
  42. serviceSubnet: 10.96.0.0/12
  43. scheduler: {}
  44. ---
  45. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  46. kind: KubeProxyConfiguration
  47. mode: ipvs # kube-proxy 模式
  48. [root@k8s-5-146 k8s]# kubeadm init --config kubeadm.yaml
  49. W0416 15:09:49.726813 6094 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
  50. [init] Using Kubernetes version: v1.19.0
  51. [preflight] Running pre-flight checks
  52. [WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
  53. [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.6. Latest validated version: 19.03
  54. [preflight] Pulling images required for setting up a Kubernetes cluster
  55. [preflight] This might take a minute or two, depending on the speed of your internet connection
  56. [preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
  57. [certs] Using certificateDir folder "/etc/kubernetes/pki"
  58. [certs] Generating "ca" certificate and key
  59. [certs] Generating "apiserver" certificate and key
  60. [certs] apiserver serving cert is signed for DNS names [k8s-5-146.k8s.host kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.5.146]
  61. [certs] Generating "apiserver-kubelet-client" certificate and key
  62. [certs] Generating "front-proxy-ca" certificate and key
  63. [certs] Generating "front-proxy-client" certificate and key
  64. [certs] Generating "etcd/ca" certificate and key
  65. [certs] Generating "etcd/server" certificate and key
  66. [certs] etcd/server serving cert is signed for DNS names [k8s-5-146.k8s.host localhost] and IPs [192.168.5.146 127.0.0.1 ::1]
  67. [certs] Generating "etcd/peer" certificate and key
  68. [certs] etcd/peer serving cert is signed for DNS names [k8s-5-146.k8s.host localhost] and IPs [192.168.5.146 127.0.0.1 ::1]
  69. [certs] Generating "etcd/healthcheck-client" certificate and key
  70. [certs] Generating "apiserver-etcd-client" certificate and key
  71. [certs] Generating "sa" key and public key
  72. [kubeconfig] Using kubeconfig folder "/etc/kubernetes"
  73. [kubeconfig] Writing "admin.conf" kubeconfig file
  74. [kubeconfig] Writing "kubelet.conf" kubeconfig file
  75. [kubeconfig] Writing "controller-manager.conf" kubeconfig file
  76. [kubeconfig] Writing "scheduler.conf" kubeconfig file
  77. [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
  78. [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
  79. [kubelet-start] Starting the kubelet
  80. [control-plane] Using manifest folder "/etc/kubernetes/manifests"
  81. [control-plane] Creating static Pod manifest for "kube-apiserver"
  82. [control-plane] Creating static Pod manifest for "kube-controller-manager"
  83. [control-plane] Creating static Pod manifest for "kube-scheduler"
  84. [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
  85. [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
  86. [kubelet-check] Initial timeout of 40s passed.
  87. [apiclient] All control plane components are healthy after 59.002281 seconds
  88. [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
  89. [kubelet] Creating a ConfigMap "kubelet-config-1.19" in namespace kube-system with the configuration for the kubelets in the cluster
  90. [upload-certs] Skipping phase. Please see --upload-certs
  91. [mark-control-plane] Marking the node k8s-5-146.k8s.host as control-plane by adding the label "node-role.kubernetes.io/master=''"
  92. [mark-control-plane] Marking the node k8s-5-146.k8s.host as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
  93. [bootstrap-token] Using token: abcdef.0123456789abcdef
  94. [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
  95. [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
  96. [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
  97. [bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
  98. [bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
  99. [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
  100. [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
  101. [addons] Applied essential addon: CoreDNS
  102. [addons] Applied essential addon: kube-proxy
  103. Your Kubernetes control-plane has initialized successfully!
  104. To start using your cluster, you need to run the following as a regular user:
  105. mkdir -p $HOME/.kube
  106. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  107. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  108. You should now deploy a pod network to the cluster.
  109. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  110. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  111. Then you can join any number of worker nodes by running the following on each as root:
  112. kubeadm join 192.168.5.146:6443 --token abcdef.0123456789abcdef \
  113. --discovery-token-ca-cert-hash sha256:a025044360496164420da8350ba17f5acf9ab6444398707f8cef568b8920059a

四、拷贝 kubeconfig 文件

  1. [root@k8s-5-146 ~]# mkdir -p $HOME/.kube
  2. [root@k8s-5-146 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. [root@k8s-5-146 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config

五、添加节点

记住初始化集群上面的配置和操作要提前做好,将 master 节点上面的 $HOME/.kube/config 文件拷贝到 node 节点对应的文件中,安装 kubeadm、kubelet、kubectl(可选),然后执行上面初始化完成后提示的 join 命令即可:

  1. # 分别在 k8s-5-147 和 k8s-5-148 上执行
  2. [root@k8s-5-147 ~]# mkdir -p $HOME/.kube
  3. [root@k8s-5-147 ~]# scp k8s-5-146.k8s.host:/etc/kubernetes/admin.conf $HOME/.kube/config
  4. [root@k8s-5-147 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
  5. # 加入集群
  6. [root@k8s-5-147 ~]# kubeadm join 192.168.5.146:6443 --token abcdef.0123456789abcdef \
  7. --discovery-token-ca-cert-hash sha256:a025044360496164420da8350ba17f5acf9ab6444398707f8cef568b8920059a
  8. [preflight] Running pre-flight checks
  9. [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.6. Latest validated version: 19.03
  10. [preflight] Reading configuration from the cluster...
  11. [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
  12. [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
  13. [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
  14. [kubelet-start] Starting the kubelet
  15. [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
  16. This node has joined the cluster:
  17. * Certificate signing request was sent to apiserver and a response was received.
  18. * The Kubelet was informed of the new secure connection details.
  19. Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

join 命令 如果忘记了上面的 join 命令可以使用命令 kubeadm token create --print-join-command 重新获取。

查看节点

  1. [root@k8s-5-146 ~]# kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-5-146.k8s.host NotReady master 17m v1.19.9
  4. k8s-5-147.k8s.host NotReady <none> 16s v1.19.9
  5. k8s-5-148.k8s.host NotReady <none> 4s v1.19.9

可以看到是 NotReady 状态,这是因为还没有安装网络插件,接下来安装网络插件,可以在文档 https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/ 中选择我们自己的网络插件,这里我们安装 flannel:

六、安装网络插件 flannel

  1. [root@k8s-5-146 ~]# cd /opt/k8s
  2. [root@k8s-5-146 k8s]# wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  3. [root@k8s-5-146 k8s]# kubectl apply -f kube-flannel.yml
  4. podsecuritypolicy.policy/psp.flannel.unprivileged created
  5. clusterrole.rbac.authorization.k8s.io/flannel created
  6. clusterrolebinding.rbac.authorization.k8s.io/flannel created
  7. serviceaccount/flannel created
  8. configmap/kube-flannel-cfg created
  9. daemonset.apps/kube-flannel-ds created
  10. [root@k8s-5-146 k8s]# kubectl get nodes
  11. NAME STATUS ROLES AGE VERSION
  12. k8s-5-146.k8s.host Ready master 46m v1.19.9
  13. k8s-5-147.k8s.host Ready <none> 29m v1.19.9
  14. k8s-5-148.k8s.host Ready <none> 29m v1.19.9
  15. # 现在全部都是Ready状态啦