溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Nginx怎么配置TCP負(fù)載均衡

發(fā)布時(shí)間:2022-02-16 16:04:00 來(lái)源:億速云 閱讀:147 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Nginx怎么配置TCP負(fù)載均衡”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Nginx怎么配置TCP負(fù)載均衡”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

Nginx怎么配置TCP負(fù)載均衡

假設(shè)Kubernetes集群已經(jīng)配置好,我們將基于CentOS為Nginx創(chuàng)建一個(gè)虛擬機(jī)。

以下是實(shí)驗(yàn)中設(shè)置的詳細(xì)信息:

  • 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倉(cāng)庫(kù)

因?yàn)閚ginx軟件包在CentOS系統(tǒng)默認(rèn)倉(cāng)庫(kù)里面沒(méi)有,所以需要安裝epel倉(cāng)庫(kù):

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

步驟2)安裝Nginx

運(yùn)行以下命令安裝nginx:

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

使用rpm命令驗(yàn)證Nginx包的詳細(xì)信息:

[root@nginxlb ~]# rpm -qi nginx

Nginx怎么配置TCP負(fù)載均衡 

配置防火墻,允許訪問(wèn)nginx的http和https服務(wù):

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

使用以下命令將SELinux設(shè)置為permissive模式,并重啟系統(tǒng)使selinux關(guān)閉生效:

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

步驟3)從Kubernetes中獲取應(yīng)用程序的NodePort詳細(xì)信息

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

Nginx怎么配置TCP負(fù)載均衡 

從上面的輸出中可以看到,每個(gè)工作節(jié)點(diǎn)的NodePort 32760映射到端口80,NodePort 32375映射到443端口。我們將在Nginx配置文件中使用這些節(jié)點(diǎn)端口來(lái)做負(fù)載均衡。

步驟4)將Nginx配置負(fù)載均衡

編輯nginx配置文件,并添加以下內(nèi)容:

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

注釋掉“server”部分(從38到57行): Nginx怎么配置TCP負(fù)載均衡 

并添加以下幾行:

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負(fù)載均衡 

保存配置文件,并退出。

  Nginx怎么配置TCP負(fù)載均衡 

根據(jù)上述更改,所有向nginx的80端口的請(qǐng)求,都將被路由到的Kubernetes工作節(jié)點(diǎn)(192.168.1.41和192.168.1.42)的NodePort(32760)端口上。

使用以下命令啟用Nginx服務(wù):

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

測(cè)試Nginx的 TCP負(fù)載均衡器

要測(cè)試nginx作為Kubernetes的TCP負(fù)載均衡是否工作正常,請(qǐng)部署基于nginx的deployment,將deployment的端口暴露為80端口,并為nginx 的deployment定義入口資源。我已經(jīng)使用以下命令來(lái)部署這些Kubernetes對(duì)象:

[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

運(yùn)行以下命令以獲取deployments,svc和ingress詳細(xì)信息: Nginx怎么配置TCP負(fù)載均衡 

更新本地主機(jī)的hosts文件,以便nginx-lb.example.com指向nginx服務(wù)器的IP地址(192.168.1.50)

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

嘗試通過(guò)瀏覽器訪問(wèn)nginx-lb.example.com Nginx怎么配置TCP負(fù)載均衡

讀到這里,這篇“Nginx怎么配置TCP負(fù)載均衡”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI