02-k8s集群测试
创建pod,pod名称为nginx,使用nginx的容器,副本数为3 [root@master ~]# kubectl create deployment nginx --image=nginx --replicas=3 返回结果: deployment.apps/nginx created 查看pod状态,这里可以看到所有POD的里的容器都是running状态 [root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-6799fc88d8-f7ccj 1/1 Running 0 87s nginx-6799fc88d8-n68qh 1/1 Running 0 87s nginx-6799fc88d8-ps94g 1/1 Running 0 87s 查看pod运行的详细信息,可以看到下列的信息,哪个容器跑在了哪个节点上面 [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-6799fc88d8-f7ccj 1/1 Running 0 3m55s 192.168.2.2 node02 <none> <none> nginx-6799fc88d8-n68qh 1/1 Running 0 3m55s 192.168.1.2 node01 <none> <none> nginx-6799fc88d8-ps94g 1/1 Running 0 3m55s 192.168.3.2 node03 <none> <none> 创建一个deployment为nginx的pod,对外的端口是8080,对内的端口是80 [root@master ~]# kubectl expose deployment nginx --port=8080 --target-port=80 service/nginx exposed 查看创建好的PDD [root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-6799fc88d8-47rfc 1/1 Running 0 66s nginx-6799fc88d8-kcms7 1/1 Running 0 66s nginx-6799fc88d8-tp7ff 1/1 Running 0 66s 查看service信息,这里可以看到nginx的负载均衡的IP是10.96.124.143,对外访问的端口是8080 [root@master ~]# kubectl get service 返回结果: NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17h nginx ClusterIP 10.96.124.143 <none> 8080/TCP 105s 查看pdo的IP连通性,这里可以看到,IP和分布在哪个节点上面 [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-6799fc88d8-47rfc 1/1 Running 0 6m40s 192.168.3.3 node03 <none> <none> nginx-6799fc88d8-kcms7 1/1 Running 0 6m40s 192.168.2.3 node02 <none> <none> nginx-6799fc88d8-tp7ff 1/1 Running 0 6m40s 192.168.1.3 node01 <none> <none> 可以用利用命令在master上面访问node01上面这台IP为192.168.1.3的POD [root@master ~]# curl -I 192.168.1.3 HTTP/1.1 200 OK Server: nginx/1.21.5 Date: Tue, 06 Jun 2023 02:40:33 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Tue, 28 Dec 2021 15:28:38 GMT Connection: keep-alive ETag: "61cb2d26-267" Accept-Ranges: bytes 查看service负载均衡的IP和端口 [root@master ~]# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 18h nginx ClusterIP 10.96.124.143 <none> 8080/TCP 10m 在master上访问负载均衡的IP10.96.124.143端口为8080,状态为200,表示可以访问 [root@master ~]# curl 10.96.124.143:8080 -I HTTP/1.1 200 OK Server: nginx/1.21.5 Date: Tue, 06 Jun 2023 02:45:06 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Tue, 28 Dec 2021 15:28:38 GMT Connection: keep-alive ETag: "61cb2d26-267" Accept-Ranges: bytes DNS解析记录,每创建一个负载均衡默认会在DNS解析记录里面创建一条A记录,比如我创建的PDO的名称叫nginx,那么这台A记录的域名就是nginx 进入容器内部测试 先来查看运行中的POD的容器 [root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-6799fc88d8-47rfc 1/1 Running 0 19m nginx-6799fc88d8-kcms7 1/1 Running 0 19m nginx-6799fc88d8-tp7ff 1/1 Running 0 19m 进入到nginx-6799fc88d8-47rfc测试 [root@master ~]# kubectl exec -it nginx-6799fc88d8-47rfc -- /bin/bash 进入容器后测试,状态码为200,说明DNS解析没有问题 root@nginx-6799fc88d8-47rfc:/# curl nginx:8080 -I HTTP/1.1 200 OK Server: nginx/1.21.5 Date: Tue, 06 Jun 2023 02:53:02 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Tue, 28 Dec 2021 15:28:38 GMT Connection: keep-alive ETag: "61cb2d26-267" Accept-Ranges: bytes 查看容器的DNS配置,这里可以看到DNS是10.96.0.10 root@nginx-6799fc88d8-47rfc:/# cat /etc/resolv.conf nameserver 10.96.0.10 search default.svc.cluster.local svc.cluster.local cluster.local options ndots:5 修改容器内部的默认页面,方便后期测试负载均衡是否正常 root@nginx-6799fc88d8-47rfc:/# echo "nginx1" > /usr/share/nginx/html/index.html 容器内部测试默认访问页面 root@nginx-6799fc88d8-47rfc:/# curl 127.0.0.1 nginx1 退出容器 root@nginx-6799fc88d8-47rfc:/# exit exit 修改nginx-6799fc88d8-kcms7的容器内部的默认访问页面 [root@master ~]# kubectl exec -it nginx-6799fc88d8-kcms7 -- /bin/bash 修改nginx默认访问页面 root@nginx-6799fc88d8-kcms7:/# echo "nginx2" > /usr/share/nginx/html/index.html 测试 root@nginx-6799fc88d8-kcms7:/# curl 127.0.0.1 nginx2 退出容器 root@nginx-6799fc88d8-kcms7:/# exit exit 修改nginx-6799fc88d8-tp7ff的容器内部的默认访问页面 [root@master ~]# kubectl exec -it nginx-6799fc88d8-tp7ff -- /bin/bash 修改nginx默认访问页面 root@nginx-6799fc88d8-tp7ff:/# echo "nginx3" > /usr/share/nginx/html/index.html 测试 root@nginx-6799fc88d8-tp7ff:/# curl 127.0.0.1 nginx3 退出容器 root@nginx-6799fc88d8-tp7ff:/# exit exit 访问负载均衡IP:10.96.124.143,测试访问页面是否轮询 [root@master ~]# curl 10.96.124.143:8080 nginx3 [root@master ~]# curl 10.96.124.143:8080 nginx1 [root@master ~]# curl 10.96.124.143:8080 nginx2