본문 바로가기

🔒 영어 기술 (English technology)

K8S Load Test (K8S 부하 테스트)

# Load Test Description and Configuration Chart

1.kubelet-cAdvisor:cAdvisor (Container Advisor) provides container users with information about the resource usage and performance characteristics of the running container.
2Aggregate: An execution daemon that collects processes and exports information about running containers.
3.k8s API Server: The API is an interface used to manage, create, and configure a Couvernetis cluster.Through this interface, users, external components, and parts of the cluster communicate with each other.
4. HPA: HPA monitors metrics, and in fact, increasing or decreasing the number of Pods is controlled by ReplicaSet or Deployment. 

5. ReplicaSet: ReplicaSet ensures availability of the number of pads to be executed and manages to run at all times as many as you specify. In other words, if you set the five pods to run all the time, if one pod is deleted, one pod will run again, allowing you to keep five.

#Image creation for load testing

1. Create a new folder.

mkdir php
cd php

2. Generate a load test code.

vim index.php

<?php
  $x = 0.0001;
  for ($i = 0; $i <= 1000000; $i++) {
    $x += sqrt($x);
  }
  echo "OK!";
?>

3. Create docker image with load test code

vim Dockerfile

FROM php:5-apache
ADD index.php /var/www/html/index.php
RUN chmod a+rx index.php

4. Build Docker Image
- Read the Dockerfile of the current folder and build it on that id.

$ docker build --tag {docker id}/php-apache .

$ docker images |grep php

kongru/php-apache                    latest              39e1797ad29c        23 seconds ago      355MB

- Build execution history: The build operation can be considered an image creation operation.

ec2-user:~/environment/php $ docker build --tag may9noy/php-apache .
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM php:5-apache
5-apache: Pulling from library/php
5e6ec7f28fb7: Pull complete 
cf165947b5b7: Pull complete 
7bd37682846d: Pull complete 
99daf8e838e1: Pull complete 
ae320713efba: Pull complete 
ebcb99c48d8c: Pull complete 
9867e71b4ab6: Pull complete 
936eb418164a: Pull complete 
bc298e7adaf7: Pull complete 
ccd61b587bcd: Pull complete 
b2d4b347f67c: Pull complete 
56e9dde34152: Pull complete 
9ad99b17eb78: Pull complete 
Digest: sha256:0a40fd273961b99d8afe69a61a68c73c04bc0caa9de384d3b2dd9e7986eec86d
Status: Downloaded newer image for php:5-apache
 ---> 24c791995c1e
Step 2/3 : ADD index.php /var/www/html/index.php
 ---> 2dd98569fdec
Step 3/3 : RUN chmod a+rx index.php
 ---> Running in 76dca74c5529
Removing intermediate container 76dca74c5529
 ---> 4d6b9c737c37
Successfully built 4d6b9c737c37
Successfully tagged may9noy/php-apache:latest
ec2-user:~/environment/php $

- If you check the docker image after the build is completed, you can see that it was created as follows.

ec2-user:~/environment/php $ docker images
REPOSITORY           TAG          IMAGE ID       CREATED             SIZE
may9noy/php-apache   latest       4d6b9c737c37   About an hour ago   355MB
jenkins/jenkins      lts-jdk11    619aabbe0502   7 weeks ago         441MB
lambci/lambda        python3.8    094248252696   8 months ago        524MB
lambci/lambda        nodejs12.x   22a4ada8399c   8 months ago        390MB
lambci/lambda        nodejs10.x   db93be728e7b   8 months ago        385MB
lambci/lambda        python3.7    22b4b6fd9260   8 months ago        946MB
lambci/lambda        python3.6    177c85a10179   8 months ago        894MB
lambci/lambda        python2.7    d96a01fe4c80   8 months ago        763MB
lambci/lambda        nodejs8.10   5754fee26e6e   8 months ago        813MB
php                  5-apache     24c791995c1e   2 years ago         355MB

- Up to this point, it can be considered that the docker image has been created and stored in the local environment, and separate work is required to upload it to the docker hub.

Try pushing the generated docker image to the docker hub.
- First, enter docker login, authenticate your personal ID and password, and log in to docker hub.

ec2-user:~/environment/php $ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: may9noy
Password: 
WARNING! Your password will be stored unencrypted in /home/ec2-user/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

- Push the generated image to the docker hub

docker push may9noy/php-apache

- docker push process

ec2-user:~/environment/php $ docker push may9noy/php-apache
Using default tag: latest
The push refers to repository [docker.io/may9noy/php-apache]
17f3175a133b: Pushed 
4642696d2b69: Pushed 
1aab22401f12: Mounted from library/php 
13ab94c9aa15: Mounted from library/php 
588ee8a7eeec: Mounted from library/php 
bebcda512a6d: Mounted from library/php 
5ce59bfe8a3a: Mounted from library/php 
d89c229e40ae: Mounted from library/php 
9311481e1bdc: Mounted from library/php 
4dd88f8a7689: Mounted from library/php 
b1841504f6c8: Mounted from library/php 
6eb3cfd4ad9e: Mounted from library/php 
82bded2c3a7c: Mounted from library/php 
b87a266e6a9c: Mounted from library/php 
3c816b4ead84: Mounted from library/php 
latest: digest: sha256:d6f0c6c65d60bdcea9edf809f089e0502f2e63167d0050187310a64aab769e13 size: 3449

