'How to Deploy from Gitlab-ci to multiple kubernetes namespaces?

I have two variable containing my namespaces names:

$KUBE_NAMESPACE_DEV ="stellacenter-dev"
$KUBE_NAMESPACE_STAGE "stellacenter-stage-uat" 

Now I want to modify the following .gitlab-ci.yaml configuration to include the namespace logic:

deploy_dev:
  stage: deploy
  image: stellacenter/aws-helm-kubectl
  before_script:
    - aws configure set aws_access_key_id ${DEV_AWS_ACCESS_KEY_ID}
    - aws configure set aws_secret_access_key ${DEV_AWS_SECRET_ACCESS_KEY}
    - aws configure set region ${DEV_AWS_DEFAULT_REGION}
  script:
    - sed -i "s/<VERSION>/${CI_COMMIT_SHORT_SHA}/g" provider-service.yml     
    - mkdir -p  $HOME/.kube
    - cp $KUBE_CONFIG_DEV $HOME/.kube/config
    - chown $(id -u):$(id -g) $HOME/.kube/config 
    - export KUBECONFIG=$HOME/.kube/config
    - kubectl apply -f ./provider-service.yml 
  only:
    - developer  

Provide-service.yml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: provider-app
  namespace: "stellacenter-dev" or "stellacenter-stage-uat" 
  labels:
    app: provider-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app : provider-app
  template:
    metadata:
      labels:
        app: provider-app
    spec:
      containers:
      - name: provider-app
        image: registry.gitlab.com/stella-center/backend-services/provider-service:<VERSION>
        imagePullPolicy: Always
        ports:
          - containerPort: 8092
      imagePullSecrets:
        - name:  gitlab-registry-token-auth

---

apiVersion: v1
kind: Service
metadata:
  name:  provider-service
  namespace: "stellacenter-dev" "stellacenter-stage-uat" 
spec:
  type: NodePort
  selector:
    app:  provider-app
  ports:
  - port:  8092
    targetPort:  8092

I don't know how to integrate the variables and the values correctly . I'm facing the error while I run pipeline.Kindly help me to sort it out.



Solution 1:[1]

You can remove the namespace: NAMESPACE from the manifest, and apply the resource in a namespace using the commandline.

- kubectl apply -f ./provider-service.yml -n ${KUBE_NAMESPACE_DEV}
- kubectl apply -f ./provider-service.yml -n ${KUBE_NAMESPACE_STAGE}

Solution 2:[2]

Just add one line above the apply command

- export KUBECONFIG=$HOME/.kube/config
- kubectl apply -f ./provider-service.yml 

using sed you can replace the respective variable into YAML file

sed -i "s, NAMESPACE,$KUBE_NAMESPACE_DEV," Provide-service.yml

Inside that YAML file keep it something like

apiVersion: v1
kind: Service
metadata:
  name:  provider-service
  namespace: NAMESPACE
spec:
  type: NodePort

You can keep one variable instead of two for Namespace management however using the sed you can set the Namespace into the YAML and apply that YAML.

While inside your repo it will be like a template, when CI will run NAMESPACE will get replaced by sed command and YAML will get applied to k8s. Accordingly, you can also keep other things as templates and replace them with sed as per need.

    apiVersion: v1
    kind: Service
    metadata:
      name:  SERVICE_NAME
      namespace: NAMESPACE
    spec:
      type: SERVICE_TYPE

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kaizendae
Solution 2 Harsh Manvar