Remove namespace from service and pod's hostname (fqdn)

10/22/2018

I am deploying separate Kubernetes cluster for each lifecycle i.e. one cluster for dev and a separate cluster for production and will be using namespaces only for logical segregation of application based on there task e.g. all DB in data namespace, UI app in web namespace etc.

Because of this design, there is no need to have namespaces in service or pod's FQDN as each service and deployment will be unique. Can anyone please suggest how I can remove namespaces from hostname e.g. if there is a service named my-service in default namespace I would like to resolve the service from other namespace using "my-service.svc.cluster.local" instead of "my-service.default.svc.cluster.local"

I am running kubernetes 1.10 and using coreDNS.

-- Vikrant Singh
coredns
dns
kubernetes

3 Answers

10/22/2018

The pods and service resources are namespaced. So you can't make them to be un-namespaced.

[iahmad@ijaz00]$ kubectl api-resources --namespaced=true
NAME                        SHORTNAMES   APIGROUP                    NAMESPACED   KIND
bindings                                                             true         Binding
configmaps                  cm                                       true         ConfigMap
endpoints                   ep                                       true         Endpoints
events                      ev                                       true         Event
limitranges                 limits                                   true         LimitRange
persistentvolumeclaims      pvc                                      true         PersistentVolumeClaim
pods                        po                                       true         Pod
podtemplates                                                         true         PodTemplate
replicationcontrollers      rc                                       true         ReplicationController
resourcequotas              quota                                    true         ResourceQuota
secrets                                                              true         Secret
serviceaccounts             sa                                       true         ServiceAccount
services                    svc                                      true         Service
controllerrevisions                      apps                        true         ControllerRevision
daemonsets                  ds           apps                        true         DaemonSet
deployments                 deploy       apps                        true         Deployment
replicasets                 rs           apps                        true         ReplicaSet
statefulsets                sts          apps                        true         StatefulSet
localsubjectaccessreviews                authorization.k8s.io        true         LocalSubjectAccessReview
horizontalpodautoscalers    hpa          autoscaling                 true         HorizontalPodAutoscaler
cronjobs                    cj           batch                       true         CronJob
jobs                                     batch                       true         Job
stacks                                   compose.docker.com          true         Stack
events                      ev           events.k8s.io               true         Event
daemonsets                  ds           extensions                  true         DaemonSet
deployments                 deploy       extensions                  true         Deployment
ingresses                   ing          extensions                  true         Ingress
networkpolicies             netpol       extensions                  true         NetworkPolicy
replicasets                 rs           extensions                  true         ReplicaSet
alertmanagers                            monitoring.coreos.com       true         Alertmanager
prometheuses                             monitoring.coreos.com       true         Prometheus
prometheusrules                          monitoring.coreos.com       true         PrometheusRule
servicemonitors                          monitoring.coreos.com       true         ServiceMonitor
networkpolicies             netpol       networking.k8s.io           true         NetworkPolicy
poddisruptionbudgets        pdb          policy                      true         PodDisruptionBudget
rolebindings                             rbac.authorization.k8s.io   true         RoleBinding
roles                                    rbac.authorization.k8s.io   true         Role
-- Ijaz Ahmad Khan
Source: StackOverflow

10/22/2018

If you are using the standard DNS config with CoreDNS or KubeDNS you can't change it as described in the other answers.

However, you can change the DNS using a StubDomain and a service discovery tool. One of the more popular ones is Consul and here's how to configure a stub domain with it.

Note that you will likely have to run your Consul cluster in Kubernetes if not the server, certainly you will need a consul agent sidecar for your pods.

-- Rico
Source: StackOverflow

10/22/2018

you can't, that is not how kubernetes is designed. You can call it with just my-service though as long as you're in the same namespace

-- Radek 'Goblin' Pieczonka
Source: StackOverflow