You are viewing documentation for KubeSphere version:v3.0.0
KubeSphere v3.0.0 documentation is no longer actively maintained. The version you are currently viewing is a static snapshot. For up-to-date documentation, see the latest version.
Deploy Apps in a Multi-cluster Project Using a Jenkinsfile
Prerequisites
- You need to enable the multi-cluster feature.
- You need to have a Docker Hub account.
- You need to enable the 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, this multi-cluster project is created on the host cluster and one member cluster), and an account (
project-regular
). This account needs to be invited to the DevOps project and the multi-cluster project with the roleoperator
. For more information, see Create Workspaces, Projects, Accounts and Roles, Multi-cluster Management and Multi-cluster Projects.
Create a Docker Hub Access Token
-
Log in to Docker Hub and select Account Settings from the menu in the top right corner.
-
Click Security and New Access Token.
-
Enter the token name and click Create.
-
Click Copy and Close and remember to save the access token.
Create Credentials
You need to create credentials in KubeSphere for the access token created so that the pipeline can interact with Docker Hub for imaging pushing. Besides, you also need to create kubeconfig credentials for the access to the Kubernetes cluster.
-
Log in to the web console of KubeSphere as
project-regular
. Go to your DevOps project and click Create in Credentials. -
In the dialog that appears, set a Credential ID, which will be used later in the Jenkinsfile, and select Account Credentials for Type. Enter your Docker Hub account name for Username and the access token just created for Token/Password. When you finish, click OK.
Tip
For more information about how to create credentials, see Credential Management. -
Click Create again and select kubeconfig for Type. Note that KubeSphere automatically populates the Content field, which is the kubeconfig of the current user account. Set a Credential ID and click OK.
Create a Pipeline
With the above credentials ready, you can create a pipeline using an example Jenkinsfile as below.
-
To create a pipeline, click Create on the Pipelines page.
-
Set a name in the pop-up window and click Next directly.
-
In this tutorial, you can use default values for all the fields. In Advanced Settings, click Create directly.
Edit the Jenkinsfile
-
In the pipeline list, click this pipeline to go to its detail page. Click Edit Jenkinsfile to define a Jenkinsfile and your pipeline runs based on it.
-
Copy and paste all the content below to the pop-up window as an example Jenkinsfile for your pipeline. You must replace the value of
DOCKERHUB_USERNAME
,DOCKERHUB_CREDENTIAL
,KUBECONFIG_CREDENTIAL_ID
,MULTI_CLUSTER_PROJECT_NAME
, andMEMBER_CLUSTER_NAME
with yours. When you finish, click OK.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 edit 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
If your pipeline runs successfully, images will be pushed to Docker Hub. If you are using Harbor, you cannot pass the parameter todocker login -u
via the Jenkins credential with environment variables. This is because every Harbor robot account username contains a$
character, which will be converted to$$
by Jenkins when used by environment variables. Learn more.
Run the Pipeline
After you save the Jenkinsfile, click Run. If everything goes well, you will see the Deployment workload in your multi-cluster project.