Interconnecting Jenkins with CCE Clusters

Installing Required Plug-ins

On Jenkins, choose System Settings > Plugin Manager. On the Available tab page, install the Kubernetes Cli and Kubernetes Continuous Deploy plug-ins.

Plug-in versions:

  • Kubernetes Continuous Deploy Plugin: 2.3.0
  • Kubernetes CLI Plugin: 1.8.3

You can also manually download and install the plug-ins.

http://updates.jenkins-ci.org/download/plugins/kubernetes-cd/

http://updates.jenkins-ci.org/download/plugins/kubernetes-cli/

Adding a Slave Node to Jenkins

Log in to Jenkins, choose Manage Jenkins > Manage Nodes, click New Node, and enter the node name slave.

Configure the node information.

Remote working directory: /home/jenkins/agent

Boot mode: Launch agent via execution of command on the master

Launch command: docker run -i -u root --rm --name agent -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker -v /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7 jenkins/agent java -jar /usr/share/jenkins/agent.jar

Configuring and Calling Kubernetes in the Pipeline Script

Import the kubeconfig configuration file to Jenkins.

  1. Create a task to build a freestyle project.

  2. Add the Jenkins credential and import the kubeconfig file of the CCE cluster.

    Build Environment - Configure Kubernetes CLI (kubectl)

    You can obtain the kubeconfig from the CCE cluster details page on HUAWEI CLOUD:

    Log in to the CCE console. In the navigation pane, choose Resource Management > Clusters. Click the name of the cluster where Jenkins is located. On the cluster details page, click the Kubectl tab and download the kubectl configuration file.

Creating a Pipeline to Connect to the Kubernetes Cluster

For details about how to use the Kubernetes Continuous Deploy plugin, visit the following website:

https://www.jenkins.io/doc/pipeline/steps/kubernetes-cd/

The detailed configuration is as follows:

node('slave'){ 
       stage('Clone') { 
              echo "1.Clone Stage" 
              git url: "https://github.com/lookforstar/jenkins-demo.git" 
              script { 
                     build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() 
              } 
       } 
       stage('Test') { 
       echo "2.Test Stage" 
       } 
       stage('Build') { 
              echo "3.Build Docker Image Stage" 
              sh "docker build -t swr.cn-east-3.myhuaweicloud.com/batch/jenkins-demo:${build_tag} ." 
       } 
       stage('Push') { 
              echo "4.Push Docker Image Stage" 
              sh "docker login -u cn-east-3@82EFQWQMJWSDOYS9BQNM -p 71b0cceac30a10b99a960360b2e1deb04ec2f1bb00e88bc5eb69f3e9fd6a6d08 swr.cn-east-3.myhuaweicloud.com" 
              sh "docker push swr.cn-east-3.myhuaweicloud.com/batch/jenkins-demo:${build_tag}" 
       } 
          stage('Deploy') {
        echo "5. Deploy Stage"
        echo "This is a deploy step to test"
        sh "sed -i 's/<BUILD_TAG>/${build_tag}/' *.yaml"
        sh "cat *.yaml"
        echo "begin to config kubenetes"
        try { 
         kubernetesDeploy(
             kubeconfigId: "sample",
             configs: "k8s.yaml")
         println "hooray, success"
        } catch (e) {
            println "oh no! Deployment failed! "
            println e
        }
        }
}

Checking the Deployment Status

Click Build Now on Jenkins and view the build result.

Log in to the SWR console and view information about the built image.

Log in to the CCE console and view the workload information.