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

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
简介: 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项目中的应用,展示其自动化构建、测试和部署的能力,提高开发效率和软件质量。
39 2
|
1月前
|
运维 Devops jenkins
DevOps实践:自动化部署与持续集成的实现之旅
本文旨在通过一个实际案例,向读者展示如何将DevOps理念融入日常工作中,实现自动化部署和持续集成。我们将从DevOps的基础概念出发,逐步深入到工具的选择、环境的搭建,以及流程的优化,最终实现一个简单而高效的自动化部署流程。文章不仅提供代码示例,更注重于实践中的思考和问题解决,帮助团队提高软件开发和运维的效率。
|
1月前
|
运维 监控 Devops
DevOps实践:自动化部署与持续集成的融合之旅
【10月更文挑战第7天】在软件开发领域,DevOps已成为一种文化和实践,它倡导开发(Dev)与运维(Ops)之间的协作与整合。本文将引导读者了解如何通过自动化部署和持续集成(CI)的实践来提升软件交付的速度和质量。我们将探讨一些实用的工具和技术,以及它们是如何帮助团队高效地管理代码变更、测试和部署的。文章将不包含代码示例,但会详细解释概念和流程,确保内容的通俗易懂和条理性。
131 62
|
7天前
|
存储 监控 Devops
DevOps实践:持续集成/持续部署(CI/CD)的实战指南
DevOps实践:持续集成/持续部署(CI/CD)的实战指南
|
16天前
|
jenkins Devops 测试技术
DevOps实践:Jenkins在持续集成与持续部署中的价值
【10月更文挑战第26天】随着DevOps理念的普及,Jenkins作为一款开源自动化服务器,在持续集成(CI)与持续部署(CD)中发挥重要作用。本文通过某中型互联网企业的实际案例,展示了Jenkins如何通过自动化构建、持续集成和持续部署,显著提升开发效率、代码质量和软件交付速度,帮助企业解决传统手工操作带来的低效和错误问题。
44 4
|
1月前
|
运维 监控 Devops
DevOps实践:持续集成与部署的自动化之旅
【10月更文挑战第7天】在软件开发领域,DevOps已成为提升效率、加速交付和确保质量的关键策略。本文将深入探讨如何通过实施持续集成(CI)和持续部署(CD)来自动化开发流程,从而优化运维工作。我们将从基础概念入手,逐步过渡到实际操作,包括工具选择、流程设计以及监控和反馈机制的建立。最终,我们不仅会展示如何实现这一自动化流程,还会讨论如何克服常见的挑战,以确保成功实施。
63 9
|
11天前
|
运维 Devops jenkins
DevOps实践之持续集成与持续交付
【10月更文挑战第32天】在软件开发的快节奏世界中,DevOps已经成为提升效率和质量的关键策略。通过将开发(Development)和运维(Operations)紧密结合,DevOps促进了更快速的软件发布和更高的可靠性。本文将深入探讨DevOps的核心组成部分——持续集成(CI)和持续交付(CD),并展示如何通过实际代码示例实现它们,以帮助团队构建更加高效和稳定的软件发布流程。
|
21天前
|
运维 安全 Devops
DevOps实践:持续集成与持续部署(CI/CD)的自动化之路
【10月更文挑战第22天】在软件交付的快速迭代中,DevOps文化和实践成为企业加速产品上市、保证质量和提升客户满意度的关键。本文将通过一个实际案例,深入探讨如何利用持续集成(Continuous Integration, CI)和持续部署(Continuous Deployment, CD)实现软件开发流程的高效自动化,包括工具选择、流程设计以及问题解决策略。我们将一起探索代码从编写到部署的全自动化旅程,揭示其对企业运维效率和产品质量所带来的深远影响。
|
3月前
|
jenkins 持续交付 开发者
自动化部署:使用Jenkins和Docker实现持续集成与交付
【8月更文挑战第31天】本文旨在为读者揭示如何通过Jenkins和Docker实现自动化部署,从而加速软件开发流程。我们将从基础概念讲起,逐步深入到实际操作,确保即使是初学者也能跟上步伐。文章将提供详细的步骤说明和代码示例,帮助读者理解并应用这些工具来优化他们的工作流程。
|
3天前
|
运维 jenkins Java
Jenkins在持续集成与持续部署中的价值
Jenkins在持续集成与持续部署中的价值

热门文章

最新文章