This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

The fewer things a man needs, the greater his happiness; the more he wishes, the less freedom he has. ———— Gorky “My University”

With Jenkins installed and using in K8S following the previous article, I’ll show you how to configure Jenkins to implement CI/CD.

This process can not only implement CI/CD for Java projects, but also automatically build images, push images, and deploy to k8S clusters for front-end and Python projects.

1. Configure the plug-in update site as a domestic source

The default plugin update site: https://updates.jenkins.io/update-center.json

Modify the plug-in update site: https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

As shown in the figure below:

2. Download common plug-ins

Download common plug-ins as shown in the figure below:

3. Configure the Kubernetes cluster

Go to System management > Node Management > Configure Clouds

There is no configuration. Click “Connection Test” and you can find that K8S is successfully connected, as shown in the picture below:

K8s cluster configuration related, as shown in the following figure:

Configure the POD template as shown below:

Add the first container to the Pod template, Jenkins /inbound-agent, as Jenkins’ slave node, as shown below:

Add a second container, Docker, to the POD template to build the image that has been pushed, as shown below:

Add a third container to the POD template: maven: 3.8.1-openJDK-11, as shown in the figure below:

Add more containers as needed. For example, if you need to build a front-end project, you can add a Node :16.6.1-slim container, as shown below:

4. Configure caching for the Maven container and mount Settings.xml

To mount a custom Settings file to Jenkins Slave Pod, you can create a Config Map Volume and then configure it on the Pod Template.

Run the following command to create a custom Settings file:

kubectl -n default create configmap maven-config --from-file=settings.xml 
Copy the code

Click Add Volume under Volume and select Config Map Volume. The other is maven configuration. To persist dependencies and improve build speed, the configuration is as shown in the figure below:

5. Build and push container images using Docker

When you use Docker image push, you need to set the access permission of the image warehouse through the following ways:

Run the following command to log in to the image repository. A config.json file is generated when you log in to the image repository:

docker login -u <username> -p <password> registry.cn-hangzhou.aliyuncs.com
Copy the code

Use the generated config.json file in the Jenkins namespace to create secret named my-secret:

kubectl create secret generic jenkins-docker-cfg -n default --from-file=/root/.docker/config.json
Copy the code

Configure the mount volume and environment variables in the Pod Template of the Jenkins system:

6. Configure kubeconfig to access k8S

Configure access to kubeconfig of K8S, which is used when kubernetesDeploy is used in pipeline

  • [Manage Credentials] -> [Jenkins] -> [Global Credentials] -> [Add Credentials]

7. Set the user name and password for pulling the private warehouse image

  • [Manage Credentials] -> [Jenkins] -> [Global Credentials] -> [Add Credentials]

8. Test the DevOps build process

Create a Pipeline project as shown below:

Enter the following content in the pipeline script:

pipeline {
     // Define the build environment for which tag to use for this build
    agent{
        node{
          label 'slave-pipeline'
        }
      }

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
        // Pull the code
        stage('git clone') {
            steps {
                git branch: "master", credentialsId: "", url: "https://gitee.com/peterwd/devops-demo.git"}}// Run the source package command
        stage('Package'){
          steps{
              container("maven") {
                  sh "mvn --version"
                  sh "mvn clean package -DskipTests"}}}// Run container image build and push commands
        stage('Image Build And Publish'){
          steps{
              container('docker') {
                   sh 'docker version'
              }
          }
        }
        
        // Run the node build command
        stage('node'){
          steps{
              container('node') {
                   sh 'npm version'
              }
          }
        }
    }
}

Copy the code

As shown in the figure below:

Click Save and build, as shown below: