-1

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
Sweta Sharma
  • 2,404
  • 4
  • 21
  • 36
  • 1
    I don't see any HPA configuration in what you've attached here; what have you tried already? Have you read through the [Horizontal Pod Autoscaler Walkthrough](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/) in the Kubernetes documentation? – David Maze Aug 03 '20 at 12:52
  • @DavidMaze Yes, I have read through the article. ```kubectl autoscale deployment simple-service-webapp-v1 --cpu-percent=50 --min=1 --max=5 ``` and then ```kubectl get hpa```. However I'm not sure how to create load on the ```/live``` endpoint. Should I create a seperate script for it. There is something called as load generator. What is the correct way to create load on ```live``` endpoint? – Sweta Sharma Aug 03 '20 at 14:37

1 Answers1

2

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
Rohit
  • 1,231
  • 10
  • 22
  • Hi Rohit, Just curious to know, why do we need to install metrics server on kube-system namespace. Can't we just add the load directly to the ```/live``` endpoint of the application? – Sweta Sharma Aug 04 '20 at 05:58
  • 1
    Without the metrics server the HPA will not get the metrics. This is the snippet from Kubernetes documentation. " The HorizontalPodAutoscaler normally fetches metrics from a series of aggregated APIs (metrics.k8s.io, custom.metrics.k8s.io, and external.metrics.k8s.io). The metrics.k8s.io API is usually provided by metrics-server, which needs to be launched separately. See metrics-server for instructions. The HorizontalPodAutoscaler can also fetch metrics directly from Heapster " [link](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/ ) – Rohit Aug 04 '20 at 06:34
  • okay, thank you for all the help. I'll install metric server on kube-system namespace. – Sweta Sharma Aug 04 '20 at 06:46
  • @SwetaSharma np. You can upvote if the answer is helpful. – Rohit Aug 04 '20 at 06:51