<p>We have multiple environments like dev, qa, prepod etc. We have namespaces based on environment. Right now we name the service with environment as suffix. e.g.,</p>

<pre><code>    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: k8s-order-service-develop-deployment
      namespace: dev
      labels:
        k8s-app: k8s-order-service-develop
    spec:
      selector:
        matchLabels:
          k8s-app: k8s-order-service-develop
</code></pre>

<p>Instead can I use the following in all namespaces? ie whether <code>deployment</code> is unique per namespace?</p>

<p>in dev env:</p>

<pre><code>    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: k8s-order-service-deployment
      namespace: dev
      labels:
        k8s-app: k8s-order-service
    spec:
      selector:
        matchLabels:
          k8s-app: k8s-order-service
</code></pre>

<p>in qa env:</p>

<pre><code>apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: k8s-order-service-deployment
      namespace: qa
      labels:
        k8s-app: k8s-order-service
    spec:
      selector:
        matchLabels:
          k8s-app: k8s-order-service
</code></pre>

jaks
<p>remove the namespace from the deployment definition and name it as deploy.yaml</p>

<pre><code> apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: k8s-order-service-develop-deployment
      labels:
        k8s-app: k8s-order-service-develop
    spec:
      selector:
        matchLabels:
          k8s-app: k8s-order-service-develop
</code></pre>

<p>then you can deploy it in a specific namespace using the below command</p>

<pre><code>kubectl create -f deploy.yaml -n &lt;namespace-name&gt;

ex: 
kubectl create -f deploy.yaml -n dev
kubectl create -f deploy.yaml -n qa
</code></pre>

<p>you can look at <a href="https://github.com/kubernetes-sigs/kustomize" rel="nofollow noreferrer">kustomize</a> for more options and felxibility</p>

<p>This way you can use same deployment files for different environments.
and each environment is isolated from the other</p>
