kubernetes how to get cluster domain (such as svc.cluster.local) inside pod?

9/28/2018

I deployed a squid proxy in each namespace cause I want to access the services from external via the squid proxy, thus I need to add the line below to the squid.conf so that I can access services just using service names:

append_domain .${namespace}.svc.cluster.local

Here is my problem:
I can get ${namespace} via metadata.namespace inside a pod, but how can I get the cluster domain ? Is it possible ?
I’ve tried this but it retruned an error when creating pod:

  - name: POD_CLUSERDOMAIN
    valueFrom:
      fieldRef:
        fieldPath: metadata.clusterName

Thanks for your help.

-- NOZUONOHIGH
kubernetes
metadata

2 Answers

9/28/2018

The cluster domain is configured from the kubelet parameters, it must be the same through the whole cluster, so, you can't get it from pod's metadata, just use it as it is: svc.cluster.local

-- Kun Li
Source: StackOverflow

10/8/2018

Alright, failed to get the current NAMESPACE inside a pod, but I find another way to reach the point -- retrieve the whole host domain from search domian in resolv.conf.

Here's the detail:

  • keep the Dockerfile unmodified
  • add a command item to deployment.yaml

    image: squid:3.5.20
    command: ["/bin/sh","-c"]
    args: [ "echo append_domain .$(awk -v s=search '{if($1 == s)print $2}' /etc/resolv.conf) >> /etc/squid/squid.conf; /usr/sbin/squid -N" ]

This will add a line like append_domain .default.svc.cluster.local to the end of file /etc/squid/squid.conf then we can access the services from external via the squid proxy just using service name now.

-- NOZUONOHIGH
Source: StackOverflow