1. 程式人生 > 實用技巧 >rancher+kubernetes負載均衡根據路徑路由服務

rancher+kubernetes負載均衡根據路徑路由服務

一、注意(最大的坑)

1、從0.22.0版開始,使用註釋的入口定義nginx.ingress.kubernetes.io/rewrite-target與先前版本不向後相容。在版本0.22.0及更高版本中,必須在捕獲組中顯式定義請求URI中需要傳遞到重寫路徑的任何子字串。

2、捕獲組儲存在編號佔位符,按時間順序,形式$1,$2... $n。這些佔位符可用作rewrite-target註釋中的引數。

3、官方文件連結:https://kubernetes.github.io/ingress-nginx/examples/rewrite/

二、kubernetes Ingress規則配置

注意配置中的:$2 和 something(/|$)(.*)

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)

三、Rancher Ingress規則配置

訪問:http:/host/model01/,請求會被髮送到web-server-01工作負載中

訪問:http:/host/model02/,請求會被髮送到web-server-02工作負載中

訪問:http:/host/model03/,請求會被髮送到web-server-03工作負載中

配置如下:

This example demonstrates how to use the Rewrite annotations

Prerequisites

You will need to make sure your Ingress targets exactly one Ingress controller by specifying the

ingress.class annotation, and that you have an ingress controllerrunningin your cluster.

Deployment

Rewriting can be controlled using the following annotations:

NameDescriptionValues
nginx.ingress.kubernetes.io/rewrite-target Target URI where the traffic must be redirected string
nginx.ingress.kubernetes.io/ssl-redirect Indicates if the location section is accessible SSL only (defaults to True when Ingress contains a Certificate) bool
nginx.ingress.kubernetes.io/force-ssl-redirect Forces the redirection to HTTPS even if the Ingress is not TLS Enabled bool
nginx.ingress.kubernetes.io/app-root Defines the Application Root that the Controller must redirect if it's in '/' context string
nginx.ingress.kubernetes.io/use-regex Indicates if the paths defined on an Ingress use regular expressions bool

Examples

Rewrite Target

Attention

Starting in Version 0.22.0, ingress definitions using the annotationnginx.ingress.kubernetes.io/rewrite-targetare not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in acapture group.

Note

Captured groupsare saved in numbered placeholders, chronologically, in the form$1,$2...$n. These placeholders can be used as parameters in therewrite-targetannotation.

Create an Ingress rule with a rewrite annotation:

$ echo '
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)
' | kubectl create -f -

In this ingress definition, any characters captured by(.*)will be assigned to the placeholder$2, which is then used as a parameter in therewrite-targetannotation.

For example, the ingress definition above will result in the following rewrites:

  • rewrite.bar.com/somethingrewrites torewrite.bar.com/
  • rewrite.bar.com/something/rewrites torewrite.bar.com/
  • rewrite.bar.com/something/newrewrites torewrite.bar.com/new

App Root

Create an Ingress rule with a app-root annotation:

$ echo "
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/app-root: /app1
  name: approot
  namespace: default
spec:
  rules:
  - host: approot.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /
" | kubectl create -f -

Check the rewrite is working

$ curl -I -k http://approot.bar.com/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.11.10
Date: Mon, 13 Mar 2017 14:57:15 GMT
Content-Type: text/html
Content-Length: 162
Location: http://stickyingress.example.com/app1
Connection: keep-alive






https://kubernetes.github.io/ingress-nginx/examples/rewrite/