k8s 查看ip地址属于哪个pod_Kubernetes从另一个Pod中查找Pod IP

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值