09 | 验证 Kubernetes 集群功能
经过上面的部署,我们已经成功安装了 Kuberentes 正常工作需要的组件。所以,本节课,我们来看下如何验证 Kubernetes 集群,查看其是否能正常工作,以确保 Kubernetes 集群被成功部署。
注意:如果没有特殊指明,本文档的所有操作均在 k8s-01 节点上执行,然后远程分发文件和执行命令。
检查节点状态
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-01 Ready <none> 11h v1.31.1
k8s-02 Ready <none> 11h v1.31.1
都为 Ready 且版本为 v1.31.1 时正常。
创建测试文件
创建一个用来测试的 nginx-ds DaemonSet 和 nginx-ds Service:
cd /opt/k8s/work
cat > nginx-ds.yml <<EOF
apiVersion: v1
kind: Service
metadata:
name: nginx-ds
labels:
app: nginx-ds
spec:
type: NodePort
selector:
app: nginx-ds
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx-ds
labels:
addonmanager.kubernetes.io/mode: Reconcile
spec:
selector:
matchLabels:
app: nginx-ds
template:
metadata:
labels:
app: nginx-ds
spec:
containers:
- name: my-nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
EOF
创建 DaemonSet 和 Service 资源:
kubectl create -f nginx-ds.yml
检查各节点的 Pod IP 连通性
$ kubectl get pods -o wide -l app=nginx-ds
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ds-6vhzz 1/1 Running 0 7m10s 10.0.1.125 k8s-01 <none> <none>
nginx-ds-rzpt2 1/1 Running 0 7m10s 10.0.0.103 k8s-02 <none> <none>
在所有 Node 上分别 ping 上面 2 个 Pod IP,看是否连通:
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
echo ">>> ${node_ip}"
ssh ${node_ip} "ping -c 1 10.0.1.125"
ssh ${node_ip} "ping -c 1 10.0.0.103"
done
结果
>>> 172.16.0.4
PING 10.0.1.125 (10.0.1.125) 56(84) bytes of data.
64 bytes from 10.0.1.125: icmp_seq=1 ttl=63 time=0.068 ms
--- 10.0.1.125 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.068/0.068/0.068/0.000 ms
PING 10.0.0.103 (10.0.0.103) 56(84) bytes of data.
64 bytes from 10.0.0.103: icmp_seq=1 ttl=63 time=0.977 ms
--- 10.0.0.103 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.977/0.977/0.977/0.000 ms
>>> 172.16.0.8
PING 10.0.1.125 (10.0.1.125) 56(84) bytes of data.
64 bytes from 10.0.1.125: icmp_seq=1 ttl=63 time=6.03 ms
--- 10.0.1.125 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 6.030/6.030/6.030/0.000 ms
PING 10.0.0.103 (10.0.0.103) 56(84) bytes of data.
64 bytes from 10.0.0.103: icmp_seq=1 ttl=63 time=0.118 ms
--- 10.0.0.103 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.118/0.118/0.118/0.000 ms
检查服务 IP 和端口可达性
$ kubectl get svc -l app=nginx-ds
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-ds NodePort 10.254.106.249 <none> 80:31385/TCP 9m3s
可见:
- Service Cluster IP:10.254.148.86;
- 服务端口:80;
- NodePort 端口:31595。
在所有 Node 上 curl Service IP:
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
echo ">>> ${node_ip}"
ssh ${node_ip} "curl -s 10.254.106.249"
done
预期输出 Nginx 欢迎页面内容:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
检查服务的 NodePort 可达性
在所有 Node 上执行:
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
echo ">>> ${node_ip}"
ssh ${node_ip} "curl -s ${node_ip}:31385"
done
预期输出 Nginx 欢迎页面内容。