istio部署demoapp应用 (五)url rewrite and url redirect

架构图

  • proxy-gateway → virtualservices/proxy → virtualservices/demoapp (/backend) → backend:8082 (Cluster)

deploy-backend.yaml  

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: backend
    version: v3.6
  name: backendv36
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  selector:
    matchLabels:
      app: backend
      version: v3.6
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: backend
        version: v3.6
    spec:
      containers:
      - image: ikubernetes/gowebserver:v0.1.0
        imagePullPolicy: IfNotPresent
        name: gowebserver
        env:
        - name: "SERVICE_NAME"
          value: "backend"
        - name: "SERVICE_PORT"
          value: "8082"
        - name: "SERVICE_VERSION"
          value: "v3.6"
        ports:
        - containerPort: 8082
          name: web
          protocol: TCP
        resources:
          limits:
            cpu: 50m
---
apiVersion: v1
kind: Service
metadata:
  name: backend
spec:
  ports:
    - name: http-web
      port: 8082
      protocol: TCP
      targetPort: 8082
  selector:
    app: backend
    version: v3.6
---

virtualservice-demoapp.yaml

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: demoapp
spec:
  hosts:
  - demoapp
  http:
  - name: rewrite
    match:
    - uri:
        prefix: /canary
    rewrite:
      uri: /
    route:
    - destination:
        host: demoapp
        subset: v11
  - name: redirect
    match:
    - uri:
        prefix: /backend
    redirect:
      uri: /
      authority: backend
      port: 8082
  - name: default
    route:
    - destination:
        host: demoapp
        subset: v10

virtualservice-proxy.yaml

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: proxy
spec:
  hosts:
  - proxy
  - "app.wgs.com"
  gateways:
  - istio-system/proxy-gateway
  - mesh  
  http:
  - name: redirect
    match:
    - uri:
        prefix: "/backend"
    redirect:
      uri: /
      authority: backend.wgs.com
      port: 80
  - name: default
    route:
    - destination:
        host: proxy

virtualservice-backend.yaml

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: backend
spec:
  hosts:
  - backend.wgs.com
  gateways:
  - istio-system/proxy-gateway
  http:
  - name: default
    route:
    - destination:
        host: backend
        port:
          number: 8082 

proxy-gateway.yaml

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: proxy-gateway
  namespace: istio-system
spec:
  selector:
    app: istio-ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "backend.wgs.com"
    - "app.wgs.com"
---

创建资源

# kubectl apply -f .
deployment.apps/backendv36 created
service/backend created
gateway.networking.istio.io/proxy-gateway created
virtualservice.networking.istio.io/backend created
virtualservice.networking.istio.io/demoapp created
virtualservice.networking.istio.io/proxy created

查看资源

查看backend pod

~# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
backendv36-859f95bd66-86tjf   2/2     Running   0          47s
backendv36-859f95bd66-blg8p   2/2     Running   0          47s
client                        2/2     Running   0          6h42m
demoappv10-78b6586d58-hq957   2/2     Running   0          6h46m
demoappv10-78b6586d58-kdv4m   2/2     Running   0          6h46m
demoappv11-78bf898c74-hckjf   2/2     Running   0          6h44m
demoappv11-78bf898c74-sn9qp   2/2     Running   0          6h44m
proxy-649b4d887d-zbx26        2/2     Running   0          6h43m

查看backend svc

~# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
backend      ClusterIP   10.100.221.1     <none>        8082/TCP   2m41s
demoapp      ClusterIP   10.100.180.239   <none>        8080/TCP   6h45m
kubernetes   ClusterIP   10.100.0.1       <none>        443/TCP    12d
proxy        ClusterIP   10.100.43.186    <none>        80/TCP     6h45m

查看backend routes

