【k8s】k8s ingress 代理集群外wps文档中台

一、背景

在采用 k8s 后,一些遗留系统或者因为迁移不方便或者因为为了同时服务于多个环境,而仍然以原来的方式运行着(不受 k8s 管理)。

如果想让 k8s 内的 pods 访问这些遗留的服务,怎么办?

现在有集群外的wps文档中台

cat /data/kubewps/apps/opendoc_lite.7.1.2403.22/app.conf

{
    "company_name": "深圳不怕影子斜但能力有限公司",
    "ext_hosts": [],
    "domains":[{
        "scheme": "http",
        "domain": "192.168.1.101",
        "proxy_server": "https://exaple.com",
        "port": 1443,
        "path": "/wps-extalnal-service",
        "encryption": false
    }]
}

我们使用传统 nginx代理wps文档中台接口配置如下:

     location /wps-external-service {
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection upgrade;
     proxy_pass http://192.168.1.101:1443;
     proxy_connect_timeout 60s;
     proxy_read_timeout 300s;
}

如何在ingress中配置 ,访问 /wpsdoc 接口 能访问到对应的后端wps服务?

二 、 k8s ingress 代理操作

  1. 你需要使用 ClusterIP 类型的服务,并通过 Endpoints 来指向多个实例的 IP。

wps-service.yaml

# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: wps-external-service
  namespace: test
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 1443
  1. 创建 Endpoints,接下来,创建 Endpoints 资源,以便指向所有实例的 IP。

wps-endpoints.yaml

# endpoints.yaml
apiVersion: v1
kind: Endpoints
metadata:
  name: wps-external-service
  namespace: test
subsets:
  - addresses:
      - ip: 192.168.1.101
    ports:
      - port: 1443
  1. 创建Ingress

你可以为wps-external-service 服务创建一个单独ingress。

ingress-wps.yaml

# ingress-wps.yaml
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: ingress-nginx-outer
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/proxy-http-version: "1.1"
    nginx.ingress.kubernetes.io/proxy-set-headers: |
      Host $host;
      X-Real-IP $remote_addr;
      X-Forwarded-For $proxy_add_x_forwarded_for;
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "60"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "300"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
  creationTimestamp: "2023-08-24T07:56:43Z"
  generation: 55
  name: ingress-nginx-outer-wps
  namespace: test
  resourceVersion: "111101388"
  uid: 81e435c1-3f03-4303-xxxx-c56cb1d50d8a

spec:
  rules:
    - host: www.example.com
      http:
        paths:
          - path: /wps-external-service
            pathType: Prefix
            backend:
              service:
                name: wps-external-service
                port:
                  number: 80

关键配置说明:

Service + Endpoints
Service 定义逻辑入口,Endpoints 直接绑定外部 IP:Port,绕过 kube-proxy。
• 确保 targetPortEndpoints.port 一致。

Ingress 注解
proxy-http-version: 强制使用 HTTP/1.1 以支持 WebSocket。
proxy-set-headers: 通过 ConfigMap 或直接注入头信息(需提前创建 ConfigMap)。
configuration-snippet: 直接嵌入原始 UpgradeConnection 设置。
• 超时时间: 单位为秒,与原始配置的 60s/300s 对应。

WebSocket 支持
若需代理 WebSocket,确保注解中包含 UpgradeConnection 头,且 Ingress Controller 版本支持。

  1. 部署service 、endpoint、ingress 资源
1. 部署服务:
kubectl apply -f wps-service.yaml
   
  
2. 部署端点:
kubectl apply -f wps-endpoints.yaml
 
     
3. 部署 Ingress:
kubectl apply -f  wps-ingress.yaml
$ kubectl  describe service  wps-external-service -n test
$ kubectl  describe ep wps-external-service -n test
$ kubectl  describe ingress  ingress-nginx-wps  -n test

三、参考

WPS生态运营产品知识库
https://p.wpseco.cn/wiki/space/61d3c0c2750c48c90a16940a

Ingress 代理集群外服务
https://mp.weixin.qq.com/s/F9s__YGqG5Jjzb0SnWXVAg

Kubernetes使用ingress反向代理外部IP
https://zahui.fan/posts/0ad6df1b/

图解 Kubernetes Ingress
https://www.qikqiak.com/post/visually-explained-k8s-ingress/

如何将外部服务纳入到k8s集群内
https://beloved.family/wx/%E5%A6%82%E4%BD%95%E5%B0%86%E5%A4%96%E9%83%A8%E6%9C%8D%E5%8A%A1%E7%BA%B3%E5%85%A5%E5%88%B0k8s%E9%9B%86%E7%BE%A4%E5%86%85

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容