使用Nginx的nginx-upsyc-module模块实现动态负载均衡

裴俊豪
2023-12-01

t
编译安装Nginx并添加微博的nginx-upsync-module模块
下载Nginx的源码

# wget http://nginx.org/download/nginx-1.16.0.tar.gz
# tar -zvxf nginx-1.16.0.tar.gz

使用git克隆模块

git clone https://github.com/onecer/nginx-upsync-module.git

安装编译工具

yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel

编译Nginx及添加模块

# cd nginx-1.16.0/
# ./configure --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-stream --with-stream_ssl_module --add-module=../nginx-upsync-module
# make && make install

添加软链接

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

使用systemd管理Nginx
添加服务文件

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

修改Nginx配置文件

vim /usr/local/nginx/nginx.conf
user nginx nginx;

pid /var/run/nginx.pid;

http {
    upstream test-service {
        server 127.0.0.1:11111;
        upsync 127.0.0.1:8500/v1/health/service/test-service upsync_timeout=6m upsync_interval=500ms upsync_type=consul_health strong_dependency=off;
        upsync_dump_path /usr/local/nginx/conf/servers/servers_test-service.conf;
  }
  
  server {
      listen 80;
      server_name localhost;
  }
include conf.d/*.conf;
}

创建Nginx的子配置文件

mkdir /usr/local/nginx/conf/conf.d
vim /usr/local/nginx/conf/conf.d/www.test.com.conf
server {
    listen 80;
    server_name www.test.com;
    
    location / {
        proxy_pass http://test-service/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 300m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        proxy_buffer_size 64k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
    }
}

创建相关的文件夹

mkdir mkdir -p /usr/local/nginx/conf/servers

启动nginx

systemctl daemon-reload && systemctl start nginx

下面部署test-service,test-service是一个JAVA项目,运用Tomcat来运行
创建项目文件夹,并将Dockerfile,项目包,和配置文件存放在里面

构建Docker镜像

docker build -f Dockerfile -t test-service:1.0.0 .

运行数据库

docker run -d -p 3306:3306 \
--name mysql \
--restart=always \
-v /var/lib/mysql:/var/lib/mysql \
-v /var/lib/mysql/conf.d:/etc/mysql/conf.d \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD=123 \
mysql:5.7

添加数据库配置文件到服务器/var/lib/mysql/conf.d目录下面

vim /var/lib/mysql/conf.d/my.cnf
[mysqld]
max_connections = 2000
sql_mode = 'NO_ENGINE_SUBSTITUTION'
default-time_zone = '+8:00'

创建test-service服务使用的数据库
MySQL [(none)]> create database testservice default character set = ‘utf8mb4’;
MySQL [(none)]> grant all on . to ‘service’@’%’ identified by ‘123’;
MySQL [(none)]> flush privileges;
导入数据

最后创建test-service服务

docker service create \
--name test-service \
--replicas 1 \
--publish 8080:8080 \
--publish 8081:8081 \
--network microservice \
--mount type=bind,src=/etc/localtime,dst=/etc/localtime,readonly \
--env SERVICE_8081_NAME=test-service \
--env SERVICE_PORT=8081 \
--env SERVICE_3306_IGNORE=true \
--env SERVICE_80_IGNORE=true \
--env SERVICE_443_IGNORE=true \
--env SERVICE_9000_IGNORE=true \
--env SERVICE_9001_IGNORE=true \
test-service:1.0.0

使用下面两个选项进行资源限制

--limit-cpu=2
--limit-memory=500MB
 类似资料: