07. 验证集群功能

本文档验证 K8S 集群是否工作正常。

注意:如果没有特殊指明,本文档的所有操作均在 zhangjun-k8s-01 节点上执行,然后远程分发文件和执行命令。

检查节点状态

  1. $ kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. zhangjun-k8s-01 Ready <none> 15m v1.16.6
  4. zhangjun-k8s-02 Ready <none> 15m v1.16.6
  5. zhangjun-k8s-03 Ready <none> 15m v1.16.6

都为 Ready 且版本为 v1.16.6 时正常。

创建测试文件

  1. cd /opt/k8s/work
  2. cat > nginx-ds.yml <<EOF
  3. apiVersion: v1
  4. kind: Service
  5. metadata:
  6. name: nginx-ds
  7. labels:
  8. app: nginx-ds
  9. spec:
  10. type: NodePort
  11. selector:
  12. app: nginx-ds
  13. ports:
  14. - name: http
  15. port: 80
  16. targetPort: 80
  17. ---
  18. apiVersion: apps/v1
  19. kind: DaemonSet
  20. metadata:
  21. name: nginx-ds
  22. labels:
  23. addonmanager.kubernetes.io/mode: Reconcile
  24. spec:
  25. selector:
  26. matchLabels:
  27. app: nginx-ds
  28. template:
  29. metadata:
  30. labels:
  31. app: nginx-ds
  32. spec:
  33. containers:
  34. - name: my-nginx
  35. image: nginx:1.7.9
  36. ports:
  37. - containerPort: 80
  38. EOF

执行测试

  1. kubectl create -f nginx-ds.yml

检查各节点的 Pod IP 连通性

  1. $ kubectl get pods -o wide -l app=nginx-ds
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. nginx-ds-j7v5g 1/1 Running 0 61s 172.30.244.1 zhangjun-k8s-01 <none> <none>
  4. nginx-ds-js8g8 1/1 Running 0 61s 172.30.82.129 zhangjun-k8s-02 <none> <none>
  5. nginx-ds-n2p4x 1/1 Running 0 61s 172.30.184.130 zhangjun-k8s-03 <none> <none>

在所有 Node 上分别 ping 上面三个 Pod IP,看是否连通:

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh ${node_ip} "ping -c 1 172.30.244.1"
  6. ssh ${node_ip} "ping -c 1 172.30.82.129"
  7. ssh ${node_ip} "ping -c 1 172.30.184.130"
  8. done

检查服务 IP 和端口可达性

  1. $ kubectl get svc -l app=nginx-ds
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. nginx-ds NodePort 10.254.116.22 <none> 80:30562/TCP 2m7s

可见:

  • Service Cluster IP:10.254.116.22
  • 服务端口:80
  • NodePort 端口:30562

在所有 Node 上 curl Service IP:

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh ${node_ip} "curl -s 10.254.116.22"
  6. done

预期输出 nginx 欢迎页面内容。

检查服务的 NodePort 可达性

在所有 Node 上执行:

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh ${node_ip} "curl -s ${node_ip}:30562"
  6. done

预期输出 nginx 欢迎页面内容。