本文档验证 K8S 集群是否工作正常。
注意:如果没有特殊指明,本文档的所有操作均在 k8s-01 节点上执行,然后远程分发文件和执行命令。
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-01 Ready <none> 4h45m v1.16.6
k8s-02 Ready <none> 4h45m v1.16.6
k8s-03 Ready <none> 4h45m v1.16.6
都为 Ready 且版本为 v1.16.6 时正常。
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:1.7.9
ports:
- containerPort: 80
EOF
$ kubectl create -f nginx-ds.yml
service/nginx-ds created
daemonset.apps/nginx-ds created
$ kubectl get pods -o wide -l app=nginx-ds
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ds-25h8m 1/1 Running 0 61m 172.30.144.2 k8s-01 <none> <none>
nginx-ds-7whgp 1/1 Running 0 61m 172.30.176.2 k8s-02 <none> <none>
nginx-ds-9b85z 1/1 Running 0 61m 172.30.200.2 k8s-03 <none> <none>
可以看到,nginx-ds的 Pod IP 分别是172.30.144.2、172.30.176.2、172.30.200.2。在所有 Node 上分别 ping 上面三个 Pod IP,看是否连通:
cat > deploy.sh << "EOF"
#!/bin/bash
cd /opt/k8s/work
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
echo ">>> ${node_ip}"
ssh ${node_ip} "ping -c 1 172.30.144.2"
ssh ${node_ip} "ping -c 1 172.30.176.2"
ssh ${node_ip} "ping -c 1 172.30.200.2"
done
EOF
$ kubectl get svc -l app=nginx-ds
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-ds NodePort 10.254.83.21 <none> 80:31573/TCP 66m
可见:
在所有 Node 上 curl Service IP:
cat > deploy.sh << "EOF"
#!/bin/bash
cd /opt/k8s/work
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
echo ">>> ${node_ip}"
ssh ${node_ip} "curl -s 10.254.83.21"
done
EOF
预期输出 nginx 欢迎页面内容。