简体   繁体   中英

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

I just tried setting up kubernetes on my bare server,

Previously I had successfully create my docker compose

There are several apps :

  • Apps A (docker image name : a-service)
  • Apps B (docker image name : b-service)

Inside Application A and B there are configs (actually there are apps A,B,C,D,etc lots of 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). Is there any chance for me to access another Kubernetes Service from another service ? As I am planning to create 1 app inside 1 deployment, and 1 service for each deployment.

Something like:

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

Thanks !

Is there any chance for me to access another Kubernetes Service from another service ?

Yes, you just need to specify DNS name of service ( type: ClusterIP works fine for this) you need to connect to as:

<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.

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:

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 .

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. Note that DNS is a cluster add-on so make sure your Kubernetes distribution provides for one or install it yourself.

Service dicovery by example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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