环境准备
3个节点,都是 Centos 7.6 系统,内核版本:3.10.0-957.12.2.el7.x86_64,在每个节点上添加 hosts 信息:
$ cat /etc/hosts

  1. 172.16.1.128 k8s-master
  2. 172.16.1.129 k8s-node01
  3. 172.16.1.130 k8s-node02

禁用防火墙:

  1. $ systemctl stop firewalld
  2. $ systemctl disable firewalld

禁用SELINUX:

  1. $ setenforce 0
  2. $ cat /etc/selinux/config
  3. SELINUX=disabled

创建/etc/sysctl.d/k8s.conf文件,添加如下内容:

  1. net.bridge.bridge-nf-call-ip6tables = 1
  2. net.bridge.bridge-nf-call-iptables = 1
  3. net.ipv4.ip_forward = 1

执行如下命令使修改生效:

  1. $ modprobe br_netfilter
  2. $ sysctl -p /etc/sysctl.d/k8s.conf

安装 ipvs

  1. $ cat > /etc/sysconfig/modules/ipvs.modules <<EOF
  2. #!/bin/bash
  3. modprobe -- ip_vs
  4. modprobe -- ip_vs_rr
  5. modprobe -- ip_vs_wrr
  6. modprobe -- ip_vs_sh
  7. modprobe -- nf_conntrack_ipv4
  8. EOF
  9. $ chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

上面脚本创建了的/etc/sysconfig/modules/ipvs.modules文件,保证在节点重启后能自动加载所需模块。使用lsmod | grep -e ip_vs -e nf_conntrack_ipv4命令查看是否已经正确加载所需的内核模块。
接下来还需要确保各个节点上已经安装了 ipset 软件包:

  1. $ yum install ipset -y

为了便于查看 ipvs 的代理规则,最好安装一下管理工具 ipvsadm:

  1. $ yum install ipvsadm -y

同步服务器时间

  1. $ yum install chrony -y
  2. $ systemctl enable chronyd
  3. $ systemctl start chronyd
  4. $ chronyc sources

关闭 swap 分区:

  1. $ swapoff -a

修改/etc/fstab文件,注释掉 SWAP 的自动挂载,使用free -m确认 swap 已经关闭。swappiness 参数调整,修改/etc/sysctl.d/k8s.conf添加下面一行:

  1. vm.swappiness=0

执行sysctl -p /etc/sysctl.d/k8s.conf使修改生效。
接下来可以安装 Docker

  1. $ yum install -y yum-utils \
  2. device-mapper-persistent-data \
  3. lvm2
  4. $ yum-config-manager \
  5. --add-repo \
  6. https://download.docker.com/linux/centos/docker-ce.repo
  7. $ yum list docker-ce --showduplicates | sort -r

可以选择安装一个版本,比如我们这里安装最新版本:

  1. $ yum install docker-ce-18.09.8-3.el7 -y

配置 Docker 镜像加速器
$ vi /etc/docker/daemon.json

  1. {
  2. "exec-opts": ["native.cgroupdriver=systemd"],
  3. "registry-mirrors" : [
  4. "https://ot2k4d59.mirror.aliyuncs.com/"
  5. ]
  6. }

启动 Docker

  1. $ systemctl start docker
  2. $ systemctl enable docker

在确保 Docker 安装完成后,上面的相关环境配置也完成了,现在我们就可以来安装 Kubeadm 了,我们这里是通过指定yum 源的方式来进行安装的:

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

当然了,上面的 yum 源是需要科学上网的,如果不能科学上网的话,我们可以使用阿里云的源进行安装:

  1. 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(我安装的是最新版,有版本要求自己设定版本):

  1. $ yum install -y kubelet-1.17.2 kubeadm-1.17.2 kubectl-1.17.2 --disableexcludes=kubernetes

可以看到我们这里安装的是 v1.17.2 版本,然后将 kubelet 设置成开机启动:

  1. $ systemctl enable kubelet.service

到这里为止上面所有的操作都需要在所有节点执行配置。

初始化集群
然后接下来在 master 节点配置 kubeadm 初始化文件,可以通过如下命令导出默认的初始化配置:

  1. $ kubeadm config print init-defaults > kubeadm.yaml

然后根据我们自己的需求修改配置,比如修改 imageRepository 的值,kube-proxy 的模式为 ipvs,另外需要注意的是我们这里是准备安装 calico 网络插件的,需要将 networking.podSubnet 设置为192.168.0.0/16:

  1. apiVersion: kubeadm.k8s.io/v1beta2
  2. bootstrapTokens:
  3. - groups:
  4. - system:bootstrappers:kubeadm:default-node-token
  5. token: abcdef.0123456789abcdef
  6. ttl: 24h0m0s
  7. usages:
  8. - signing
  9. - authentication
  10. kind: InitConfiguration
  11. localAPIEndpoint:
  12. advertiseAddress: 172.16.1.128
  13. bindPort: 6443
  14. nodeRegistration:
  15. criSocket: /var/run/dockershim.sock
  16. name: k8s-master
  17. taints:
  18. - effect: NoSchedule
  19. key: node-role.kubernetes.io/master
  20. ---
  21. apiServer:
  22. timeoutForControlPlane: 4m0s
  23. apiVersion: kubeadm.k8s.io/v1beta2
  24. certificatesDir: /etc/kubernetes/pki
  25. clusterName: kubernetes
  26. controllerManager: {}
  27. dns:
  28. type: CoreDNS
  29. etcd:
  30. local:
  31. dataDir: /var/lib/etcd
  32. imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
  33. kind: ClusterConfiguration
  34. kubernetesVersion: v1.17.2
  35. networking:
  36. dnsDomain: cluster.local
  37. podSubnet: 192.168.0.0/16
  38. serviceSubnet: 10.96.0.0/12
  39. scheduler: {}
  40. ---
  41. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  42. kind: KubeProxyConfiguration
  43. mode: ipvs

