DevOps integrates Jenkins+k8s

Table of contents

1. Introduction to DevOps

2. Install git tools

3. Install jdk, maven, Jenkins

4. Jenkins implements basic pull operations

5. Jenkins implementation-sonarqbue code quality detection deployment

6. Jenkins realizes making custom images and pushing harbor deployment

7. First experience with Jenkins pipeline

8. Pipeline in Jenkins - Packaging quality detection, push docker deployment, and enterprise WeChat push messages

9. Pipeline in Jenkins, k8s deployment, and enterprise WeChat push messages

10. Jenkins automated pipeline, k8s deployment, and enterprise WeChat push messages


1. Introduction to DevOps

Software development initially consisted of two teams:

  • The development plan is designed and built from scratch by the development team. The system requires constant iterative updates.

  • The operation and maintenance team will test the code of the development team and then deploy it online. I hope the system operates stably and safely.

It seems like two teams with different goals need to work together to complete the development of a software.

After the development team specifies the plan and completes coding, it needs to be provided to the operation and maintenance team.

The operation and maintenance team reports back to the development team the bugs that need to be fixed and some tasks that need to be reworked.

At this time, the development team needs to often wait for feedback from the operation and maintenance team. This undoubtedly prolongs the event and delays the entire software development cycle.

There will be a way for the development team to move on to the next project while they wait. Wait for feedback from the operations team on previous code.

But this means that a complete project will take a longer period to develop the final code.


Based on the current situation of the Internet, agile development is more recommended, which leads to faster iteration of the project. However, due to communication problems between the development team and the operation and maintenance team, the time cost of launching a new version will be very high. This goes against the original purpose of agile development.

So what if the development team and operation and maintenance team are integrated into one team to collaborate on a set of software? This is called DevOps.

DevOps literally means the abbreviation of Development & Operations, which is development & operation and maintenance.

Although the literal meaning only involves the development team and operation and maintenance team, in fact, the QA testing team is also involved.

You can see on the Internet that the DevOps symbol is similar to an infinity symbol.

This shows that DevOps is a process of continuous improvement of efficiency and continuous work

The DevOps approach allows companies to respond to updates and market changes more quickly, development can be delivered quickly, and deployment is more stable.

The core is to simplify the process between Dev and Ops teams and make the overall software development process faster.

The overall software development process includes:

  • PLAN: The development team develops a development plan based on the customer's goals

  • CODE: Starting the coding process according to the PLAN, different versions of the code need to be stored in a library.

  • BUILD: After the coding is completed, the code needs to be built and run.

  • TEST: After successfully building the project, you need to test the code for bugs or errors.

  • DEPLOY: After the code has been manually tested and automated tested, it is deemed that the code is ready for deployment and handed over to the operations team.

  • OPERATE: The operations team deploys the code to the production environment.

  • MONITOR: After the project is deployed and launched, continuous monitoring of the product is required.

  • INTEGRATE: The feedback received in the monitoring phase is then sent back to the PLAN phase. The overall iterative process is the core of DevOps, namely continuous integration and continuous deployment.

In order to ensure that the overall process can be completed efficiently, there are relatively common tools at each stage, as shown below:

Finally, DevOps can be defined: DevOps emphasizes how efficient organizational teams use automated tool collaboration and communication to complete software life cycle management, thereby delivering more stable software faster and more frequently.

Automated tool collaboration and communication to complete software life cycle management

2. Install git tools

serverA host installation

In the code stage, we need to store different versions of code in a warehouse. Common version control tools are SVN or Git. Here we use Git as the version control tool and GitLab as the remote warehouse.

2.1 Git installation

https://git-scm.com/ (fool-proof installation)

2.2 GitLab installation

Prepare the server separately and install it using Docker

  • View the GitLab mirror

    docker search gitlab
  • Pull the GitLab image

    docker pull gitlab/gitlab-ce
  • Prepare docker-compose.yml file

  • mkdir -p /data/git
    vim /data/git/docker-compose.yml

    version: '3.1'
    services:
      gitlab:
        image: 'gitlab/gitlab-ce:latest'
        container_name: gitlab
        restart: always
        environment:
          GITLAB_OMNIBUS_CONFIG: |
            external_url 'http://10.1.100.225:8929'#自己安装git的服务器IP
            gitlab_rails['gitlab_shell_ssh_port'] = 2224
        ports:
          - '8929:8929'
          - '2224:2224'
        volumes:
          - './config:/etc/gitlab'
          - './logs:/var/log/gitlab'
          - './data:/var/opt/gitlab'
    
    
  • Start the container (it will take a while...)

    docker-compose up -d
  • Visit GitLab homepage

  • http://10.1.100.225:8929

