Prerequisites

  • Kubernetes 1.12+
  • Helm 2.11+ or Helm 3.0-beta3+

Check version:

[email protected]:~# helm version
version.BuildInfo{Version:"v3.2.1", GitCommit:"fe51cd1e31e6a202cba7dead9552a6d418ded79a", GitTreeState:"clean", GoVersion:"go1.13.10"}

[email protected]:~# kubectl get node
NAME     STATUS   ROLES    AGE    VERSION
svr070   Ready    master   3d1h   v1.18.2
svr071   Ready    master   3d1h   v1.18.2
svr072   Ready    master   3d1h   v1.18.2
svr073   Ready    <none>   3d1h   v1.18.2
svr074   Ready    <none>   3d1h   v1.18.2

Installing the Chart

To install the chart with the release name my-release:

$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm install my-release bitnami/metrics-server

These commands deploy Metrics Server on the Kubernetes cluster in the default configuration. The Parameters section lists the parameters that can be configured during installation.

Tip: List all releases using helm list

[email protected]:~# helm install my-release bitnami/metrics-server
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
** Please be patient while the chart is being deployed **

The metric server has been deployed.

########################################################################################
### ERROR: The metrics.k8s.io/v1beta1 API service is not enabled in the cluster      ###
########################################################################################
You have disabled the API service creation for this release. As the Kubernetes version in the cluster
does not have metrics.k8s.io/v1beta1, the metrics API will not work with this release unless:
  You complete your metrics-server release by running:

[email protected]:~# helm upgrade my-release bitnami/metrics-server \
LAST DEPLOYED: Tue May 19 03:28:24 2020
TEST SUITE: None
NOTES:
** Please be patient while the chart is being deployed **

The metric server has been deployed.

In a few minutes you should be able to list metrics using the following
command:

  kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes"

Check pod metric-server

[email protected]:~# kubectl get pods --all-namespaces | grep metric
default             my-release-metrics-server-799b6b445d-95nmn                 1/1     Running   0          8m29s

# edit metric-server deployment to add the flags

$ kubectl edit deploy -n kube-system metrics-server
# args:
# - --kubelet-insecure-tls
# - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname

Uninstalling the Chart

To uninstall/delete the my-release deployment:

$ helm delete my-release

The command removes all the Kubernetes components associated with the chart and deletes the release.

Test AutoScalling Kubernetes

File:application/php-apache.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache
spec:
  selector:
    matchLabels:
      run: php-apache
  replicas: 1
  template:
    metadata:
      labels:
        run: php-apache
    spec:
      containers:
      - name: php-apache
        image: k8s.gcr.io/hpa-example
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m

---

apiVersion: v1
kind: Service
metadata:
  name: php-apache
  labels:
    run: php-apache
spec:
  ports:
  - port: 80
  selector:
    run: php-apache

Run the following command:

kubectl apply -f https://k8s.io/examples/application/php-apache.yaml
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
horizontalpodautoscaler.autoscaling/php-apache autoscaled

We may check the current status of autoscaler by running:

[email protected]:~# kubectl get hpa
NAME                     REFERENCE                TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
php-apache               Deployment/php-apache    0%/50%    1         10        1          24h

Increase load

Now, we will see how the autoscaler reacts to increased load. We will start a container, and send an infinite loop of queries to the php-apache service (please run it in a different terminal):

kubectl run -it --rm load-generator --image=busybox /bin/sh

Hit enter for command prompt

while true; do wget -q -O- http://php-apache; done

Within a minute or so, we should see the higher CPU load by executing:

[email protected]:~# kubectl get hpa
NAME                     REFERENCE                TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
php-apache               Deployment/php-apache    225%/50%   1         10        5          24h

Source:

https://hub.helm.sh/charts/bitnami/metrics-server
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/