---
# System prepended metadata

title: Lab 06
tags: [2021/03/18]

---

# Lab 06

- [ ] Expose Service in Service Mesh
- [ ] Routing based on Path
- [ ] Canary Release
- [ ] Mirror

## Expose Service

建立 Deployment 物件
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "true"
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: docker.io/dennischou/showip:latest
```

建立 Service 物件
```yaml
apiVersion: v1
kind: Service
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  type: ClusterIP
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: myapp
```

建立 Gateway
```yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: userX-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 8080
      name: http
      protocol: HTTP
    hosts:
    - "userX.train.test.lab"
```

建立 virtualservice
```yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: userX-vsvc
spec:
  hosts:
  - "userX.train.test.lab"
  gateways:
  - userX-gateway
  http:
  - match:
    - uri:
        prefix: /userX
    rewrite:
      uri: /
    route:
    - destination:
        host: myapp
        port: 
          number: 8080
```

## Routing based on Path

建立 Deployment 物件部署
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-v1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "true"
      labels:
        app: myapp
        version: v1
    spec:
      containers:
      - name: myapp
        image: docker.io/dennischou/showip:latest
```

建立 Deployment 物件部署
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-v2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      version: v2
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "true"
      labels:
        app: myapp
        version: v2
    spec:
      containers:
      - name: myapp
        image: docker.io/dennischou/showip:latest
```

建立 Service 物件
```yaml
apiVersion: v1
kind: Service
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  type: ClusterIP
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: myapp
```

建立 Gateway 物件
```yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: userX-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "userX.train.test.lab"
```

建立 VirtualService 物件
```yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: userX-vsvc
spec:
  hosts:
  - "userX.train.test.lab"
  gateways:
  - userX-gateway
  http:
  - match:
    - uri:
        prefix: /v1
    rewrite:
      uri: /
    route:
    - destination:
        host: myapp
        port:
          number: 8080
        subset: v1
  - match:
    - uri:
        prefix: /v2
    rewrite:
      uri: /
    route:
    - destination:
        host: myapp
        port:
          number: 8080
        subset: v2
```

建立 DestinationRule 物件
```yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: path
spec:
  host: myapp
  trafficPolicy:
    loadBalancer:
      simple: RANDOM
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN
```

## Canary

建立 VirtualService 物件
```yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: userX-vsvc
spec:
  hosts:
  - "userX.train.test.lab"
  gateways:
  - userX-gateway
  http:
  - match:
    - uri:
        exact: /    
    route:
    - destination:
        host: myapp
        port:
          number: 8080
        subset: v1
      weight: 80
    - destination:
        host: myapp
        port:
          number: 8080
        subset: v2
      weight: 20
```

建立 DestinationRule 物件
```yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: path
spec:
  host: myapp
  trafficPolicy:
    loadBalancer:
      simple: ROUND_ROBIN
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
```

模擬呼叫
```bash
for i in {1..100}; do curl http://userX.train.test.lab; done
```

開啟 Kiali 介面檢查觀察流量圖

## Mirror

建立 v1 Deployment 物件
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: vertx-greet-v1
spec:
  selector:
    matchLabels:
      app: vertx-greet
      version: v1
  replicas: 1
  template:
    metadata:
      labels:
        app: vertx-greet
        version: v1
      annotations:
        sidecar.istio.io/inject: "true"
    spec:
      containers:
        - name: vertx-greet
          image: quay.io/redhattraining/ossm-vertx-greet:latest
          ports:
            - containerPort: 8080
          resources:
            limits:
              memory: "200Mi"
              cpu: "250m"
```

建立 v2 Deployment 物件
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: vertx-greet-v2
spec:
  selector:
    matchLabels:
      app: vertx-greet
      version: v2
  replicas: 1
  template:
    metadata:
      labels:
        app: vertx-greet
        version: v2
      annotations:
        sidecar.istio.io/inject: "true"
    spec:
      containers:
        - name: vertx-greet
          image: quay.io/redhattraining/ossm-vertx-greet:latest
          ports:
            - containerPort: 8080
          resources:
            limits:
              memory: "200Mi"
              cpu: "250m"
          env:
          - name: GREETING
            value: "Hola Mundo!"
```

建立 Service 物件
```yaml
apiVersion: v1
kind: Service
metadata:
  name: vertx-greet
  labels:
    app: vertx-greet
spec:
  type: ClusterIP
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: vertx-greet

```

建立 VirtualService 物件
```yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: user0-vsvc
spec:
  hosts:
  - "user0.train.test.lab"
  gateways:
  - user0-gateway
  http:
  - match:
    - uri:
        exact: /
    route:
    - destination:
        host: vertx-greet
        port:
          number: 8080
        subset: v1
    mirror:
      host: vertx-greet
      port:
        number: 8080
      subset: v2
    mirror_percent: 10
```

建立 DestinationRule 物件
```yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: path
spec:
  host: vertx-greet
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
```

模擬呼叫
```
for i in {1..100}; do curl http://userX.train.test.lab; done
```

開啟 Grafana Dashboard

---

###### tags: `2021/03/18`