Kubernetes : How to configure / implement HPA to use CPU for scaling

311 views Asked by At

I'm new to Kubernetes. I've a application written in go language which has a /live endpoint. I need to run scale service based on CPU configuration. How can I implement HPA (horizontal pod autoscale) based on CPU configuration. Can someone help me? Thanks! Below is the code for /live endpoint

    router.Get("/live", netHttp.HandlerFunc(
        func(w netHttp.ResponseWriter, r *netHttp.Request) {
            http.NewHandler(pg).ServeHTTP(w, r)
        },
    ))

Below is the service and deployment code:

apiVersion: v1
kind: Service
metadata:
  name: simple-service-webapp-service
  labels:
    app: simple-service-webapp
spec:
  ports:
  - port: 8080
    name: http
  selector:
    app: simple-service-webapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: simple-service-webapp-v1
  labels:
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: simple-service-webapp
      version: v1
  template:
    metadata:
      labels:
        app: simple-service-webapp
        version: v1
    spec:
      containers:
      - name: simple-service-webapp
        image: docker.io/225517/simple-service-webapp:v1
        resources:
          requests:
            cpu: 100m
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
        env:
          - name: POSTGRES_URL
            value: postgres://user:pass@postgresdb/simple-service?sslmode=disable
          - name: POSTGRES_HOST
            value: postgresdb
          - name: POSTGRES_PORT
            value: "5432"
          - name: POSTGRES_DB
            value: simple-service 
          - name: POSTGRES_USER
            value: user
          - name: POSTGRES_PASSWORD
            value: pass
          - name: POSTGRES_SSLMODE
            value: disable
        readinessProbe:
            httpGet:
                path: /live
                port: 8080
---

Below is the hpa configuration I applied:

kubectl autoscale deployment simple-service-webapp-v1 --cpu-percent=50 --min=1 --max=5
kubectl get hpa
1

There are 1 answers

4
Rohit On BEST ANSWER

HPA based on resource metrics like CPU require you to install a metrics server on Kube-system namespace before you configure HPA for a deployment

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.7/components.yaml

You can check if metrics server works correctly by below command which should display node utilisation.

kubectl top node