<p>Currently our k8s project has following use case were the namespaces are hardcoded into the values.yaml and application source codes</p>

<pre><code>(apps) namespace - NS1                 

&gt; micro-service-A1

&gt; micro-service-A2

&gt; micro-service-A3

(database) namespace - DB1

&gt; mongo-service

(messaging) namespace - MB1

&gt; kafka-zk-service
</code></pre>

<p>We want to run multiple sets of above services(apps, database, messaging) in unique namespaces defined by each Engineer(Developer)
 such that each developer can safely bring down/play-around changing  the complete set belonging to him without worrying impact on other Developers' namespace.</p>

<p><strong># Developer1 (set)</strong></p>

<hr>

<pre><code>(apps) namespace - Dev1   

&gt; micro-service-A1

&gt; micro-service-A2

&gt; micro-service-A3

(database) namespace - Dev1_DB

&gt; mongo-service

(messaging) namespace - Dev1_MB

&gt; kafka-zk-service
</code></pre>

<p><strong># Developer2 (set)</strong></p>

<hr>

<pre><code>(apps) namespace - Dev2                

&gt; micro-service-A1

&gt; micro-service-A2

&gt; micro-service-A3

(database) namespace - Dev2_DB

&gt; mongo-service

(messaging) namespace - Dev2_MB                     

&gt; kafka-zk-service
</code></pre>

<p>What should be the configuration of the yamls &amp; application source code such that dynamic deployment is feasible in any namespace of developers choice?</p>

<h2>Externalize configuration</h2>

<p>It is good to <em>externalize your configuration</em> so that you can use a different configuration without building a new image.</p>

<p>Use a ConfigMap for configuration with address e.g. to other services or databases. See <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">DNS for Services and Pods</a> for adressing.</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: config
data:
  SERVICE_A: service-a.a-namespace.svc.cluster.local
  SERVICE_B: service-b.b-namespace.svc.cluster.local
  DB: db.local
</code></pre>

<p>Use the values from your ConfigMap as <em>environment variables</em> in your app by mapping it in your <code>Pod</code> or <code>Deployment</code> in the PodTemplate</p>

<pre><code>  containers:
    - name: app-container
      image: k8s.gcr.io/app-image
      env:
        - name: SERVICE_A_ADDRESS
          valueFrom:
            configMapKeyRef:
              name: config
              key: SERVICE_A
        - name: SERVICE_B_ADDRESS
          valueFrom:
            configMapKeyRef:
              name: config
              key: SERVICE_B
</code></pre>

<h2>Service with External Name</h2>

<p>If you want to move a service to a new namespace but keep the addressing, you can leave a <a href="https://kubernetes.io/docs/concepts/services-networking/service/#externalname" rel="nofollow noreferrer">Service with External Name</a></p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: service-a
  namespace: namespace-a
spec:
  type: ExternalName
  externalName: service-c.namespace-c.svc.cluster.local
  ports:
  - port: 80
</code></pre>
