67. 集群升级

由于课程中的集群版本是 v1.10.0,这个版本相对有点旧了,最新版本都已经 v1.14.x 了,为了尽量保证课程内容的更新度,所以我们需要将集群版本更新。我们的集群是使用的 kubeadm 搭建的,我们知道使用 kubeadm 搭建的集群来更新是非常方便的,但是由于我们这里版本跨度太大,不能直接从 1.10.x 更新到 1.14.x,kubeadm 的更新是不支持跨多个主版本的,所以我们现在是 1.10,只能更新到 1.11 版本了,然后再重 1.11 更新到 1.12…… 不过版本更新的方式方法基本上都是一样的,所以后面要更新的话也挺简单了,下面我们就先将集群更新到 v1.11.0 版本。

更新集群

首先我们保留 kubeadm config 文件:

  1. $ kubeadm config view
  2. api:
  3. advertiseAddress: 10.151.30.11
  4. bindPort: 6443
  5. controlPlaneEndpoint: ""
  6. auditPolicy:
  7. logDir: /var/log/kubernetes/audit
  8. logMaxAge: 2
  9. path: ""
  10. authorizationModes:
  11. - Node
  12. - RBAC
  13. certificatesDir: /etc/kubernetes/pki
  14. cloudProvider: ""
  15. criSocket: /var/run/dockershim.sock
  16. etcd:
  17. caFile: ""
  18. certFile: ""
  19. dataDir: /var/lib/etcd
  20. endpoints: null
  21. image: ""
  22. keyFile: ""
  23. imageRepository: k8s.gcr.io
  24. kubeProxy:
  25. config:
  26. bindAddress: 0.0.0.0
  27. clientConnection:
  28. acceptContentTypes: ""
  29. burst: 10
  30. contentType: application/vnd.kubernetes.protobuf
  31. kubeconfig: /var/lib/kube-proxy/kubeconfig.conf
  32. qps: 5
  33. clusterCIDR: 10.244.0.0/16
  34. configSyncPeriod: 15m0s
  35. conntrack:
  36. max: null
  37. maxPerCore: 32768
  38. min: 131072
  39. tcpCloseWaitTimeout: 1h0m0s
  40. tcpEstablishedTimeout: 24h0m0s
  41. enableProfiling: false
  42. healthzBindAddress: 0.0.0.0:10256
  43. hostnameOverride: ""
  44. iptables:
  45. masqueradeAll: false
  46. masqueradeBit: 14
  47. minSyncPeriod: 0s
  48. syncPeriod: 30s
  49. ipvs:
  50. minSyncPeriod: 0s
  51. scheduler: ""
  52. syncPeriod: 30s
  53. metricsBindAddress: 127.0.0.1:10249
  54. mode: ""
  55. nodePortAddresses: null
  56. oomScoreAdj: -999
  57. portRange: ""
  58. resourceContainer: /kube-proxy
  59. udpIdleTimeout: 250ms
  60. kubeletConfiguration: {}
  61. kubernetesVersion: v1.10.0
  62. networking:
  63. dnsDomain: cluster.local
  64. podSubnet: 10.244.0.0/16
  65. serviceSubnet: 10.96.0.0/12
  66. nodeName: ydzs-master
  67. privilegedPods: false
  68. token: ""
  69. tokenGroups:
  70. - system:bootstrappers:kubeadm:default-node-token
  71. tokenTTL: 24h0m0s
  72. tokenUsages:
  73. - signing
  74. - authentication
  75. unifiedControlPlaneImage: ""

将上面的imageRepository值更改为:gcr.azk8s.cn/google_containers,然后保存内容到文件 kubeadm-config.yaml 中(当然如果你的集群可以获取到 grc.io 的镜像可以不用更改)。

然后更新 kubeadm:

  1. $ yum makecache fast && yum install -y kubeadm-1.11.0-0 kubectl-1.11.0-0
  2. $ kubeadm version
  3. kubeadm version: &version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.0", GitCommit:"91e7b4fd31fcd3d5f436da26c980becec37ceefe", GitTreeState:"clean", BuildDate:"2018-06-27T20:14:41Z", GoVersion:"go1.10.2", Compiler:"gc", Platform:"linux/amd64"}

因为 kubeadm upgrade plan 命令执行过程中会去 dl.k8s.io 获取版本信息,这个地址是需要科学方法才能访问的,所以我们可以先将 kubeadm 更新到目标版本,然后就可以查看到目标版本升级的一些信息了。

执行 upgrade plan 命令查看是否可以升级:

  1. $ kubeadm upgrade plan
  2. [preflight] Running pre-flight checks.
  3. [upgrade] Making sure the cluster is healthy:
  4. [upgrade/config] Making sure the configuration is correct:
  5. [upgrade/config] Reading configuration from the cluster...
  6. [upgrade/config] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
  7. I0518 18:50:12.844665 9676 feature_gate.go:230] feature gates: &{map[]}
  8. [upgrade] Fetching available versions to upgrade to
  9. [upgrade/versions] Cluster version: v1.10.0
  10. [upgrade/versions] kubeadm version: v1.11.0
  11. [upgrade/versions] WARNING: Couldn't fetch latest stable version from the internet: unable to get URL "https://dl.k8s.io/release/stable.txt": Get https://dl.k8s.io/release/stable.txt: dial tcp 35.201.71.162:443: i/o timeout
  12. [upgrade/versions] WARNING: Falling back to current kubeadm version as latest stable version
  13. [upgrade/versions] WARNING: Couldn't fetch latest version in the v1.10 series from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.10.txt": Get https://dl.k8s.io/release/stable-1.10.txt: dial tcp 35.201.71.162:443: i/o timeout
  14. Components that must be upgraded manually after you have upgraded the control plane with 'kubeadm upgrade apply':
  15. COMPONENT CURRENT AVAILABLE
  16. Kubelet 3 x v1.10.0 v1.11.0
  17. Upgrade to the latest stable version:
  18. COMPONENT CURRENT AVAILABLE
  19. API Server v1.10.0 v1.11.0
  20. Controller Manager v1.10.0 v1.11.0
  21. Scheduler v1.10.0 v1.11.0
  22. Kube Proxy v1.10.0 v1.11.0
  23. CoreDNS 1.1.3
  24. Kube DNS 1.14.8
  25. Etcd 3.1.12 3.2.18
  26. You can now apply the upgrade by executing the following command:
  27. kubeadm upgrade apply v1.11.0
  28. _____________________________________________________________________

我们可以先使用 dry-run 命令查看升级信息:

  1. $ kubeadm upgrade apply v1.11.0 --config kubeadm-config.yaml --dry-run

注意要通过--config指定上面保存的配置文件,该配置文件信息包含了上一个版本的集群信息以及修改搞得镜像地址。

查看了上面的升级信息确认无误后就可以执行升级操作了:

  1. $ kubeadm upgrade apply v1.11.0 --config kubeadm-config.yaml
  2. kubeadm upgrade apply v1.11.0 --config kubeadm-config.yaml
  3. [preflight] Running pre-flight checks.
  4. I0518 18:57:29.134722 12284 feature_gate.go:230] feature gates: &{map[]}
  5. [upgrade] Making sure the cluster is healthy:
  6. [upgrade/config] Making sure the configuration is correct:
  7. [upgrade/config] Reading configuration options from a file: kubeadm-config.yaml
  8. I0518 18:57:29.179231 12284 feature_gate.go:230] feature gates: &{map[]}
  9. [upgrade/apply] Respecting the --cri-socket flag that is set with higher priority than the config file.
  10. [upgrade/version] You have chosen to change the cluster version to "v1.11.0"
  11. [upgrade/versions] Cluster version: v1.10.0
  12. [upgrade/versions] kubeadm version: v1.11.0
  13. [upgrade/confirm] Are you sure you want to proceed with the upgrade? [y/N]: y
  14. [upgrade/prepull] Will prepull images for components [kube-apiserver kube-controller-manager kube-scheduler etcd]
  15. [upgrade/apply] Upgrading your Static Pod-hosted control plane to version "v1.11.0"...
  16. Static pod: kube-apiserver-ydzs-master hash: 3abd7df4382a9b60f60819f84de40e11
  17. Static pod: kube-controller-manager-ydzs-master hash: 1a0f3ccde96238d31012390b61109573
  18. Static pod: kube-scheduler-ydzs-master hash: 2acb197d598c4730e3f5b159b241a81b

隔一段时间看到如下信息就证明集群升级成功了:

  1. ......
  2. [bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
  3. [bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
  4. [addons] Applied essential addon: CoreDNS
  5. [addons] Applied essential addon: kube-proxy
  6. [upgrade/successful] SUCCESS! Your cluster was upgraded to "v1.11.0". Enjoy!
  7. [upgrade/kubelet] Now that your control plane is upgraded, please proceed with upgrading your kubelets if you haven't already done so.

由于上面我们已经更新过 kubectl 了,现在我们用 kubectl 来查看下版本信息:

  1. $ kubectl version
  2. Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.0", GitCommit:"91e7b4fd31fcd3d5f436da26c980becec37ceefe", GitTreeState:"clean", BuildDate:"2018-06-27T20:17:28Z", GoVersion:"go1.10.2", Compiler:"gc", Platform:"linux/amd64"}
  3. Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.0", GitCommit:"91e7b4fd31fcd3d5f436da26c980becec37ceefe", GitTreeState:"clean", BuildDate:"2018-06-27T20:08:34Z", GoVersion:"go1.10.2", Compiler:"gc", Platform:"linux/amd64"}

可以看到现在 Server 端和 Client 端都已经是 v1.11.0 版本了,然后查看下 Pod 信息:

  1. $ kubectl get pods -n kube-system
  2. NAME READY STATUS RESTARTS AGE
  3. authproxy-oauth2-proxy-798cff85fc-pc8x5 1/1 Running 0 34d
  4. cert-manager-796fb45d79-wcrfp 1/1 Running 2 34d
  5. coredns-7f6746b7f-2cs2x 1/1 Running 0 5m
  6. coredns-7f6746b7f-clphf 1/1 Running 0 5m
  7. etcd-ydzs-master 1/1 Running 0 10m
  8. kube-apiserver-ydzs-master 1/1 Running 0 7m
  9. kube-controller-manager-ydzs-master 1/1 Running 0 7m
  10. kube-flannel-ds-amd64-jxzq9 1/1 Running 8 64d
  11. kube-flannel-ds-amd64-r56r9 1/1 Running 3 64d
  12. kube-flannel-ds-amd64-xw9fx 1/1 Running 2 64d
  13. kube-proxy-gqvdg 1/1 Running 0 3m
  14. kube-proxy-sn7xb 1/1 Running 0 3m
  15. kube-proxy-vbrr7 1/1 Running 0 2m
  16. kube-scheduler-ydzs-master 1/1 Running 0 6m
  17. nginx-ingress-controller-587b4c68bf-vsqgm 1/1 Running 2 34d
  18. nginx-ingress-default-backend-64fd9fd685-lmxhw 1/1 Running 1 34d
  19. tiller-deploy-847cfb9744-5cvh8 1/1 Running 0 4d

更新 kubelet

可以看到我们之前的 kube-dns 服务已经被 coredns 取代了,这是因为在 v1.11.0 版本后就默认使用 coredns 了,我们也可以访问下集群中的服务看是否有影响,然后查看下集群的 Node 信息:

  1. $ kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. ydzs-master Ready master 64d v1.10.0
  4. ydzs-node1 Ready <none> 64d v1.10.0
  5. ydzs-node2 Ready <none> 64d v1.10.0

可以看到版本并没有更新,这是因为节点上的 kubelet 还没有更新的,我们可以通过 kubelet 查看下版本:

  1. $ kubelet --version
  2. Kubernetes v1.10.0

这个时候我们去手动更新下 kubelet:

  1. $ yum install -y kubelet-1.11.0-0
  2. # 安装完成后查看下版本
  3. $ kubelet --version
  4. Kubernetes v1.11.0
  5. # 然后重启 kubelet 服务
  6. $ systemctl daemon-reload
  7. $ systemctl restart kubelet
  8. $ kubectl get nodes
  9. NAME STATUS ROLES AGE VERSION
  10. ydzs-master Ready master 64d v1.11.0
  11. ydzs-node1 Ready <none> 64d v1.10.0
  12. ydzs-node2 Ready <none> 64d v1.10.0

注意事项:

  • 如果节点上 swap 没有关掉重启 kubelet 服务会报错,所以最好是关掉 swap,执行命令:swapoff -a 即可。
  • 1.11.0 版本的 kubelet 默认使用的pod-infra-container-image镜像名称为:k8s.gcr.io/pause:3.1,所以最好先提前查看下集群节点上是否有这个镜像,因为我们之前 1.10.0 版本的集群默认的名字为k8s.gcr.io/pause-amd64:3.1,所以如果节点上还是之前的 pause 镜像的话,需要先重新打下镜像 tag:
  1. $ docker tag k8s.gcr.io/pause-amd64:3.1 k8s.gcr.io/pause:3.1

没有的话可以提前下载到节点上也可以通过配置参数进行指定,在文件/var/lib/kubelet/kubeadm-flags.env中添加如下参数信息:

  1. KUBELET_KUBEADM_ARGS=--cgroup-driver=cgroupfs --cni-bin-dir=/opt/cni/bin --cni-conf-dir=/etc/cni/net.d --network-plugin=cni --pod-infra-container-image=cnych/pause-amd64:3.1

可以看到我们更新了 kubelet 的节点版本信息已经更新了,同样的方式去把另外两个节点 kubelet 更新即可。

另外需要注意的是最好在节点上的 kubelet 更新之前将节点设置为不可调度,更新完成后再设置回来,可以避免不必要的错误。

最后看下升级后的集群:

  1. $ kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. ydzs-master Ready master 64d v1.11.0
  4. ydzs-node1 Ready <none> 64d v1.11.0
  5. ydzs-node2 Ready <none> 64d v1.11.0

到这里我们的集群就升级成功了,我们可以用同样的方法将集群升级到 v1.12.x、v1.13.x、v1.14.x 版本,而且升级过程中是不会影响到现有业务的。