Uniqueness of deployment definition in kubernetes namespace

4/24/2019

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.,

    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

Instead can I use the following in all namespaces? ie whether deployment is unique per namespace?

in dev env:

    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

in qa env:

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
-- jaks
kubernetes
namespaces

2 Answers

4/24/2019

You definitely can create same deployments in different namespace. Just be careful when updating deployment in incorrect environment/namespace. Using namespace as a part of shell prompt may be useful.

-- Vasily Angapov
Source: StackOverflow

4/24/2019

remove the namespace from the deployment definition and name it as deploy.yaml

 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

then you can deploy it in a specific namespace using the below command

kubectl create -f deploy.yaml -n <namespace-name>

ex: 
kubectl create -f deploy.yaml -n dev
kubectl create -f deploy.yaml -n qa

you can look at kustomize for more options and felxibility

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

-- P Ekambaram
Source: StackOverflow