英文:
How to set the Pod DNS in Kubernetes
问题
我想知道是否有办法为 StatefulSet 中的 Pod 设置 DNS,这样我就可以直接调用它们。
我已经阅读了 Kubernetes 文档,但我没有找到有用的信息。
英文:
I wanted to know if there is any way that I can set a DNS for the Pod in the StatefulSet, so I can call them directly.
I have read the Kubernetes documentation, but I haven't seen anything useful.
答案1
得分: 3
你可以直接命中 POD,如果使用带有 headless 服务的 StatefulSet
因此,如果有三个副本运行 web-0、web-1、web-2,你可以使用 curl
web-0.<service-name>.<namespace-name>.svc.cluster.local
POD 名称
<pod-name>.<service-name>.<namespace-name>.svc.cluster.local
但重要的部分是你的服务应该是 headless
示例
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 3
minReadySeconds: 10
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: registry.k8s.io/nginx
ports:
- containerPort: 80
name: web
官方文档参考:https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id
英文:
You can directly hit the POD if using the statefulset with headless service
So if you have three replicas running web-0, web-1, web-2 you can use curl
web-0.<service-name>.<namespace-name>.svc.cluster.local
POD name
<pod-name>.<service-name>.<namespace-name>.svc.cluster.local
But the important part is your service should be headless
Example
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 3
minReadySeconds: 10
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: registry.k8s.io/nginx
ports:
- containerPort: 80
name: web
Official doc ref : https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论