菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow 设置breadcrumb arrow 设置分布式 k6breadcrumb arrow 用法breadcrumb arrow 使用 k6 Operator 和 k6 扩展
开源

使用 k6 Operator 和 k6 扩展

默认情况下,k6 Operator 使用 ghcr.io/grafana/k6-operator:latest-runner 作为测试作业的容器镜像。

如果您想使用使用 k6 扩展 构建的 xk6,您需要创建自己的镜像并覆盖 TestRun Kubernetes 资源上的 image 属性。

例如,这是一个用于构建带有 xk6-output-influxdb 扩展的 k6 二进制文件的 Dockerfile

Dockerfile
# Build the k6 binary with the extension
FROM golang:1.20 as builder

RUN go install go.k6.io/xk6/cmd/xk6@latest

# For our example, we'll add support for output of test metrics to InfluxDB v2.
# Feel free to add other extensions using the '--with ...'.
RUN xk6 build \
    --with github.com/grafana/xk6-output-influxdb@latest \
    --output /k6

# Use the operator's base image and override the k6 binary
FROM grafana/k6:latest
COPY --from=builder /k6 /usr/bin/k6

您可以通过执行以下命令,基于此 Dockerfile 构建镜像

bash
docker build -t k6-extended:local .

构建完成后,您可以将生成的 k6-extended:local 镜像推送到您的 Kubernetes 集群可以访问的镜像仓库。

然后您可以如下使用该镜像

yaml
# k6-resource-with-extensions.yml

apiVersion: k6.io/v1alpha1
kind: TestRun
metadata:
  name: k6-sample-with-extensions
spec:
  parallelism: 4
  script:
    configMap:
      name: my-stress-test
      file: test.js
  runner:
    image: k6-extended:local
    env:
      - name: K6_OUT
        value: xk6-influxdb=http://influxdb.somewhere:8086/demo

请注意,此示例使用 k6-extended:latest 覆盖了默认镜像,并且包含了 xk6-output-influxdb 扩展所需的环境变量。