k8s集群中service的域名解析、pod的域名解析
前言
在k8s集群中,service和pod都可以通过域名的形式进行相互通信,换句话说,在k8s集群内,通过service和pod的域名,可以直接访问内部应用,不必在通过service ip地址进行通信,一般的,我们创建service的时候不建议指定service的clusterIP,而是让k8s自动为service分配一个clusterIP,这样,service的IP是自动分配,但是service名字总是固定的吧,这样在集群内部就可以直接通过service的域名来连接即可,如前端pod应用直接通过service域名来连接后端pod。
service的域名
<servicename>.<namespace>.svc.<clusterdomain>
其中,servicename为service名称,namespace为service所处的命名空间,clusterdomain是k8s集群设计的域名后缀,默认为cluster.local
演示示例:
下面,我们通过创建一个deployment和service,然后创建一个测试pod,在测试pod中通过访问service域名的形式访问应用,验证service域名是否正常。如下所示:
# 创建一个deployment,有3个副本
[root@master service]# vim deployment-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
env: dev
tiar: front
name: deployment-nginx
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:1.7.9
imagePullPolicy: IfNotPresent
name: nginx-container
ports:
- containerPort: 80
name: http
protocol: TCP
restartPolicy: Always
#创建一个service,用于反向代理上面创建的deployment的pod
[root@master service]# vim svc-deployment-nginx.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: svc-deployment-nginx
namespace: default
spec:
ports:
- name: nginx-port
nodePort: 30080
port: 80
protocol: TCP
targetPort: http
selector:
app: nginx
type: NodePort
#创建一个pod用于测试
[root@master service]# cat ../pod/pod-busybox.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-command
labels:
env: dev
namespace: default
spec:
nodeName: node2
containers:
- image: busybox
name: busybox-container
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt;sleep 3;done;"]
resources:
limits:
cpu: 2
memory: 2G
requests:
cpu: 1
memory: 500M
[root@master service]#
# 在测试pod中直接访问service的域名
[root@master service]# kubectl exec -it pod-command -- /bin/sh #进入到测试pod中
/ # wget http://svc-deployment-nginx.default.svc.cluster.local:80 #这个pod没有curl命令,所以通过wget命令下载
Connecting to svc-deployment-nginx.default.svc.cluster.local:80 (10.111.193.190:80) #下载成功
saving to 'index.html'
index.html 100% |*******************************************************************************************************************************************************************************************| 612 0:00:00 ETA
'index.html' saved
/ # cat index.html #下载成功,这是nginx的index文件,说明通过service域名访问是正常的
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
/ #
以上,说明,在pod内部通过访问service域名的形式访问其他服务,service的域名解析是正常的。
注意:不要在宿主机上curl service的域名,这样是访问不通的,因为service的域名解析是k8s集群内部的,不是外部所主机的。service的域名解析是供k8s集群内部应用之间进行访问的。
pod的域名解析(存疑)
pod的DNS域名格式为:<pod-ip>.<namespace>.pod.<clusterdomain>
,其中,pod-ip
需要使用-将ip直接的点替换掉,namespace为pod所在的命名空间,clusterdomain是k8s集群设置的域名后缀,一般默认为 cluster.local
,演示如下:10-244-1-223.default.pod.cluster.local
对应deployment、deamonset等创建的pod,还可以<pod-ip>.<deployment-name>.<namespace>.svc.<clusterdomain>
访问。
对于StatefulSet创建的pod,statefulset.spec.serviceName字段解释,其pod的域名为:pod-specific-string.serviceName.default.svc.cluster.local
总结
service的域名解析很重要,我们只需要在k8s集群内部访问service的域名即可,因为service的名称总是固定的,既然是固定的,那么可以直接通过访问service的域名方式来访问service。
相关文章
- 一文了解MQTT协议
- ITSP Infosys公司调查发现:人工智能和数据科学并未给企业带来经济价值
- 亚马逊云科技:六大能力助力中国企业出海行稳致远
- Kubernetes 1.24发布了—有什么新变化
- 企业应如何看待云锁定?
- 一文带你了解 SSL 协议
- 中科创达:发力全球,为更多企业降本增效
- 人工智能如何改善筹款工作
- 中国信通院李洁:从“东数西算”看算网发展
- Wi-Fi 6E,你还好吗?
- 当办公室里的机器人隔板突然围住你,网友:社恐福音!
- 亚马逊云科技:借数智融合之力,驱动企业创新
- 19岁美国男孩击败国际象棋世界冠军,被指利用AI作弊!
- 企业为实施云原生开发做好准备了吗?
- 将遗留数据迁移和管理到云端的六个障碍
- 七位IT专家畅谈:如何启动和扩展成功的AI试点项目
- Canalys:2022年第一季度全球云基础设施服务支出达559亿美元
- 未来,我们将如何进行信息搜索?
- 如何使用不同的命令启动已经停止运行的 Docker 容器?
- 人工智能如何帮助解决劳动力短缺问题