简体   繁体   English

Kubernetes从另一个Pod找到Pod IP

[英]Kubernetes to find Pod IP from another Pod

I have the following pods hello-abc and hello-def . 我有以下pods hello-abchello-def

And I want to send data from hello-abc to hello-def . 我想将数据从hello-abc发送到hello-def

How would pod hello-abc know the IP address of hello-def ? pod hello-abc如何知道hello-def的IP地址?

And I want to do this programmatically. 我想以编程方式执行此操作。

What's the easiest way for hello-abc to find where hello-def ? hello-abc找到hello-def的最简单方法是什么?

---
apiVersion: extensions/v1beta1
kind: Deployment

metadata:
name: hello-abc-deployment

spec:
replicas: 1

template:
    metadata:
    labels:
        app: hello-abc
    spec:
    containers:
    - name: hello-abc
        image: hello-abc:v0.0.1
        imagePullPolicy: Always
        args: ["/hello-abc"]
        ports:
        - containerPort: 5000

---
apiVersion: extensions/v1beta1
kind: Deployment

metadata:
name: hello-def-deployment

spec:
replicas: 1

template:
    metadata:
    labels:
        app: hello-def
    spec:
    containers:
    - name: hello-def
        image: hello-def:v0.0.1
        imagePullPolicy: Always
        args: ["/hello-def"]
        ports:
        - containerPort: 5001

---
apiVersion: v1
kind: Service

metadata:
name: hello-abc-service

spec:
ports:
- port: 80
    targetPort: 5000
    protocol: TCP

selector:
    app: hello-abc
type: NodePort

---
apiVersion: v1
kind: Service

metadata:
name: hello-def-service

spec:
ports:
- port: 80
    targetPort: 5001
    protocol: TCP

selector:
    app: hello-def
type: NodePort

Preface 前言

Since you have defined a service that routes to each deployment, if you have deployed both services and deployments into the same namespace, you can in many modern kubernetes clusters take advantage of kube-dns and simply refer to the service by name. 由于您已定义了路由到每个部署的服务,因此如果您已将服务和部署部署到同一命名空间,则可以在许多现代kubernetes集群中利用kube-dns并简单地按名称引用服务。

Unfortunately if kube-dns is not configured in your cluster (although it is unlikely) you cannot refer to it by name. 不幸的是,如果您的群集中未配置kube-dns (尽管不太可能),则无法通过名称引用它。

You can read more about DNS records for services here 您可以在此处阅读有关服务的DNS记录的更多信息

In addition Kubernetes features "Service Discovery" Which exposes the ports and ips of your services into any container which is deployed into the same namespace. 此外,Kubernetes还具有“服务发现”功能,可将服务的端口和ips暴露在部署到同一名称空间的任何容器中。

Solution

This means, to reach hello-def you can do so like this 这意味着,要达到hello-def,你可以这样做

curl http://hello-def-service:${HELLO_DEF_SERVICE_PORT}

based on Service Discovery https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables 基于服务发现https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables

Caveat : Its very possible that if the Service port changes, only pods that are created after the change in the same namespace will receive the new environment variables. 警告 :如果服务端口发生更改,则很可能只有在同一命名空间中更改后创建的pod才会收到新的环境变量。

External Access 外部访问

In addition, you can also reach this your service externally since you are using the NodePort feature, as long as your NodePort range is accessible from outside. 此外,只要您可以从外部访问NodePort范围,您就可以在外部使用NodePort功能从外部获得此服务。

This would require you to access your service by node-ip:nodePort 这将要求您通过node-ip:nodePort访问您的服务

You can find out the NodePort which was randomly assigned to your service with kubectl describe svc/hello-def-service 你可以找到随机分配给你的服务的NodePort,用kubectl describe svc/hello-def-service

Ingress 入口

To reach your service from outside you should implement an ingress service such as nginx-ingress 要从外部访问您的服务,您应该实现入站服务,例如nginx-ingress

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

Sidecar 边门

If your 2 services are tightly coupled, you can include both in the same pod using the Kubernetes Sidecar feature. 如果您的2项服务紧密耦合,您可以使用Kubernetes Sidecar功能将它们包含在同一个pod中。 In this case, both containers in the pod would share the same virtual network adapter and accessible via localhost:$port 在这种情况下,pod中的两个容器将共享相同的虚拟网络适配器,并可通过localhost:$port

https://kubernetes.io/docs/concepts/workloads/pods/pod/#uses-of-pods https://kubernetes.io/docs/concepts/workloads/pods/pod/#uses-of-pods


Service Discovery 服务发现

When a Pod is run on a Node, the kubelet adds a set of environment variables for each active Service. 当Pod在节点上运行时,kubelet为每个活动服务添加一组环境变量。 It supports both Docker links compatible variables (see makeLinkVariables) and simpler {SVCNAME}_SERVICE_HOST and {SVCNAME}_SERVICE_PORT variables, where the Service name is upper-cased and dashes are converted to underscores. 它支持Docker链接兼容变量(请参阅makeLinkVariables)和更简单的{SVCNAME} _SERVICE_HOST和{SVCNAME} _SERVICE_PORT变量,其中服务名称为大写,短划线转换为下划线。

Read more about service discovery here: https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables 在此处阅读有关服务发现的更多信息: https//kubernetes.io/docs/concepts/services-networking/service/#environment-variables

You should be able to reach hello-def-service from pods in hello-abc via DNS as specified here: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services 您应该能够通过DNS在hello-abc通过pod获取hello-def-service ,如下所示: https//kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services

However, kube-dns or CoreDNS has to be configured/installed in your k8s cluster before DNS records can be utilized in your cluster. 但是,在群集中使用DNS记录之前,必须在k8s群集中配置/安装kube-dnsCoreDNS

Specifically, you should be reach hello-def-service via the DNS record http://hello-def-service for the service running in the same namespace as hello-abc-service 具体来说,您应该达到hello-def-service通过DNS记录http://hello-def-service在相同的命名空间运行服务hello-abc-service

And you should be able to reach hello-def-service running in another namespace ohter_namespace via the DNS record hello-def-service.other_namespace.svc.cluster.local . 您应该能够通过DNS记录hello-def-service.other_namespace.svc.cluster.local在另一个名称空间ohter_namespace运行hello-def-service

If, for some reason, you do not have DNS add-ons installed in your cluster, you still can find the virtual IP of the hello-def-service via environment variables in hello-abc pods. 如果由于某种原因,您没有在群集中安装DNS加载项,您仍然可以通过hello-abc pod中的环境变量找到hello-def-service的虚拟IP。 As is documented here . 正如此处所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM