Assets for the Introduction to Kubernetes with Amazon EKS article on Dev.to
It is assumed you already have a Kubernetes cluster up and running, and that you can connect to it with kubectl
. If not, see:
- Minikube, kind for setting up a local, single-node cluster
- kubeadm for a self-managed, multi-node cluster
- Various managed Kubernetes offerings by cloud providers for a production-grade, multi-node cluster without the headache of self-management as in kubeadm
Now clone this repo and make it your working directory:
$ git clone https://github.com/DonaldKellett/k8s-intro-eks.git && cd k8s-intro-eks
Create our namespace, deploy the Pod and deploy the Service:
$ kubectl apply -f namespace.yaml
$ kubectl apply -f pod.yaml
$ kubectl apply -f clusterip-service.yaml
The pod is running an HTTPS web server so we'll refer to it as our website subsequently.
Take note of the cluster IP of our Service with the following command:
$ kubectl get svc -n donaldsebleung-com
Export it to an environment variable CLUSTER_IP
. Afterwards, spawn a shell within our pod and query our website with wget
through our cluster IP:
$ kubectl exec \
-n donaldsebleung-com \
-it donaldsebleung-com \
-- /bin/bash -c "wget -qO - https://$CLUSTER_IP --no-check-certificate"
First delete our existing pod and service, if any:
$ kubectl delete -f clusterip-service.yaml
$ kubectl delete -f pod.yaml
Now ensure an appropriate load balancer controller is deployed to your Kubernetes cluster. For example, with Amazon EKS, you need an AWS Load Balancer Controller.
Now deploy our Deployment and Service:
$ kubectl apply -f deployment.yaml
$ kubectl apply -f loadbalancer-service.yaml
Discover the external IP (or DNS name) of your service:
$ kubectl get svc -n donaldsebleung-com
Now visit that external IP / DNS name using your favorite web browser, prefixing it with https://
if necessary, and ignore any warnings from your browser about a self-signed certificate.
Apply a modified configuration for our Deployment, which uses a newer container image for the underlying pods:
$ kubectl apply -f deployment-patched.yaml
Now visit the website again over the next few minutes. You should eventually notice that the slogan on the homepage has changed from "IT consultant by day, software developer by night" to "Cloud, virtualization and open source enthusiast".
Ensure a metrics server is deployed to your Kubernetes cluster. Now set resource limits on containers in pods within our deployment, then deploy a horizontal pod autoscaler, often abbreviated HPA:
$ kubectl apply -f deployment-patched-with-limit.yaml
$ kubectl apply -f hpa.yaml
Wait for a few moments (maybe a minute or two), then query our HPA to see that we now have 1 replica instead of 3:
$ kubectl get hpa -n donaldsebleung-com
Bombard our website with requests - you might want to do this in multiple terminal windows / tabs. This assumes the EXTERNAL_IP
environment variable is set accordingly:
$ while true; do wget -qO - "https://$EXTERNAL_IP" --no-check-certificate > /dev/null; done
Wait for a few more minutes, then notice that the number of replicas is scaled up to 10 (or some other number greater than 1, depending on the amount of load you produced):
$ kubectl get hpa -n donaldsebleung-com
Stop the bombardment, the wait a few minutes yet again and see the number of replicas scaled back down to 1:
$ kubectl get hpa -n donaldsebleung-com
Delete the namespace. This also delete all resources within that namespace:
$ kubectl delete -f namespace.yaml
You may also want to decommission the cluster to save costs, if you are using a managed pay-as-you-go Kubernetes offering by a cloud provider.