nginx四层负载均衡
电脑版发表于:2020/2/2 0:34
实验环境
CentOS-7-x86_64-DVD-1810.iso
本机IP:10.4.7.11
安装Nginx
执行安装命令
yum install nginx -y
配置Nginx
通过 Vim 打开 Nginx.conf 文件
vim /etc/nginx/nginx.conf
举例在末尾添加如下配置
stream { upstream kube-apiserver { server 10.4.7.21:6443 max_fails=3 fail_timeout=30s; server 10.4.7.22:6443 max_fails=3 fail_timeout=30s; } server { listen 7443; proxy_connect_timeout 2s; proxy_timeout 900s; proxy_pass kube-apiserver; } }
方法 | 描述 |
---|---|
upstream appname | 需要转发的应用名称 |
upstream --》server | 需要转发的ip以及端口 |
upstream --》server --》max_fails | 从新连接的最大次数 |
upstream --》server --》max_timeout | 从新连接的最长时间 |
server | 本机设置 |
server --》listen | 本机监听的端口 |
server --》proxy_connect_timeout | 一台上游服务器如果拒绝TCP连接超过proxy_connect_timeout配置的时间,将会被认为已经失效。在这种情况下,Nginx立刻尝试连接upstream组内的另一台正常的服务器。连接失败信息将会记录到Nginx的错误日志中。 |
server --》proxy_timeout | 超时时常 |
server --》proxy_pass | 需要代理转发的应用与端口 |
测试:大家只需要在 Xshell 远程访问本机的 IP地址以及7443端口 即可跳转到 末尾IP为21 和 末尾IP为22 其中一个当中
检查Nginx配置
检查配置
[root@hdss7-11 etcd]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
启动nginx
systemctl start nginx
开机自启动nginx
systemctl enable nginx
然后就搭建好了
Nginx多点配置,本机宕机怎么办?
从节点IP:10.4.7.12
需要建立 VIPS 来进行检查
安装 keepalived
yum install keepalived -y
配置 keepalived
vim /etc/keepalived/check_port.sh
配置如下
#!/bin/bash CHK_PORT=$1 if [ -n "$CHK_PORT" ];then PORT_PROCESS=`ss -lnt|grep $CHK_PORT|wc -l` if [ $PORT_PROCESS -eq 0 ];then echo "Port $CHK_PORT Is Not Used ,End." fi else echo "Check Port Cant Be Empty!" fi
增加执行权限
chmod +x /etc/keepalived/check_port.sh
主节点修改文件 /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id 10.4.7.11 } vrrp_script chk_nginx { script "/etc/keepalived/check_port.sh 7443" interval 2 weight -20 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 251 priority 100 advert_int 1 mcast_src_ip 10.4.7.11 nopreempt authentication { auth_type PASS auth_pass 11111111 } track_script { chk_nginx } virtual_ipaddress { 10.4.7.10 } }
从节点修改文件 /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id 10.4.7.12 } vrrp_script chk_nginx { script "/etc/keepalived/check_port.sh 7443" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 251 priority 90 advert_int 1 mcast_src_ip 10.4.7.12 authentication { auth_type PASS auth_pass 11111111 } track_script { chk_nginx } virtual_ipaddress { 10.4.7.10 } }
启动 keepalived
systemctl start keepalived
开机自启动 keepalived
systemctl enable keepalived
解决
相关资料参考
https://www.cnblogs.com/gongjingyun123--/p/11424424.html
https://www.cnblogs.com/yanjieli/p/10682064.html
外围话题
tnblog
Bind9 可以搭建本地网关 (已亲测)
harbor 私有docker云管理 很方便的 (已亲测)