部署 Kubernetes Workers 节点

本部分将会部署三个 Kubernetes Worker 节点。每个节点上将会安装以下服务:

事前準备

以下命令需要在所有 worker 节点上面都运行一遍,包括 worker-0, worker-1worker-2。可以使用 gcloud 命令登录到 worker 节点上,比如

  1. gcloud compute ssh worker-0

部署 Kubernetes Worker 节点

安装 OS 依赖组件:

  1. sudo apt-get -y install socat

socat 命令用于支持 kubectl port-forward 命令。

下载并安装 worker 二进制文件

  1. wget -q --show-progress --https-only --timestamping \
  2. https://github.com/containernetworking/plugins/releases/download/v0.6.0/cni-plugins-amd64-v0.6.0.tgz \
  3. https://github.com/kubernetes-incubator/cri-containerd/releases/download/v1.0.0-beta.0/cri-containerd-1.0.0-beta.0.linux-amd64.tar.gz \
  4. https://storage.googleapis.com/kubernetes-release/release/v1.9.0/bin/linux/amd64/kubectl \
  5. https://storage.googleapis.com/kubernetes-release/release/v1.9.0/bin/linux/amd64/kube-proxy \
  6. https://storage.googleapis.com/kubernetes-release/release/v1.9.0/bin/linux/amd64/kubelet

创建安装目录:

  1. sudo mkdir -p \
  2. /etc/cni/net.d \
  3. /opt/cni/bin \
  4. /var/lib/kubelet \
  5. /var/lib/kube-proxy \
  6. /var/lib/kubernetes \
  7. /var/run/kubernetes

安装 worker 二进制文件

  1. sudo tar -xvf cni-plugins-amd64-v0.6.0.tgz -C /opt/cni/bin/
  2. sudo tar -xvf cri-containerd-1.0.0-beta.0.linux-amd64.tar.gz -C /
  3. chmod +x kubectl kube-proxy kubelet
  4. sudo mv kubectl kube-proxy kubelet /usr/local/bin/

配置 CNI 网路

查询当前计算节点的 Pod CIDR 范围:

  1. POD_CIDR=$(curl -s -H "Metadata-Flavor: Google" \
  2. http://metadata.google.internal/computeMetadata/v1/instance/attributes/pod-cidr)

生成 bridge 网络插件配置文件

  1. cat > 10-bridge.conf <<EOF
  2. {
  3. "cniVersion": "0.3.1",
  4. "name": "bridge",
  5. "type": "bridge",
  6. "bridge": "cnio0",
  7. "isGateway": true,
  8. "ipMasq": true,
  9. "ipam": {
  10. "type": "host-local",
  11. "ranges": [
  12. [{"subnet": "${POD_CIDR}"}]
  13. ],
  14. "routes": [{"dst": "0.0.0.0/0"}]
  15. }
  16. }
  17. EOF

生成 loopback 网络插件配置文件

  1. cat > 99-loopback.conf <<EOF
  2. {
  3. "cniVersion": "0.3.1",
  4. "type": "loopback"
  5. }
  6. EOF

将网络插件配置文件移动到 CNI 配置目录中:

  1. sudo mv 10-bridge.conf 99-loopback.conf /etc/cni/net.d/

配置 Kubelet

  1. sudo mv ${HOSTNAME}-key.pem ${HOSTNAME}.pem /var/lib/kubelet/
  2. sudo mv ${HOSTNAME}.kubeconfig /var/lib/kubelet/kubeconfig
  3. sudo mv ca.pem /var/lib/kubernetes/

生成 kubelet.service systemd 配置文件:

  1. cat > kubelet.service <<EOF
  2. [Unit]
  3. Description=Kubernetes Kubelet
  4. Documentation=https://github.com/kubernetes/kubernetes
  5. After=cri-containerd.service
  6. Requires=cri-containerd.service
  7. [Service]
  8. ExecStart=/usr/local/bin/kubelet \\
  9. --allow-privileged=true \\
  10. --anonymous-auth=false \\
  11. --authorization-mode=Webhook \\
  12. --client-ca-file=/var/lib/kubernetes/ca.pem \\
  13. --cloud-provider= \\
  14. --cluster-dns=10.32.0.10 \\
  15. --cluster-domain=cluster.local \\
  16. --container-runtime=remote \\
  17. --container-runtime-endpoint=unix:///var/run/cri-containerd.sock \\
  18. --image-pull-progress-deadline=2m \\
  19. --kubeconfig=/var/lib/kubelet/kubeconfig \\
  20. --network-plugin=cni \\
  21. --pod-cidr=${POD_CIDR} \\
  22. --register-node=true \\
  23. --runtime-request-timeout=15m \\
  24. --tls-cert-file=/var/lib/kubelet/${HOSTNAME}.pem \\
  25. --tls-private-key-file=/var/lib/kubelet/${HOSTNAME}-key.pem \\
  26. --v=2
  27. Restart=on-failure
  28. RestartSec=5
  29. [Install]
  30. WantedBy=multi-user.target
  31. EOF

配置 Kube-Proxy

  1. sudo mv kube-proxy.kubeconfig /var/lib/kube-proxy/kubeconfig

生成 kube-proxy.service systemd 配置文件:

  1. cat > kube-proxy.service <<EOF
  2. [Unit]
  3. Description=Kubernetes Kube Proxy
  4. Documentation=https://github.com/kubernetes/kubernetes
  5. [Service]
  6. ExecStart=/usr/local/bin/kube-proxy \\
  7. --cluster-cidr=10.200.0.0/16 \\
  8. --kubeconfig=/var/lib/kube-proxy/kubeconfig \\
  9. --proxy-mode=iptables \\
  10. --v=2
  11. Restart=on-failure
  12. RestartSec=5
  13. [Install]
  14. WantedBy=multi-user.target
  15. EOF

启动 worker 服务

  1. sudo mv kubelet.service kube-proxy.service /etc/systemd/system/
  2. sudo systemctl daemon-reload
  3. sudo systemctl enable containerd cri-containerd kubelet kube-proxy
  4. sudo systemctl start containerd cri-containerd kubelet kube-proxy

记得在所有 worker 节点上面都运行一遍,包括 worker-0, worker-1worker-2

验证

登入任意一台控制节点:

  1. gcloud compute ssh controller-0

列出目前已注册的 Kubernetes 节点:

  1. kubectl get nodes

输出为

  1. NAME STATUS ROLES AGE VERSION
  2. worker-0 Ready <none> 18s v1.9.0
  3. worker-1 Ready <none> 18s v1.9.0
  4. worker-2 Ready <none> 18s v1.9.0

下一步:配置 Kubectl