In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The editor of this article introduces "how to configure TCP load balancer in Nginx" in detail. The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to configure TCP load balancer in Nginx" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
Assuming that the Kubernetes cluster is configured, we will create a virtual machine for Nginx based on CentOS.
The following are the details set up in the lab:
Nginx (CenOS8 Minimal)-192.168.1.50Kube Master-192.168.1.40Kube Worker 1-192.168.1.41Kube Worker 2-192.168.1.42
Step 1) install the epel repository
Because the nginx package is not available in the default repository of the CentOS system, you need to install the epel repository:
[root@nginxlb ~] # dnf install epel-release-y
Step 2) install Nginx
Run the following command to install nginx:
[root@nginxlb ~] # dnf install nginx-y
Use the rpm command to verify the details of the Nginx package:
[root@nginxlb ~] # rpm-qi nginx
Configure a firewall to allow access to nginx's http and https services:
[root@nginxlb] # firewall-cmd-- permanent-- add-service=http [root@nginxlb ~] # firewall-cmd-- permanent-- add-service=https [root@nginxlb ~] # firewall-cmd-reload
Use the following command to set SELinux to permissive mode and restart the system for selinux shutdown to take effect:
[root@nginxlb] # sed-is / ^ SELINUX =. * $/ SELINUX=permissive/ / etc/selinux/config [root@nginxlb ~] # reboot
Step 3) get the NodePort details of the application from Kubernetes
[kadmin@k8s-master] $kubectl get all-n ingress-nginx
As you can see from the output above, the NodePort 32760 map for each worker node is mapped to port 80 remiere NodePort 32375 to port 443. We will use these node ports in the Nginx configuration file for load balancing.
Step 4) configure Nginx for load balancing
Edit the nginx configuration file and add the following:
[root@nginxlb ~] # vim / etc/nginx/nginx.conf
Comment out the "server" section (lines 38 to 57):
And add the following lines:
Upstream backend {server 192.168.1.41 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;}}
Save the configuration file and exit.
Based on the above changes, all requests to port 80 of nginx will be routed to the NodePort (32760) port of the Kubernetes worker node (192.168.1.41 and 192.168.1.42).
Use the following command to enable the Nginx service:
[root@nginxlb ~] # systemctl start nginx [root@nginxlb ~] # systemctl enable nginx
Test the TCP load balancer for Nginx
To test whether nginx works properly as TCP load balancer for Kubernetes, deploy nginx-based deployment, expose the port of deployment as port 80, and define the ingress resource for the deployment of nginx. I have used the following command to deploy these Kubernetes objects:
[kadmin@k8s-master] $kubectl create deployment nginx-deployment-- image=nginxdeployment.apps/nginx-deployment created [kadmin@k8s-master ~] $kubectl expose deployments nginx-deployment-- name=nginx-deployment-- type=NodePort-- port=80service/nginx-deployment exposed
Run the following command to get deployments,svc and ingress details:
Update the hosts file of the local host so that nginx-lb.example.com points to the IP address of the nginx server (192.168.1.50)
[root@localhost ~] # echo "192.168.1.50 nginx-lb.example.com" > > / etc/hosts
Try to access nginx-lb.example.com through a browser
After reading this, the article "how to configure Nginx for TCP load balancer" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to learn more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.