【问题标题】:Calling the pods using pod's name instead of host and port使用 pod 的名称而不是主机和端口调用 pod
【发布时间】:2020-07-21 17:35:18
【问题描述】:
我在本地 minikube 集群内运行一组微服务(使用 helm 图表),它们相互通信。每个服务的主机和端口都通过value-dev.yaml 传递给其他服务,并且通信正常。现在我需要更进一步,将连接调用从http://helm-chart-name:PORT/ 更改为http://helm-chart-name/ 或http://service-pod-name/。我试图这样做,但没有奏效。有没有办法做到这一点?
【问题讨论】:
标签:
kubernetes
minikube
coredns
【解决方案1】:
在您的服务中(特别是)将 port: 编号设置为 80。这是 HTTP 的默认 TCP 端口号,因此如果 URL 中没有 ...:12345 端口号,将使用该端口号。 targetPort: 需要匹配 pod 正在侦听的任何端口;它不需要匹配port:。
apiVersion: v1
kind: Service
metadata:
name: {{ include "chart.fullname" . }}
spec:
selector:
{{- include "chart.selectorLabels" . | nindent 4 }}
ports:
- name: http
protocol: TCP
port: 80 # default HTTP port
targetPort: 3000 # port number the matching Pod uses
现在其他服务可以调用http://helm-chart-name/,而无需明确提供端口号。
(您几乎总是需要使用 Service 来接受与 pod 的连接;您通常不会直接与 pod 通信,而且除了某些特殊情况外,这样做很棘手。)
【解决方案2】:
如果需要通过pod访问,DNS解析如下:
pod-ip-address.deployment-name.my-namespace.svc.cluster-domain.example。鉴于 pod 的短暂性以及运行多个同类型 pod 的可能性,我建议使用服务抽象来进行集群内通信,就像您需要的那样。服务解析为这种格式的 DNS my-svc.my-namespace.svc.cluster-domain.example。您还可以使用无头服务并解析为具有特定(主机)名称的 Pod。参考DNS Reolution details here