I have the following pods hello-abc and hello-def.

And I want to send data from hello-abc to hello-def.

How would pod hello-abc know the IP address of hello-def?

And I want to do this programmatically.

What's the easiest way for hello-abc to find where 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.

Unfortunately if kube-dns is not configured in your cluster (although it is unlikely) you cannot refer to it by name.

You can read more about DNS records for services here

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.

Solution

This means, to reach hello-def you can do so like this

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

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.

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.

This would require you to access your service by node-ip:nodePort

You can find out the NodePort which was randomly assigned to your service with kubectl describe svc/hello-def-service

Ingress

To reach your service from outside you should implement an ingress service such as nginx-ingress

Sidecar

If your 2 services are tightly coupled, you can include both in the same pod using the Kubernetes Sidecar feature. In this case, both containers in the pod would share the same virtual network adapter and accessible via localhost:$port

Service Discovery

When a Pod is run on a Node, the kubelet adds a set of environment

variables for each active Service. 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.

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