Deploy Apps in a Multi-cluster Project Using Jenkinsfile

Prerequisites

Create Docker Hub Access Token

  1. Sign in Docker Hub and select Account Settings from the menu in the top right corner.

    dockerhub-settings

  2. Click Security and New Access Token.

    dockerhub-create-token

  3. Enter the token name and click Create.

    dockerhub-token-ok

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

    dockerhub-token-copy

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.

  1. Log in the web console of KubeSphere as project-regular. Go to your DevOps project and click Create in Credentials.

    create-dockerhub-id

  2. In the dialogue 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.

    credential-docker-create

    提示

    For more information about how to create credentials, see Credential Management.

  3. 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-kubeconfig

Create a Pipeline

With the above credentials ready, you can create a pipeline using an example Jenkinsfile as below.

  1. To create a pipeline, click Create on the Pipelines page.

    create-pipeline

  2. Set a name in the pop-up window and click Next directly.

    set-pipeline-name

  3. In this tutorial, you can use default values for all the fields. In Advanced Settings, click Create directly.

    create-pipeline-2

Edit Jenkinsfile

  1. 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.

    edit-jenkinsfile

  2. 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, and MEMBER_CLUSTER_NAME with yours. When you finish, click OK.

    1. pipeline {
    2. agent {
    3. node {
    4. label 'maven'
    5. }
    6. }
    7. environment {
    8. REGISTRY = 'docker.io'
    9. // username of dockerhub
    10. DOCKERHUB_USERNAME = 'yuswift'
    11. APP_NAME = 'devops-go-sample'
    12. // ‘dockerhubid’ is the dockerhub credential id you created on ks console
    13. DOCKERHUB_CREDENTIAL = credentials('dockerhubid')
    14. // the kubeconfig credential id you created on ks console
    15. KUBECONFIG_CREDENTIAL_ID = 'multi-cluster'
    16. // mutli-cluster project name under your own workspace
    17. MULTI_CLUSTER_PROJECT_NAME = 'devops-with-go'
    18. // the member cluster name you want to deploy app on
    19. // in this tutorial, you are assumed to deploy app on host and only one member cluster
    20. // for more member clusters, please edit manifest/multi-cluster-deploy.yaml
    21. MEMBER_CLUSTER_NAME = 'c9'
    22. }
    23. stages {
    24. stage('docker login') {
    25. steps {
    26. container('maven') {
    27. sh 'echo $DOCKERHUB_CREDENTIAL_PSW | docker login -u $DOCKERHUB_CREDENTIAL_USR --password-stdin'
    28. }
    29. }
    30. }
    31. stage('build & push') {
    32. steps {
    33. container('maven') {
    34. sh 'git clone https://github.com/yuswift/devops-go-sample.git'
    35. sh 'cd devops-go-sample && docker build -t $REGISTRY/$DOCKERHUB_USERNAME/$APP_NAME .'
    36. sh 'docker push $REGISTRY/$DOCKERHUB_USERNAME/$APP_NAME'
    37. }
    38. }
    39. }
    40. stage('deploy app to multi cluster') {
    41. steps {
    42. container('maven') {
    43. script {
    44. withCredentials([
    45. kubeconfigFile(
    46. credentialsId: 'multi-cluster',
    47. variable: 'KUBECONFIG')
    48. ]) {
    49. sh 'envsubst < devops-go-sample/manifest/multi-cluster-deploy.yaml | kubectl apply -f -'
    50. }
    51. }
    52. }
    53. }
    54. }
    55. }
    56. }

    备注

    If your pipeline runs successfully, images will be pushed to Docker Hub. If you are using Harbor, you cannot pass the parameter to docker 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 Pipeline

After you save the Jenkinsfile, click Run. If everything goes well, you will see the Deployment workload in your multi-cluster project.

multi-cluster-ok