从安装NGINX到创建负载均衡器为止

简而言之

我试了一下NGINX,似乎可以很容易地创建负载均衡器。
※这次我在Ubuntu环境下进行了测试。

安装 NGINX

安装NGINX

sudo apt-get update
sudo apt-get install nginx

可以通过systemd init系统来确认服务是否正在运行。

$systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-11-09 02:26:51 UTC; 17min ago
       Docs: man:nginx(8)
   Main PID: 2857918 (nginx)
      Tasks: 7 (limit: 28758)
     Memory: 8.2M
     CGroup: /system.slice/nginx.service
             ├─2857918 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─2857919 nginx: worker process
             ├─2857920 nginx: worker process
             ├─2857921 nginx: worker process
             ├─2857922 nginx: worker process
             ├─2857923 nginx: worker process
             └─2857924 nginx: worker process

Nov 09 02:26:51 doktor-loadtest systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 09 02:26:51 doktor-loadtest systemd[1]: Started A high performance web server and a reverse proxy server.
image.png (96.4 kB)

负载均衡器的设置

打开位于/etc/nginx/的nginx.conf文件

vim /etc/nginx/nginx.conf

在HTTP字段中添加以下内容
→默认情况下,负载均衡器采用轮询方式
※本次将直接连接到在Pod内运行的Flask服务器

http {
    #追加部分↓
    upstream backend {
        server 10.1.204.5:3100;
        server 10.1.204.7:3100;
        server 10.1.204.8:3100;#分散先サーバーのIPアドレス:portを記述
    }
    server {
        listen 8888;

        location / {
            proxy_pass http://backend;
        }
    }

Pod信息↓

nogisora@nginx-loadbalancer:/etc/nginx$ microk8s kubectl get pod -owide
NAME                         READY   STATUS    RESTARTS   AGE     IP           NODE                 NOMINATED NODE   READINESS GATES
cpuup-pod1-fdcc5b4df-9bq8r   1/1     Running   0          2d12h   10.1.204.5   nginx-loadbalancer   <none>           <none>
cpuup-pod2-f8b7b8498-7gbdv   1/1     Running   0          69m     10.1.204.7   nginx-loadbalancer   <none>           <none>
cpuup-pod3-97d997668-qv4zc   1/1     Running   0          68m     10.1.204.8   nginx-loadbalancer   <none>           <none>

3. 重新启动Nginx。

sudo service nginx restart

通过运行 `nginx -t` 命令,如果输出以下结果,则表示配置已成功更改完毕。

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
image.png (18.5 kB)

参考文献:
1. https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04-ja
2. https://4geek.net/notes-about-nginx/
3. https://qiita.com/WisteriaWave/items/fa2e7f4442aee497fe46
4. https://qiita.com/tukiyo3/items/52345d51c6bc27602bb3

参考资料:
1. 在Ubuntu 20.04上安装Nginx的教程(日文) – https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04-ja
2. 有关Nginx的笔记 – https://4geek.net/notes-about-nginx/
3. WisteriaWave的文章,Nginx相关 – https://qiita.com/WisteriaWave/items/fa2e7f4442aee497fe46
4. tukiyo3的文章,Nginx相关 – https://qiita.com/tukiyo3/items/52345d51c6bc27602bb3

广告
将在 10 秒后关闭
bannerAds