Skip to content

Instantly share code, notes, and snippets.

@alexanderkjeldaas
Last active January 10, 2024 19:07
Show Gist options
  • Save alexanderkjeldaas/58ff756f3d75419df69223946bcba0ca to your computer and use it in GitHub Desktop.
Save alexanderkjeldaas/58ff756f3d75419df69223946bcba0ca to your computer and use it in GitHub Desktop.
Setup k3s on Hetzner with CSI drivers
#!/bin/bash
LOCATION=${HCLOUD_LOCATION:-nbg1-dc3}
if [ -z "$HCLOUD_TOKEN" ]; then
echo "You need to set HCLOUD_TOKEN to an Hetzner API token!";
exit 1
fi
if [ -z "$SSH_KEY" ]; then
echo "You need to set SSH_KEY to the name of the ssh key you want to use!";
exit 1
fi
for bin in hcloud jq; do
if ! which $bin >/dev/null; then
echo "Cannot find the $bin binary!"
exit 1
fi
done
if ! hcloud ssh-key describe $SSH_KEY > /dev/null; then
echo "Could not find ssh key $SSH_KEY on hetzner"
exit 1
fi
if ! hcloud server describe k3s-master > /dev/null; then
echo "Creating a small (2GB memory) €2.49 master"
hcloud server create --name k3s-master --image ubuntu-18.04 --type cx11 --ssh-key $SSH_KEY --location $LOCATION
fi
hcloud server ssh k3s-master 'curl -sfL https://get.k3s.io | sh -'
mkdir -p $HOME/.kube
IP=$(hcloud server describe k3s-master -o json | jq -r .public_net.ipv4.ip)
hcloud server ssh k3s-master cat /etc/rancher/k3s/k3s.yaml | perl -pe "s/127.0.0.1/$IP/g" > $HOME/.kube/config-k3s
kubectl apply -f https://raw.githubusercontent.com/kubernetes/csi-api/release-1.13/pkg/crd/manifests/csidriver.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/csi-api/release-1.13/pkg/crd/manifests/csinodeinfo.yaml
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
name: hcloud-csi
namespace: kube-system
stringData:
token: $HCLOUD_TOKEN
EOF
kubectl apply -f https://raw.githubusercontent.com/hetznercloud/csi-driver/master/deploy/kubernetes/hcloud-csi.yml
# Verify that persistent volume claims are working.
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: hcloud-volumes
---
kind: Pod
apiVersion: v1
metadata:
name: my-csi-app
spec:
containers:
- name: my-frontend
image: busybox
volumeMounts:
- mountPath: "/data"
name: my-csi-volume
command: [ "sleep", "1000000" ]
volumes:
- name: my-csi-volume
persistentVolumeClaim:
claimName: csi-pvc
EOF
echo "Waiting for our test-pod called my-csi-app to come up"
sleep 20
# Check that /data looks like a Hetzner volume.
kubectl exec -it my-csi-app -- /bin/sh
kubectl delete pod my-csi-app
echo "# ***************************************************************"
echo "export KUBECONFIG=$HOME/.kube/config-k3s"
echo "# ***************************************************************"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment