经过上面的部署,我们已经成功安装了 Kubernetes 正常工作需要的组件。所以,本节课,我们来看下如何验证 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-rxwtz 1/1 Running 0 35s 10.0.1.151 k8s-02 <none> <none>
nginx-ds-zkqsn 1/1 Running 0 35s 10.0.0.43 k8s-01 <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.151"
ssh ${node_ip} "ping -c 1 10.0.0.43"
done
检查服务 IP 和端口可达性#
$ kubectl get svc -l app=nginx-ds
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-ds NodePort 10.254.201.8 <none> 80:31041/TCP 2m47s
可见:
- Service Cluster IP:10.254.201.8
- 服务端口:80
- NodePort 端口:31041
在所有 Node 上 curl Service IP:
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
echo ">>> ${node_ip}"
ssh root@${node_ip} "curl -s 10.254.201.8"
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 root@${node_ip} "curl -s ${node_ip}:31041"
done
预期输出 nginx 欢迎页面内容。