「KubernetesのLoadBalancerって本当に負荷分散してるの～？」試さずにはいられない

こんにちは
株式会社クラスアクト インフラストラクチャ事業部の大塚です。

今回はmicrok8sで構築した以下環境で本当に負荷分散されているのかを確認してみたいと思います。
具体的にはそれぞれのnode上にいるpod内のapache2のhtmlファイルをそれぞれ固有のものに変更。何度かLoadBalancerのIPアドレス:9090ポートにWebブラウジングして出力される内容変わるよね～ってことを確認します。
![k8s-ページ28.drawio.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3219385/51535b4a-463c-ae97-6066-2c69955bb86c.png)



https://qiita.com/ohtsuka-shota/items/ad62014ee93bfbc57c9f


# htmlファイルの変更
nginx-httpd-deployment-759b9b5f-vq8rpのweb-httpdコンテナ
黄色枠内のpod/コンテナのhtmlを修正します。
![k8s-ページ30.drawio.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3219385/c7b30f05-caac-92f0-1315-3b1f894bfa06.png)


```
root@k8s-master:~# kubectl get pod -o wide
NAME                                    READY   STATUS    RESTARTS   AGE    IP             NODE           NOMINATED NODE   READINESS GATES
nginx-httpd-deployment-759b9b5f-vq8rp   2/2     Running   0          3h5m   10.1.235.209   k8s-master     <none>           <none>
nginx-httpd-deployment-759b9b5f-rsp86   2/2     Running   0          3h5m   10.1.79.68     k8s-worker01   <none>           <none>
nginx-httpd-deployment-759b9b5f-h8k67   2/2     Running   0          3h5m   10.1.69.197    k8s-worker02   <none>           <none>
root@k8s-master:~# kubectl exec -it nginx-httpd-deployment-759b9b5f-vq8rp -c web-httpd -- /bin/bash 
root@nginx-httpd-deployment-759b9b5f-vq8rp:/usr/local/apache2# cd /usr/local/apache2/htdocs
root@nginx-httpd-deployment-759b9b5f-vq8rp:/usr/local/apache2/htdocs# vi index.html
root@nginx-httpd-deployment-759b9b5f-vq8rp:/usr/local/apache2/htdocs# cat index.html
<html><body><h1>It works on master!!</h1></body></html>
root@nginx-httpd-deployment-759b9b5f-vq8rp:/usr/local/apache2/htdocs# /usr/local/apache2/bin/apachectl restart
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.1.235.209. Set the 'ServerName' directive globally to suppress this message
```


nginx-httpd-deployment-759b9b5f-rsp86のweb-httpdコンテナ
黄色枠内のpod/コンテナのhtmlを修正します。
![k8s-ページ30.drawio (1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3219385/d92e10d6-1ca6-dd2e-a769-07e0c5173fb7.png)


```
root@k8s-master:~# kubectl get pod -o wide 
NAME                                    READY   STATUS    RESTARTS   AGE    IP             NODE           NOMINATED NODE   READINESS GATES 
nginx-httpd-deployment-759b9b5f-vq8rp   2/2     Running   0          3h7m   10.1.235.209   k8s-master     <none>           <none> 
nginx-httpd-deployment-759b9b5f-rsp86   2/2     Running   0          3h7m   10.1.79.68     k8s-worker01   <none>           <none> 
nginx-httpd-deployment-759b9b5f-h8k67   2/2     Running   0          3h7m   10.1.69.197    k8s-worker02   <none>           <none> 
root@k8s-master:~# kubectl exec -it nginx-httpd-deployment-759b9b5f-rsp86 -c web-httpd -- /bin/bash 
root@nginx-httpd-deployment-759b9b5f-rsp86:/usr/local/apache2# cd /usr/local/apache2/htdocs 
root@nginx-httpd-deployment-759b9b5f-rsp86:/usr/local/apache2/htdocs# vi index.html 
root@nginx-httpd-deployment-759b9b5f-rsp86:/usr/local/apache2/htdocs# cat index.html 
<html><body><h1>It works on worker01!!</h1></body></html> 
root@nginx-httpd-deployment-759b9b5f-rsp86:/usr/local/apache2/htdocs# /usr/local/apache2/bin/apachectl restart 
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.1.79.68. Set the 'ServerName' directive globally to suppress this message
```

nginx-httpd-deployment-759b9b5f-h8k67のweb-httpdコンテナ
黄色枠内のpod/コンテナのhtmlを修正します。
![k8s-ページ30.drawio (2).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3219385/f7454630-784d-cc18-e391-d0fadecdaf4b.png)


```
root@k8s-master:~# kubectl get pod -o wide 
NAME                                    READY   STATUS    RESTARTS   AGE    IP             NODE           NOMINATED NODE   READINESS GATES 
nginx-httpd-deployment-759b9b5f-vq8rp   2/2     Running   0          3h9m   10.1.235.209   k8s-master     <none>           <none> 
nginx-httpd-deployment-759b9b5f-rsp86   2/2     Running   0          3h9m   10.1.79.68     k8s-worker01   <none>           <none> 
nginx-httpd-deployment-759b9b5f-h8k67   2/2     Running   0          3h9m   10.1.69.197    k8s-worker02   <none>           <none> 
root@k8s-master:~# kubectl exec -it nginx-httpd-deployment-759b9b5f-h8k67 -c web-httpd -- /bin/bash 
root@nginx-httpd-deployment-759b9b5f-h8k67:/usr/local/apache2# cd /usr/local/apache2/htdocs 
root@nginx-httpd-deployment-759b9b5f-h8k67:/usr/local/apache2/htdocs# vi index.html 
root@nginx-httpd-deployment-759b9b5f-h8k67:/usr/local/apache2/htdocs# cat index.html 
<html><body><h1>It works on worker02!!</h1></body></html> 
root@nginx-httpd-deployment-759b9b5f-h8k67:/usr/local/apache2/htdocs# /usr/local/apache2/bin/apachectl restart 
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.1.69.197. Set the 'ServerName' directive globally to suppress this message
```

# 実験

何度か接続を繰り返しました。
同じIPアドレスとポート番号ですが、出力されている内容が変わっていることが分かります。
LoadBalancerはちゃんと仕事をしていますね。

![aaaaaa.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3219385/bd26d4cf-7b03-b6a1-788e-4e58d95dae2a.png)

![bbbbbb.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3219385/54d8c54b-c1fa-4dc5-81d2-5a8fb6279112.png)

![cccccc.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3219385/ebdba033-799d-0b68-9e7e-05f034bb536f.png)