简体   繁体   中英

How to get ip/address of service in k8s

I would like to create service A (redis instance) and service B (application).

Application would like to use service A (redis).

How can I get some automaticaly address/url of service A inside k8s cluster without expose to internet?

Something like:

redis://service-a-url:6379

I don't know which technic of k8s should I use.

So for example your redis service should look like this:

apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    run: redis
spec:
  ports:
  - port: 6379
    targetPort: 6379
    protocol: TCP
  selector:
    run: redis

The service is type ClusterIP (because if you will not specify service type in yaml file by default it will be ClusterIP type) that you don't have to access service from the outside the cluster. There are more types of service - find information here: services-kubernetes .

Take a look: connecting-app-service , app-service-redis .

Kubernetes supports two modes of finding a Service - environment variables and DNS. Kubernetes has a specific DNS cluster addon Service that automatically assigns DNS names to other Services.

Every single Service created in the cluster has its own assigned DNS name. A client Pod's DNS search list will include the Pod's own namespace and the cluster's default domain by default. This is best illustrated by example:

Assume a Service named example in the Kubernetes namespace ns . A Pod running in namespace ns can look up this service by simply doing a DNS query for example . A Pod running in namespace test can look up this service by doing a DNS query for example.ns .

Find more here: Kubernetes DNS-Based Service Discovery , dns-name-service .

You will be able to access your service within the cluster using following record:

  • <service>.<ns>.svc.<zone>. <ttl>

For example: redis.default.svc.cluster.local

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