Kubernetes ( k8s ) Traefik 多个中间件 ( Middleware) 使用

Kubernetes ( k8s ) Traefik 多个中间件 ( Middleware) 使用

Traefik: 2.0.7

---
# 帐号密码
# 生成访问帐号密码 htpasswd -nb admin 123456 | base64 -w 0
apiVersion: v1
kind: Secret
type: Opaque
metadata:
  name: java-demo
  namespace: linuxhub-dev
data:
  users: YWRtaW46JGFwcjEkWTFia1BCbXQkQm9NREVIdW1ieXhaeWxKNkZDLkJTLwoK

---
# 中间件1( stripPrefix 去除前缀 )
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: java-demo-strip-prefix
  namespace: linuxhub-dev
spec:
  stripPrefix:
    prefixes:
      - /api/java-demo
---

# 中间件2( basicAuth 基本认证 )
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: java-demo-basic-auth
  namespace: linuxhub-dev
spec:
  basicAuth:
    secret: java-demo

---

# 中间件3 ( 链 ,将多个中间件到一起 )
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: java-demo
  namespace: linuxhub-dev
spec:
  chain:
    middlewares:
    - name: java-demo-strip-prefix
    - name: java-demo-basic-auth

---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: java-demo
  namespace: linuxhub-dev
  labels:
    app: java-demo
spec:
  entryPoint:
    - web
  routes:
  - match: Host(`k8s-test.linuxhub.cn`) && PathPrefix(`/api/java-demo`)
    kind: Rule
    services:
    - name: java-demo
      port: 8080
    middlewares:
    - name: java-demo
      namespace: linuxhub-dev
$ kubectl -n linuxhub-dev get Middleware                         
NAME                     AGE
java-demo                33m
java-demo-basic-auth     33m
java-demo-strip-prefix   33m

验证

验证地址: http://k8s-test.linuxhub.cn/api/java-demo/index
帐号密码: admin/123456

你可能感兴趣的:(Kubernetes ( k8s ) Traefik 多个中间件 ( Middleware) 使用)