DevOps · k6 ·

[Kubernetes] Install K6-operator Using Helm Chart

Helm 설치 및 설명 참고 {: .prompt-info }

Install k6-operator #

1helm repo add grafana https://grafana.github.io/helm-charts
2helm repo update
3helm install k6-operator grafana/k6-operator

K6-operator 설치 참고

Customize Default Configuration #

Install Customize Default Configuration #

1helm install [RELEASE NAME] [Chart.yaml 경로] -f [YAML 파일 또는 URL에 값 지정 (여러 개를 지정가능)] -n [NAMESPACE NAME]
1helm install k6-operator grafana/k6-operator -f override-values.yaml -n [NAMESPACE NAME]

k6 resource 설정 관련 #

  • Resource yaml 작성

     1apiVersion: k6.io/v1alpha1
     2kind: TestRun
     3metadata:
     4  name: k6-sample
     5spec:
     6  parallelism: 4
     7  arguments: --out influxdb=http://influxdb:8086/k6
     8  arguments: -o xk6-influxdb=http://localhost:8086
     9  arguments: -o xk6-prometheus-rw --tag testid=test
    10  arguments: -o experimental-prometheus-rw    # prometheus : --enable-feature=remote-write-receiver
    11  cleanup: 'post'   # configure the automatic deletion of all resources
    12  script:
    13    configMap:
    14      name: k6-test
    15      file: test.js
    16  separate: false
    17  runner:
    18    image: <custom-image>
    19    metadata:
    20      labels:
    21        cool-label: foo
    22      annotations:
    23        cool-annotation: bar
    24    securityContext:
    25      runAsUser: 1000
    26      runAsGroup: 1000
    27      runAsNonRoot: true
    28    resources:
    29      limits:
    30        cpu: 200m
    31        memory: 1000Mi
    32      requests:
    33        cpu: 100m
    34        memory: 500Mi
    35  starter:
    36    image: <custom-image>
    37    metadata:
    38      labels:
    39        cool-label: foo
    40      annotations:
    41        cool-annotation: bar
    42    securityContext:
    43      runAsUser: 2000
    44      runAsGroup: 2000
    45      runAsNonRoot: true
  • 적용

    1kubectl apply -f /path/to/your/k6-resource.yml
  • 삭제

    1kubectl delete -f /path/to/your/k6-resource.yml

Run k6 사용법

Stream real-time

InfluxDB 설치 참고 {: .prompt-info }

Dockerfile Build with xk6-output-influxdb #

 1# Build the k6 binary with the extension
 2FROM golang:1.20 as builder
 3
 4RUN go install go.k6.io/xk6/cmd/xk6@latest
 5# For our example, we'll add support for output of test metrics to InfluxDB v2.
 6# Feel free to add other extensions using the '--with ...'.
 7RUN xk6 build \
 8    --with github.com/grafana/xk6-output-influxdb@latest \
 9    --output /k6
10
11# Use the operator's base image and override the k6 binary
12FROM grafana/k6:latest
13COPY --from=builder /k6 /usr/bin/k6

Dockerfile Build with xk6-output-prometheus-remote #

1# Build the k6 binary with the extension
2FROM golang:1.18.1 as builder
3
4RUN go install go.k6.io/xk6/cmd/xk6@latest
5RUN xk6 build --output /k6 --with github.com/grafana/xk6-output-prometheus-remote@latest
6
7# Use the operator's base image and override the k6 binary
8FROM grafana/k6:latest
9COPY --from=builder /k6 /usr/bin/k6

k6 resource 예시 #

 1apiVersion: k6.io/v1alpha1
 2kind: K6
 3metadata:
 4  name: k6-sample
 5spec:
 6  arguments: -o xk6-prometheus-rw --tag testid=test
 7  parallelism: 1
 8  runner:
 9    image: <custom-image>
10    env:
11    - name: K6_PROMETHEUS_RW_SERVER_URL
12      value: http://kube-prometheus-stack-prometheus.monitoring:9090/api/v1/write
13    - name: K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM
14      value: "true"
15    image: k6-prometheus:v1
16  starter:
17    image: <custom-image>
18  script:
19    configMap:
20      file: scritps.js
21      name: test-script

테스트 JavaScript #

Ex 1. #

 1import http from 'k6/http';
 2import { sleep } from 'k6';
 3
 4export const options = {
 5  stages: [
 6    { duration: '10s', target: 10 },
 7    { duration: '10s', target: 20 },
 8    { duration: '10s', target: 30 },
 9    { duration: '10s', target: 0 },
10  ],
11};
12
13export default function () {
14  http.get('http://test.k6.io');
15  sleep(1);
16}

Ex 2. #

 1import http from 'k6/http';
 2import { check } from 'k6';
 3
 4export const options = {
 5  insecureSkipTLSVerify: true,
 6  stages: [
 7    { target: 200, duration: '30s' },
 8    { target: 0, duration: '30s' },
 9  ],
10};
11
12export default function () {
13  const result = http.get('https://test-api.k6.io/public/crocodiles/');
14  check(result, {
15    'http response status code is 200': result.status === 200,
16  });
17}

테스트 JavaScript 적용 #

1kubectl -n [NAMESPACE NAME] create configmap test-script --from-file /home/documents/k6/scritps.js 

K6 Load Test 참고 {: .prompt-info }

Uninstall the Chart #

1helm uninstall [RELEASE NAME] -n [NAMESPACE NAME]
Advertisement