1. 说明

此文档描述了一种转发集群外部 HTTP 站点并利用 Ingress 进行重映射的方法。在本文中使用 nginx 进行转发。

此处以转发外部地址 https://10.0.0.254:8006 为例,此Web站点使用 自签名证书,同时需要 Websocket 连接,并且有 上传、下载文件 的需求。

2. 创建配置

根据需求创建以下配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: web-proxy
  name: conf-web-proxy
  namespace: share-app
data:
  pve.conf: |
    server{
    listen [::]:80;
    listen 80;
    server_name pve.d7z.net; (1)
    set $proxy_url 'https://10.0.0.254:8006'; (2)
    location / {
        proxy_http_version 1.1; (3)
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass $proxy_url;
        proxy_buffering off;
        client_max_body_size 0; (4)
        proxy_connect_timeout 3600s;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        send_timeout 3600s;
    }
    }
其中
1 表示目标域名
2 表示要转发的地址
3 使用 http 1.1
4 禁用最大 body 限制

3. 创建转发程序

导入以下配置,创建地址转发, 如果你的节点网络环境复杂,则需要配置节点亲和性:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sts-web-proxy
  namespace: share-app
  labels:
    app: web-proxy
spec:
  serviceName: svc-web-proxy
  selector:
    matchLabels:
      app: web-proxy
  replicas: 2
  template:
    metadata:
      labels:
        app: web-proxy
    spec:
      containers:
        - name: nginx
          image: docker.io/library/nginx:1.21.1
          volumeMounts:
            - name: nginx-data
              mountPath: /etc/nginx/conf.d/
      volumes:
        - name: nginx-data
          configMap:
            name: conf-web-proxy
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: web-proxy
  name: svc-web-proxy
  namespace: share-app
spec:
  ports:
    - name: web
      protocol: TCP
      port: 80
  selector:
    app: web-proxy

4. 创建 Ingress 配置

使用以下配置,创建可用的 Ingress 配置, 注意,在部署完成再次更改配置后可能需要重建 Pod。在 Conf 配置中有多个域名,则需要在 Ingress 下配置多个转发。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-web-proxy
  namespace: share-app
  labels:
    app: web-proxy
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: "0"
spec:
  ingressClassName: nginx-public
  tls:
    - hosts:
        - pve.d7z.net
      secretName: tls-pub-d7z
  rules:
    - host: pve.d7z.net
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: svc-web-proxy
                port:
                  name: web