Skip to content

Instantly share code, notes, and snippets.

@simonswine
Last active April 1, 2019 10:35
Show Gist options
  • Save simonswine/10ad9ae14f8c818d4234f2971d32785e to your computer and use it in GitHub Desktop.
Save simonswine/10ad9ae14f8c818d4234f2971d32785e to your computer and use it in GitHub Desktop.
Run kubernetes in Kind
apiVersion: batch/v1
kind: Job
metadata:
name: christian-kind
spec:
template:
spec:
initContainers:
- name: install-docker-bin
command:
- /bin/sh
- -e
- -x
- -c
- |
cp -a $(which docker) /opt/docker/bin
image: docker:18.09-dind
volumeMounts:
- mountPath: /opt/docker/bin
name: docker-bin
resources:
requests:
cpu: 10m
restartPolicy: Never
containers:
- name: kind
image: golang:1.12.1
command:
- /bin/bash
- -e
- -x
- -c
- |
#!/bin/bash
# signal termination to sidecar
trap "touch /var/run/terminated" EXIT
# put docker into the path
export PATH=/opt/docker/bin:$PATH
# install kind
go get -u sigs.k8s.io/kind
# show docker info
docker info
# create kind cluster
kind create cluster
# copy kubectl
docker cp kind-control-plane:/kind/bin/kubectl /usr/local/bin/kubectl
# show status of kind cluster
export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
kubectl get pods --all-namespaces
kubectl cluster-info
volumeMounts:
- mountPath: /opt/docker/bin
name: docker-bin
- mountPath: /var/run
name: docker-sock
resources:
requests:
cpu: 10m
- name: dind
image: docker:18.09-dind
command:
- /bin/sh
- -e
- -x
- -c
- |
# run docker in background
dockerd \
--host=unix:///var/run/docker-sock/docker.sock \
--storage-driver=overlay &
CHILD_PID=$!
# watch termination file
(while true; do if [[ -f "/var/run/docker-sock/terminated" ]]; then kill $CHILD_PID; echo "Killed $CHILD_PID because the main container terminated."; fi; sleep 1; done) &
# wait for docker
wait $CHILD_PID || true
# always return 0 if terminated
if [[ -f "/var/run/docker-sock/terminated" ]]; then exit 0; echo "Job completed. Exiting..."; fi
resources:
requests:
cpu: 500m
memory: 1Gi
securityContext:
privileged: true
capabilities:
add: ["SYS_ADMIN"]
volumeMounts:
- mountPath: /lib/modules
name: modules
readOnly: true
- mountPath: /sys/fs/cgroup
name: cgroup
- mountPath: /var/lib/docker
name: docker-graph
- mountPath: /var/run/docker-sock
name: docker-sock
volumes:
- name: modules
hostPath:
path: /lib/modules
type: Directory
- name: cgroup
hostPath:
path: /sys/fs/cgroup
type: Directory
- name: docker-graph
emptyDir: {}
- name: docker-bin
emptyDir: {}
- name: docker-sock
emptyDir: {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment