Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Client extension for interacting with Kubernetes clusters from your k6 tests.

License

NotificationsYou must be signed in to change notification settings

grafana/xk6-kubernetes

Go ReferenceVersion BadgeBuild Status

xk6-kubernetes

A k6 extension for interacting with Kubernetes clusters while testing.

Build

To build a customk6 binary with this extension, first ensure you have the prerequisites:

  1. Downloadxk6:

    go install go.k6.io/xk6/cmd/xk6@latest
  2. Build the k6 binary:

    xk6 build --with github.com/grafana/xk6-kubernetes

    Thexk6 build command creates a k6 binary that includes the xk6-kubernetes extension in your local folder. This k6 binary can now run a k6 test usingxk6-kubernetes APIs.

Development

To make development a little smoother, use theMakefile in the root folder. The default target will format your code, run tests, and create ak6 binary with your local code rather than from GitHub.

git clone git@github.com:grafana/xk6-kubernetes.gitcd xk6-kubernetesmake

Using thek6 binary withxk6-kubernetes, run the k6 test as usual:

./k6 run k8s-test-script.js

Usage

The API assumes akubeconfig configuration is available at any of the following default locations:

  • at the location pointed by theKUBECONFIG environment variable
  • at$HOME/.kube

APIs

Generic API

This API offers methods for creating, retrieving, listing and deleting resources of any of the supported kinds.

MethodParametersDescription
applymanifest stringcreates a Kubernetes resource given a YAML manifest or updates it if already exists
createspec objectcreates a Kubernetes resource given its specification
deletekindremoves the named resource
name
namespace
getkindreturns the named resource
name
namespace
listkindreturns a collection of resources of a given kind
namespace
updatespec objectupdates an existing resource

Examples

Creating a pod using a specification

import{Kubernetes}from'k6/x/kubernetes';constpodSpec={apiVersion:"v1",kind:"Pod",metadata:{name:"busybox",namespace:"testns"},spec:{containers:[{name:"busybox",image:"busybox",command:["sh","-c","sleep 30"]}]}}exportdefaultfunction(){constkubernetes=newKubernetes();kubernetes.create(pod)constpods=kubernetes.list("Pod","testns");console.log(`${pods.length} Pods found:`);pods.map(function(pod){console.log(`${pod.metadata.name}`)});}

Creating a job using a YAML manifest

import{Kubernetes}from'k6/x/kubernetes';constmanifest=`apiVersion: batch/v1kind: Jobmetadata:  name: busybox  namespace: testnsspec:  template:    spec:      containers:      - name: busybox        image: busybox        command: ["sleep", "300"]    restartPolicy: Never`exportdefaultfunction(){constkubernetes=newKubernetes();kubernetes.apply(manifest)constjobs=kubernetes.list("Job","testns");console.log(`${jobs.length} Jobs found:`);pods.map(function(job){console.log(`${job.metadata.name}`)});}

Helpers

Thexk6-kubernetes extension offers helpers to facilitate common tasks when setting up a tests. All helper functions work in a namespace to facilitate the development of tests segregated by namespace. The helpers are accessed using the following method:

MethodParametersDescription
helpersnamespacereturns helpers that operate in the given namespace. If none is specified, "default" is used

The methods above return an object that implements the following helper functions:

MethodParametersDescription
getExternalIPservicereturns the external IP of a service if any is assigned before timeout expires
timeout in seconds
waitPodRunningpod namewaits until the pod is in 'Running' state or the timeout expires. Returns a boolean indicating of the pod was ready or not. Throws an error if the pod is Failed.
timeout in seconds
waitServiceReadyservice namewaits until the given service has at least one endpoint ready or the timeout expires
timeout in seconds

Examples

Creating a pod and wait until it is running

import{Kubernetes}from'k6/x/kubernetes';letpodSpec={apiVersion:"v1",kind:"Pod",metadata:{name:"busybox",namespace:"default"},spec:{containers:[{name:"busybox",image:"busybox",command:["sh","-c","sleep 30"]}]}}exportdefaultfunction(){constkubernetes=newKubernetes();// create podkubernetes.create(pod)// get helpers for test namespaceconsthelpers=kubernetes.helpers()// wait for pod to be runningconsttimeout=10if(!helpers.waitPodRunning(pod.metadata.name,timeout)){console.log(`"pod${pod.metadata.name} not ready after${timeout} seconds`)}}

About

Client extension for interacting with Kubernetes clusters from your k6 tests.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp