Deployment with HPA #
We will create a simple helloweb application with resource limits and hpa based on CPU usage. First create a GKE clutser and login to the cluster via kubectl.
Create the deployment with resource #
kubectl apply -f dc-myapp.yaml
Make sure you have define resources in the deployment yaml inroder to make hpa (based on cpu) to be worked.
dc-myapp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloweb
labels:
app: hello
spec:
selector:
matchLabels:
app: hello
tier: web
template:
metadata:
labels:
app: hello
tier: web
spec:
containers:
- name: hello-app
image: gcr.io/google-samples/hello-app:1.0
ports:
- containerPort: 8080
resources:
limits:
cpu: 200m
memory: 300Mi
requests:
cpu: 100m
memory: 100Mi
Create LoadBalancer service for the deployment #
kubectl apply -f svc-myapp.yaml
svc-myapp.yaml
apiVersion: v1
kind: Service
metadata:
name: helloweb
labels:
app: hello
spec:
selector:
app: hello
tier: web
ports:
- port: 80
targetPort: 8080
type: LoadBalancer
Create HPA for the deployment #
Create autoscale when cpu hit for 50%
kubectl autoscale deployment helloweb --max 6 --min 2 --cpu-percent 50
See the hpa. This will show all the hpa objects in the namespace
kubectl get hpa
Get LoadBalancer IP #
Shows all the services in the namesapace. Identify the helloweb svc and its external IP. Using that external IP you can access the app via a web browser.
kubectl get svc
Generate load for the app #
Here we create some load to that app. This will increase the cpu and then hpa will kick in and will spin few new pods.s
ab -k -c 1000 -n 2000000 http://<IP-of-the-loadbalancer>/