k8s集群通过nginx-ingress做tcp\udp 4层网络转发

k8s集群通过nginx-ingress做tcp\udp 4层网络转发

检查nginx-ingress是否开启tcp\udp转发

- args:
    - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
    - --udp-services-configmap=$(POD_NAMESPACE)/udp-services

示例 kuard-demo.yaml

apiVersion: apps/v1
kind: Deployment
Metadata:
  name: kuard
spec:
  selector:
    matchLabels:
      app: kuard
  replicas: 1
  template:
    Metadata:
      labels:
        app: kuard
    spec:
      containers:
      #gcr.azk8s.cn/kuar-demo/kuard-amd64:1
      - image: paulcapestany/kuard-amd64:1
        imagePullPolicy: Always
        name: kuard
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
Metadata:
  name: kuard
spec:
  ports:
  - port: 9527
    targetPort: 8080
    protocol: TCP
  selector:
    app: kuard

更新configmaps

$kubectl get cm -n ingress-Nginx 
NAME                              DATA   AGE
ingress-controller-leader-Nginx   0      10m
Nginx-configuration               0      10m
tcp-services                      2      10m
udp-services                      0      10m

tcp-services.yaml

apiVersion: v1
kind: ConfigMap
Metadata:
  name: tcp-services
  namespace: ingress-Nginx
data:
  9527: "default/kuard:9527"

进入nginx-ingress容器查看TCP services处会出现对应的负载配置

cat Nginx.conf

# TCP services

server {
        preread_by_lua_block {
                ngx.var.proxy_upstream_name="tcp-default-kuard-9527";
        }

        listen                  9527;

        proxy_timeout           600s;
        proxy_pass              upstream_balancer;

}

# UDP services

最后即可通过边缘节点 ip:9527 访问。当pod节点库容后红线标记的hostname也会随刷新变化。

参考

https://www.hyacinthless.ml/83.html

相关文章

Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一...
本地项目配置 1 复制 luffy/settings/dev.py为prop.py 修改l...
nginx不仅可以隐藏版本信息,还支持自定义web服务器信息 先看...
一 、此次漏洞分析 1 nginx HTTP/2漏洞 [nginx-announce] ng...
###进入nginx 目录cd /usr/local/nginx###递归显示 2 级目录...
在cmd命令窗口输入下面命令进行查看 tasklist /fi "ima...