devops-在jenkins-slave(k8s)中集成Jmeter使用

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: devops-在jenkins-slave(k8s)中集成maven使用


环境准备

centos7

k8s集群

jenkins + gitlab




下载jenkins相关插件



HTML Publisher plugin

image.png





创建流水线




image.png




流水线配置





image.png



脚本内容


声明式,构建slave节点的流水线。

// 为了解决jmeter生成的HTML报告无法显示的问题。参考https://blog.csdn.net/qq_29427355/article/details/82424467
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
pipeline {  agent {    kubernetes {      //yamlFile 'KubernetesPod.yaml'      cloud 'kubernetes'      yaml '''---apiVersion: v1
kind: Pod
metadata:  namespace: devops
spec:  nodeSelector:    kubernetes.io/os: linux
  restartPolicy: Never
  serviceAccountName: jenkins
  containers:    - name: "docker"      image: "docker:latest"      imagePullPolicy: "IfNotPresent"      resources:        limits: {}        requests: {}      command:      - cat
      tty: true      volumeMounts:        - mountPath: /var/run/docker.sock
          name: docker-sock
          readOnly: false    - name: "python3"      image: "python:3.8.6"      imagePullPolicy: "IfNotPresent"      resources:        limits: {}        requests: {}      command:      - cat
      tty: true      volumeMounts:        - mountPath: /var/run/docker.sock
          name: docker-sock
          readOnly: false    - name: "maven36"      image: "yfy/maven:3.6-jdk-11"      imagePullPolicy: "IfNotPresent"      resources:        limits: {}        requests: {}      command:      - cat
      tty: true      volumeMounts:        - mountPath: /root/.m2/repository
          name: maven-repo
          readOnly: false    - name: "jmeter55"      image: "justb4/jmeter:latest"      imagePullPolicy: "IfNotPresent"      resources:        limits: {}        requests: {}      command:      - cat
      tty: true      volumeMounts:        - mountPath: /plugin
          name: jmeter-plugins
          readOnly: false  volumes:    - hostPath:        path: /var/run/docker.sock
      name: docker-sock
    - hostPath:        path: /data/devops/jenkins/.m/repository
      name: maven-repo
    - hostPath:        path: /data/devops/jenkins/tools/jmeter/plugins
      name: jmeter-plugins
'''}}    options {        //设置管道运行的超时时间,在此之后,Jenkins将中止管道
        timeout(time: 20, unit: 'MINUTES')
        // 失败重试次数
        retry(2)
        //输出时间戳
        timestamps()
}    environment {        //构建分支 master、test
        // Brand='test'        //定义Maven编译环境 dev/test/prod
        ACTIVE = "test"        //jar包名称
        JAR_NAME = 'yss-datamiddle-multiplecheck.jar'        //JOB所在根目录,JOB_NAME
        WORKSPACE_PATH = "/opt/dataMiddle-service/workspace/${JOB_NAME}/"}  stages {        stage('checkout') {          steps {            checkout([$class: 'GitSCM',                      branches: [[name: '*/main']],                      extensions: [],                      userRemoteConfigs: [[credentialsId: 'gitlab-author', url: 'http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git']]])
}}        stage('jmeter55'){            steps{                container('jmeter55'){                    // jmeter -n -t test.jmx -l test.jtl -e -o /path
                    // # -n: 以非GUI形式运行Jmeter                     // # -t:source.jmx 脚本路径                     // # -l result.jtl 运行结果保存路径(.jtl),此文件必须不存在                     // # -e:在脚本运行结束后生成html报告                     // # -o:用于存放html报告的目录                    sh "jmeter --version"                    sh "jmeter  -n -Jthreadcount=5 -t jmeter-test.jmx -l reports/jmeter-test-report.jtl -e -o reportshtml/"}}}        stage("性能测试报告") {            steps {             // echo '---Performance Reports---'             // perfReport filterRegex: '', showTrendGraphs: true, sourceDataFiles: 'reports/*.jtl'              echo '--- HTML Report ---'               //展示测试报告
                publishHTML([allowMissing: false,                            alwaysLinkToLastBuild: false,                            keepAll: false,                            reportDir: 'reportshtml',                            reportFiles: 'index.html',                            reportName: 'jmeterHTMLReport',                            reportTitles: '',                            useWrapperFileDirectly: true]                )
}}    // stage('maven3.6'){    //     steps{    //         container('maven36'){    //             sh "mvn -v"    //             sh "mvn clean package -DskipTests=true"    //         }    //     }    // }    // stage('build-allure-results') {    //     steps {    //         container('python3') {    //             sh """    //             python --version
    //             pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
    //             python main.py ${allure_results_path}    //             """    //         }    //     }    // }    // stage('build-allure-export') {    //     steps {    //         // 通过jenkins的全局工具配置,下载allure工具
    //         allure includeProperties: false, jdk: '',  results: [[path: '${allure_results_path}']]    //     }    // }    // stage('docker build') {    //   steps {    //     container('docker') {    //       sh """    //         docker version
    //       """    //     }    //     container('python3') {    //       print('user mutil container...')
    //     }    //   }    // }}}




使用kubernets作为slave节点配置


kubernetes: slave节点

yaml: 声明式定义pod template

pipeline {  agent {    kubernetes {      //yamlFile 'KubernetesPod.yaml'      cloud 'kubernetes'      yaml '''




在pod-template中使用jmeter容器




    - name: "jmeter55"      image: "justb4/jmeter:latest"      imagePullPolicy: "IfNotPresent"      resources:        limits: {}        requests: {}      command:      - cat
      tty: true      volumeMounts:        - mountPath: /plugin
          name: jmeter-plugins
          readOnly: false


- mountPath: /plugin  :  容器内持久化挂载的jmeter插件




  volumes:    - hostPath:        path: /var/run/docker.sock
      name: docker-sock
    - hostPath:        path: /data/devops/jenkins/.m/repository
      name: maven-repo
    - hostPath:        path: /data/devops/jenkins/tools/jmeter/plugins
      name: jmeter-plugins


- hostPath:

       path: /data/devops/jenkins/tools/jmeter/plugins

     name: jmeter-plugins


jmeter插件在宿主机上的位置




注意:提前把jmeter使用的相关插件拷贝到 此目录下:

/data/devops/jenkins/tools/jmeter/plugins


如图所示:


image.png




checkout仓库



        stage('checkout') {          steps {            checkout([$class: 'GitSCM',                      branches: [[name: '*/main']],                      extensions: [],                      userRemoteConfigs: [[credentialsId: 'gitlab-author', url: 'http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git']]])
}}


使用jmeter容器生成HTML报表


        stage('jmeter55'){            steps{                container('jmeter55'){                    // jmeter -n -t test.jmx -l test.jtl -e -o /path
                    // # -n: 以非GUI形式运行Jmeter                     // # -t:source.jmx 脚本路径                     // # -l result.jtl 运行结果保存路径(.jtl),此文件必须不存在                     // # -e:在脚本运行结束后生成html报告                     // # -o:用于存放html报告的目录                    sh "jmeter --version"                    sh "jmeter  -n -Jthreadcount=5 -t jmeter-test.jmx -l reports/jmeter-test-report.jtl -e -o reportshtml/"}}}




        stage("性能测试报告") {            steps {             // echo '---Performance Reports---'             // perfReport filterRegex: '', showTrendGraphs: true, sourceDataFiles: 'reports/*.jtl'              echo '--- HTML Report ---'               //展示测试报告
                publishHTML([allowMissing: false,                            alwaysLinkToLastBuild: false,                            keepAll: false,                            reportDir: 'reportshtml',                            reportFiles: 'index.html',                            reportName: 'jmeterHTMLReport',                            reportTitles: '',                            useWrapperFileDirectly: true]                )
}}



执行流水线


Started by user jenkins
[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline]{[Pipeline] node
Created Pod: kubernetes devops/jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0
Still waiting to schedule task
‘jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0’ is offline
Agent jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0 is provisioned from template jmeter-pytest-pipeline_16-nzxgn-n639w
---apiVersion: "v1"kind: "Pod"metadata:  annotations:    buildUrl: "http://192.168.10.201:30080/job/jmeter-pytest-pipeline/16/"    runUrl: "job/jmeter-pytest-pipeline/16/"  labels:    jenkins: "slave"    jenkins/label-digest: "ae6ed240d3a142da64181870bc68b00255b964cc"    jenkins/label: "jmeter-pytest-pipeline_16-nzxgn"  name: "jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0"  namespace: "devops"spec:  containers:  - command:    - "cat"    image: "docker:latest"    imagePullPolicy: "IfNotPresent"    name: "docker"    resources:      limits: {}      requests: {}    tty: true    volumeMounts:    - mountPath: "/var/run/docker.sock"      name: "docker-sock"      readOnly: false    - mountPath: "/home/jenkins/agent"      name: "workspace-volume"      readOnly: false  - command:    - "cat"    image: "python:3.8.6"    imagePullPolicy: "IfNotPresent"    name: "python3"    resources:      limits: {}      requests: {}    tty: true    volumeMounts:    - mountPath: "/var/run/docker.sock"      name: "docker-sock"      readOnly: false    - mountPath: "/home/jenkins/agent"      name: "workspace-volume"      readOnly: false  - command:    - "cat"    image: "yfy/maven:3.6-jdk-11"    imagePullPolicy: "IfNotPresent"    name: "maven36"    resources:      limits: {}      requests: {}    tty: true    volumeMounts:    - mountPath: "/root/.m2/repository"      name: "maven-repo"      readOnly: false    - mountPath: "/home/jenkins/agent"      name: "workspace-volume"      readOnly: false  - command:    - "cat"    image: "justb4/jmeter:latest"    imagePullPolicy: "IfNotPresent"    name: "jmeter55"    resources:      limits: {}      requests: {}    tty: true    volumeMounts:    - mountPath: "/plugin"      name: "jmeter-plugins"      readOnly: false    - mountPath: "/home/jenkins/agent"      name: "workspace-volume"      readOnly: false  - env:    - name: "JENKINS_SECRET"      value: "********"    - name: "JENKINS_AGENT_NAME"      value: "jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0"    - name: "JENKINS_NAME"      value: "jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0"    - name: "JENKINS_AGENT_WORKDIR"      value: "/home/jenkins/agent"    - name: "JENKINS_URL"      value: "http://192.168.10.201:30080/"    image: "jenkins/inbound-agent:4.11-1-jdk11"    name: "jnlp"    resources:      limits: {}      requests:        memory: "256Mi"        cpu: "100m"    volumeMounts:    - mountPath: "/home/jenkins/agent"      name: "workspace-volume"      readOnly: false  nodeSelector:    kubernetes.io/os: "linux"  restartPolicy: "Never"  serviceAccountName: "jenkins"  volumes:  - hostPath:      path: "/var/run/docker.sock"    name: "docker-sock"  - hostPath:      path: "/data/devops/jenkins/.m/repository"    name: "maven-repo"  - hostPath:      path: "/data/devops/jenkins/tools/jmeter/plugins"    name: "jmeter-plugins"  - emptyDir:      medium: ""    name: "workspace-volume"Running on jmeter-pytest-pipeline-16-nzxgn-n639w-jqzn0 in /home/jenkins/agent/workspace/jmeter-pytest-pipeline
[Pipeline]{[Pipeline] withEnv
[Pipeline]{[Pipeline] timeout
Timeout set to expire in 20 min
[Pipeline]{[Pipeline] retry
[Pipeline]{[Pipeline] timestamps
[Pipeline]{[Pipeline] stage
[Pipeline]{ (checkout)
[Pipeline] checkout
08:32:00  The recommended git tool is: NONE
08:32:06  using credential gitlab-author
08:32:06  Cloning the remote Git repository
08:32:07  Cloning repository http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git
08:32:07   > git init /home/jenkins/agent/workspace/jmeter-pytest-pipeline # timeout=1008:32:08  Fetching upstream changes from http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git
08:32:08   > git --version # timeout=1008:32:08   > git --version # 'git version 2.30.2'08:32:08  using GIT_ASKPASS to set credentials gitlab-author
08:32:08   > git fetch --tags --force --progress -- http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git +refs/heads/*:refs/remotes/origin/* # timeout=1008:32:09   > git config remote.origin.url http://192.168.10.200:8085/yfy-test/jmeter-pytest-scaffolding.git # timeout=1008:32:09   > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=1008:32:10  Avoid second fetch
08:32:10  Checking out Revision 6deb2f3a38f079cd55fbd1d9189872c75d1ea676 (refs/remotes/origin/main)
08:32:10   > git rev-parse refs/remotes/origin/main^{commit}# timeout=1008:32:10   > git config core.sparsecheckout # timeout=1008:32:10   > git checkout -f 6deb2f3a38f079cd55fbd1d9189872c75d1ea676 # timeout=1008:32:13  Commit message: "Update Jenkinsfile"08:32:13   > git rev-list --no-walk 40205a28a67e6a6b48cee7b8f2c98e6bea88ff8c # timeout=10[Pipeline]}[Pipeline] // stage
[Pipeline] stage
[Pipeline]{ (jmeter55)
[Pipeline] container
[Pipeline]{[Pipeline] sh
08:32:14  + jmeter --version
08:32:19  Dec 18, 2022 1:32:18 AM java.util.prefs.FileSystemPreferences$1 run
08:32:19  INFO: Created user preferences directory.
08:32:19      _    ____   _    ____ _   _ _____       _ __  __ _____ _____ _____ ____
08:32:19     / \  |  _ \ / \  / ___| | | | ____|     | |  \/  | ____|_   _| ____|  _ \
08:32:19    / _ \ | |_) / _ \| |   | |_| |  _|    _  | | |\/| |  _|   | | |  _| | |_) |
08:32:19   / ___ \|  __/ ___ \ |___|  _  | |___  | |_| | |  | | |___  | | | |___|  _ <
08:32:19  /_/   \_\_| /_/   \_\____|_| |_|_____|  \___/|_|  |_|_____| |_| |_____|_| \_\ 5.5
08:32:19  
08:32:19  Copyright (c) 1999-2022 The Apache Software Foundation
08:32:19  
[Pipeline] sh
08:32:20  + jmeter -n '-Jthreadcount=5' -t jmeter-test.jmx -l reports/jmeter-test-report.jtl -e -o reportshtml/
08:32:23  Creating summariser <summary>
08:32:23  Created the tree successfully using jmeter-test.jmx
08:32:23  Starting standalone test @ December 18, 2022 1:32:22 AM CET (1671323542996)
08:32:23  Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
08:32:25  summary =      2 in 00:00:01 =    1.4/s Avg:   560 Min:    81 Max:  1039 Err:     0 (0.00%)
08:32:25  Tidying up ...    @ December 18, 2022 1:32:24 AM CET (1671323544991)
08:32:26  ... end of run
[Pipeline]}[Pipeline] // container
[Pipeline]}[Pipeline] // stage
[Pipeline] stage
[Pipeline]{ (性能测试报告)
[Pipeline] echo
08:32:27  --- HTML Report ---
[Pipeline] publishHTML
08:32:27  [htmlpublisher] Archiving HTML reports...
08:32:27  [htmlpublisher] Archiving at PROJECT level /home/jenkins/agent/workspace/jmeter-pytest-pipeline/reportshtml to /var/lib/jenkins/jobs/jmeter-pytest-pipeline/htmlreports/jmeterHTMLReport
[Pipeline]}[Pipeline] // stage
[Pipeline]}[Pipeline] // timestamps
[Pipeline]}[Pipeline] // retry
[Pipeline]}[Pipeline] // timeout
[Pipeline]}[Pipeline] // withEnv
[Pipeline]}[Pipeline] // node
[Pipeline]}[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS



查看HTML测试报告


reportng html报告在jenkins中显示有问题,或者 css样式不显示

如下不显示内容

image.png




解决jmeter测试报告HTML页面无法正常显示



在pipeline代码中添加以下代码:


// 为了解决jmeter生成的HTML报告无法显示的问题。参考https://blog.csdn.net/qq_29427355/article/details/82424467
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")


再次执行流水线,流水线会失败,并输出一下内容:


image-20221218001336651.png


点击提示中的超链接,授权并批准administrators权限

image-20221218001547157.png


或者


在系统管理 》进行授权

image-20221218001445245.png




授权成功以后,重新构建流水线,再次查看HTML报表



image.png



参考相关代码:


https://gitee.com/yyyfly/devops-k8s-pytest-jmeter




相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
15天前
|
jenkins Devops Java
DevOps实践:Jenkins在持续集成与持续部署中的价值
【10月更文挑战第27天】在快速发展的软件开发领域,DevOps实践日益重要。Jenkins作为一款流行的开源自动化服务器,在持续集成(CI)和持续部署(CD)中扮演关键角色。本文通过案例分析,探讨Jenkins在Java项目中的应用,展示其自动化构建、测试和部署的能力,提高开发效率和软件质量。
38 2
|
2天前
|
运维 jenkins Java
Jenkins在持续集成与持续部署中的价值
Jenkins在持续集成与持续部署中的价值
|
7天前
|
存储 监控 Devops
DevOps实践:持续集成/持续部署(CI/CD)的实战指南
DevOps实践:持续集成/持续部署(CI/CD)的实战指南
|
16天前
|
jenkins Devops 测试技术
DevOps实践:Jenkins在持续集成与持续部署中的价值
【10月更文挑战第26天】随着DevOps理念的普及,Jenkins作为一款开源自动化服务器,在持续集成(CI)与持续部署(CD)中发挥重要作用。本文通过某中型互联网企业的实际案例,展示了Jenkins如何通过自动化构建、持续集成和持续部署,显著提升开发效率、代码质量和软件交付速度,帮助企业解决传统手工操作带来的低效和错误问题。
44 4
|
10天前
|
运维 Devops jenkins
DevOps实践之持续集成与持续交付
【10月更文挑战第32天】在软件开发的快节奏世界中,DevOps已经成为提升效率和质量的关键策略。通过将开发(Development)和运维(Operations)紧密结合,DevOps促进了更快速的软件发布和更高的可靠性。本文将深入探讨DevOps的核心组成部分——持续集成(CI)和持续交付(CD),并展示如何通过实际代码示例实现它们,以帮助团队构建更加高效和稳定的软件发布流程。
|
4月前
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
287 6
|
4月前
|
Java 关系型数据库 MySQL
如何实现Springboot+camunda+mysql的集成
【7月更文挑战第2天】集成Spring Boot、Camunda和MySQL的简要步骤: 1. 初始化Spring Boot项目,添加Camunda和MySQL驱动依赖。 2. 配置`application.properties`,包括数据库URL、用户名和密码。 3. 设置Camunda引擎属性,指定数据源。 4. 引入流程定义文件(如`.bpmn`)。 5. 创建服务处理流程操作,创建控制器接收请求。 6. Camunda自动在数据库创建表结构。 7. 启动应用,测试流程启动,如通过服务和控制器开始流程实例。 示例代码包括服务类启动流程实例及控制器接口。实际集成需按业务需求调整。
361 4
|
4月前
|
消息中间件 Java 测试技术
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
317 1
|
5月前
|
消息中间件 Java Kafka
springboot集成kafka
springboot集成kafka
168 2
|
4月前
|
消息中间件 Java Kafka
Spring Boot与Apache Kafka Streams的集成
Spring Boot与Apache Kafka Streams的集成