~# istioctl proxy-config routes proxy-649b4d887d-zbx26
NAME                                                                      DOMAINS                                               MATCH                  VIRTUAL SERVICE
kubernetes-dashboard.kubernetes-dashboard.svc.cluster.local:443           *                                                     /*                     
grafana.istio-system.svc.cluster.local:3000                               *                                                     /*                     
9090                                                                      kiali.istio-system, 10.100.48.203                     /*                     
9090                                                                      prometheus.istio-system, 10.100.42.102                /*                     
20001                                                                     kiali.istio-system, 10.100.48.203                     /*                     
16685                                                                     tracing.istio-system, 10.100.92.199                   /*                     
15010                                                                     istiod.istio-system, 10.100.149.76                    /*                     
80                                                                        app.wgs.com                                           /backend*              proxy.default
80                                                                        app.wgs.com                                           /*                     proxy.default
80                                                                        demoapp.default.svc.cluster.local                     /canary*               demoapp.default
80                                                                        demoapp.default.svc.cluster.local                     /backend*              demoapp.default
80                                                                        demoapp.default.svc.cluster.local                     /*                     demoapp.default
80                                                                        istio-egressgateway.istio-system, 10.100.121.95       /*                     
80                                                                        istio-ingressgateway.istio-system, 10.100.145.112     /*                     
80                                                                        proxy, proxy.default + 1 more...                      /backend*              proxy.default
80                                                                        proxy, proxy.default + 1 more...                      /*                     proxy.default
80                                                                        tracing.istio-system, 10.100.92.199                   /*                     
9411                                                                      jaeger-collector.istio-system, 10.100.122.235         /*                     
9411                                                                      zipkin.istio-system, 10.100.29.132                    /*                     
8080                                                                      demoapp, demoapp.default + 1 more...                  /canary*               demoapp.default
8080                                                                      demoapp, demoapp.default + 1 more...                  /backend*              demoapp.default
8080                                                                      demoapp, demoapp.default + 1 more...                  /*                     demoapp.default
dashboard-metrics-scraper.kubernetes-dashboard.svc.cluster.local:8000     *                                                     /*                     
InboundPassthroughClusterIpv4                                             *                                                     /*                     
15014                                                                     istiod.istio-system, 10.100.149.76                    /*                     
istio-ingressgateway.istio-system.svc.cluster.local:15021                 *                                                     /*                     
kube-dns.kube-system.svc.cluster.local:9153                               *                                                     /*                     
jaeger-collector.istio-system.svc.cluster.local:14250                     *                                                     /*                     
8082                                                                      backend, backend.default + 1 more...                  /*                     
                                                                          *                                                     /healthz/ready*        
inbound|8080||                                                            *                                                     /*                     
InboundPassthroughClusterIpv4                                             *                                                     /*                     
inbound|8080||                                                            *                                                     /*                     
                                                                          *                                                     /stats/prometheus*     
jaeger-collector.istio-system.svc.cluster.local:14268                     *                                                     /*                     

查看Istio-ingress-gateway svc EXTERNAL-IP

~# kubectl get svc -n istio-system 
NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP       PORT(S)                                                                      AGE
grafana                ClusterIP      10.100.124.243   <none>            3000/TCP                                                                     4d19h
istio-egressgateway    ClusterIP      10.100.121.95    <none>            80/TCP,443/TCP                                                               4d19h
istio-ingressgateway   LoadBalancer   10.100.145.112   192.168.174.250   15021:32417/TCP,80:46683/TCP,443:37652/TCP,31400:59146/TCP,15443:30622/TCP   4d19h
istiod                 ClusterIP      10.100.149.76    <none>            15010/TCP,15012/TCP,443/TCP,15014/TCP                                        4d19h
jaeger-collector       ClusterIP      10.100.122.235   <none>            14268/TCP,14250/TCP,9411/TCP                                                 4d19h
kiali                  ClusterIP      10.100.48.203    <none>            20001/TCP,9090/TCP                                                           4d19h
prometheus             ClusterIP      10.100.42.102    <none>            9090/TCP                                                                     4d19h
tracing                ClusterIP      10.100.92.199    <none>            80/TCP,16685/TCP                                                             4d19h
zipkin                 ClusterIP      10.100.29.132    <none>            9411/TCP                                                                     4d19h

添加域名解析

192.168.174.250  app.wgs.com backend.wgs.com

访问demoapp

访问v10子集

root@client # curl proxy
Proxying value: iKubernetes demoapp v1.0 !! ClientIP: 127.0.0.6, ServerName: demoappv10-78b6586d58-kdv4m, ServerIP: 172.20.154.244!
 - Took 13 milliseconds.

访问v11子集

root@client # curl proxy/canary
Proxying value: iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-78bf898c74-sn9qp, ServerIP: 172.20.154.245!
 - Took 6 milliseconds.

访问backend

root@client # curl proxy/backend -I
HTTP/1.1 301 Moved Permanently
location: http://backend.wgs.com:80/
date: Tue, 25 Oct 2022 10:49:18 GMT
server: envoy
transfer-encoding: chunked

 

Istio url重写示例

apiVersion: networking.istio.io/v1beta1 
kind: VirtualService
metadata:
  name: webserver 
spec:
  hosts: 
  ……
  - name: redirect
    match:
    - uri:
        prefix: "/backend" 
      redirect:
        uri: /
        authority: backend 
        port: 8082
  - name: rewrite 
    match:
      uri:
        prefix: "/healthz" 
      rewrite:
        uri: /livez 
      route:
        destination:
          host: webserver
  - name: default 
    route:
    - destination: 
        host: webserver 
        subset: v10

参考文档

https://istio.io/latest/zh/docs/reference/config/networking/virtual-service/#HTTPMatchRequest

posted @ 2022-10-25 19:00  小吉猫  阅读(206)  评论(0编辑  收藏  举报