美文网首页kubernetes
【k8s】k8s ingress 对接口做redirect

【k8s】k8s ingress 对接口做redirect

作者: Bogon | 来源:发表于2024-10-12 16:28 被阅读0次

需求实现:

http://www.example.com/login ---> https://www.xyz.com:8443/login/login.httml
http://www.example.com/login/ ---> https://www.xyz.com:8443/login/login.httml

# 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: xxxxxx
    nginx.ingress.kubernetes.io/configuration-snippet: |
      add_header 'Access-Control-Allow-Origin' "$http_origin";
      add_header Access-Control-Allow-Credentials false;
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/permanent-redirect: https://www.xyz.com:8443/login/login.httml
  creationTimestamp: "2023-08-24T07:56:43Z"
  generation: 2
  name: xxxxxx
  namespace: test
  resourceVersion: "114549163"
  uid: 375380c2-2f5f-47bf-a0d7-1fbebaed5b63
spec:
  rules:
  - host: www.example.com
    http:
      paths:

      - backend:
          service:
            name: microservice
            port:
              name: http
        path: /login
        pathType: Prefix

      - backend:
          service:
            name: microservice
            port:
              name: http
        path: /login/
        pathType: Prefix

这段 Ingress 配置定义了一个入口资源,用于将外部请求路由到 Kubernetes 集群内部的服务。

主要元素包括:

  1. metadata:包含一些注释和元数据,例如 ingress 类、SSL 重定向、CORS 头等。
  2. spec:指定了路由规则,其中 hostwww.example.com,表示针对该域名的请求。
  3. http.paths:定义了两个路径(/login/login/),都指向名为 microservice 的服务,使用 HTTP 协议。

最终,这个 Ingress 配置将特定路径的请求重定向到相应的服务,支持 CORS,并强制 SSL 重定向。

配置中的 nginx.ingress.kubernetes.io/permanent-redirect 注释指定了一个永久重定向。

具体来说,它会将所有访问 www.example.com 的请求重定向到指定的 URL,即 https://www.xyz.com:8443/login/login.httml

这种重定向使用 HTTP 301 状态码,意味着搜索引擎和浏览器会将该 URL 视为资源的永久位置。这样可以有效地引导用户或客户端到新的地址进行认证或登录。同时,结合 force-ssl-redirect 注释,确保所有请求都使用 HTTPS,从而增强安全性。

这个 Ingress 配置中,具体访问什么会重定向到哪里:

访问示例和重定向行为

  1. 直接访问主机

    • 请求:用户在浏览器中输入 http://www.example.com/loginhttp://www.example.com/login/
    • 行为:由于配置了 nginx.ingress.kubernetes.io/force-ssl-redirect,这个 HTTP 请求会被强制重定向到 HTTPS:
      • 重定向到https://www.example.com/loginhttps://www.example.com/login/
  2. 访问 /login 路径

    • 请求:用户在浏览器中输入 https://www.example.com/login
    • 行为:由于配置了 nginx.ingress.kubernetes.io/permanent-redirect,这会导致进一步的重定向:
      • 重定向到https://www.xyz.com:8443/login/login.httml
    • 这意味着用户实际上会被引导到身份验证服务的授权页面。
  3. 访问 /login/ 路径

    • 请求:用户在浏览器中输入 https://www.example.com/login/
    • 行为:同样会根据 nginx.ingress.kubernetes.io/permanent-redirect 进行重定向:
      • 重定向到:同样是 https://www.xyz.com:8443/login/login.httml

总结

  • 所有访问 http://www.example.com/loginhttp://www.example.com/login/ 的请求都会首先被重定向到 HTTPS,然后再次重定向到身份验证服务。
  • 这种配置主要用于实现 OAuth2.0 的认证流程,将用户从入口应用引导到身份验证系统。

相关文章

网友评论

    本文标题:【k8s】k8s ingress 对接口做redirect

    本文链接:https://www.haomeiwen.com/subject/mcznrjtx.html