View the initial password of the root user

docker exec -it gitlab cat /etc/gitlab/initial_root_password

You need to change your password after logging in for the first time

3. Install jdk, maven, Jenkins

1. Install jdk and maven

JDK package download address: Java Downloads | Oracle

MAven download address: Maven – Download Apache Maven

tar -zxvf jdk-8u231-linux-x64.tar.gz -C /usr/local/
tar -zxvf apache-maven-3.6.3-bin.tar.gz -C /usr/local/

cd /usr/local
mv apache-maven-3.6.3/ maven
mv jdk1.8.0_231/ jdk

1.1 Edit maven configuration

vim /usr/local/maven/conf/settings.xml


<!--#maven配置阿里云仓库,在159行插入-->
<mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>central</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>


<!--#maven配置jdk,在252行插入-->
<profile>    
     <id>jdk1.8</id>    
     <activation>    
         <activeByDefault>true</activeByDefault>    
         <jdk>1.8</jdk>    
    </activation>    
    <properties>    
    	<maven.compiler.source>1.8</maven.compiler.source>    
    	<maven.compiler.target>1.8</maven.compiler.target>    
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    
    </properties>     
</profile> 

<!--#maven配置jdk,在257行插入-->
   <activeProfiles>
      <activeProfile>jdk1.8</activeProfile>
   </activeProfiles>

2.Jenkins introduction

Jenkins is an open source software project and a continuous integration tool developed based on Java.

Jenkins is widely used, and most Internet companies use Jenkins together with GitLab, Docker, and K8s as the core tools to implement DevOps.

The most powerful thing about Jenkins is the plug-in. Jenkins officially provides a large number of plug-in libraries to automate various trivial functions in the CI/CD process.

The main job of Jenkins is to pull the project code that can be built on GitLab and build it, and then choose to publish it to the test environment or production environment according to the process.

Generally, after the code on GitLab undergoes a lot of testing, the release version is determined and then released to the production environment.

CI/CD can be understood as:

  • The CI process is to use Jenkins to pull, build, and create images of code and hand them over to testers for testing.

    • Continuous integration: allows software code to be continuously integrated into the trunk and automatically built and tested.

  • The CD process is to use Jenkins to pull, build, and create images of the tagged release code and hand them over to operation and maintenance personnel for deployment.

    • Continuous delivery: Make continuously integrated code available for manual deployment.

    • Continuous deployment: allow code that can be delivered continuously to be deployed automatically anytime, anywhere

 

2.0 download jenkins

docker pull jenkins/jenkins:2.319.1-lts

vim /data/jenkins/docker-compose.yml

version: "3.1"
services:
  jenkins:
    image: jenkins/jenkins
    container_name: jenkins
    ports:
      - 8080:8080
      - 50000:50000
    volumes:
      - ./data/:/var/jenkins_home/
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker
      - /etc/docker/daemon.json:/etc/docker/daemon.json

2.1 Start jenkins


#修改Jenkins用户权限
cd /var/run

chown root:root docker.sock

#其他用户有读和写权限
chmod o+rw docker.sock


cd /data/jenkins/
docker-compose up -d

#授权
chmod 777 data/

cat /data/jenkins/data/hudson.model.UpdateCenter.xml
#重新启动Jenkins容器后,由于Jenkins需要下载大量内容,但是由于默认下载地址下载速度较慢,
#需要重新设置下载地址为国内镜像站# 清华大学的插件源也可以
# 修改数据卷中的hudson.model.UpdateCenter.xml文件
# 将下载地址替换为http://mirror.esuni.jp/jenkins/updates/update-center.json

# 清华大学的插件源也可以
#https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

#重启
docker-compose restart

#查看日志
docker logs -f jenkins

2.2 Visit the page

http://10.1.100.225:8080
1. Enter the password 2. Select the plug-in to install 3. Click to install

English interface installation plug-in Manage Jenkins–Manage Plugins-Available search plug-in
Locale
Localization
Git Parameter
Publish Over SSH

2.3 Configure jenkins


etc. /usr/local/maven/ /data/jenkins/data/

mv /usr/local/jdk/ /data/jenkins/data/

2.4 Test pulling code

System Management-System Configuration-Publish over SSH-SSH Servers

#Customized project name
name
test

#Host IP
Hostname
10.1.100.25

#Host username
Username
root

#Pull the project path
Remote Directory
/data/work/mytest

Click Advanced
√ Use password authentication, or use a different key

#Enter the server password
Passphrase / Password
xxxx

Click Test Test Configuration

4. Jenkins implements basic pull operations

1. Software download

Link: https://pan.baidu.com/s/1Jkyh_kgrT2o388Xiujbdeg?pwd=b7rx
Extraction code: b7rx

Basic plug-ins for the local operating environment.
Install git, maven , jdk8.
Install development tools IDEA: 2019.2.3 x64

2. Modify the maven path in IDEA

3. Create a project File - New - Project

 3.1 Spring Initalizr - Project SDK: java version “1.8.0_333” - Next

3.2 Modify JDK -Next

3.3 Create Spring Web

3.4 Click Finish

3.5 Create the controller directory and helloController file

code

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class helloController {


    @RequestMapping("/test")


    public String test(){
        return "hello jenkins";
    }


}

3.6 Click to run

3.7 Browser input: http://localhost:8080/test

3.8 Prepare to push the project-build local svc push path

 

3.9 Click OK

 

4.0 Right-click on the project name and select Git

4.1 Select the file to be uploaded-fill in the comments-Conmmit

4.2 Select Push

4.3 View the git warehouse address

4.4 Fill in the git warehouse address and submit the code

4.5 Log in to the warehouse to view the code. Congratulations on your success! ! ! !

4.6 Jenkins pulling code

 

4.7 Build environment to pull code

4.8 View logs

5. Jenkins implementation-sonarqbue code quality detection deployment

1.Install sonarqube
  

docker pull postgres
docker pull sonarqube:8.9.6-community

1.1 Modify kernel parameters

vi /etc/sysctl.conf
vm.max_map_count=262144

sysctl -p

mkdir -p /data/sonarqube
cd /data/sonarqube

cat docker-compose.yml

version: '3.1'
services:
  db:
    image: postgres
    container_name: db
    ports:
      - 5432:5432
    networks:
      - sonarnet
    volumes:
      - ~/sonarqube/postgresql/:/var/lib/postgresql
      - ~/sonarqube/datasql/:/var/lib/postgresql/data
#配置数据库用户名和密码
    environment:
      TZ: Asia/Shanghai
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
#下载社区长期支持版本
  sonarqube:
    image: sonarqube:8.9.6-community
    container_name: sonarqube
#让db先跑起来
    depends_on:
      - db
    volumes:
      - ~/sonarqube/extensions:/opt/sonarqube/extensions
      - ~/sonarqube/logs:/opt/sonarqube/logs
      - ~/sonarqube/data:/opt/sonarqube/data
      - ~/sonarqube/conf:/opt/sonarqube/conf
    ports:
      - 9000:9000
#让2个容器在一个网络中运行
    networks:
      - sonarnet
#数据库连接地址
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
#网络桥接方式
networks:
  sonarnet:
    driver: bridge

1.2 Startup

docker-compose up -d

1.3 View logs

docker logs -f sonarqube

#Enter sonarqube page ip: 9000 Default account password admin/admin
#Install Chinese plug-in Administrator -Marketplace - Query Chines in the search box below Agree I Chinese-lunderstand the risk - Download Install After downloading, prompt to restart Restart Server


 

2. Add the maven plug-in in the project to pom.xml. Add the following content to the penultimate line, the packaging name of the mytest project.

    <build>
        <finalName>mytest</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

3. Create a new docker directory in the same level directory of the project

3.1 Create a new file Dockerfile

FROM daocloud.io/library/java:8u40-jdk
COPY mytest.jar /usr/local/
WORKDIR /usr/local/
CMD java -jar mytest.jar

3.2 Create a new file ocker-compose.yml

version: '3.1'
services:
  mytest:
    build:
      context: ./
      dockerfile: Dockerfile
    image: mytest:v1.0.0
    #容器名称
    container_name: mytest
    ports:
      - 8081:8080

