Skip to content

Latest commit

 

History

History
90 lines (68 loc) · 2.79 KB

CREATE-K3D.md

File metadata and controls

90 lines (68 loc) · 2.79 KB

k3d Setup for Linkerd and Cert-Manager in Production Workshop

This is the documentation - and executable code! - for creating a simple k3d cluster for the Service Mesh Academy the "Linkerd and Cert-Manager in Production" workshop. The easiest way to use this file is to execute it with demosh.

Things in Markdown comments are safe to ignore when reading this later. When executing this with demosh, things after the horizontal rule below (which is just before a commented @SHOW directive) will get displayed.

We'll do nothing at all if there's already cluster named pki.

set -e
DEMOSH_QUIET_FAILURE=true

if kubectl --context pki config get-contexts pki >/dev/null 2>&1; then \
    echo "Cluster 'pki' already exists" >&2 ;\
    exit 1 ;\
fi

The Linkerd and Cert-Manager in Production workshop can use pretty much any kind of cluster, but using k3d for it can be particularly convenient. Here, we'll set up the one cluster that we need, but we won't install anything yet.

The only weird bits here are:

  1. We're deliberately not deploying k3d local-storage, metrics, or Traefik -- we don't need them, and it's a little easier on the host not to run them.

  2. We're mapping ports 80 and 443 through from the host network to the k3d cluster, to make it easier to access the LoadBalancer service we'll use.

k3d cluster create pki \
        --port "80:80@loadbalancer" \
        --port "443:443@loadbalancer" \
        --k3s-arg='--no-deploy=local-storage,metrics-server@server:*' \
        --k3s-arg '--no-deploy=traefik@server:*;agents:*' \
        --kubeconfig-update-default \
        --kubeconfig-switch-context=false

After that, rename the context so it doesn't start with k3d-...

kubectl config rename-context k3d-pki pki

...switch into it...

kubectx pki

...and then wait until the cluster has some running pods. The kubectl command in the loop will give [] when no pods exist, so any result with more than 2 characters in it indicates that some pods exist. (This is obviously a pretty basic check, but it's the way to do this without needing jq or the like.)

while true; do \
    count=$(kubectl get pods -n kube-system -l k8s-app=kube-dns -o jsonpath='{ .items }' | wc -c) ;\
    if [ $count -gt 2 ]; then break; fi ;\
done

Finally, wait for the kube-dns Pod to be ready. (This is the reason that the previous loop is there: trying to wait for a Pod that doesn't yet exist will throw an error.)

kubectl wait pod --for=condition=ready \
        --namespace=kube-system --selector=k8s-app=kube-dns \
        --timeout=1m

Done! The pki cluster should be ready for the rest of the workshop.