美文网首页
【ingress】k8s ingress 通过注解配置,实现接口

【ingress】k8s ingress 通过注解配置,实现接口

作者: Bogon | 来源:发表于2024-06-24 16:39 被阅读0次

如果我们实现访问微服务test01工程/path/to/api01 路由到 微服务test02工程 /path/to/api02 ,nginx如何配置?

$ cat /usr/local/openresty/nginx/conf/conf.d/upstream/upstest01.conf

#-----------------------------------------------#
upstream upstest01 {
    server 172.22.192.158:11648;
}
#-----------------------------------------------#

$ cat /usr/local/openresty/nginx/conf/conf.d/upstream/upstest02.conf

#-----------------------------------------------#
upstream upstest02 {
    server 172.22.192.159:12520;
}
#-----------------------------------------------#

$ cat /usr/local/openresty/nginx/conf/conf.d/test01.conf

location ^~ /path/to/api01  {
             proxy_pass http://upstest02/path/to/api02;
             proxy_set_header X-Forwarded-Proto http;
             proxy_set_header X-Real-IP $http_X_Real_IP;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header Host $host;
             proxy_set_header x-request-rid $request_id;
          }

如果是 nginx ingres 如何配置?

$ cat k8s.ingress-nginx-test01-to-test02.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: test-ingress-nginx-outer
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /path/to/api02
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
  creationTimestamp: "2023-08-24T07:56:43Z"
  generation: 1
  name: k8s.ingress-nginx-test01-to-test02
  namespace: test
  resourceVersion: "114549172"
  uid: 280bba9d-ebd1-4f91-a143-1d6201409d75
spec:
  rules:
  - host: www.example.com
    http:
      paths:
      - backend:
          service:
            name: test02
            port:
              name: http
        path: /path/to/api01
        pathType: ImplementationSpecific

创建ingress:

$ kubectl  apply  -f k8s.ingress-nginx-test01-to-test02.yaml -n test

删除ingress:

$ kubectl  delete -f k8s.ingress-nginx-test01-to-test02.yaml -n  test

$ kubectl describe ingress k8s.ingress-nginx-test01-to-test02 -n test

Name:             ingress k8s.ingress-nginx-test01-to-test02 
Namespace:        test
Address:          10.96.92.163
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host                  Path  Backends
  ----                  ----  --------
  portal.cndrealty.com 
                        /path/to/test01  test02:http (10.86.24.231:12520,10.86.25.34:12520)
Annotations:            kubernetes.io/ingress.class: test-ingress-nginx-outer
                        nginx.ingress.kubernetes.io/force-ssl-redirect: false
                        nginx.ingress.kubernetes.io/rewrite-target: /path/to/test02
                        nginx.ingress.kubernetes.io/ssl-redirect: false
Events:
  Type    Reason  Age    From                      Message
  ----    ------  ----   ----                      -------
  Normal  CREATE  5m33s  nginx-ingress-controller  Ingress test/k8s.ingress-nginx-test01-to-test02
  Normal  CREATE  5m33s  nginx-ingress-controller  Ingress test/k8s.ingress-nginx-test01-to-test02
  Normal  UPDATE  5m4s   nginx-ingress-controller  Ingress test/k8s.ingress-nginx-test01-to-test02
  Normal  UPDATE  5m4s   nginx-ingress-controller  Ingress test/k8s.ingress-nginx-test01-to-test02

这段YAML文件是一个Kubernetes Ingress对象的配置文件,用于定义如何将外部的HTTP请求路由到集群内部的服务。

metadata

  • annotations: 注解用于向Ingress对象添加额外的配置信息。在这里使用了四个注解:

    • kubernetes.io/ingress.class: test-ingress-nginx-outer:指定Ingress控制器的类别为test-ingress-nginx-outer,表示该Ingress资源由名为test-ingress-nginx-outer的Ingress控制器处理。
    • nginx.ingress.kubernetes.io/force-ssl-redirect: "false":禁用强制SSL重定向。
    • nginx.ingress.kubernetes.io/rewrite-target: /path/to/api02:重写路径,将请求重定向到/path/to/api02
    • nginx.ingress.kubernetes.io/ssl-redirect: "false":禁用SSL重定向。
  • creationTimestamp: 对象创建的时间戳。

  • generation: 对象的生成数,用于跟踪对象的变化。

  • name: Ingress对象的名称为k8s.ingress-nginx-test01-to-test02

  • namespace: Ingress对象所属的命名空间为test

  • resourceVersion: 资源的版本号。

  • uid: 唯一标识该资源的UUID。

spec

  • rules: 定义Ingress的规则,规定了如何处理接收到的请求。
    • host: 指定请求的主机名为www.example.com,表示此Ingress规则仅适用于该主机名的请求。
    • http: 指定这是一个HTTP类型的规则,用于处理HTTP请求。
      • paths: 定义了路径匹配的配置。
        • backend: 定义了后端服务的配置。

          • service:
            • name: 后端服务的名称为test02,表示请求将转发到名为test02的Kubernetes Service。
            • port:
              • name: 指定了服务端口的名称为http
        • path: 指定了路径匹配规则,将以/path/to/api01开头的请求转发到上述定义的test02服务。

        • pathType: 指定路径的类型为ImplementationSpecific,表示具体的路径处理由Ingress控制器实现决定。

总结来说,这个Ingress配置的作用是将所有来自www.example.com主机的以/path/to/api01开头的HTTP请求路由到名为test02的Kubernetes Service上的http端口,并进行路径重写,将路径重定向到/path/to/api02

相关文章

网友评论

      本文标题:【ingress】k8s ingress 通过注解配置,实现接口

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