3.3 Add the new file and push it to the git repository

3.4 Add tags to projects in git repository

4. Jenkins installs the plug-in SonarQube Scanner

4.1 Install sonar -scanner

#下载地址 https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/
unzip sonar-scanner-cli-4.6.0.2311-linux.zip
mv sonar-scanner-4.6.0.2311-linux sonar-scanner
#把sonar-scanner 复制到 jenkins容器数据卷中
mv sonar-scanner /data/jenkins/data/

#修改sonar-scanner基础配置文件
cat /data/jenkins/data/sonar-scanner/conf/sonar-scanner.properties
#换成自己的IP##################################
sonar.host.url=http://10.66.66.64:9000
sonar.sourceEncoding=UTF-8
##############################################

4.2jenkins global tool configuration

4.3 sonarqbue generates secret key

4.4 Configure sonarqbue url and token key

 

4.5 Publish over SSH configures the path to pull the code and the IP account password

4.6 Add tag label to jenkins project

4.7 Add code quality detection-

 

sonar.projectname=${JOB_NAME}
sonar.projectKey=${JOB_NAME}
sonar.source=./
sonar.java.binaries=target

4.8 Post-build operations

cd /usr/local/test/docker
mv ../target/*.jar ./

docker-compose down
docker-compose up -d --build

#删除多余的<none>镜像
docker image prune -f

4.9 Select tag to start deploying the project

5.0 View quality inspection status

6. Jenkins realizes making custom images and pushing harbor deployment

1. Delete docker -compose.yml from the project, modify the version information, and push it to the git warehouse

2. New tag label v3.0.0 in gti warehouse

3. Delete the previous post-build operation-Send build artifacts over SSH

4.Add build step-execute shell

mv target/*.jar docker/
docker build -t mytest:$tag docker/
docker login -u admin -p Harbor12345 192.168.1.6:80
docker tag mytest:$tag 192.168.1.6:80/repo/mytest:$tag
docker push 192.168.1.6:80/repo/mytest:$tag

5. Server editing script


cat > /root/deploy.sh  <<EOF
horbar_addr=$1
horbar_repo=$2
project=$3
version=$4
echo "容器运行时端口"
container_port=$5
echo "宿主机映射端口"
host_prot=$6

echo "镜像名称"
imagesName=$horbar_addr/$horbar_repo/$project:$version

echo $imagesName

echo "拿到正在运行的id"
containerId=`docker ps -a |grep ${project} | awk '{print $1}'`

echo $containerId

echo "存在id 停止删除进程"
if [ "$containerId" != "" ] ; then
  docker stop $containerId
  docker rm $containerId
fi

echo "打印工程  tag版本"
tag=`docker images | grep ${project} | awk '{print $2}'`

echo $tag


echo  "versin中包含tag版本,删除镜像"
if [[ "$tag" =~ "$version" ]] ; then
  docker rmi $imagesName
fi

echo  "登入harbor仓库"
docker login -u admin -p Harbor12345 $horbar_addr

echo  "推送镜像"
docker pull $imagesName


echo  "删除 none多余镜像"
docker images | grep none | awk '{print $3}'| xargs docker rmi --force


docker run -d -p $host_prot:$container_port --name $project $imagesName

echo "success"


 <<EOF


6.Script authorization

chmod a+x deploy.sh
#查看当前环境变量
echo $PATH
#脚本移动到环境变量中,让其他用户可执行
mv deploy.sh /usr/bin/

7. Add port character parameters to the project

 

8. Add post-build operation-Send build artifacts over SSH

9. Run the project and view the logs

7. First experience with Jenkins pipeline

1. Create a new project

 

 2. Cover all the following codes in the pipeline-Hello word-Apply-Save-Build Now

//All script commands are placed in the pipeline
pipeline {     //In which cluster node to execute the task,     agent any

    environment {
        key = 'valus'
    }

    stages {         stage('Pull the git warehouse code') {             steps {                 echo 'Pull the git warehouse code-SUCCESS'             }         }         stage('Build the project through maven') {             steps {                 echo 'Build the project through maven-SUCCESS'             }                     }         stage('Code quality inspection through sonarQube') {             steps {                 echo 'Code quality inspection through sonarQube-SUCCESS'             }                     }         stage('Create a custom image through docker') {             steps {                 echo 'Create a custom image through docker- SUCCESS'             }                          }





    
    





        





        





        
        stage('Push the custom image to Harbor') {             steps {                 echo 'Push the custom image to Harbor - SUCCESS'             }                     }         stage('Notify the target server via Publish Over SSH') {             steps {                 echo 'Via Publish Over SSH' Notify target server - SUCCESS'             }                     } }     }




        





        
        

3. Maintenance script

 

8. Pipeline in Jenkins - Packaging quality detection, push docker deployment, and enterprise WeChat push messages

Enterprise WeChat - Configure Jenkins - Install plug-in - In plug-in management, install the plug-in Qy Wechat Notification, and restart Jenkins after installation

English interface installation plug-in Manage Jenkins–Manage Plugins-Available search plug-in
Locale
Localization
Git Parameter
Publish Over SSH
Qy Wechat Notification
SonarQube Scanner

1. Add tag to project


Dashboard-pipeline-General-parameterized build process-Git parameters-name (tag)-description (code version tag)-default value (origin/master)-apply-save

2. Pull the git warehouse code


Edit project pipeline-pipeline-pipeline syntax-fragment generator-example steps (checkout: Check out from version control)

Repository URL


http://192.168.1.6:8929/root/pipeline.git

Click-Generate pipeline script

checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.1.6:8929/root/pipeline.git']]])

Go back to the git repository, modify the Jenkinsfile, and save it

//*/master 是默认拉取最新的代码, 我们自定义了tag版本,这里需要引入$tag
    stages {
        stage('拉取git仓库代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.1.6:8929/root/pipeline.git']]])
            }
        }

