部署规划

主节点 172.16.32.131 k8s-master
从节点1 172.16.32.132 k8s-slave-1
从节点2 172.16.32.133 k8s-slave-2

环境准备

关闭防火墙

  1. systemctl stop firewalld
  2. systemctl disable firewalld

关闭swap空间

  1. swapoff -a # 临时关闭
  2. free -h # 查看交换空间是否关闭
  3. # 永久关闭
  4. vi /etc/fstab
  5. # 将swap那一行进行注释即可

修改ip地址

  1. vim /etc/sysconfig/network-scirpts/ifcfg-ens33
  2. TYPE=Ethernet
  3. PROXY_METHOD=none
  4. BROWSER_ONLY=no
  5. BOOTPROTO=static
  6. DEFROUTE=yes
  7. IPV4_FAILURE_FATAL=no
  8. IPV6INIT=yes
  9. IPV6_AUTOCONF=yes
  10. IPV6_DEFROUTE=yes
  11. IPV6_FAILURE_FATAL=no
  12. IPV6_ADDR_GEN_MODE=stable-privacy
  13. NAME=ens33
  14. UUID=ea105e65-d671-45be-955b-145a78bd2fae
  15. DEVICE=ens33
  16. IPADDR=172.16.32.130
  17. GATEWAY=172.16.32.2
  18. DNS1=172.16.32.2
  19. ONBOOT=yes

修改ip主机映射

  1. 172.16.32.131 k8s-master
  2. 172.16.32.132 k8s-slave-1
  3. 172.16.32.133 k8s-slave-2

安装Docker

  1. wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O/etc/yum.repos.d/docker-ce.repo
  2. yum -y install docker-ce-18.06.1.ce-3.el7

启动Docker

  1. [root@localhost ~]# systemctl enable docker
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
  3. [root@localhost ~]# systemctl start docker

添加k8s的软件源

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

更新缓存

  1. yum clean all && yum makecache

安装Kubectl,kubelet,kubeadm

  1. yum install -y kubelet kubectl kubeadm

如果出现报错公钥未安装,如下的报错的话

  1. efd73a4178ebf9939f86b4200dba0247a57ead65f2403d8576b241faf478ac42-kubectl-1.18.8-0.x86_64.rpm 的公钥尚未安装
  2. 失败的软件包是:kubectl-1.18.8-0.x86_64
  3. GPG 密钥配置为:https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg

执行如下的命令,安装公钥即可

  1. wget https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
  2. rpm --import yum-key.gpg
  3. wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  4. rpm --import rpm-package-key.gpg

开机自启动kubelet

  1. systemctl enable kubelet

安装Kubernetes

1. 将上述环境准备好的虚拟机,复制两台,作为slave

克隆结果如下

image.png

修改机器的ip地址

按照部署规划上的ip地址进行修改

2. 部署k8s-master

输入如下命令,初始化

  1. kubeadm init \
  2. --apiserver-advertise-address=172.16.32.131 \
  3. --image-repository registry.aliyuncs.com/google_containers \
  4. --kubernetes-version v1.18.0 \
  5. --service-cidr=10.1.0.0/16 \
  6. --pod-network-cidr=10.244.0.0/16

