Set Up Minikube, Monitoring with prometheus and grafana, SetUp Horizontal Pod Autoscaler (HPA)
What you’ll need
- 2 CPUs or more
- 2GB of free memory
- 20GB of free disk space
- Internet connection
- Container or virtual machine manager, such as: Docker, QEMU, Hyperkit, Hyper-V, KVM, Parallels, Podman, VirtualBox, or VMware Fusion/Workstation
Installation
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb
sudo dpkg -i minikube_latest_amd64.deb
#Start your cluster
minikube start
#If you already have kubectl installed
kubectl get po -A
#Create a sample deployment and expose it on port 3000:
kubectl create deployment hello-minikube --laly9999/coming-soon-website:2
kubectl expose deployment hello-minikube --type=NodePort --port=3000
# use kubectl to forward the port:
kubectl port-forward service/hello-minikube 3005:3000
>>>>Tada! Your application is now available at http://localhost:3002
Manage your cluster
#Pause Kubernetes without impacting deployed applications:
minikube pause
#Unpause a paused instance:
minikube unpause
#Halt the cluster:
minikube stop
#Change the default memory limit (requires a restart):
minikube config set memory 9001
#Browse the catalog of easily installed Kubernetes services:
minikube addons list
#Create a second cluster running an older Kubernetes release:
minikube start -p aged --kubernetes-version=v1.16.1
#Delete all of the minikube clusters:
minikube delete --all
Monitoring with prometheus and grafana
Configure Prometheus:
kubectl create ns prometheus
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install my-prometheus prometheus-community/prometheus --version 25.8.2 -n prometheus
watch kubectl get pods -n prometheus
kubectl --namespace=prometheus port-forward deploy/my-prometheus-server 9090
Configure Grafana:
kubectl create ns grafana
helm repo add grafana https://grafana.github.io/helm-charts
helm install my-grafana grafana/grafana --version 7.0.19 -n grafana
export POD_NAME=$(kubectl get pods --namespace grafana -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=my-grafana" -o jsonpath="{.items[0].metadata.name}")
kubectl get secret --namespace grafana my-grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
kubectl port-forward --namespace grafana $POD_NAME 3000:3000
Go to the browser -> localhost:3000/login ; username=admin -> data sources -> prometheus -> URL=http://localhost:9090
If not working:
kubectl get pods -n prometheus
kubectl describe pod my-prometheus-server-######## -n prometheus #to get IP
Go to the browser -> localhost:3000/login ; username=admin -> data sources -> prometheus -> URL=http://IP:9090
Grafana -> new dashboard -> import dashboard -> import via grafana =3119
To get different dashboard ID: https://grafana.com/grafana/dashboards/?dataSource=prometheus
SetUp Horizontal Pod Autoscaler (HPA):
Horizontal scaling means that the response to increased load is to deploy more Pods. This is different from vertical scaling, which for Kubernetes would mean assigning more resources (for example: memory or CPU) to the Pods that are already running for the workload.
#run the following command to enable metrics-server:
minikube addons enable metrics-server
#Run and expose hello-minikube server
vim deployment.yml
----------
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-minikube
spec:
replicas: 1
selector:
matchLabels:
app: hello-minikube
template:
metadata:
labels:
app: hello-minikube
spec:
containers:
- name: hello-minikube
image: laly9999/coming-soon-website:2
resources:
requests:
cpu: "100m" # Request 100 milliCPU (0.1 CPU core)
memory: "128Mi" # Request 128 MiB of memory
limits:
cpu: "500m" # Limit to 500 milliCPU (0.5 CPU core)
memory: "256Mi" # Limit to 256 MiB of memory
-----------------------
kubectl apply -f deployment.yml
------------------------
vim service.yml
apiVersion: v1
kind: Service
metadata:
name: hello-minikube-service
spec:
selector:
app: hello-minikube # This should match the labels in your deployment
ports:
- protocol: TCP
port: 3005 # Port exposed by the service on the cluster
targetPort: 3000 # Port the service forwards traffic to in the pods
nodePort: 30000 # Port exposed on the nodes of the cluster (30000-32767 range)
type: NodePort # Type of service
--------------------
kubectl apply -f service.yml
------------------------
# Create an HPA for the hello-minikube deployment
vim hpa.yml
-----------------
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hello-minikube
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hello-minikube
minReplicas: 1
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
-----------
kubectl apply -f hpa.yaml
---------------------
#kubectl edit hpa hello-minikube #to update
#check the current status of the newly-made HorizontalPodAutoscaler
kubectl get hpa
minikube ip
kubectl get services
#Increase the load
kubectl run -i --tty load-generator --rm --image=laly9999/coming-soon-website:2 --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://192.168.49.2:30000/; done"
#To temporarily disable the HPA and manually set the desired replica count,
kubectl delete hpa hello-minikube
kubectl scale deployment hello-minikube --replicas=1
kubectl apply -f hpa.yaml
After running the increase load:
kubectl get hpa hello-minikube --watch
Within a minute or so, you should see the higher CPU load; for example:
Here, CPU consumption has increased to 282% of the request. As a result, the Deployment was resized to 6 replicas:
kubectl get deployment hello-minikube
You should see the replica count matching the figure from the HorizontalPodAutoscaler
############Stop generating load
# type Ctrl+C to end the watch when you’re ready
kubectl get hpa hello-minikube --watch
and the Deployment also shows that it has scaled down:
kubectl get deployment hello-minikube
Once CPU utilization dropped to 1, the HPA automatically scaled the number of replicas back down to 1.
Autoscaling the replicas may take a few minutes.