# creates a cluster
k3d cluster create mycluster -p "8081:80@loadbalancer" -p "8082:443@loadbalancer" --agents 2
# displays cluster information (kubectl configuration is automatically updated and set to use the new cluster context)
kubectl cluster-info
# ensures coredns and traefik (ingress controller) are deployed by default (k3s behavior)
kubectl get deploy -n kube-system
# (optional) writes and uses specific kubectl configuration
export KUBECONFIG="$(k3d kubeconfig write mycluster)"
# creates a nginx (web server) deployment
kubectl create deployment nginx --image=nginx
# exposes the deployment with a service
kubectl create service clusterip nginx --tcp=80:80
# provides an ingress to the service
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx
annotations:
ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- host: nginx.dev.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx
port:
number: 80
EOF
# checks everything is ok
kubectl get svc,pod,deploy,ingress
# makes sure the website can be reached
curl localhost:8081/
Update hosts file
127.0.0.1 nginx.dev.local
Make sure ingress is working
curl nginx.dev.local:8081/
Clean-up
# deletes the cluster
k3d cluster delete mycluster