Location>code7788 >text

Running Locust with Selenium in Kubernetes: Installing Chrome and ChromeDriver

Popularity:134 ℃/2024-10-27 10:12:14

Performance and user experience are critical in modern software development, and load testing and automated testing can help us achieve this. In this article, we'll discuss how to run Locust and Selenium in a Kubernetes environment and detail how to install Chrome and ChromeDriver.

1. Dockerfile configuration

First, we need to create a Dockerfile to build a Docker image with Locust and Selenium. Here are the contents of the Dockerfile:

FROM locustio/locust:2.31.3

# Set the version of 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  https:///chrome-for-testing-public/$CHROME_VERSION/linux64/ && \
    unzip  && \
    rm  && \
    mv chrome-linux64 /opt/chrome/ && \
    ln -s /opt/chrome/chrome /usr/local/bin/ && \
    wget -q -O  https:///chrome-for-testing-public/$CHROME_VERSION/linux64/ && \
    unzip -j chromedriver-linux64/chromedriver && \
    rm && \
    mv chromedriver /usr/local/bin/

# Set up Chrome's configuration and cache directory
ENV XDG_CONFIG_HOME=/tmp/.chromium
ENV XDG_CACHE_HOME=/tmp/.chromium

COPY . .
RUN pip install -r 

account for

  1. Base Mirror: Uselocustio/locust as the base mirror image.
  2. Installation of dependencies: Update the package manager and install the necessary libraries to ensure that Chrome and ChromeDriver are running properly.
  3. Downloading and installing Chrome and ChromeDriver: fromGoogle's repositoryDownload Chrome and ChromeDriver.
  4. configuration directory: Setting Chrome's configuration and cache directories via environment variables is an important step. If you don't set this correctly, you may have permissions issues in Kubernetes.puppeteer-sharp

typical example

locust=2.31.3
selenium==4.21.0

2. Chrome options configuration

When using Selenium, we need to configure some options for Chrome to ensure that it works properly in headless mode. Below is an example of code to get the Chrome options:

import platform
from selenium import webdriver

def is_running_in_linux():
    return () == 'Linux'

def get_chrome_options():
    is_in_linux = is_running_in_linux()
    options_custom = ()

    # Linux lower Chrome options (as in computer software settings)
    if is_in_linux:
        options_custom.add_argument("--headless") # Headerless mode
        options_custom.add_argument('--disable-gpu') # Disable GPU acceleration
        options_custom.add_argument("--no-sandbox") # Disable sandbox mode
    else.
        options_custom.add_argument("--start-maximized") # Maximize window on startup

    # Other general options
    options_custom.add_argument("--disable-dev-shm-usage") # Resolve resource constraints
    options_custom.add_argument("--ignore-ssl-errors=yes") # Ignore SSL errors
    options_custom.add_argument("--disable-cache") # Disable caching

    return options_custom

account for

  • Operating System Detection: Select the appropriate Chrome option for your current operating system.
  • headless mode: Use headless mode in a Linux environment to run Chrome without a graphical interface.
  • Disable sandboxing: In a Kubernetes environment, disabling sandbox mode avoids potential permissions issues.

3. Locust user definitions

Here's a simple Locust user example that uses Selenium to control Chrome's access to a specific page:

from locust import User, task

class GetUrl(User):    
    customdriver = None  

    def on_start(self):               
         = (options=get_chrome_options())

    @task
    def load_page(self):
        ("")  # Replace as necessary with actual URL

account for

  • user-defined: Creates a file that inherits fromUser class that uses Selenium to control Chrome.
  • priming operation: Initialized at user startupcustomdriver
  • Mandate definition: inload_page method performs the actual page load operation.

4. Kubernetes deployment

After completing the Dockerfile and code, you can build it as a Docker image and deploy it in Kubernetes. Below is an example of a basic Kubernetes YAML configuration:

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 # set up Chrome The configuration directory of the
            - name: XDG_CACHE_HOME
              value: /tmp/.chromium # set up Chrome The cache directory of the
---
apiVersion: v1
kind: Service
metadata:
  name: locust-service
spec:
  type: NodePort
  ports:
    - port:8089
      targetPort: 8089
  selector:
    app: locust

account for

  • Deployment: Defines Locust's Deployment, specifying the container image and service port.
  • Service: Create a Service that enables external access to the Locust Web Interface.

reach a verdict

With the above steps, we have successfully run Locust and Selenium in Kubernetes with Chrome and ChromeDriver installed.Ensuring that you configure the correct environment variables and Chrome options can greatly improve your stability in a Kubernetes environment. If you have additional needs, you can extend and tweak them to suit your project.