k8s部署-18-集群测试
通过这几篇文章,相信我们已经通过kubernetes-the-hard-way的方式,将k8s集群搭建成功了,那么如何进行验证呢?无非就是新建一个daemonset嘛,然后通过检测各种网络连通性验证,那么话不多说,直接来实操吧。
新建nginx的yaml文件
建立配置文件
[root@node1 ~]# 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
spec:
selector:
matchLabels:
app: nginx-ds
template:
metadata:
labels:
app: nginx-ds
spec:
containers:
- name: my-nginx
image: nginx:1.19
ports:
- containerPort: 80
EOF
[root@node1 ~]#
生成daemonset
[root@node1 ~]# kubectl apply -f nginx-ds.yml
检查生成情况
由于没有指定namespace,所以默认在default的命名空间中
[root@node1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-ds-m8wrf 1/1 Running 0 22s
nginx-ds-zc5qt 1/1 Running 0 22s
[root@node1 ~]#
# 获取到pod的IP地址
[root@node1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ds-m8wrf 1/1 Running 0 2m31s 10.200.135.3 node3 <none> <none>
nginx-ds-zc5qt 1/1 Running 0 2m31s 10.200.104.2 node2 <none> <none>
[root@node1 ~]#
# 分别在node2和node3这两个worker节点上ping一下
[root@node2 ~]# ping 10.200.135.3
[root@node2 ~]# ping 10.200.104.2
[root@node3 ~]# ping 10.200.135.3
[root@node3 ~]# ping 10.200.104.2
# 检查svc的可达性
[root@node1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 22h
nginx-ds NodePort 10.233.127.233 <none> 80:30323/TCP 4m12s
[root@node1 ~]#
# 获取到IP为10.233.127.233
# 然后在worker节点上分别curl一下,看看是否成功返回nginx界面。
[root@node2 ~]# curl 10.233.127.233
<!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>
[root@node2 ~]#
# 再检查通过node的IP地址是否能通,使用如下命令,在worker节点上执行
[root@node2 ~]# curl 10.200.135.3:80
[root@node2 ~]# curl 10.200.104.2:80
检查dns
# 创建一个nginx的pod
[root@node1 ~]# cat > pod-nginx.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: docker.io/library/nginx:1.19
ports:
- containerPort: 80
EOF
[root@node1 ~]#
# 使配置生效
[root@node1 ~]# kubectl apply -f pod-nginx.yaml
pod/nginx created
[root@node1 ~]#
# 查看现在的pod
[root@node1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 51s
nginx-ds-m8wrf 1/1 Running 0 9m11s
nginx-ds-zc5qt 1/1 Running 0 9m11s
[root@node1 ~]#
# 进入pod
[root@node1 ~]# kubectl exec nginx -it -- /bin/bash
# 查看DNS
root@nginx:/# cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local localdomain
nameserver 169.254.25.10
options ndots:5
root@nginx:/#
# 检查名字是否可以正常解析
root@nginx:/# curl nginx-ds # 经过检测是可以的
日志功能
# 查看现在有的pod
[root@node1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 3m55s
nginx-ds-m8wrf 1/1 Running 0 12m
nginx-ds-zc5qt 1/1 Running 0 12m
[root@node1 ~]#
# 可以看到现在有3个pod,我们随机选择一个,查看下日志
[root@node1 ~]# kubectl logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/03/20 12:14:04
2022/03/20 12:14:04 [notice] 1#1: nginx/1.21.6
2022/03/20 12:14:04 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/03/20 12:14:04 [notice] 1#1: OS: Linux 3.10.0-862.el7.x86_64
2022/03/20 12:14:04 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/03/20 12:14:04 [notice] 1#1: start worker processes
2022/03/20 12:14:04 [notice] 1#1: start worker process 31
[root@node1 ~]#
exec功能
[ ]
NAME READY STATUS RESTARTS AGE
nginx-ds-m8wrf 1/1 Running 0 13m
nginx-ds-zc5qt 1/1 Running 0 13m
[ ]
[ ]
Error: flag needs an argument: 'l' in -l
See 'kubectl get --help' for usage.
[ ]
NAME READY STATUS RESTARTS AGE
nginx-ds-m8wrf 1/1 Running 0 13m
nginx-ds-zc5qt 1/1 Running 0 13m
[ ]
nginx version: nginx/1.21.6
[ ]
至此,我们的k8s集群也验证完毕了,恭喜下自己吧。
往期推荐
添加关注,带你高效运维