Kubernetes从另一个Pod中查找Pod IP [英] Kubernetes to find Pod IP from another Pod
问题描述
我有以下吊舱hello-abc
和hello-def
.
我想将数据从hello-abc
发送到hello-def
.
pod hello-abc
如何知道hello-def
的IP地址?
How would pod hello-abc
know the IP address of hello-def
?
我想以编程的方式做到这一点.
And I want to do this programmatically.
hello-abc
查找hello-def
位置的最简单方法是什么?
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
推荐答案
前言
由于已经定义了路由到每个部署的服务,因此,如果您已将服务和部署都部署到了相同的名称空间,则在许多现代kubernetes集群中,您可以利用kube-dns并简单地按名称引用该服务.
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.
不幸的是,如果群集中未配置kube-dns
(尽管不太可能),则无法按名称引用它.
Unfortunately if kube-dns
is not configured in your cluster (although it is unlikely) you cannot refer to it by name.
此外,Kubernetes具有服务发现"功能,该功能可将服务的端口和ips公开到部署在相同名称空间中的任何容器中.
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.
解决方案
这意味着,要达到hello-def,您可以像这样
This means, to reach hello-def you can do so like this
curl http://hello-def-service:${HELLO_DEF_SERVICE_PORT}
基于服务发现 https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables
注意事项:很有可能,如果服务端口发生更改,只有在相同名称空间中更改后创建的Pod才会收到新的环境变量.
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.
外部访问权限
此外,只要您可以从外部访问NodePort范围,就可以使用NodePort功能从外部获得此服务.
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.
这将要求您通过node-ip:nodePort
This would require you to access your service by node-ip:nodePort
您可以使用kubectl describe svc/hello-def-service
入口
要从外部获得服务,您应该实施一个入口服务,例如nginx-ingress
To reach your service from outside you should implement an ingress service such as nginx-ingress
https://github.com/helm/charts/tree /master/stable/nginx-ingress https://github.com/kubernetes/ingress-nginx
侧车
如果您的2个服务紧密结合,则可以使用Kubernetes Sidecar功能将这两个服务都包含在同一个容器中.在这种情况下,pod中的两个容器将共享同一个虚拟网络适配器,并且可以通过localhost:$port
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
https://kubernetes.io/docs/概念/工作量/吊舱/吊舱/#uses-of-pods
服务发现
在节点上运行Pod时,kubelet将添加一组环境 每个活动服务的变量.它支持两个Docker链接 兼容变量(请参见makeLinkVariables)且更简单 {SVCNAME} _SERVICE_HOST和{SVCNAME} _SERVICE_PORT变量,其中 服务名称为大写,破折号转换为下划线.
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.
在此处阅读有关服务发现的更多信息: https://kubernetes.io/docs/concepts/services-networking /service/#environment-variables
Read more about service discovery here: https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables
这篇关于Kubernetes从另一个Pod中查找Pod IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!