I built a custom k6 image with xk6-kubernetes and xk6-exec
below is my dockerfile -
#stage 1
FROM golang:1.21 as builder
WORKDIR $GOPATH/src/go.k6.io/k6
ADD . .
USER root
RUN go install go.k6.io/xk6/cmd/xk6@latest
RUN CGO_ENABLED=0 xk6 build
–with GitHub - grafana/xk6-kubernetes: Client extension for interacting with Kubernetes clusters from your k6 tests.
–output /tmp/k6
FROM builder as exec-builder
RUN xk6 build
–with github.com/grafana/xk6-exec@latest
–output /tmp/k6-exec
FROM alpine:3.18
USER root
RUN apk add --no-cache ca-certificates curl
&& adduser -D -u 12345 -g 12345 k6
COPY --from=builder /tmp/k6 /usr/bin/k6
COPY --from=exec-builder /tmp/k6-exec /usr/bin/k6-exec
USER 12345
WORKDIR /home/k6
ENTRYPOINT [“k6”]
how do i use k6-oprator testRun CRD to run below script that requires k6-exec.
script.js
// script.js
import exec from ‘k6/x/exec’;
export default function () {
console.log(exec.command(“date”));
console.log(exec.command(“ls”,[“-a”,“-l”]));
}
since the command is now
k6-exec run script.js
however k6-operator by default uses only k6
k6-testRun.yaml -
apiVersion: k6.io/v1alpha1
kind: TestRun
metadata:
name: k6-sample
spec:
parallelism: 1
script:
configMap:
name: k6-test-cm
file: “check-metrics.js”
separate: false
runner:
image:
imagePullPolicy: Always
metadata:
labels:
cool-label: foo
annotations:
cool-annotation: bar
securityContext:
runAsUser: 1000
runAsGroup: 1000
runAsNonRoot: true
resources:
limits:
cpu: “200m”
memory: “1000Mi”
requests:
cpu: “100m”
memory: “500Mi”
serviceAccountName: k6-sa
starter:
image:
imagePullPolicy: Always
metadata:
labels:
cool-label: foo
annotations:
cool-annotation: bar
securityContext:
runAsUser: 2000
runAsGroup: 2000
runAsNonRoot: true
serviceAccountName: k6-sa
Hi @gunjanvmirchandani
You have to push your custom k6 image into a docker registry (e.g. Docker Hub), and then use the image as runner.image
You can find documentation here: GitHub - grafana/k6-operator: An operator for running distributed k6 tests.
I hope this helps
Thanks @bandorko yes, i did push to artifactory in our case and using the same for runner and starter.
my main problem is this image now has 2 binaries - k6 (xk6-kubernetes) and k6-exec.
the sample jobs that k6-operator creates by defaults runs - k6 run script.js
however the script.js requires k6-exec binary.
I have the import statement, however it fails
k logs k6-sample-initializer-qpwxf
time=“2024-04-01T20:59:12Z” level=error msg="GoError: unknown module: k6/x/exec\
if i exec into the image and run -
Need some help on how can we make us of both extensions or do we need separate image per extension?
@gunjanvmirchandani you can set both –with argument for the same xk6 build run in the dockerfile to get a binary with both extensions. And you should leave the starter image on default, because starter pod just runs a curl command and not a k6 process, but you should set the initializer.image to your custom image because initializer pod also runs k6.
thanks @bandorko for the reply.
sorry for multiple questions, i am still little confused
in the k6-resource.yaml, we only have starter and runner images -
there is no initializer image
k6-resource.yml
apiVersion: k6.io/v1alpha1
kind: TestRun
metadata:
name: k6-sample
spec:
parallelism: 4
script:
configMap:
name: k6-test
file: test.js
separate: false
runner:
** image: **
metadata:
labels:
cool-label: foo
annotations:
cool-annotation: bar
securityContext:
runAsUser: 1000
runAsGroup: 1000
runAsNonRoot: true
resources:
limits:
cpu: 200m
memory: 1000Mi
requests:
cpu: 100m
memory: 500Mi
starter:
** image: **
metadata:
labels:
cool-label: foo
annotations:
cool-annotation: bar
securityContext:
runAsUser: 2000
runAsGroup: 2000
runAsNonRoot: true
This is my dockerfile.
If i add the image to runner, it fails to execute the scrip that needs k6-exec with error -
k logs k6-sample-initializer-zxzvn
time=“2024-04-03T21:38:01Z” level=error msg=“GoError: unknown module: k6/x/exec\n\tat go.k6.io/k6/js.(*requireImpl).require-fm (native)\n\tat file:///test/script.js:2:0(21)\n” hint=“script exception”
@gunjanvmirchandani please modify your dockerfile:
RUN CGO_ENABLED=0 xk6 build
–with http://github.com/grafana/xk6-kubernetes –with github.com/grafana/xk6-exec@latest
–output /tmp/k6
And add an initializer section to your k6-resource.yaml
1 Like
perfect thanks @bandorko understood. let me give it a try. Appreciate your help
thanks a lot @bandorko it worked.
i can run the simple script. any idea how we can run kubectl commands?
Basically i am trying to run metrics command via k6 test case -
kubectl top pods
kubectl top nodes
1 Like