如何在Kubernetes中设置Pod的DNS

huangapple go评论70阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月31日 20:44:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定