Assuming that the Kubernetes cluster has been configured, we will create a virtual machine for Nginx based on CentOS.
The following are the details of the settings in the experiment:
Nginx (CenOS8 Minimal) – 192.168.1.50
Kube Master – 192.168.1.40
Kube Worker 1 – 192.168.1.41
Step 1) Install the epel warehouse
Because the nginx software package is not in the default warehouse of the CentOS system, you need to install the epel warehouse:[root@nginxlb ~]# dnf install epel-release -y
Step 2) Install Nginx
Run the following command to install nginx:[root@nginxlb ~]# dnf install nginx -y
[root@nginxlb ~]# rpm -qi nginx
[root@nginxlb ~]# firewall-cmd --permanent --add-service=http[root@nginxlb ~]# firewall-cmd --permanent --add-service=https[root@nginxlb ~]# firewall-cmd –reload
[root@nginxlb ~]# sed -i s/^SELINUX=.*$/SELINUX=permissive/ /etc/selinux/config[root@nginxlb ~]# reboot
Step 3) Get the application’s NodePort details from Kubernetes
[kadmin@k8s-master ~]$ kubectl get all -n ingress-nginx
Step 4) Configure Nginx for load balancing
Edit the nginx configuration file and add the following:[root@nginxlb ~]# vim /etc/nginx/nginx.conf
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; } }
[root@nginxlb ~]# systemctl start nginx[root@nginxlb ~]# systemctl enable nginx
Test Nginx’s TCP load balancer
To test whether nginx works as a TCP load balancer for Kubernetes Normal, please deploy a deployment based on nginx, expose the deployment port as port 80, and define the entry resource for the nginx deployment. I have used the following commands to deploy these Kubernetes objects:[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
[root@localhost ~]# echo "192.168.1.50 nginx-lb.example.com" >> /etc/hosts
The above is the detailed content of How to configure TCP load balancing in Nginx. For more information, please follow other related articles on the PHP Chinese website!