/local-k8s-virtual-host

Local kubernetes development environment with name-based virtual hosting

Primary LanguageHTMLMIT LicenseMIT

Local k8s development environment with name-based virtual hosting

#1 Required tools

#2 Build and test WEB container (with docker)

Build potato web image.

docker build --rm --tag 'potato/web' potato-web

Run potato web container.

docker run --detach --rm --name potato-web --publish 9090:8080 potato/web

Test potato web container.

curl localhost:9090 --header 'Host: blue.potato.com'
curl localhost:9090 --header 'Host: green.potato.com'

Stop potato web container.

docker stop potato-web

#3 Use k3d to setup a local k8s cluster

Create a local registry called k3d-registry.potato.com. The k3d- prefix will be added automatically by k3d.

k3d registry create registry.potato.com --port 12345

Add registry domain name into hosts file

sudo hostctl add domains potato k3d-registry.potato.com
hostctl list

Create a cluster called potato-cluster that uses the registry.

k3d cluster create potato-cluster --registry-use k3d-registry.potato.com:12345

Tag and push potato web image to the registry.

docker tag potato/web:latest k3d-registry.potato.com:12345/potato/web:0.1
docker push k3d-registry.potato.com:12345/potato/web:0.1

#4 Run potato web image in the k8s cluster

Apply k8s configuration to run potato web.

kubectl apply -f potato-web.yaml

Request (test) blue and green pages from potato web within k8s cluster.

POTATO_INGRESS_IP=$(kubectl get ingress/potato-web-ingress -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo ">> Ingress IP: ${POTATO_INGRESS_IP}"
curl $POTATO_INGRESS_IP --header 'Host: blue.potato.com'
curl $POTATO_INGRESS_IP --header 'Host: green.potato.com'

Delete all artifacts created from the configuration file

kubectl delete -f potato-web.yaml

#5 Use tilt to run potato web image in k8s (k3d) cluster

Start tilt.

tilt up

Add potato web domain names into hosts file

POTATO_INGRESS_IP=$(kubectl get ingress/potato-web-ingress -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo ">> Ingress IP: ${POTATO_INGRESS_IP}"
sudo hostctl add domains --ip $POTATO_INGRESS_IP potato blue.potato.com green.potato.com
hostctl list

Open tilt UI and click the links or open a web browser and test the blue and green domain names.

Remove potato web domain names from hosts file

sudo hostctl remove domains potato blue.potato.com green.potato.com

Stop tilt.

tilt down

#6 Delete k8s cluster created with k3d

Remove registry domain name from hosts file

sudo hostctl remove domains potato k3d-registry.potato.com

Delete registry.

k3d registry delete k3d-registry.potato.com

Delete cluster.

k3d cluster delete potato-cluster