在 Kubernetes 中运行 Locust 与 Selenium:安装 Chrome 和 ChromeDriver
在现代软件开发中,性能和用户体验是至关重要的,而负载测试和自动化测试可以帮助我们实现这一目标。在本文中,我们将讨论如何在 Kubernetes 环境中运行 Locust 和 Selenium,并详细介绍如何安装 Chrome 和 ChromeDriver。
1. Dockerfile 配置
首先,我们需要创建一个 Dockerfile,以构建一个包含 Locust 和 Selenium 的 Docker 镜像。以下是 Dockerfile 的内容:
FROM locustio/locust:2.31.3 # 设置 Chrome 的版本
ENV CHROME_VERSION 130.0.6723.69 USER root
RUN apt-get update -qq -y && \
apt-get install -y wget unzip && \
apt-get install -y \
libasound2 \
libatk-bridge2.0-0 \
libgtk-4-1 \
libnss3 \
xdg-utils && \
wget -q -O chrome-linux64.zip https://storage.googleapis.com/chrome-for-testing-public/$CHROME_VERSION/linux64/chrome-linux64.zip && \
unzip chrome-linux64.zip && \
rm chrome-linux64.zip && \
mv chrome-linux64 /opt/chrome/ && \
ln -s /opt/chrome/chrome /usr/local/bin/ && \
wget -q -O chromedriver-linux64.zip https://storage.googleapis.com/chrome-for-testing-public/$CHROME_VERSION/linux64/chromedriver-linux64.zip && \
unzip -j chromedriver-linux64.zip chromedriver-linux64/chromedriver && \
rm chromedriver-linux64.zip && \
mv chromedriver /usr/local/bin/ # 设置 Chrome 的配置和缓存目录
ENV XDG_CONFIG_HOME=/tmp/.chromium
ENV XDG_CACHE_HOME=/tmp/.chromium COPY . .
RUN pip install -r requirements.txt
解释
- 基础镜像:使用
locustio/locust作为基础镜像。 - 安装依赖:更新包管理器并安装必要的库,以确保 Chrome 和 ChromeDriver 正常运行。
- 下载和安装 Chrome 和 ChromeDriver:从 Google 的存储库下载 Chrome 和 ChromeDriver。
- 配置目录:通过环境变量设置 Chrome 的配置和缓存目录,这一步非常重要。若未设置正确,可能会在 Kubernetes 中出现权限问题,具体参考 puppeteer-sharp
requirements.txt 示例
locust=2.31.3
selenium==4.21.0
2. Chrome 选项配置
在使用 Selenium 时,我们需要为 Chrome 配置一些选项,以确保它能够在无头模式下正常工作。以下是获取 Chrome 选项的代码示例:
import platform
from selenium import webdriver def is_running_in_linux():
return platform.system() == 'Linux' def get_chrome_options():
is_in_linux = is_running_in_linux()
options_custom = webdriver.ChromeOptions() # Linux 下的 Chrome 选项
if is_in_linux:
options_custom.add_argument("--headless") # 无头模式
options_custom.add_argument('--disable-gpu') # 禁用 GPU 加速
options_custom.add_argument("--no-sandbox") # 禁用沙箱模式
else:
options_custom.add_argument("--start-maximized") # 启动时最大化窗口 # 其他通用选项
options_custom.add_argument("--disable-dev-shm-usage") # 解决资源限制问题
options_custom.add_argument("--ignore-ssl-errors=yes") # 忽略 SSL 错误
options_custom.add_argument("--disable-cache") # 禁用缓存 return options_custom
解释
- 操作系统检测:根据当前操作系统选择适当的 Chrome 选项。
- 无头模式:在 Linux 环境中使用无头模式,以便在没有图形界面的情况下运行 Chrome。
- 禁用沙箱:在 Kubernetes 环境中,禁用沙箱模式可以避免潜在的权限问题。
3. Locust 用户定义
下面是一个简单的 Locust 用户示例,使用 Selenium 控制 Chrome 访问特定页面:
from locust import User, task class GetUrl(User):
customdriver = None def on_start(self):
self.customdriver = webdriver.Chrome(options=get_chrome_options()) @task
def load_page(self):
self.customdriver.get("http://example.com") # 根据需要替换为实际 URL
解释
- 用户定义:创建一个继承自
User的类,使用 Selenium 控制 Chrome。 - 启动时操作:在用户启动时初始化
customdriver。 - 任务定义:在
load_page方法中执行实际的页面加载操作。
4. Kubernetes 部署
完成 Dockerfile 和代码后,可以将其构建为 Docker 镜像,并在 Kubernetes 中部署。以下是一个基本的 Kubernetes YAML 配置示例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: locust
spec:
replicas: 1
selector:
matchLabels:
app: locust
template:
metadata:
labels:
app: locust
spec:
containers:
- name: locust
image: your-docker-image:latest
ports:
- containerPort: 8089
env:
- name: XDG_CONFIG_HOME
value: /tmp/.chromium # 设置 Chrome 的配置目录
- name: XDG_CACHE_HOME
value: /tmp/.chromium # 设置 Chrome 的缓存目录
---
apiVersion: v1
kind: Service
metadata:
name: locust-service
spec:
type: NodePort
ports:
- port: 8089
targetPort: 8089
selector:
app: locust
解释
- Deployment:定义 Locust 的 Deployment,指定容器镜像和服务端口。
- Service:创建一个 Service,使外部能够访问 Locust Web 界面。
结论
通过以上步骤,我们成功在 Kubernetes 中运行了 Locust 和 Selenium,并安装了 Chrome 和 ChromeDriver。确保配置正确的环境变量和 Chrome 选项,可以大大提高在 Kubernetes 环境中的稳定性。如果您有更多需求,可以根据项目的具体情况进行扩展和调整。
在 Kubernetes 中运行 Locust 与 Selenium:安装 Chrome 和 ChromeDriver的更多相关文章
- python+ubuntu+selenium安装chrome和chromedriver
请确保selenium已经安装成功,没安装的可以pip install selenium 安装chrome 在终端输入 下载安装包 wget https://dl.google.com/linux/d ...
- 【Linux】【Selenium】安装Chrome和ChromeDriver的配置
转自:https://www.cnblogs.com/longronglang/p/8078898.html 1.安装chrome sudo apt-get install libxss1 libap ...
- centos 无界面 服务器 安装chrome部署chromedriver
转:https://blog.csdn.net/u013849486/article/details/79466359 基本 做完了,要弄进docker里面去了的时候,才搜到 docker-chrom ...
- ubuntu 安装chrome 和chromedriver
1. chromedriver 下载地址: https://npm.taobao.org/mirrors/chromedriver 在这里找到对应的驱动 2. 安装谷歌浏览器 2.1 安装依赖 ap ...
- linux安装chrome及chromedriver(转)
1.chrome: curl https://intoli.com/install-google-chrome.sh | bash 1.1.centos安装chrome: 從 Google 下載最新版 ...
- 在kubernetes中运行单节点有状态MySQL应用
Deploy MySQL https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-applicati ...
- 如何解决selenium打开chrome提示chromedriver.exe已停止工作
场景:启动Chrome,打开URL,提示“disconnected: unable to connect to renderer” 解决方法:chromedriver与chrome的对应关系表, 需要 ...
- Nodejs的安装配置及如何在sublimetext2中运行js
Nodejs的安装配置及如何在sublimetext2中运行js听语音 | 浏览:4554 | 更新:2015-06-16 11:29 Nodejs的安装配置及如何在sublimetext2中运行js ...
- Kubernetes 中的核心组件与基本对象概述
Kubernetes 是 Google 基于 Borg 开源的容器编排调度,用于管理容器集群自动化部署.扩容以及运维的开源平台.作为云原生计算基金会 CNCF(Cloud Native Computi ...
- 在 Kubernetes 上运行高可用的 Kafka 集群
转载自:https://www.qikqiak.com/post/deploy-kafka-ha-on-k8s/ Apache Kafka 是目前最流行的分布式消息发布订阅系统,虽然 Kafka 非常 ...
随机推荐
- CentOS下离线安装gcc环境,图文详细,方法全面
CentOS下离线安装gcc环境,图文详细,方法全面 下载 方式1:如果有网的虚拟机还没有安装,可以直接 yum install --downloadonly --downloaddir=/root/ ...
- .NET+WPF 桌面快速启动工具 GeekDesk
前言 大家在平时工作中,是不是经常为了找某个文件或者应用而在电脑桌面上来回翻找?桌面图标乱七八糟,每次找东西都像在大海捞针一样. 今天给大家介绍一个开源项目 GeekDesk,它能够让桌面焕然一新,工 ...
- yum下载包保存到本地
1.使用yumdownloadonly下载RPM包及依赖包 #下载yumdownloadonly插件 yum install yum-plugin-downloadonly # yum 下载rpm包到 ...
- Mac安装Adobe PS_AE_PR等系列软件提示错误代码146怎么办?
在安装Mac版Adobe系列软件的时候,不管PS.AE.PR.AI等,如果出现错误代码146,下面两个方法能够轻松解决. 解决方法一:需要我们打开「系统设置」-「隐私与安全」-「App管理」,打开「i ...
- C 语言编译过程
编译过程 编译过程是将高级编程语言(如 C 语言)写成的源代码转换成机器可以执行的低级机器语言(通常是二进制代码)的过程.这个过程一般可以分为几个阶段: 预处理(Preprocessing): 预处理 ...
- 【Linux】之切换root用户与重启系统相关命令
一.切换用户 <Linux中怎么从root用户切换到普通用户> su是在用户间切换,可以是从普通用户切换到root用户, test@ubuntu:~$ su Password: root@ ...
- [python][selenium] Web UI自动化8种页面元素定位方式
关联文章:Web UI自动化页面切换iframe框架 简单的加个前置知识: 第一:webdriver.Chrome()这句话,通过WebDriver的构造方法,拿到浏览器驱动的对象,然后通过这个对象, ...
- JavaScript 中 structuredClone 和 JSON.parse(JSON.stringify()) 克隆对象的区别
JavaScript 中 structuredClone 和 JSON.parse(JSON.stringify()) 克隆对象的异同点 一.什么是 structuredClone? 1. struc ...
- Nuxt Kit 中的上下文处理
title: Nuxt Kit 中的上下文处理 date: 2024/9/16 updated: 2024/9/16 author: cmdragon excerpt: Nuxt Kit 提供的上下文 ...
- [OI] Kruskal 重构树
算法介绍 Kruskal 重构树用于快速判断节点的连通性. 考虑到,假如两个节点是联通的,则他们之间总会有一条边被选入最小生成树内,因此他们在最小生成树内也是联通的. 也就是说,我们可以通过求最小生成 ...