gpt4 book ai didi

kubernetes DNS Pod 名称解析

转载 作者:行者123 更新时间:2023-12-02 02:11:46 26 4
gpt4 key购买 nike

我正在尝试在我的 EKS Kubernetes 集群 v1.10.3 上使用 dns pod 名称解析。我的理解是,创建 headless 服务将创建我需要的必要的 pod 名称记录,但我发现这不是真的。我错过了什么吗?

也欢迎其他关于如何实现这一目标的想法。找不到替代解决方案。

添加更新

我还不够清楚。本质上我需要的是这样解决: worker -767cd94c5c-c5bq7 -> 10.0.10.10 worker -98dcd94c5d-cabq6 -> 10.0.10.11等等......

我真的不需要循环 DNS,只需在某处读到这可能是一个解决方法。谢谢!

# my service
apiVersion: v1
kind: Service
metadata:
...
name: worker
namespace: airflow-dev
resourceVersion: "374341"
selfLink: /api/v1/namespaces/airflow-dev/services/worker
uid: 814251ac-acbe-11e8-995f-024f412c6390
spec:
clusterIP: None
ports:
- name: worker
port: 8793
protocol: TCP
targetPort: 8793
selector:
app: airflow
tier: worker
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}





# my pod
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: 2018-08-31T01:39:37Z
generateName: worker-69887d5d59-
labels:
app: airflow
pod-template-hash: "2544381815"
tier: worker
name: worker-69887d5d59-6b6fc
namespace: airflow-dev
ownerReferences:
- apiVersion: extensions/v1beta1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: worker-69887d5d59
uid: 16019507-ac6b-11e8-995f-024f412c6390
resourceVersion: "372954"
selfLink: /api/v1/namespaces/airflow-dev/pods/worker-69887d5d59-6b6fc
uid: b8d82a6b-acbe-11e8-995f-024f412c6390
spec:
containers:
...
...
name: worker
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
...
...
dnsPolicy: ClusterFirst
nodeName: ip-10-0-1-226.us-west-2.compute.internal
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: airflow
serviceAccountName: airflow
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
...
...
status:
conditions:
- lastProbeTime: null
lastTransitionTime: 2018-08-31T01:39:37Z
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: 2018-08-31T01:39:40Z
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: 2018-08-31T01:39:37Z
status: "True"
type: PodScheduled
containerStatuses:
...
...
lastState: {}
name: worker
ready: true
restartCount: 0
state:
running:
startedAt: 2018-08-31T01:39:39Z
hostIP: 10.0.1.226
phase: Running
podIP: 10.0.1.234
qosClass: BestEffort
startTime: 2018-08-31T01:39:37Z





# querying the service dns record works!
airflow@worker-69887d5d59-6b6fc:~$ nslookup worker.airflow-dev.svc.cluster.local
Server: 172.20.0.10
Address: 172.20.0.10#53

Name: worker.airflow-dev.svc.cluster.local
Address: 10.0.1.234





# querying the pod name does not work :(
airflow@worker-69887d5d59-6b6fc:~$ nslookup worker-69887d5d59-6b6fc.airflow-dev.svc.cluster.local
Server: 172.20.0.10
Address: 172.20.0.10#53

** server can't find worker-69887d5d59-6b6fc.airflow-dev.svc.cluster.local: NXDOMAIN

airflow@worker-69887d5d59-6b6fc:~$ nslookup worker-69887d5d59-6b6fc.airflow-dev.pod.cluster.local
Server: 172.20.0.10
Address: 172.20.0.10#53

*** Can't find worker-69887d5d59-6b6fc.airflow-dev.pod.cluster.local: No answer

最佳答案

在内部,我建议使用服务 DNS 记录来指向 Pod,您已经确认该记录有效。这当然不需要您有 Headless 服务来使用服务 DNS。

kube-dns 自动记录的工作方式如下:

pod -> 同一命名空间中的服务:curl http://servicename

pod -> 不同命名空间中的服务:curl http://servicename.namespace

在此处了解有关服务发现的更多信息:https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables

您可以在此处阅读有关服务的 DNS 记录的更多信息 https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services

如果您需要外部自定义名称解析,我建议使用 nginx-ingress:

https://github.com/helm/charts/tree/master/stable/nginx-ingress https://github.com/kubernetes/ingress-nginx

编辑:包括有关实际 Pod DNS 的详细信息

v1.2 引入了一个 beta 功能,用户可以指定 Pod 注释 pod.beta.kubernetes.io/subdomain 来指定 Pod 的子域。最终域名将是“...svc.”。例如,主机名注释设置为“foo”、子域注释设置为“bar”、命名空间“my-namespace”中的 Pod 将具有 FQDN“foo.bar.my-namespace.svc.cluster”。本地”

A Records and hostname based on Pod's hostname and subdomain fields Currently when a pod is created, its hostname is the Pod's metadata.name value.

With v1.2, users can specify a Pod annotation, pod.beta.kubernetes.io/hostname, to specify what the Pod's hostname should be. The Pod annotation, if specified, takes precedence over the Pod's name, to be the hostname of the pod. For example, given a Pod with annotation pod.beta.kubernetes.io/hostname: my-pod-name, the Pod will have its hostname set to "my-pod-name".

With v1.3, the PodSpec has a hostname field, which can be used to specify the Pod's hostname. This field value takes precedence over the pod.beta.kubernetes.io/hostname annotation value.

v1.2 introduces a beta feature where the user can specify a Pod annotation, pod.beta.kubernetes.io/subdomain, to specify the Pod's subdomain. The final domain will be "...svc.". For example, a Pod with the hostname annotation set to "foo", and the subdomain annotation set to "bar", in namespace "my-namespace", will have the FQDN "foo.bar.my-namespace.svc.cluster.local"

With v1.3, the PodSpec has a subdomain field, which can be used to specify the Pod's subdomain. This field value takes precedence over the pod.beta.kubernetes.io/subdomain annotation value.

https://unofficial-kubernetes.readthedocs.io/en/latest/concepts/services-networking/dns-pod-service/

关于kubernetes DNS Pod 名称解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52107830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com