简体   繁体   English

如何在应用程序内部使用Kubernetes Pod或部署IP

[英]How to get kubernetes pods or deployment IP to be used inside application

I just tried setting up kubernetes on my bare server, 我只是尝试在裸机上设置kubernetes,

Previously I had successfully create my docker compose 以前我已经成功创建了docker compose

There are several apps : 有几个应用程序:

  • Apps A (docker image name : a-service) 应用程序A(docker映像名称:a-service)
  • Apps B (docker image name : b-service) 应用程序B(docker映像名称:b-service)

Inside Application A and B there are configs (actually there are apps A,B,C,D,etc lots of em) 在应用程序A和B中有配置(实际上有应用程序A,B,C,D等很多em)

The config file is something like this 配置文件是这样的

IPFORSERVICEA=http://a-service:port-number/path/to/something
IPFORSERVICEB=http://b-service:port-number/path/to/something

At least above config work in docker compose (the config is inside app level, which require to access another apps). 至少以上配置工作在docker compose中(该配置位于应用程序级别内,需要访问其他应用程序)。 Is there any chance for me to access another Kubernetes Service from another service ? 我是否有机会从其他服务访问另一个Kubernetes服务? As I am planning to create 1 app inside 1 deployment, and 1 service for each deployment. 正如我计划在1个部署中创建1个应用程序,并为每个部署创建1个服务。

Something like: 就像是:

App -> Deployment -> Service(i.e: NodePort,ClusterIP)

Thanks ! 谢谢 !

Is there any chance for me to access another Kubernetes Service from another service ? 我是否有机会从其他服务访问另一个Kubernetes服务?

Yes, you just need to specify DNS name of service ( type: ClusterIP works fine for this) you need to connect to as: 是的,您只需要指定服务的DNS名称( type: ClusterIP可以正常工作),您需要连接为:

<service_name>.<namespace>.svc.cluster.local

In this case such domain name will be correctly resolved into internal IP address of the service you need to connect to using built-in DNS. 在这种情况下,此类域名将被正确解析为您需要使用内置DNS连接到的服务的内部IP地址。

For example: 例如:

nginx-service.web.svc.cluster.local

where nginx-service - name of your service and web - is apps's namespace, so service yml definition can look like: 其中nginx-service您的服务和web的名称-是应用程序的名称空间,因此service yml定义如下所示:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: web
spec:
  ports:
  - name: http
    protocol: TCP
    port: 80
  selector:
    app: nginx
  type: ClusterIP

See official docs to get more information. 请参阅官方文档以获取更多信息。

Use Kubernetes service discovery . 使用Kubernetes 服务发现

Service discovery is the process of figuring out how to connect to a service. 服务发现是弄清楚如何连接到服务的过程。 While there is a service discovery option based on environment variables available, the DNS-based service discovery is preferable. 尽管存在基于可用环境变量的服务发现选项,但是基于DNS的服务发现是更可取的。 Note that DNS is a cluster add-on so make sure your Kubernetes distribution provides for one or install it yourself. 请注意,DNS是集群的附加组件,因此请确保您的Kubernetes发行版提供了该集群,或者您可以自行安装。

Service dicovery by example 服务发现示例

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

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