简体   繁体   English

如何在 k8s 中获取服务的 ip/地址

[英]How to get ip/address of service in k8s

I would like to create service A (redis instance) and service B (application).我想创建服务 A(redis 实例)和服务 B(应用程序)。

Application would like to use service A (redis).应用程序想使用服务 A (redis)。

How can I get some automaticaly address/url of service A inside k8s cluster without expose to internet?如何在不暴露于互联网的情况下,在 k8s 集群中自动获取服务 A 的地址/url?

Something like:就像是:

redis://service-a-url:6379

I don't know which technic of k8s should I use.我不知道我应该使用哪种 k8s 技术。

So for example your redis service should look like this:例如,您的 redis 服务应如下所示:

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.该服务是ClusterIP类型(因为如果您不在 yaml 文件中指定服务类型,默认情况下它将是 ClusterIP 类型),您不必从集群外部访问服务。 There are more types of service - find information here: services-kubernetes .还有更多类型的服务 - 在这里查找信息: services-kubernetes

Take a look: connecting-app-service , app-service-redis .看一下: connecting-app-serviceapp-service-redis

Kubernetes supports two modes of finding a Service - environment variables and DNS. Kubernetes 支持两种查找服务的模式——环境变量和 DNS。 Kubernetes has a specific DNS cluster addon Service that automatically assigns DNS names to other Services. Kubernetes 有一个特定的 DNS 集群插件服务,它自动将 DNS 名称分配给其他服务。

Every single Service created in the cluster has its own assigned DNS name.在集群中创建的每个服务都有自己分配的 DNS 名称。 A client Pod's DNS search list will include the Pod's own namespace and the cluster's default domain by default.默认情况下,客户端 Pod 的 DNS 搜索列表将包括 Pod 自己的命名空间和集群的默认域。 This is best illustrated by example:最好通过示例来说明这一点:

Assume a Service named example in the Kubernetes namespace ns .假设在 Kubernetes 命名空间ns中有一个名为example的服务。 A Pod running in namespace ns can look up this service by simply doing a DNS query for example .例如,在命名空间ns中运行的 Pod 可以通过简单地执行 DNS 查询example查找此服务。 A Pod running in namespace test can look up this service by doing a DNS query for example.ns .在命名空间test中运行的 Pod 可以通过对example.ns执行 DNS 查询来查找此服务。

Find more here: Kubernetes DNS-Based Service Discovery , dns-name-service .在此处查找更多信息: Kubernetes 基于 DNS 的服务发现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例如: redis.default.svc.cluster.local

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

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