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

2024-06-24  本文已影响0人  Bogon

如果我们实现访问微服务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

spec

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

上一篇 下一篇

猜你喜欢

热点阅读