购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

4.5 测试集群的正确性

在lab1节点上执行如下的步骤,验证Kubernetes集群部署是否正确可用。

1)部署Nginx应用:


$ kubectl create deployment nginx --image=nginx:alpine
deployment.apps/nginx created

2)暴露Nginx应用:


$ kubectl expose deployment nginx --name=nginx-service --port=80 --target-
    port=80
service/nginx-service exposed
$ kubectl expose deployment nginx --type=NodePort --name=nginx-service-nodeport 
    --port=80 --target-port=80
service/nginx-service-nodeport exposed

3)查看Nginx应用状态:


$ kubectl get deploy
NAME       DESIRED      CURRENT      UP-TO-DATE      AVAILABLE      AGE
nginx      1            1            1               1              2m9s
$ kubectl get pod
NAME                        READY      STATUS       RESTARTS      AGE
nginx-65d5c4f7cc-ztkkf      1/1        Running      0             2m9s
$ kubectl get svc
NAME                    TYPE       CLUSTER-IP      EXTERNAL-IP  PORT(S)       AGE
kubernetes              ClusterIP  10.96.0.1       <none>       443/TCP       3h5m
nginx-service           ClusterIP  10.106.97.98    <none>       80/TCP        96s
nginx-service-nodeport  NodePort   10.106.177.223  <none>       80:31994/TCP  44s

4)通过Pod IP访问Nginx应用。

查看Nginx应用的Pod IP:


$ kubectl get pod -o wide
NAME                    READY  STATUS   RESTARTS  AGE   IP          NODE  NOMINATED NODE
nginx-65d5c4f7cc-ztkkf  1/1    Running  0         6m4s  10.244.1.3  lab2  <none>

访问Nginx应用,10.244.1.3为上一步骤中查看到的Pod IP:


curl -I http://10.244.1.3/
HTTP/1.1 200 OK
Server: nginx/1.15.8
Date: Sun, 13 Jan 2019 04:54:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 26 Dec 2018 23:21:49 GMT
Connection: keep-alive
ETag: "5c240d0d-264"
Accept-Ranges: bytes

5)通过Cluster IP访问Nginx应用。

查看Nginx应用的Cluster IP:


$ kubectl get svc nginx-service
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
nginx-service   ClusterIP   10.106.97.98   <none>        80/TCP    22m

访问Nginx应用,10.106.97.98为上一步骤中查看到的Cluster IP:


curl -I http://10.106.97.98/
HTTP/1.1 200 OK
Server: nginx/1.15.8
Date: Sun, 13 Jan 2019 04:55:03 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 26 Dec 2018 23:21:49 GMT
Connection: keep-alive
ETag: "5c240d0d-264"
Accept-Ranges: bytes

6)通过NodePort访问Nginx应用。

查看Nginx应用的Cluster IP:


$ kubectl get svc nginx-service-nodeport
NAME                    TYPE      CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx-service-nodeport  NodePort  10.105.227.107  <none>        80:31499/TCP   110s

访问Nginx应用,31499为上一步骤中查看到的NodePort:


curl -I http://lab1:31499/
HTTP/1.1 200 OK
Server: nginx/1.15.8
Date: Sun, 13 Jan 2019 04:55:58 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 26 Dec 2018 23:21:49 GMT
Connection: keep-alive
ETag: "5c240d0d-264"
Accept-Ranges: bytes
curl -I http://lab2:31499/
HTTP/1.1 200 OK
Server: nginx/1.15.8
Date: Sun, 13 Jan 2019 04:56:37 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 26 Dec 2018 23:21:49 GMT
Connection: keep-alive
ETag: "5c240d0d-264"
Accept-Ranges: bytes
curl -I http://lab3:31499/
HTTP/1.1 200 OK
Server: nginx/1.15.8
Date: Sun, 13 Jan 2019 04:56:54 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 26 Dec 2018 23:21:49 GMT
Connection: keep-alive
ETag: "5c240d0d-264"
Accept-Ranges: bytes

7)DNS解析测试。

部署测试Pod:


#生成部署文件
$ cat >dns-test.yaml<<EOF
apiVersion: v1
kind: Pod
metadata:
  name: dns-test
spec:
  containers:
  - image: radial/busyboxplus:curl
    name: dns-test
    stdin: true
    tty: true
    resources:
      requests:
        cpu: 50m
        memory: 50Mi
      limits:
        cpu: 100m
        memory: 100Mi
EOF
#部署
$ kubectl apply -f dns-test.yaml
#查看状态
$ kubectl get pod dns-test
NAME             READY         STATUS          RESTARTS         AGE
dns-test         1/1           Running         0                84s

DNS解析测试:


$ kubectl exec dns-test -- nslookup kubernetes
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name:      kubernetes
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local
$ kubectl exec dns-test -- nslookup nginx-service
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name:      nginx-service
Address 1: 10.106.97.98 nginx-service.default.svc.cluster.local
$ kubectl exec dns-test -- nslookup www.baidu.com
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name:      www.baidu.com
Address 1: 115.239.210.27
Address 2: 115.239.211.112

8)通过DNS访问Nginx应用:


$ kubectl exec dns-test -- curl -s -I http://nginx-service/
HTTP/1.1 200 OK
Server: nginx/1.15.8
Date: Sun, 13 Jan 2019 05:12:22 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 26 Dec 2018 23:21:49 GMT
Connection: keep-alive
ETag: "5c240d0d-264"
Accept-Ranges: bytes

9)清理: sodOsv6cloAiZ5vINaF3DynGTctdXry+Wehd2q2K/E8ygWThWNS1om7LtbBuc74N


$ kubectl delete service nginx-service-nodeport
$ kubectl delete service nginx-service
$ kubectl delete deployment nginx
$ kubectl delete -f dns-test.yaml

点击中间区域
呼出菜单
上一章
目录
下一章
×

打开