详细输出如下

  1. [root@k8s-master ~]# kubeadm init \
  2. > --apiserver-advertise-address=172.16.32.131 \
  3. > --image-repository registry.aliyuncs.com/google_containers \
  4. > --kubernetes-version v1.18.0 \
  5. > --service-cidr=10.1.0.0/16 \
  6. > --pod-network-cidr=10.244.0.0/16
  7. W0819 13:21:43.316491 21757 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
  8. [init] Using Kubernetes version: v1.18.0
  9. [preflight] Running pre-flight checks
  10. [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
  11. [preflight] Pulling images required for setting up a Kubernetes cluster
  12. [preflight] This might take a minute or two, depending on the speed of your internet connection
  13. [preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
  14. [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
  15. [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
  16. [kubelet-start] Starting the kubelet
  17. [certs] Using certificateDir folder "/etc/kubernetes/pki"
  18. [certs] Generating "ca" certificate and key
  19. [certs] Generating "apiserver" certificate and key
  20. [certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.1.0.1 172.16.32.131]
  21. [certs] Generating "apiserver-kubelet-client" certificate and key
  22. [certs] Generating "front-proxy-ca" certificate and key
  23. [certs] Generating "front-proxy-client" certificate and key
  24. [certs] Generating "etcd/ca" certificate and key
  25. [certs] Generating "etcd/server" certificate and key
  26. [certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [172.16.32.131 127.0.0.1 ::1]
  27. [certs] Generating "etcd/peer" certificate and key
  28. [certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [172.16.32.131 127.0.0.1 ::1]
  29. [certs] Generating "etcd/healthcheck-client" certificate and key
  30. [certs] Generating "apiserver-etcd-client" certificate and key
  31. [certs] Generating "sa" key and public key
  32. [kubeconfig] Using kubeconfig folder "/etc/kubernetes"
  33. [kubeconfig] Writing "admin.conf" kubeconfig file
  34. [kubeconfig] Writing "kubelet.conf" kubeconfig file
  35. [kubeconfig] Writing "controller-manager.conf" kubeconfig file
  36. [kubeconfig] Writing "scheduler.conf" kubeconfig file
  37. [control-plane] Using manifest folder "/etc/kubernetes/manifests"
  38. [control-plane] Creating static Pod manifest for "kube-apiserver"
  39. [control-plane] Creating static Pod manifest for "kube-controller-manager"
  40. W0819 13:22:01.791049 21757 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
  41. [control-plane] Creating static Pod manifest for "kube-scheduler"
  42. W0819 13:22:01.791825 21757 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
  43. [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
  44. [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
  45. [apiclient] All control plane components are healthy after 22.007752 seconds
  46. [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
  47. [kubelet] Creating a ConfigMap "kubelet-config-1.18" in namespace kube-system with the configuration for the kubelets in the cluster
  48. [upload-certs] Skipping phase. Please see --upload-certs
  49. [mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
  50. [mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
  51. [bootstrap-token] Using token: dst7dd.l0z5rm6p6mubwmao
  52. [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
  53. [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
  54. [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
  55. [bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
  56. [bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
  57. [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
  58. [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
  59. [addons] Applied essential addon: CoreDNS
  60. [addons] Applied essential addon: kube-proxy
  61. Your Kubernetes control-plane has initialized successfully!
  62. To start using your cluster, you need to run the following as a regular user:
  63. mkdir -p $HOME/.kube
  64. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  65. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  66. You should now deploy a pod network to the cluster.
  67. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  68. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  69. Then you can join any number of worker nodes by running the following on each as root:
  70. kubeadm join 172.16.32.131:6443 --token dst7dd.l0z5rm6p6mubwmao \
  71. --discovery-token-ca-cert-hash sha256:c4012f88574ff04c9de7199f3e3550cd4a5b0b328e08e92f636f2a199400899f

执行如下的命令

  1. mkdir -p $HOME/.kube
  2. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. sudo chown $(id -u):$(id -g) $HOME/.kube/config

查看节点的情况

  1. [root@k8s-master ~]# kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master NotReady master 3m44s v1.18.8

3. Master节点,安装网络插件

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

如果上述的连接不行,请用如下的连接

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

执行输出如下

  1. podsecuritypolicy.policy/psp.flannel.unprivileged created
  2. clusterrole.rbac.authorization.k8s.io/flannel configured
  3. clusterrolebinding.rbac.authorization.k8s.io/flannel unchanged
  4. serviceaccount/flannel unchanged
  5. configmap/kube-flannel-cfg configured
  6. daemonset.apps/kube-flannel-ds-amd64 created
  7. daemonset.apps/kube-flannel-ds-arm64 created
  8. daemonset.apps/kube-flannel-ds-arm created
  9. daemonset.apps/kube-flannel-ds-ppc64le created
  10. daemonset.apps/kube-flannel-ds-s390x created

查看是否部署成功

  1. [root@k8s-master ~]# kubectl get pods -n kube-system
  2. NAME READY STATUS RESTARTS AGE
  3. coredns-7ff77c879f-8s8dg 1/1 Running 0 12m
  4. coredns-7ff77c879f-dpxk6 1/1 Running 0 12m
  5. etcd-k8s-master 1/1 Running 0 13m
  6. kube-apiserver-k8s-master 1/1 Running 0 13m
  7. kube-controller-manager-k8s-master 1/1 Running 0 13m
  8. kube-flannel-ds-amd64-q88b8 1/1 Running 0 103s
  9. kube-proxy-fxgvg 1/1 Running 0 12m
  10. kube-scheduler-k8s-master 1/1 Running 0 13m

查看Master节点的状态,可以看出,已经ready

  1. [root@k8s-master ~]# kubectl get nodes;
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master Ready master 14m v1.18.8

4. Slave节点加入集群

执行在 初始化Master节点 最后输出的命令

  1. kubeadm join 172.16.32.131:6443 --token dst7dd.l0z5rm6p6mubwmao \
  2. --discovery-token-ca-cert-hash sha256:c4012f88574ff04c9de7199f3e3550cd4a5b0b328e08e92f636f2a199400899f

详细输出如下

  1. [root@k8s-slave-1 ~]# kubeadm join 172.16.32.131:6443 --token dst7dd.l0z5rm6p6mubwmao \
  2. > --discovery-token-ca-cert-hash sha256:c4012f88574ff04c9de7199f3e3550cd4a5b0b328e08e92f636f2a199400899f
  3. W0819 13:39:06.164705 22157 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set.
  4. [preflight] Running pre-flight checks
  5. [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
  6. [preflight] Reading configuration from the cluster...
  7. [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
  8. [kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.18" ConfigMap in the kube-system namespace
  9. [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
  10. [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
  11. [kubelet-start] Starting the kubelet
  12. [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
  13. This node has joined the cluster:
  14. * Certificate signing request was sent to apiserver and a response was received.
  15. * The Kubelet was informed of the new secure connection details.
  16. Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

在Master节点上,查看节点的状态

如果看到如下的输出结果,三个ready,即集群部署成功

  1. [root@k8s-master ~]# kubectl get nodes;
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master Ready master 22m v1.18.8
  4. k8s-slave-1 Ready <none> 5m34s v1.18.8
  5. k8s-slave-2 Ready <none> 4m9s v1.18.8

k8s集群搭建完成

参考文档: https://www.cnblogs.com/caoxb/p/11243472.html