3.maven build package


Edit project pipeline-pipeline-pipeline syntax-snippet generator-example steps (sh: Shell Script)

Shell Script

/var/jenkins_home/maven/bin/mvn clean package -DskipTests

Generate pipeline script

sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'

4. Do code quality inspection through sonarQube

Edit project pipeline-pipeline-pipeline syntax-fragment generator-sample steps (sh: Shell Script)-add the generated pipeline script to the git repository and modify the Jenkinsfile

#Source code location-Dsonar.source=./
#Project name-Dsonar.projectname
#Project identification-Dsonar.projectKey
#Compiled directory-Dsonar.java.binaries
#Secret key-Dsonar.login

Shell Script
 

/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=./target/ -Dsonar.login=c5f80db608830252de0b368c9aaecc3a8d95463f

Generate pipeline script

sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=./target/ -Dsonar.login=c5f80db608830252de0b368c9aaecc3a8d95463f'

5. Create a custom image through docker

Edit project pipeline-pipeline-pipeline syntax-fragment generator-sample steps (sh: Shell Script)-add the generated pipeline script to the git repository and modify the Jenkinsfile

Shell Script

mv ./target/*.jar ./docker/
docker build -t ${JOB_NAME}:${tag} ./docker/

Generate pipeline script

sh '''mv ./target/*.jar ./docker/
docker build -t ${JOB_NAME}:${tag} ./docker/'''

6. Push the custom image to Harbor

git repository-modify Jenkinsfile

    //声明全局变量,方便后面使用
    environment {
	    harboUser = 'admin'
        harborPass = 'Harbor12345'
        harborAddress = '192.168.1.6:80'
        harborRepo = 'repo'
	}


		stage('将自定义镜像推送到Harbor') {
            steps {
                sh '''docker login -u ${harboUser} -p ${harborPass} ${harborAddress}
docker tag ${JOB_NAME}:${tag} ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}
docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}'''
            }			
        }

7. Notify the target server through Publish Over SSH

pipeline-parameterized build process-add parameters-character parameters

container_port 8080 The port occupied inside the container
host_prot 8081 The host mapped port

Edit project pipeline-pipeline-pipeline syntax-fragment generator-sample steps (sshPublisher:Send build artifacts over SSH)-add the generated pipeline script to the git repository and modify the Jenkinsfile

Exec command

deploy.sh $harborAddress $harborRepo $JOB_NAME $tag $container_port $host_prot

Generate pipeline script

sshPublisher(publishers: [sshPublisherDesc(configName: 'test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "deploy.sh $harborAddress $harborRepo $JOB_NAME $tag $container_port $host_prot", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])

A pitfall in the pipeline script, the '' here does not reference the variables in the Jenkinsfile file,
'deploy.sh $harborAddress $harborRepo $JOB_NAME $tag $container_port $host_prot'

Need to be replaced with ""
"deploy.sh $harborAddress $harborRepo $JOB_NAME $tag $container_port $host_prot"

8. Set up corporate WeChat

Open the enterprise WeChat mobile app and add the robot to the group robot in the group settings.
After the addition is successful, copy the Webhook address and use it when configuring Jenkins.

Edit project pipeline-pipeline-pipeline syntax-fragment generator-sample steps (qyWechatNotification: Enterprise WeChat notification) -Add the generated pipeline script to the git repository and modify the Jenkinsfile

9. Pipeline in Jenkins, k8s deployment, and enterprise WeChat push messages

1. Jenkins server settings for password-free login to the k8s-mast server

#Jenkins server-enter the jenkins container
docker exec -it jenkins bash

#Enter the jenkins container-generate the public and private keys for password-free login, and press Enter according to the prompts
ssh-keygen -t rsa

#Enter the jenkins container - view the jenkins secret key
cat /var/jenkins_home/.ssh/id_rsa.pub

#k8s-mast server authorized_keys Add Jenkins server secret key
echo "xxxxxx" >> /root/.ssh/authorized_keys

1.2 Jenkins adds k8s server information

System Management-System Configuration-Publish over SSH -New-

 Set the password and click Advanced-√ Use password authentication, or use a different key-Passphrase / Password (fill in your own password)-Test Configuration (click to test)

2. Copy pipeline.yaml to k8s-mast to generate pipeline syntax

Edit project pipeline-pipeline-pipeline syntax-fragment generator-example steps (shhPublisher: Send build artifacts over SSH)

Name-(k8s)

Source files-(pipeline.yaml)

Generate pipeline script

sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'pipeline.yaml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])

3. Remotely execute pipeline.yaml in the k8s-mast server


Edit project pipeline-pipeline-pipeline syntax-snippet generator-example steps (sh: Shell Script)

Shell Script
ssh [email protected] kubectl apply -f /usr/local/k8s/pipeline.yaml

Generate the pipeline script
sh 'ssh [email protected] kubectl apply -f /usr/local/k8s/pipeline.yaml'


 

4. Create a new pipeline.yaml file in the git warehouse

apiVersion: v1
kind: Service
metadata:
  namespace: test
  name: pipeline
  labels:
    app: pipeline
spec:
  ports:
    - port: 8081
      protocol: TCP
      targetPort: 8080
  selector:
    app: pipeline
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: test
  name: pipeline
  labels:
    app: pipeline
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pipeline
  template:
    metadata:
      labels:
        app: pipeline
    spec:
      containers:
        - name: pipeline
          image: 192.168.1.10:80/repo/pipeline:v4.0.0
          #一直从仓库拉取镜像
          imagePullPolicy: Always 
          ports:
            - containerPort: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  namespace: test
  name: pipeline-ingress
spec:
  rules:
  #记得给自己的k8s集群服务器添加本地host域名解析
    - host: pipeline.rw.com
      http:
        paths:
          - backend:
              serviceName: pipeline
              servicePort: 8081
            path: /

5. Modify the Jenkinsfile file in the git warehouse

//所有的脚本命令都放在pipeline中
pipeline {
    //执行任务再哪个集群节点中执行
    agent any
    //声明全局变量,方便后面使用
    environment {
	    harboUser = 'admin'
        harborPass = 'Harbor12345'
        harborAddress = '192.168.1.10:80'
        harborRepo = 'repo'
	}

//*/master 是默认拉取最新的代码, 我们自定义了tag版本,这里需要引入$tag
    stages {
        stage('拉取git仓库代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.1.9:8929/root/pipeline.git']]])
            }
        }
	
	    stage('通过maven构建项目') {
            steps {
                sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
            }			
        }
		
		stage('通过sonarQube做代码质量检测') {
            steps {
                sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=./target/ -Dsonar.login=c5f80db608830252de0b368c9aaecc3a8d95463f'
            }			
        }

		
		stage('通过docker制作自定义镜像') {
            steps {
                sh '''mv ./target/*.jar ./docker/
docker build -t ${JOB_NAME}:${tag} ./docker/'''
            }			
        }
		
		stage('将自定义镜像推送到Harbor') {
            steps {
                sh '''docker login -u ${harboUser} -p ${harborPass} ${harborAddress}
docker tag ${JOB_NAME}:${tag} ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}
docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}'''
            }			
        }
		
		stage('拷贝yaml 到mast') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'pipeline.yaml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }			
        }
//192.168.1.2 是k8s-mast主节点		
		stage('远程mast 执行yaml') {
            steps {
                sh 'ssh [email protected] kubectl apply -f /usr/local/k8s/pipeline.yaml'

            }			
        }

		stage('通知企业微信') {
            steps {
                qyWechatNotification mentionedId: '', mentionedMobile: '', webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0461ffcf-3a23-4cda-8757-371cc0xx46be'
            }			
        }
		


		
    }
}

6. Execution pipeline

10. Jenkins automated pipeline, k8s deployment, and enterprise WeChat push messages

1.Jenkins-Install plug-in-In the plug-in management, install the plug-in GitLab, and restart Jenkins after installation

2.Jenkins-System Configuration-Gitlab


Remove √
Enable authentication for '/project' end-point
application save

3.Git repository

Meun-Admin-Settings-Network-Outbound requests-Expand
打钩 √Allow requests to the local network from web hooks and services

SaveSave changes

Projectzd-Settings-Webhooks-URL (http://192.168.1.5:8080/project/zd) -Add webhook

4. Test

5. Modify pipeline

pipeline.yaml

apiVersion: v1
kind: Service
metadata:
  namespace: test
  name: zd
  labels:
    app: zd
spec:
  ports:
    - port: 8081
      protocol: TCP
      targetPort: 8080
  selector:
    app: zd
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: test
  name: zd
  labels:
    app: zd
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zd
  template:
    metadata:
      labels:
        app: zd
    spec:
      containers:
        - name: zd
          image: 192.168.1.225:80/repo/zd:latest
          ports:
            - containerPort: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  namespace: test
  name: pipeline-ingress
spec:
  rules:
    - host: nginx.boge.com
      http:
        paths:
          - backend:
              serviceName: zd
              servicePort: 8081
            path: /

6. Modify Jenkinsfile

 Jenkinsfile

//所有的脚本命令都放在pipeline中
pipeline {
    //执行任务再哪个集群节点中执行
    agent any
    //声明全局变量,方便后面使用
    environment {
	    harboUser = 'admin'
        harborPass = 'Harbor12345'
        harborAddress = '192.168.1.225:80'
        harborRepo = 'repo'
	}

//*/master 是默认拉取最新的代码, 我们自定义了tag版本,这里需要引入$tag
    stages {
        stage('拉取git仓库代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.1.225:8929/root/zd.git']]])
            }
        }


	    stage('通过maven构建项目') {
            steps {
                sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
            }
        }

	stage('通过sonarQube做代码质量检测') {
            steps {
                sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=./target/ -Dsonar.login=c5f80db608830252de0b368c9aaeccxx8d95463f'
            }			
        }


		stage('通过docker制作自定义镜像') {
            steps {
                sh '''mv ./target/*.jar ./docker/
                docker build -t ${JOB_NAME}:latest ./docker/'''
            }
        }

		stage('将自定义镜像推送到Harbor') {
            steps {
                sh '''docker login -u ${harboUser} -p ${harborPass} ${harborAddress}
                docker tag ${JOB_NAME}:latest ${harborAddress}/${harborRepo}/${JOB_NAME}:latest
                docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:latest'''
            }
        }

		stage('拷贝yaml 到mast') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'pipeline.yaml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }

		stage('远程mast 执行yaml') {
            steps {
                sh 'ssh [email protected] kubectl apply -f /usr/local/k8s/pipeline.yaml'
                sh 'ssh [email protected] kubectl -n test rollout restart deployment zd'

            }
        }


		stage('通知企业微信') {
            steps {
                qyWechatNotification mentionedId: '', mentionedMobile: '', webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0461ffcf-3a23-4cda-8757-371xx0cd46be'
            }
        }

    }
}

7. Develop push code and automatically deploy


 

Guess you like

Origin blog.csdn.net/qq_35583325/article/details/126936804