Create a Multi-cluster Pipeline

As cloud providers offer different hosted Kubernetes services, DevOps pipelines have to deal with use cases where multiple Kubernetes clusters are involved.

This tutorial demonstrates how to create a multi-cluster pipeline on KubeSphere.

Prerequisites

Workflow Overview

This tutorial uses three clusters to serve as three isolated environments in the workflow. See the diagram as below.

use-case-for-multi-cluster

The three clusters are used for development, testing, and production respectively. Once codes get submitted to a Git repository, a pipeline will be triggered to run through the following stages—Unit Test, SonarQube Analysis, Build & Push, and Deploy to Development Cluster. Developers use the development cluster for self-testing and validation. When developers give approval, the pipeline will proceed to the stage of Deploy to Testing Cluster for stricter validation. Finally, the pipeline, with necessary approval ready, will reach the stage of Deploy to Production Cluster to provide services externally.

Hands-on Lab

Step 1: Prepare clusters

See the table below for the role of each cluster.

Cluster NameCluster RoleUsage
hostHost clusterTesting
shireMember clusterProduction
rohanMember clusterDevelopment

Note

These Kubernetes clusters can be hosted across different cloud providers and their Kubernetes versions can also vary. Recommended Kubernetes versions for KubeSphere 3.4: v1.20.x, v1.21.x, * v1.22.x, * v1.23.x, * v1.24.x, * v1.25.x, and * v1.26.x. For Kubernetes versions with an asterisk, some features of edge nodes may be unavailable due to incompatability. Therefore, if you want to use edge nodes, you are advised to install Kubernetes v1.21.x.

Step 2: Create a workspace

  1. Log in to the web console of the host cluster as ws-manager. On the Workspaces page, click Create.

  2. On the Basic Information page, name the workspace devops-multicluster, select ws-admin for Administrator, and click Next.

  3. On the Cluster Settings page, select all three clusters and click Create.

  4. The workspace created is displayed in the list. You need to log out of the console and log back in as ws-admin to invite both project-admin and project-regular to the workspace and grant them the role workspace-self-provisioner and workspace-viewer respectively. For more information, refer to Create Workspaces, Projects, Users and Roles.

Step 3: Create a DevOps project

  1. Log out of the console and log back in as project-admin. Go to the DevOps Projects page and click Create.

  2. In the displayed dialog box, enter multicluster-demo for Name, select host for Cluster Settings, and then click OK.

    Note

    Only clusters with the DevOps component enabled will be available in the drop-down list.

  3. The DevOps project created is displayed in the list. Make sure you invite the project-regular user to this project and assign it the operator role. For more information, refer to Create Workspaces, Projects, Users and Roles.

Step 4: Create projects on clusters

You must create the projects as shown in the table below in advance. Make sure you invite the project-regular user to these projects and assign it the operator role. For more information about how to create a project, refer to Create Workspaces, Projects, Users and Roles.

Cluster NameUsageProject Name
hostTestingkubesphere-sample-prod
shireProductionkubesphere-sample-prod
rohanDevelopmentkubesphere-sample-dev

Step 5: Create credentials

  1. Log out of the console and log back in as project-regular. On the DevOps Projects page, click the DevOps project multicluster-demo.

  2. On the Credentials page, you need to create the credentials as shown in the table below. For more information about how to create credentials, refer to Credential Management and Create a Pipeline Using a Jenkinsfile.

    Credential IDTypeWhere to Use
    hostkubeconfigThe host cluster for testing
    shirekubeconfigThe member cluster for production
    rohankubeconfigThe member cluster for development
    dockerhub-idUsername and passwordDocker Hub
    sonar-tokenAccess tokenSonarQube

    Note

    You have to manually enter the kubeconfig of your member clusters when creating the kubeconfig credentials shire and rohan. Make sure your host cluster can access the API Server addresses of your member clusters.

  3. Five credentials are created in total.

Step 6: Create a pipeline

  1. Go to the Pipelines page and click Create. In the displayed dialog box, enter build-and-deploy-application for Name and click Next.

  2. On the Advanced Settings tab, click Create to use the default settings.

  3. The pipeline created is displayed in the list. Click its name to go to the details page.

  4. Click Edit Jenkinsfile and copy and paste the following contents. Make sure you replace the value of DOCKERHUB_NAMESPACE with your own value, and then click OK.

    1. pipeline {
    2. agent {
    3. node {
    4. label 'maven'
    5. }
    6. }
    7. parameters {
    8. string(name:'BRANCH_NAME',defaultValue: 'master',description:'')
    9. }
    10. environment {
    11. DOCKER_CREDENTIAL_ID = 'dockerhub-id'
    12. PROD_KUBECONFIG_CREDENTIAL_ID = 'shire'
    13. TEST_KUBECONFIG_CREDENTIAL_ID = 'host'
    14. DEV_KUBECONFIG_CREDENTIAL_ID = 'rohan'
    15. REGISTRY = 'docker.io'
    16. DOCKERHUB_NAMESPACE = 'your Docker Hub account ID'
    17. APP_NAME = 'devops-maven-sample'
    18. SONAR_CREDENTIAL_ID = 'sonar-token'
    19. TAG_NAME = "SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER"
    20. }
    21. stages {
    22. stage('checkout') {
    23. steps {
    24. container('maven') {
    25. git branch: 'master', url: 'https://github.com/kubesphere/devops-maven-sample.git'
    26. }
    27. }
    28. }
    29. stage('unit test') {
    30. steps {
    31. container('maven') {
    32. sh 'mvn clean test'
    33. }
    34. }
    35. }
    36. stage('sonarqube analysis') {
    37. steps {
    38. container('maven') {
    39. withCredentials([string(credentialsId: "$SONAR_CREDENTIAL_ID", variable: 'SONAR_TOKEN')]) {
    40. withSonarQubeEnv('sonar') {
    41. sh "mvn sonar:sonar -Dsonar.login=$SONAR_TOKEN"
    42. }
    43. }
    44. }
    45. }
    46. }
    47. stage('build & push') {
    48. steps {
    49. container('maven') {
    50. sh 'mvn -Dmaven.test.skip=true clean package'
    51. sh 'docker build -f Dockerfile-online -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
    52. withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$DOCKER_CREDENTIAL_ID" ,)]) {
    53. sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
    54. sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
    55. }
    56. }
    57. }
    58. }
    59. stage('push latest') {
    60. steps {
    61. container('maven') {
    62. sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:latest '
    63. sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:latest '
    64. }
    65. }
    66. }
    67. stage('deploy to dev') {
    68. steps {
    69. container('maven') {
    70. withCredentials([
    71. kubeconfigFile(
    72. credentialsId: env.DEV_KUBECONFIG_CREDENTIAL_ID,
    73. variable: 'KUBECONFIG')
    74. ]) {
    75. sh 'envsubst < deploy/dev-all-in-one/devops-sample.yaml | kubectl apply -f -'
    76. }
    77. }
    78. }
    79. }
    80. stage('deploy to staging') {
    81. steps {
    82. container('maven') {
    83. input(id: 'deploy-to-staging', message: 'deploy to staging?')
    84. withCredentials([
    85. kubeconfigFile(
    86. credentialsId: env.TEST_KUBECONFIG_CREDENTIAL_ID,
    87. variable: 'KUBECONFIG')
    88. ]) {
    89. sh 'envsubst < deploy/prod-all-in-one/devops-sample.yaml | kubectl apply -f -'
    90. }
    91. }
    92. }
    93. }
    94. stage('deploy to production') {
    95. steps {
    96. container('maven') {
    97. input(id: 'deploy-to-production', message: 'deploy to production?')
    98. withCredentials([
    99. kubeconfigFile(
    100. credentialsId: env.PROD_KUBECONFIG_CREDENTIAL_ID,
    101. variable: 'KUBECONFIG')
    102. ]) {
    103. sh 'envsubst < deploy/prod-all-in-one/devops-sample.yaml | kubectl apply -f -'
    104. }
    105. }
    106. }
    107. }
    108. }
    109. }

    Note

    The flag -o in the mvn commands indicates that the offline mode is enabled. If you have relevant maven dependencies and caches ready locally, you can keep the offline mode on to save time.

  5. After the pipeline is created, you can view its stages and steps on the graphical editing panel as well.

Step 7: Run the pipeline and check the results

  1. Click Run to run the pipeline. The pipeline will pause when it reaches the stage deploy to staging as resources have been deployed to the cluster for development. You need to manually click Proceed twice to deploy resources to the testing cluster host and the production cluster shire.

  2. After a while, you can see the pipeline status shown as Successful.

  3. Check the pipeline running logs by clicking View Logs in the upper-right corner. For each stage, you click it to inspect logs, which can be downloaded to your local machine for further analysis.

  4. Once the pipeline runs successfully, click Code Check to check the results through SonarQube.

  5. Go to the Projects page, and you can view the resources deployed in different projects across the clusters by selecting a specific cluster from the drop-down list.

Feedback

Was this page Helpful?

Yes No