0
点赞
收藏
分享

微信扫一扫

Kubernetes 内部DNS访问 Service


在 Cluster 中,除了可以通过 Cluster IP 访问 Service,Kubernetes 还提供了更为方便的 DNS 访问。

[root@k8s-master ~]# kubectl get deployment  -n kube-system
NAME READY UP-TO-DATE AVAILABLE AGE
coredns 1/1 1 1 25h

coredns 是一个 DNS 服务器。每当有新的 Service 被创建,coredns 会添加该 Service 的 DNS 记录。Cluster 中的 Pod 可以通过 ​​<SERVICE_NAME>.<NAMESPACE_NAME>​​ 访问 Service。

[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 25d
nodeport-svc NodePort 10.0.0.171 <none> 3000:30090/TCP 28h

用 ​​nslookup​​​ 查看 ​​nodeport-svc​​的 DNS 的信息。

/ # nslookup nodeport-svc.default
Server: 10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local

Name: nodeport-svc.default
Address 1: 10.0.0.171 nodeport-svc.default.svc.cluster.local

比如可以用 ​​httpd-svc.default​​​ 访问 Service ​​httpd-svc​​。

/ # wget nodeport-svc.default:3000
Connecting to nodeport-svc.default:3000 (10.0.0.171:3000)
saving to 'index.html'
index.html 100% |*********************************************************| 41 0:00:00 ETA
'index.html' saved

如果要访问其他 namespace 中的 Service,就必须带上 namesapce 了。Kubernetes 集群内部可以通过 Cluster IP 和 DNS 访问 Service。

举报

相关推荐

0 条评论