首页 > 解决方案 > GET_HOSTS_FROM 变量的值是多少?

问题描述

我正在关注 Kubernetes 教程:https ://kubernetes.io/docs/tutorials/stateless-application/guestbook/#creating-the-redis-master-service

但是有一条线我不明白。在前端部署中有 GET_HOSTS_FROM 变量。它的值为“dns”。是进一步评估还是保留为“dns”?

这是整个相应的 yaml:

#application/guestbook/frontend-deployment.yaml 

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: frontend
  labels:
    app: guestbook
spec:
  selector:
    matchLabels:
      app: guestbook
      tier: frontend
  replicas: 3
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google-samples/gb-frontend:v4
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # Using `GET_HOSTS_FROM=dns` requires your cluster to
          # provide a dns service. As of Kubernetes 1.3, DNS is a built-in
          # service launched automatically. However, if the cluster you are using
          # does not have a built-in DNS service, you can instead
          # access an environment variable to find the master
          # service's host. To do so, comment out the 'value: dns' line above, and
          # uncomment the line below:
          # value: env
        ports:
        - containerPort: 80

标签: kubernetes

解决方案


的值GET_HOSTS_FROM不会进一步评估 - 它仍然是“dns”。

在这里查看应用程序的源代码,GET_HOSTS_FROM用于确定 Redis 主服务器和从服务器的主机是否来自环境,或者默认情况下是服务器和服务器的 Kubernetes 服务名称:

  $host = 'redis-master';
  if (getenv('GET_HOSTS_FROM') == 'env') {
    $host = getenv('REDIS_MASTER_SERVICE_HOST');
  }

当主机名是 Kubernetes 服务名称时,它们将使用集群的DNS进行解析。

值得一提的是 pod 是如何引用相同命名空间和不同命名空间中的服务的(摘自上面给出的文档的链接):

...如果您在 Kubernetes 命名空间“my-ns”中有一个名为“my-service”的服务,则控制平面和 DNS 服务一起为“my-service.my-ns”创建一个 DNS 记录。“my-ns”命名空间中的 Pod 应该能够通过简单地为 my-service 进行名称查找来找到它(“my-service.my-ns”也可以)。


推荐阅读