website/content/en/docs/devops-user-guide/examples/multi-cluster-project-examp...
yuswift f1f336d6fa fix some grammar error
Signed-off-by: yuswift <yuswiftli@yunify.com>
2020-10-31 23:35:22 +08:00

4.9 KiB
Raw Blame History

title keywords description linkTitle weight
How to deloy applications cross multiple clusters kubernetes, docker, devops, jenkins, multiple cluster Deloy applications cross multiple clusters 300

Prerequisites

  • You need to enable multi-cluster.
  • You need to enable KubeSphere DevOps System on your host cluster.
  • You need to create a workspace with multiple clusters, a DevOps project on your host cluster, a multi-cluster project(in this tutorial, your are assumed to add host and only one member cluster to the multi-cluster project), and a project-regular user account, and this account needs to be invited into the DevOps project and the multi-cluster project. See multi-cluster and project-administration

Get Dockerhub Credential

Vist dockerhub, log into the site, click account settings.

Click Security and New Access Token.

Enter the name of the access token, then save it.

Click Copy and Close and remember to save the access token.

Create Credentials

Log into KubeSphere, enter into the created DevOps project on your host cluster and create the following credential under Project Management → Credentials:

The Username is your dockerhub username. Password is the access token you just copied.

After you have created your dockerhub credential, you still need create a kubeconfig type credential.

Create a pipeline

Fill in the pipeline's basic information in the pop-up window, enter the name of pipeline and set the others as default value.

Edit jenkins file

Click Edit Jenkins File button under your pipeline and paste the following text into the pop-up window. You need to replace DOCKERHUB_USERNAME, DOCKERHUB_CREDENTIAL, KUBECONFIG_CREDENTIAL_ID, PROJECT_NAME as yours.

pipeline {
  agent {
    node {
      label 'maven'
    }

  }
  
  environment {
    REGISTRY = 'docker.io'
    // username of dockerhub
    DOCKERHUB_USERNAME = 'yuswift'
    APP_NAME = 'devops-go-sample'
    // dockerhubid is the dockerhub credential id you created on ks console
    DOCKERHUB_CREDENTIAL = credentials('dockerhubid')
    // the kubeconfig credential id you created on ks console
    KUBECONFIG_CREDENTIAL_ID = 'multi-cluster'
    // mutli-cluster project name under your own workspace
    MULTI_CLUSTER_PROJECT_NAME = 'devops-with-go'
    // the member cluster name you want to deploy app on
    // in this tutorial, you are assumed to deploy app on host and only one member cluster
    // for more member clusters, please manifest/multi-cluster-deploy.yaml
    MEMBER_CLUSTER_NAME = 'c9'
  }  
  
  stages {
    stage('docker login') {
      steps {
        container('maven') {
          sh 'echo $DOCKERHUB_CREDENTIAL_PSW  | docker login -u $DOCKERHUB_CREDENTIAL_USR --password-stdin'
        }

      }
    }
    
    stage('build & push') {
      steps {
        container('maven') {
          sh 'git clone https://github.com/yuswift/devops-go-sample.git'
          sh 'cd devops-go-sample && docker build -t $REGISTRY/$DOCKERHUB_USERNAME/$APP_NAME .'
          sh 'docker push $REGISTRY/$DOCKERHUB_USERNAME/$APP_NAME'
        }
      }
    }
    
    stage('deploy app to multi cluster') {
      steps {
        container('maven') {
          script {
            withCredentials([
              kubeconfigFile(
                credentialsId: 'multi-cluster',
                variable: 'KUBECONFIG')
              ]) {
              sh 'envsubst < devops-go-sample/manifest/multi-cluster-deploy.yaml | kubectl apply -f -'
              }
            }
          }
        }
      }
    }
  }

Note:

  • You are assumed to push images into dockehub. If you are using Harbor robot account, you can not pass the parameter to docker login -u via jenkins credential with environment variable. Because every harbor-robot-account username contains a "$" character, which will be converted into "$$" by jenkins when used by environment varibles. See more about this.

Run the pipline

After you have saved the jenkins file, click the Run button. If everything goes well, you will see two deployment workload under your multi-cluster project.