What is the value of GET_HOSTS_FROM variable?

11/6/2019

I am following Kubernetes tutorial: https://kubernetes.io/docs/tutorials/stateless-application/guestbook/#creating-the-redis-master-service

However there is one line i do not understand. In frontend-deployment there is GET_HOSTS_FROM variable. Its value is "dns". Is it evaluated further or does it remain as "dns"?

This is the whole corresponding 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
-- hal
kubernetes

1 Answer

11/6/2019

The value for the GET_HOSTS_FROM isn't evaluated any further - it remains as "dns".

Looking at the application's source code here, GET_HOSTS_FROM is used to determine if the hosts for the Redis primary and slave will come from the environment or, by default, be the Kubernetes service names for the primary and the slave:

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

When the host names are Kubernetes Service names, they will be resolved using cluster's DNS.

It is worth mentioning how pods can reference Services in the same vs. a different namespace (the excerpt is from the link to the docs given above):

...if you have a Service called "my-service" in a Kubernetes Namespace "my-ns", the control plane and the DNS Service acting together create a DNS record for "my-service.my-ns". Pods in the "my-ns" Namespace should be able to find it by simply doing a name lookup for my-service ("my-service.my-ns" would also work).

-- apisim
Source: StackOverflow