hostAliases element in Kubernetes Replication Controller

5/17/2018

I'm trying to 'push' entries into a containers /etc/hosts file by using the 'hostAliases' element of a Pod when defining a Replication Controller.

This is the Replication Controller definition:

{
  "kind": "ReplicationController",
  "apiVersion": "v1",
  "metadata": {
    "name": "my-test",
    "namespace": "default",
    "selfLink": "/api/v1/namespaces/default/replicationcontrollers/my-test",
    "uid": "1f80363b-5a1d-11e8-b352-08002725d524",
    "resourceVersion": "70872",
    "generation": 1,
    "creationTimestamp": "2018-05-17T21:56:16Z",
    "labels": {
      "name": "my-test"
    }
  },
  "spec": {
    "replicas": 2,
    "selector": {
      "name": "my-test"
    },
    "template": {
      "metadata": {
        "creationTimestamp": null,
        "labels": {
          "name": "my-test"
        }
      },
      "spec": {
        "containers": [
          {
            "name": "cont0",
            "image": "192.168.97.10:5000/test_img:kubes",
            "command": [
              "/usr/local/bin/php"
            ],
            "args": [
              "/opt/code/myscript.php",
              "myarg"
            ],
            "resources": {},
            "terminationMessagePath": "/dev/termination-log",
            "terminationMessagePolicy": "File",
            "imagePullPolicy": "IfNotPresent"
          }
        ],
        "hostAliases": [
          {
            "ip": "10.0.2.2",
            "hostnames": [ "mylaptop"],
          }
        ],
        "restartPolicy": "Always",
        "terminationGracePeriodSeconds": 30,
        "dnsPolicy": "ClusterFirst",
        "securityContext": {},
        "schedulerName": "default-scheduler"
      }
    }
  },
  "status": {
    "replicas": 2,
    "fullyLabeledReplicas": 2,
    "observedGeneration": 1
  }
}

However the hostAliases just seems to be ignored...

Any pointers on whether this is supported, and if not, any alternative methods of achieving the same?

-- KennetRunner
kubernetes

1 Answer

5/18/2018

I hope this is the solution, as I quickly verified it and it worked. I created a simple nginx deployment and then edited it to add the hostAliases entry:

With below snippet, I was getting an error and could not save the deployment

"hostAliases": [
          {
            "ip": "10.0.2.2",
            "hostnames": [ "mylaptop"],
          }
        ],

The problem is that comma , after the [ "mylaptop"] - once I removed it and then saved deployment:

"hostAliases": [
          {
            "ip": "10.0.2.2",
            "hostnames": [ "mylaptop"]
          }
        ],

I could see entry in the hosts file:

$kubectl exec -i -t nginx-5f6bbbdf97-m6d92 -- sh
# 
# 
# cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.8.4.147  nginx-5f6bbbdf97-m6d92

# Entries added by HostAliases.
10.0.2.2    mylaptop
-- Vishal Biyani
Source: StackOverflow