nginx?ingress代理websocket流量的配置方法
1 概述:
1.1 環(huán)境
版本信息如下:
a、操作系統(tǒng):centos 7.6
b、kubernetes版本:v1.15.0
c、ingress nginx版本:0.47.0
2 nginx ingress是否支持代理websocket流量
nginx ingress 默認(rèn)支持websocket協(xié)議,因此ingress實(shí)例不需要額外配置。
值得注意的是,proxy-read-timeout和proxy-send-timeout的默認(rèn)值是60秒,應(yīng)該根據(jù)實(shí)際情況增加此兩個(gè)參數(shù)的值。如果使用默認(rèn)值60,則websocket客戶端超過(guò)60秒沒(méi)有給websocket服務(wù)端發(fā)送信息,再次發(fā)送數(shù)據(jù)時(shí)是無(wú)效的,例如使用websocat命令時(shí),出現(xiàn)WebSocketError: I/O failure。

3 ingress樣例
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
# 根據(jù)實(shí)際情況調(diào)整超時(shí)時(shí)間,默認(rèn)值為60
nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
name: ws
namespace: default
spec:
rules:
- host: apigateway
http:
paths:
- backend:
serviceName: ws
servicePort: 3000
path: /4 部署
4.1 部署nginx ingress
將以下文件進(jìn)行kubectl apply,本案例中是以daemonset形式部署nginx controller,使用host網(wǎng)絡(luò)。
apiVersion: v1
kind: Namespace
metadata:
name: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-ngin
---
kind: ConfigMap
name: nginx-configuration
namespace: ingress-nginx
name: tcp-services
name: udp-services
data:
resolv.conf: |
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local lj.io
options ndots:5
name: resolver
kind: ServiceAccount
name: nginx-ingress-serviceaccount
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
name: nginx-ingress-clusterrole
rules:
- apiGroups:
- ""
resources:
- configmaps
- endpoints
- nodes
- pods
- secrets
verbs:
- list
- watch
- get
- services
- "extensions"
- "networking.k8s.io"
- ingresses
- update
- events
- create
- patch
- ingresses/status
kind: Role
name: nginx-ingress-role
- namespaces
resourceNames:
# Defaults to "<election-id>-<ingress-class>"
# Here: "<ingress-controller-leader>-<nginx>"
# This has to be adapted if you change either parameter
# when launching the nginx-ingress-controller.
- "ingress-controller-leader-nginx"
kind: RoleBinding
name: nginx-ingress-role-nisa-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
subjects:
- kind: ServiceAccount
name: nginx-ingress-serviceaccount
namespace: ingress-nginx
kind: ClusterRoleBinding
name: nginx-ingress-clusterrole-nisa-binding
kind: ClusterRole
kind: Service
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
sessionAffinity: None
type: ClusterIP
apiVersion: apps/v1
kind: DaemonSet
name: nginx-ingress-controller
matchLabels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
template:
metadata:
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
annotations:
prometheus.io/port: "10254"
prometheus.io/scrape: "true"
spec:
serviceAccountName: nginx-ingress-serviceaccount
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: nginx-ingress-controller
image: bitnami/nginx-ingress-controller:0.47.0
args:
- /nginx-ingress-controller
- --configmap=$(POD_NAMESPACE)/nginx-configuration
- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
- --udp-services-configmap=$(POD_NAMESPACE)/udp-services
- --publish-service=$(POD_NAMESPACE)/ingress-nginx
- --annotations-prefix=nginx.ingress.kubernetes.io
securityContext:
allowPrivilegeEscalation: true
capabilities:
drop:
- ALL
add:
- NET_BIND_SERVICE
# www-data -> 33
runAsUser: 33
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
fieldPath: metadata.namespace
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
livenessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 10254
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
readinessProbe:部署效果如下,

4.2 設(shè)置域名
在本案例中使用/etc/hosts文件解析域名,本機(jī)機(jī)器IP是192.168.0.70。
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.0.70 apigateway
4.3 部署websocket服務(wù)端
服務(wù)端進(jìn)程監(jiān)聽(tīng)的端口是3000,是簡(jiǎn)單的echo server。
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ws
name: ws
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: ws
template:
metadata:
labels:
app: ws
spec:
containers:
- image: elegantmonkeys/websockets-demo:latest
imagePullPolicy: IfNotPresent
name: echo
ports:
- containerPort: 3000
protocol: TCP
resources:
limits:
cpu: "0.2"
memory: 100Mi
requests:
cpu: 100m
memory: 100Mi
---
apiVersion: v1
kind: Service
metadata:
labels:
app: ws
name: ws
namespace: default
spec:
ports:
- name: ws
port: 3000
protocol: TCP
targetPort: 3000
selector:
app: ws
type: NodePort

4.4 創(chuàng)建ingress資源
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
# 根據(jù)實(shí)際情況調(diào)整超時(shí)時(shí)間,默認(rèn)值為60
nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
name: ws
namespace: default
spec:
rules:
- host: apigateway
http:
paths:
- backend:
serviceName: ws
servicePort: 3000
path: /
4.5 下載websockt客戶端
cd /tmp wget -O websocat https://github.com/vi/websocat/releases/download/v1.9.0/websocat_linux64 chmod 755 websocat mv websocat /usr/bin/
4.6 測(cè)試
使用websocat命令通過(guò)ingress nginx連接echo server。

5 小結(jié):
ingress nginx默認(rèn)支持websocket協(xié)議,使用長(zhǎng)連接協(xié)議時(shí)需要注意連接超時(shí)的設(shè)置,讀取和發(fā)送超時(shí)的注解參數(shù)分別是:nginx.ingress.kubernetes.io/proxy-read-timeout和nginx.ingress.kubernetes.io/proxy-send-timeout。
到此這篇關(guān)于nginx ingress代理websocket流量的文章就介紹到這了,更多相關(guān)nginx ingress代理websocket內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx中g(shù)zip壓縮提升網(wǎng)站速度的實(shí)現(xiàn)方法
這篇文章主要介紹了nginx中g(shù)zip壓縮提升網(wǎng)站速度的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
nginx配置https加密訪問(wèn)的詳細(xì)教程
這篇文章主要介紹了nginx配置https加密訪問(wèn)的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
nginx啟動(dòng)服務(wù)提示98: Address already in use錯(cuò)誤的解決
這篇文章主要給大家介紹了nginx啟動(dòng)服務(wù)提示98: Address already in use錯(cuò)誤的解決方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05
nginx http響應(yīng)限速的具體實(shí)現(xiàn)
本文主要介紹了nginx http響應(yīng)限速的具體實(shí)現(xiàn),可以使用limite_rate和limit_rate_after來(lái)限制HTTP響應(yīng)的速度,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
Nginx配置之location的匹配優(yōu)先級(jí)淺析
這篇文章主要給大家介紹了關(guān)于Nginx配置之location的匹配優(yōu)先級(jí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Nginx具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Nginx實(shí)現(xiàn)自簽名SSL證書(shū)生成與配置實(shí)現(xiàn)
本文主要介紹了Nginx實(shí)現(xiàn)自簽名SSL證書(shū)生成與配置實(shí)現(xiàn),文章將詳細(xì)介紹生成自簽名SSL證書(shū)的步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Nexus使用nginx代理實(shí)現(xiàn)支持HTTPS協(xié)議
這篇文章主要介紹了Nexus使用nginx代理實(shí)現(xiàn)支持HTTPS協(xié)議,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05

