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搭建和管理企业级网站应用
相关文章
|
6天前
|
运维 监控 Devops
DevOps实践:持续集成与持续部署(CI/CD)的精髓
在软件工程的海洋里,DevOps如同一艘高效的船只,而持续集成(Continuous Integration, CI)和持续部署(Continuous Deployment, CD)则是推动这艘船前行的强大双桨。本文将深入探索CI/CD的核心概念,揭示其如何助力团队高效协同、提升软件质量和加速产品上市。
22 1
|
10天前
|
jenkins 持续交付 开发工具
"引爆效率革命!Docker+Jenkins+GIT+Tomcat:解锁持续集成魔法,一键部署Java Web应用的梦幻之旅!"
【8月更文挑战第9天】随着软件开发复杂度的增加,自动化变得至关重要。本文通过实例展示如何结合Docker、Jenkins、Git与Tomcat建立高效的持续集成(CI)流程。Docker确保应用环境一致性;Jenkins自动化处理构建、测试和部署;Git管理源代码版本;Tomcat部署Web应用。在Jenkins中配置Git插件并设置项目,集成Docker构建Tomcat应用镜像并运行容器。此外,通过自动化测试、代码质量检查、环境隔离和日志监控确保CI流程顺畅,从而显著提高开发效率和软件质量。
30 3
|
10天前
|
jenkins Java 持续交付
【一键搞定!】Jenkins 自动发布 Java 代码的神奇之旅 —— 从零到英雄的持续集成/部署实战秘籍!
【8月更文挑战第9天】随着软件开发自动化的发展,持续集成(CI)与持续部署(CD)已成为现代流程的核心。Jenkins 作为一款灵活且功能丰富的开源 CI/CD 工具,在业界应用广泛。以一家电商公司的 Java 后端服务为例,通过搭建 Jenkins 自动化发布流程,包括创建 Jenkins 项目、配置 Git 仓库、设置构建触发器以及编写构建脚本等步骤,可以实现代码的快速可靠部署。
33 2
|
13天前
|
Kubernetes Devops 测试技术
DevOps实践:持续集成和持续部署(CI/CD)在现代企业中的应用
随着软件开发行业的迅猛发展,DevOps文化及其核心实践—持续集成(Continuous Integration, CI)与持续部署(Continuous Deployment, CD)—已成为提升软件交付速度和质量的关键策略。本文将深入探讨CI/CD的理论基础,并结合真实案例分析其在现代企业中的实际应用效果,旨在为读者提供一套可行的实施指南。
|
5天前
|
Kubernetes jenkins 网络安全
Jenkins Pipeline 流水线 - 使用代理节点,Remote SSH 对 K8S 进行升级
Jenkins Pipeline 流水线 - 使用代理节点,Remote SSH 对 K8S 进行升级
12 0
|
5天前
|
Kubernetes jenkins 持续交付
Jenkins + SVN/Git + Maven + Docker + 阿里云镜像 + Kubernetes(K8S)
Jenkins + SVN/Git + Maven + Docker + 阿里云镜像 + Kubernetes(K8S)
10 0
|
5天前
|
jenkins 持续交付 开发工具
自动化开发之旅:Docker携手Jenkins,与Git和Tomcat共舞持续集成
【8月更文挑战第13天】在软件开发中,持续集成(CI)通过自动化构建、测试与部署提升效率与稳定性。Docker、Jenkins、Git和Tomcat构成CI的黄金组合:`git push`触发Jenkins作业,利用Docker确保环境一致性,最终将应用部署至Tomcat。首先配置Git Webhooks以触发Jenkins;接着在Jenkins中创建作业并使用Docker插件模拟真实环境;通过Maven构建项目;最后部署至Tomcat。这套流程减少人为错误,提高开发效率,展示了技术的力量与流程的革新。
15 0
|
6天前
|
运维 Devops 测试技术
DevOps实践:持续集成与持续部署(CI/CD)的精髓
在软件工程领域,DevOps已经从一种概念演变为提升开发效率和产品质量的关键实践。本文将深入浅出地探讨如何通过实施持续集成(CI)和持续部署(CD)来优化软件开发流程,确保快速迭代同时保持高稳定性。我们将从基础概念出发,逐步深入到实际操作技巧,最后讨论如何克服常见的挑战,旨在为读者提供一条清晰的道路,以实现更高效、更可靠的软件交付。
18 0
|
26天前
|
运维 监控 Devops
DevOps实践:持续集成与持续部署的黄金路径
在数字化时代,快速迭代和高质量软件交付成为企业竞争的核心。本文深入探讨了DevOps文化下,持续集成(CI)与持续部署(CD)的最佳实践,旨在为读者提供一套实现高效、自动化的软件发布流程的方法论。通过分析现代软件开发的挑战,结合具体案例,本文详细阐述了如何构建一个灵活、高效的CI/CD流水线,以及如何利用监控和反馈机制不断优化这一过程。文章不仅适合运维人员阅读,同时也为软件开发者和项目经理提供了宝贵的参考。
|
5天前
|
运维 监控 Devops
DevOps实践之路:从理论到现实
本文将深入浅出地探讨DevOps的核心概念、实施步骤及面临的挑战,通过实际案例分析,展示如何将DevOps理念融入日常运维工作,提升软件开发与运维效率。文章旨在为读者提供一条清晰的DevOps实践路径,帮助团队实现更快的交付周期和更高的产品质量。