然后使用上面的配置文件进行初始化:

  1. $ kubeadm init --config kubeadm.yaml
  2. [init] Using Kubernetes version: v1.17.2
  3. [preflight] Running pre-flight checks
  4. [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 19.03.1. Latest validated version: 18.09
  5. [preflight] Pulling images required for setting up a Kubernetes cluster
  6. [preflight] This might take a minute or two, depending on the speed of your internet connection
  7. [preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
  8. [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
  9. [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
  10. [kubelet-start] Activating the kubelet service
  11. [certs] Using certificateDir folder "/etc/kubernetes/pki"
  12. [certs] Generating "etcd/ca" certificate and key
  13. [certs] Generating "etcd/server" certificate and key
  14. [certs] etcd/server serving cert is signed for DNS names [ydzs-master localhost] and IPs [10.151.30.11 127.0.0.1 ::1]
  15. [certs] Generating "etcd/peer" certificate and key
  16. [certs] etcd/peer serving cert is signed for DNS names [ydzs-master localhost] and IPs [10.151.30.11 127.0.0.1 ::1]
  17. [certs] Generating "apiserver-etcd-client" certificate and key
  18. [certs] Generating "etcd/healthcheck-client" certificate and key
  19. [certs] Generating "ca" certificate and key
  20. [certs] Generating "apiserver" certificate and key
  21. [certs] apiserver serving cert is signed for DNS names [ydzs-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.151.30.11]
  22. [certs] Generating "apiserver-kubelet-client" certificate and key
  23. [certs] Generating "front-proxy-ca" certificate and key
  24. [certs] Generating "front-proxy-client" certificate and key
  25. [certs] Generating "sa" key and public key
  26. [kubeconfig] Using kubeconfig folder "/etc/kubernetes"
  27. [kubeconfig] Writing "admin.conf" kubeconfig file
  28. [kubeconfig] Writing "kubelet.conf" kubeconfig file
  29. [kubeconfig] Writing "controller-manager.conf" kubeconfig file
  30. [kubeconfig] Writing "scheduler.conf" kubeconfig file
  31. [control-plane] Using manifest folder "/etc/kubernetes/manifests"
  32. [control-plane] Creating static Pod manifest for "kube-apiserver"
  33. [control-plane] Creating static Pod manifest for "kube-controller-manager"
  34. [control-plane] Creating static Pod manifest for "kube-scheduler"
  35. [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
  36. [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
  37. [kubelet-check] Initial timeout of 40s passed.
  38. [apiclient] All control plane components are healthy after 42.012149 seconds
  39. [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
  40. [kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
  41. [upload-certs] Skipping phase. Please see --upload-certs
  42. [mark-control-plane] Marking the node ydzs-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
  43. [mark-control-plane] Marking the node ydzs-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
  44. [bootstrap-token] Using token: abcdef.0123456789abcdef
  45. [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
  46. [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
  47. [bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
  48. [bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
  49. [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
  50. [addons] Applied essential addon: CoreDNS
  51. [addons] Applied essential addon: kube-proxy
  52. Your Kubernetes control-plane has initialized successfully!
  53. To start using your cluster, you need to run the following as a regular user:
  54. mkdir -p $HOME/.kube
  55. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  56. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  57. You should now deploy a pod network to the cluster.
  58. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  59. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  60. Then you can join any number of worker nodes by running the following on each as root:
  61. kubeadm join 172.16.1.128:6443 --token abcdef.0123456789abcdef \
  62. --discovery-token-ca-cert-hash sha256:deb5158b39948a4592ff48512047ea6e45b288c248872724a28f15008962178b

可以看到最新验证的 docker 版本是18.09,虽然是一个 warning,所以最好还是安装18.09版本的 docker。

拷贝 kubeconfig 文件

  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

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

  1. kubeadm join 172.16.1.128:6443 --token abcdef.0123456789abcdef \
  2. --discovery-token-ca-cert-hash sha256:deb5158b39948a4592ff48512047ea6e45b288c248872724a28f15008962178b
  3. [preflight] Reading configuration from the cluster...
  4. [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
  5. [kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace
  6. [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
  7. [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
  8. [kubelet-start] Activating the kubelet service
  9. [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
  10. This node has joined the cluster:
  11. * Certificate signing request was sent to apiserver and a response was received.
  12. * The Kubelet was informed of the new secure connection details.
  13. Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

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

执行成功后运行 get nodes 命令:

  1. # kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master Ready master 3d18h v1.17.2
  4. k8s-node01 Ready node01 3d18h v1.17.2
  5. k8s-node02 Ready node02 3d17h v1.17.2

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

  1. $ wget https://docs.projectcalico.org/v3.8/manifests/calico.yaml

因为有节点是多网卡,所以需要在资源清单文件中指定内网网卡
$ vi calico.yaml

  1. ......
  2. spec:
  3. containers:
  4. - env:
  5. - name: DATASTORE_TYPE
  6. value: kubernetes
  7. - name: IP_AUTODETECTION_METHOD # DaemonSet中添加该环境变量
  8. value: interface=ens33 # 指定内网网卡
  9. - name: WAIT_FOR_DATASTORE
  10. value: "true"
  11. ......

$ kubectl apply -f calico.yaml # 安装calico网络插件

隔一会儿查看 Pod 运行状态:

  1. # kubectl get pod -n kube-system
  2. NAME READY STATUS RESTARTS AGE
  3. calico-kube-controllers-64d7686ff8-q4wwg 1/1 Running 0 10m
  4. calico-node-92b29 1/1 Running 0 3d17h
  5. calico-node-f8shx 1/1 Running 7 3d17h
  6. calico-node-jsk9r 1/1 Running 0 3d17h
  7. coredns-6cd559f5d5-448x5 1/1 Running 0 3d18h
  8. coredns-6cd559f5d5-tprrj 1/1 Running 0 3d18h
  9. etcd-k8s-master 1/1 Running 1 3d18h
  10. kube-apiserver-k8s-master 1/1 Running 4 3d18h
  11. kube-controller-manager-k8s-master 1/1 Running 18 3d18h
  12. kube-proxy-7hk2n 1/1 Running 1 3d18h
  13. kube-proxy-ffslj 1/1 Running 1 3d17h
  14. kube-proxy-txhcq 1/1 Running 1 3d18h
  15. kube-scheduler-k8s-master 1/1 Running 17 3d18h

网络插件运行成功了,node 状态也正常了:

  1. # kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master Ready master 3d18h v1.17.2
  4. k8s-node01 Ready node01 3d18h v1.17.2
  5. k8s-node02 Ready node02 3d17h v1.17.2

用同样的方法添加另外一个节点即可。

安装 Dashboard

  1. $ wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
  2. $ vi kubernetes-dashboard.yaml

修改镜像名称

  1. ......
  2. containers:
  3. - args:
  4. - --auto-generate-certificates
  5. image: gcr.azk8s.cn/google_containers/kubernetes-dashboard-amd64:v1.10.1
  6. imagePullPolicy: IfNotPresent
  7. ......

修改Service为NodePort类型

  1. ......
  2. selector:
  3. k8s-app: kubernetes-dashboard
  4. type: NodePort
  5. ......

直接创建:

  1. $ kubectl apply -f kubernetes-dashboard.yaml
  2. $ kubectl get pods -n kube-system -l k8s-app=kubernetes-dashboard
  3. NAME READY STATUS RESTARTS AGE
  4. kubernetes-dashboard-fcfb4cbc-t462n 1/1 Running 0 50m
  5. $ kubectl get svc -n kube-system -l k8s-app=kubernetes-dashboard
  6. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  7. kubernetes-dashboard NodePort 10.110.172.49 <none> 443:32497/TCP 55m

然后可以通过上面的 32497 端口去访问 Dashboard,要记住使用 https,Chrome不生效可以使用Firefox测试:
kubeadm搭建单master的K8S集群 - 图1
k8s dashboard login
然后创建一个具有全局所有权限的用户来登录Dashboard:(admin.yaml)

  1. kind: ClusterRoleBinding
  2. apiVersion: rbac.authorization.k8s.io/v1beta1
  3. metadata:
  4. name: admin
  5. annotations:
  6. rbac.authorization.kubernetes.io/autoupdate: "true"
  7. roleRef:
  8. kind: ClusterRole
  9. name: cluster-admin
  10. apiGroup: rbac.authorization.k8s.io
  11. subjects:
  12. - kind: ServiceAccount
  13. name: admin
  14. namespace: kube-system
  15. ---
  16. apiVersion: v1
  17. kind: ServiceAccount
  18. metadata:
  19. name: admin
  20. namespace: kube-system
  21. labels:
  22. kubernetes.io/cluster-service: "true"
  23. addonmanager.kubernetes.io/mode: Reconcile

直接创建:

  1. $ kubectl apply -f admin.yaml
  2. $ kubectl get secret -n kube-system|grep admin-token
  3. admin-token-d5jsg kubernetes.io/service-account-token 3 1d
  4. $ kubectl get secret admin-token-d5jsg -o jsonpath={.data.token} -n kube-system |base64 -d# 会生成一串很长的base64后的字符串

然后用上面的base64解码后的字符串作为token登录Dashboard即可:
最终我们就完成了使用 kubeadm 搭建 v1.17.2 版本的 kubernetes 集群、coredns、ipvs、calico。

配置命令自动补全

  1. yum install -y bash-completion
  2. source /usr/share/bash-completion/bash_completion
  3. source <(kubectl completion bash)
  4. echo "source <(kubectl completion bash)" >> ~/.bashrc