dockerkuberneteskubectl

How to add custom host entries to kubernetes Pods?


My application communicates to some services via hostnames. When running my application as a docker container i used to add hostnames to the /etc/hosts of the hostmachine and run the container using --net=host.

Now I'm running my containers in kubernetes cluster. I would like to know how can i add the /etc/hosts entries to the pod via yaml.

I'm using kubernetes v1.5.3.


Solution

  • This works and also looks simpler:

    kind: Service
    apiVersion: v1 
    metadata:
        name: {HOST_NAME} 
        spec:
          ports:
            - protocol: TCP
              port: {PORT}
              targetPort: {PORT}
          type: ExternalName
          externalName: {EXTERNAL_IP}
    

    Now you can use the HOST_NAME from the pod directly to access the external machine.