搜索
首页运维NginxNginx怎么配置TCP负载均衡

Nginx怎么配置TCP负载均衡

May 19, 2023 am 08:29 AM
nginxtcp

Nginx怎么配置TCP负载均衡

假设Kubernetes集群已经配置好,我们将基于CentOS为Nginx创建一个虚拟机。

以下是实验中设置的详细信息:

  • Nginx (CenOS8 Minimal) – 192.168.1.50

  • Kube Master – 192.168.1.40

  • Kube Worker 1 – 192.168.1.41

  • Kube Worker 2 – 192.168.1.42

步骤1)安装epel仓库

因为nginx软件包在CentOS系统默认仓库里面没有,所以需要安装epel仓库:

[root@nginxlb ~]# dnf install epel-release -y

步骤2)安装Nginx

运行以下命令安装nginx:

[root@nginxlb ~]# dnf install nginx -y

使用rpm命令验证Nginx包的详细信息:

[root@nginxlb ~]# rpm -qi nginx

Nginx怎么配置TCP负载均衡 

配置防火墙,允许访问nginx的http和https服务:

[root@nginxlb ~]# firewall-cmd --permanent --add-service=http[root@nginxlb ~]# firewall-cmd --permanent --add-service=https[root@nginxlb ~]# firewall-cmd –reload

使用以下命令将SELinux设置为permissive模式,并重启系统使selinux关闭生效:

[root@nginxlb ~]# sed -i s/^SELINUX=.*$/SELINUX=permissive/ /etc/selinux/config[root@nginxlb ~]# reboot

步骤3)从Kubernetes中获取应用程序的NodePort详细信息

[kadmin@k8s-master ~]$  kubectl get all -n ingress-nginx

Nginx怎么配置TCP负载均衡 

从上面的输出中可以看到,每个工作节点的NodePort 32760映射到端口80,NodePort 32375映射到443端口。我们将在Nginx配置文件中使用这些节点端口来做负载均衡。

步骤4)将Nginx配置负载均衡

编辑nginx配置文件,并添加以下内容:

[root@nginxlb ~]# vim /etc/nginx/nginx.conf

注释掉“server”部分(从38到57行): Nginx怎么配置TCP负载均衡 

并添加以下几行:

upstream backend {
  server 192.168.1.41:32760;
  server 192.168.1.42:32760;
}

server {
  listen 80;
  location / {
      proxy_read_timeout 1800;
      proxy_connect_timeout 1800;
      proxy_send_timeout 1800;
      send_timeout 1800;
      proxy_set_header        Accept-Encoding   "";
      proxy_set_header        X-Forwarded-By    $server_addr:$server_port;
      proxy_set_header        X-Forwarded-For   $remote_addr;
      proxy_set_header        X-Forwarded-Proto $scheme;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://backend;
  }

   location /nginx_status {
       stub_status;
   }
}

Nginx怎么配置TCP负载均衡 

保存配置文件,并退出。

  Nginx怎么配置TCP负载均衡 

根据上述更改,所有向nginx的80端口的请求,都将被路由到的Kubernetes工作节点(192.168.1.41和192.168.1.42)的NodePort(32760)端口上。

使用以下命令启用Nginx服务:

[root@nginxlb ~]# systemctl start nginx[root@nginxlb ~]# systemctl enable nginx

测试Nginx的 TCP负载均衡器

要测试nginx作为Kubernetes的TCP负载均衡是否工作正常,请部署基于nginx的deployment,将deployment的端口暴露为80端口,并为nginx 的deployment定义入口资源。我已经使用以下命令来部署这些Kubernetes对象:

[kadmin@k8s-master ~]$ kubectl create deployment nginx-deployment --image=nginx
deployment.apps/nginx-deployment created
[kadmin@k8s-master ~]$ kubectl expose deployments nginx-deployment  --name=nginx-deployment --type=NodePort --port=80
service/nginx-deployment exposed

运行以下命令以获取deployments,svc和ingress详细信息: Nginx怎么配置TCP负载均衡 

更新本地主机的hosts文件,以便nginx-lb.example.com指向nginx服务器的IP地址(192.168.1.50)

[root@localhost ~]# echo "192.168.1.50  nginx-lb.example.com" >> /etc/hosts

尝试通过浏览器访问nginx-lb.example.com Nginx怎么配置TCP负载均衡

以上是Nginx怎么配置TCP负载均衡的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
在Nginx和Apache之间进行选择:适合您的需求在Nginx和Apache之间进行选择:适合您的需求Apr 15, 2025 am 12:04 AM

NGINX和Apache各有优劣,适合不同场景。1.NGINX适合高并发和低资源消耗场景。2.Apache适合需要复杂配置和丰富模块的场景。通过比较它们的核心特性、性能差异和最佳实践,可以帮助你选择最适合需求的服务器软件。

nginx怎么启动nginx怎么启动Apr 14, 2025 pm 01:06 PM

问题:如何启动 Nginx?答案:安装 Nginx启动 Nginx验证 Nginx 是否已启动探索其他启动选项自动启动 Nginx

怎么查看nginx是否启动怎么查看nginx是否启动Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎么关闭nginx怎么关闭Apr 14, 2025 pm 01:00 PM

要关闭 Nginx 服务,请按以下步骤操作:确定安装类型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服务:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自动启动(可选):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

nginx在windows中怎么配置nginx在windows中怎么配置Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

nginx403错误怎么解决nginx403错误怎么解决Apr 14, 2025 pm 12:54 PM

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

linux怎么启动nginxlinux怎么启动nginxApr 14, 2025 pm 12:51 PM

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

linux怎么查看nginx是否启动linux怎么查看nginx是否启动Apr 14, 2025 pm 12:48 PM

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具