6. Distribute as a pod to a Kunernetes cluster for load testing

vim hpa-test.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: may9noy/php-apache #자신의 docker 저장소로 변경
        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

7. Distribute the created yaml file

kubectl apply -f hpa-test.yaml

deployment.apps/php-apache created
service/php-apache created

8.Hpa Distribution

- Create an autoscaleer.

vim autoscaler.yaml

- Increase and reduce the number of replica to meet the average cpu usage of php-apache, the load test pod created above, at 50%.If it exceeds 50%, create up to 10 new pods, and if it is less than 50%, reduce the number of pods generated pods.

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50

- Execute command

kubectl apply -f autoscaler.yaml

- You can check the system load detected by the current hpa and the number of pods to be managed by the hpa command.

ec2-user:~/environment/php $ kubectl get hpa
NAME         REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   <unknown>/50%   1         10        0          11s
ec2-user:~/environment/php $

I haven't made any requests to the server yet, so I can see that the current cpu consumption is 0%. 
(TARGET is the average of the pods controlled by deployment. For example, if the number of pods is 10, it represents the average usage of 10. /50% means that if the average usage of the pod exceeds 50%, we will scale out the pod and continue to expand it to 10.)

9. Load Test

- Execute command

kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"

- If an error similar to the following occurs, you should install isio.

failed calling webhook "namespace.sidecar-injector.istio.io"

1) Install Istio in AWS EKS cluster

Download Istio 1-1.

1) First, download the latest version of Istio installation file.

curl -L https://istio.io/downloadIstio | sh -

(If you want to install a specific version of Istio instead of the latest version, you can download it using the following command.)

curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.6.8 TARGET_ARCH=x86_64 sh -

2) Once the installation is complete, go to the installed directory. 

cd istio-1.11.3

In that directory, the isioctl client binary file exists in the bin/directory, and the sample application for practice exists in the sample/directory.

3) Next, add the isioctl client to PATH.

export PATH=$PWD/bin:$PATH

2-2 Installation of Istio

Next, let's install Istio.Istio installation is performed through isioctl. 

istioctl install

If you install it without any options through the above command, Istio will be installed in the Kubernetes cluster through the default configuration profile.When installing in a commercial environment, it is recommended to install it with the above default settings. 

Let's take a look at how the autoscaler responds as the load increases.
Open another window and send an infinite loop query to the php-apache service.

2-3. Checking Istio Installation

Once the installation is complete, check if Istio is installed properly.

kubectl get deploy -n istio-system
ec2-user:~/environment/istio-1.11.3 $ kubectl get deploy -n istio-system
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
istio-ingressgateway   1/1     1            1           13m
istiod                 1/1     1            1           14m
ec2-user:~/environment/istio-1.11.3 $

- It can be confirmed that Istio is running normally.

Next, let's do a load test.
- Run the error command above again to run the load test.

kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"

- After 1 to 2 minutes, if you look at the load status with the hpa command, you can see that the TARGET value has increased.

$ kubectl get hpa

NAME         REFERENCE               TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   248%/50%   1         10        1          12m7s

- Let's check the progress of HPA.

ec2-user:~/environment $ kubectl get hpa -w
NAME         REFERENCE               TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   132%/50%   1         10        6          3m25s
php-apache   Deployment/php-apache   129%/50%   1         10        6          3m45s
php-apache   Deployment/php-apache   129%/50%   1         10        6          4m46s

Also, if you look at the deployment controller, you can see that the number of replicas in the pod has increased to six.

ec2-user:~/environment $ kubectl get deploy php-apache
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
php-apache   2/6     6            2           5m49s
ec2-user:~/environment $           5           12m

- Stop the load from the terminal where the busybox container was launched to Ctrl+C and check the result after a few minutes.

kubectl get hpa

NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   0%/50%    1         10        5          11m

- You can see that the usage of cpu has dropped to 0% and the number of pod replica for deployment has also decreased to one.

kubectl get deploy php-apache

NAME         READY   UP-TO-DATE   AVAILABLE   AGE
php-apache   1/1     1            1           19m

- Final confirmation: You can see that the number of replicas increased to 6 and then decreased to 1 after the service was discontinued.You can see that the target load has also been reduced to 1%.You can see that the load is reduced while the replicas is reduced in scale-out.

ec2-user:~/environment $ kubectl get hpa -w
NAME         REFERENCE               TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   132%/50%   1         10        6          3m25s
php-apache   Deployment/php-apache   129%/50%   1         10        6          3m45s
php-apache   Deployment/php-apache   129%/50%   1         10        6          4m46s
php-apache   Deployment/php-apache   129%/50%   1         10        6          5m46s
php-apache   Deployment/php-apache   1%/50%     1         10        6          6m46s
php-apache   Deployment/php-apache   1%/50%     1         10        6          11m
php-apache   Deployment/php-apache   1%/50%     1         10        1          11m

So far, we've looked at the scale-out and scale-in of autoscaling through the Kubernetes load test.