对于服务的访问,我们先在Kubernetes集群内进行访问测试,有两种访问方式如下所示:
·通过Cluster IP访问。
·通过集群内的DNS访问。
当如上的两种服务访问方式正常后,我们可以通过NodePort暴露给外部,然后使用浏览器进行访问测试。
1.在kubenetes集群内访问测试
(1)通过Cluster IP访问
$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6h6m service-go ClusterIP 10.108.207.28 <none> 80/TCP 4h48m service-js ClusterIP 10.97.251.7 <none> 80/TCP 4h48m service-lua ClusterIP 10.103.165.139 <none> 80/TCP 4h48m service-node ClusterIP 10.102.21.34 <none> 80/TCP 4h48m service-python ClusterIP 10.103.119.8 <none> 80/TCP 4h48m $ curl http://10.108.207.28/env {"message":"go v2"} $ curl http://10.103.165.139/env {"message":"lua v1"} $ curl http://10.102.21.34/env {"message":"node v1","upstream":[{"message":"go v1","response_time":"0.01"}]} $ curl http://10.103.119.8/env {"message":"python v1","upstream":[{"message":"lua v2","response_time":0.12},{"message":"node v2","response_time":0.12,"upstream":[{"message":"go v2","response_time":"0.01"}]}]} $ curl -I http://10.97.251.7/ HTTP/1.1 200 OK last-modified: Sun, 16 Dec 2018 07:49:03 GMT content-length: 548 content-disposition: inline; filename="index.html" accept-ranges: bytes content-type: text/html; charset=utf-8 vary: Accept-Encoding date: Sun, 13 Jan 2019 10:27:13 GMT Connection: keep-alive
(2)通过集群内的DNS访问
1)创建用于访问的测试容器:
$ kubectl apply -f kubernetes/dns-test.yaml
2)服务解析测试:
$ kubectl exec dns-test -c dns-test -- nslookup service-go Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: service-go Address 1: 10.108.207.28 service-go.default.svc.cluster.local $ kubectl exec dns-test -c dns-test -- nslookup service-node Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: service-node Address 1: 10.102.21.34 service-node.default.svc.cluster.local $ kubectl exec dns-test -c dns-test -- nslookup service-lua Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: service-lua Address 1: 10.103.165.139 service-lua.default.svc.cluster.local $ kubectl exec dns-test -c dns-test -- nslookup service-python Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: service-python Address 1: 10.103.119.8 service-python.default.svc.cluster.local $ kubectl exec dns-test -c dns-test -- nslookup service-js Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: service-js Address 1: 10.97.251.7 service-js.default.svc.cluster.local
3)服务访问测试:
$ kubectl exec dns-test -c dns-test -- curl -s http://service-go/env {"message":"go v2"} $ kubectl exec dns-test -c dns-test -- curl -s http://service-node/env {"message":"node v1","upstream":[{"message":"go v2","response_time":"0.01"}]} $ kubectl exec dns-test -c dns-test -- curl -s http://service-lua/env {"message":"lua v1"} $ kubectl exec dns-test -c dns-test -- curl -s http://service-python/env {"message":"python v2","upstream":[{"message":"lua v2","response_time":0.05},{"message":"node v2","upstream":[{"message":"go v1","response_time":"0.00"}],"response_time":0.05}]} $ kubectl exec dns-test -c dns-test -- curl -s -I http://service-js/ HTTP/1.1 200 OK last-modified: Sun, 16 Dec 2018 07:49:13 GMT content-length: 505 content-disposition: inline; filename="index.html" accept-ranges: bytes content-type: text/html; charset=utf-8 vary: Accept-Encoding date: Sun, 13 Jan 2019 10:31:23 GMT Connection: keep-alive
2.在浏览器中访问测试
1)使用NodePort暴露服务。使用NodePort暴露sevice-js和service-python服务的v1版本:
$ kubectl expose deployment service-js-v1 --type=NodePort --name=service-js-nodeport --port=80 --target-port=80 service/service-js-nodeport exposed $ kubectl expose deployment service-python-v1 --type=NodePort --name=service-python-nodeport --port=80 --target-port=80 service/service-python-nodeport exposed
2)在浏览器中访问sevice-js和service-python服务。
获取service-js外部访问地址:
$ SERVICE_JS_PORT=$(kubectl get service service-js-nodeport -o jsonpath='{.spec.ports[0].nodePort}') $ HOST=$(kubectl get node lab2 -o 'jsonpath={.status.addresses[0].address}') $ GATEWAY_URL=$HOST:$SERVICE_JS_PORT $ echo http://$GATEWAY_URL/ $ curl -I http://$GATEWAY_URL/
获取service-python外部访问地址:
$ SERVICE_PYTHON_PORT=$(kubectl get service service-python-nodeport -o jsonpath='{.spec.ports[0].nodePort}') $ HOST=$(kubectl get node lab2 -o 'jsonpath={.status.addresses[0].address}') $ GATEWAY_URL=$HOST:$SERVICE_PYTHON_PORT $ echo http://$GATEWAY_URL/env $ curl http://$GATEWAY_URL/env
使用上面步骤获取的URL地址在浏览器中访问。
访问service-js地址,结果如图6-6所示。
图6-6 在浏览器中访问测试
访问service-python服务地址,结果如下:
{"message":"python v1","upstream":[{"message":"lua v1","response_time":0.15},{"message":"node v1","response_time":0.25,"upstream":[{"message":"go v2","response_time":"0.01"}]}]}
【清理】
删除访问测试服务时创建的资源:
$ kubectl delete service service-js-nodeport service-python-nodeport service "service-js-nodeport" deleted service "service-python-nodeport" deleted $ kubectl delete -f kubernetes/dns-test.yaml pod "dns-test" deleted $ kubectl delete -f service/go/service-go.yaml $ kubectl delete -f service/node/service-node.yaml $ kubectl delete -f service/lua/service-lua.yaml $ kubectl delete -f service/python/service-python.yaml $ kubectl delete -f service/js/service-js.yaml