Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins and Kubernetes Integration using with Helm

Tags:

I would like to integrate our Jenkins and Kubernetes clusters which works different servers.I have 2 cluster per stage and production. I already create a 2 name spaces on stage cluster to divide development and stage. I divide my values.yaml such as below.

  • valeus.dev.yaml
  • values.stage.yaml
  • values.prod.yaml

So according to the GIT_BRANCH value; I would like to set namespace variable and deploy via helm install command. At this circumstances,

My question is, what is the best way to connect 2 cluster in Jenkinsfile for this condition cause for dev and test namespace I need to one cluster , for production I need to deploy another cluster.

 stage('deploy') {
   steps {
      script {
        if (env.GIT_BRANCH == "origin/master") {
            def namepsace="dev"
            sh "helm upgrade --install -f values.dev.yaml --namespace ${namespace}"
        } else if (env.GIT_BRANCH =="origin/test"){
            def namepsace="stage"
            sh "helm upgrade --install -f values.stage.yaml --namespace ${namespace}"

        } else { 
            def namepsace="prod"
            sh "helm upgrade --install -f values.prod.yaml --namespace ${namespace}"
        }
like image 473
semural Avatar asked Jan 15 '20 09:01

semural


People also ask

How does helm work with Kubernetes?

How Does Helm Work? Helm and Kubernetes work like a client/server application. The Helm client pushes resources to the Kubernetes cluster. The server-side depends on the version: Helm 2 uses Tiller while Helm 3 got rid of Tiller and entirely relies on the Kubernetes API.


1 Answers

you will need to create the Jenkins secrets to add both kubeconfig files for your k8s Clusters, and in the if statement you load the kubeconfig for your environment

for example using your code above

stage('deploy') {
  steps {
    script {
      if (env.GIT_BRANCH == "origin/master") {
        def namepsace="dev"
        withCredentials([file(credentialsId: 'kubeconfig-dev', variable: 'config')]) {
          sh """
          export KUBECONFIG=\${config}
          helm upgrade --install -f values.dev.yaml --namespace ${namespace}"
          """
        }
      } else if (env.GIT_BRANCH =="origin/test"){
        def namepsace="stage"
        withCredentials([file(credentialsId: 'kubeconfig-stage', variable: 'config')]) {
          sh """
          export KUBECONFIG=\${config}
          helm upgrade --install -f values.dev.yaml --namespace ${namespace}"
          """
        }
      } else {
        def namepsace="prod"
        withCredentials([file(credentialsId: 'kubeconfig-prod', variable: 'config')]) {
          sh """
          export KUBECONFIG=\${config}
          helm upgrade --install -f values.dev.yaml --namespace ${namespace}"
          """
        }
      }
    }
  }
}
like image 185
cpanato Avatar answered Sep 27 '22 16:09

cpanato