<p>I'm a newbie here, and i am in the process of preparing my development and production environment for a webapp Microservices architecture. I have injected environment variables through my deployment files in order to control the environment by which the app will be running. For example a Microservice running in development mode will work with development databases. So my question is, what do you think is the best practice to control multiple environment deployments. Should i have multiple deployment files, one for each environment for the same Microservice? Or is there a better practice?
Thanks</p>

<p>Typically you want that applications running on the <code>dev</code> environment don't interfere with applications running on the <code>production</code> environment. Using Kubernetes, you can get this kind of isolation <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/" rel="nofollow noreferrer">using different namespaces</a> for different environments. That way, objects on the <code>production</code> namespace are different from objects on the <code>dev</code> namespace. Another, more expensive approach would be to use different k8s clusters for different environments.</p>

<p>Having this setup, you would deploy your application in the namespace for the specific environment where you want to deploy to, creating the <code>Deployment</code> object on that namespace. This <code>Deployment</code> would make use of a <code>ConfigMap</code> object containing your application environment variables. The variables inside the <code>ConfigMap</code> will be different on each namespace/environment.
That way, your <code>Deployment</code> object is the same across different environments, which let's you be more confident that what you are testing on <code>dev</code> is the same that you will use on <code>production</code>. The <code>Deployment</code> object is the same, but it uses different variables to run.</p>

<p>Using <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">the example Pod from the documentation</a></p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: special-config
              # Specify the key associated with the value
              key: special.how
  restartPolicy: Never
</code></pre>

<p>Saving that content into <code>pod.yaml</code>, we can create it in our namespaces with <code>kubectl -n my-namespace-development create pod.yaml</code>, and <code>kubectl -n my-namespace-production create pod.yaml</code>.</p>

<p>This Pod would use a ConfigMap called <code>special-config</code>. This configmap can be created like <code>kubectl -n my-namespace-production create configmap special-config --from-literal=special.how=very</code> in production, and <code>kubectl -n my-namespace-development create configmap special-config --from-literal=special.how=very-dev</code>.</p>

<p>As you can see, the configmap content is different on each namespace. So the Pod created on the namespace called <code>my-namespace-production</code> will use the value <code>very</code>, but the pod created on the namespace <code>my-namespace-development</code> will use the value <code>very-dev</code>.</p>
