# 網站提供 https 安全連線服務 - Istio 範例

前面兩篇文章，都是以 **nginx ingress** 為範例，今日來簡略說明 **Istio** 面向的 **HTTPS** 範例。

### 憑證管理

在 **Istio** 預設範疇中，也是使用`Cert-Manager`方式管理、申請憑證( **Let’s Encrypt** )。觀念與設定方面皆與[此篇憑證管理](https://app.gitbook.com/@fufu/s/kk8s/task-memory/22.web-https-certificates)大致相同。\
透過定義`Certificate yaml`進行憑證申請，但在 **Istio ingress** 使用上有一點要注意，就是憑證的儲存名稱：`secretName: istio-ingressgateway-certs`  \
如果沒遵循此約定，則 **Istio ingress** 無法直接存取此憑證。

#### Certificate yaml

```yaml
apiVersion: certmanager.k8s.io/v1alpha1
kind: Certificate
metadata:
  name: cert-demo-web.com
  namespace: istio-system
spec:
  acme:
    config:
    - dns01:
        provider: gcp-dns
      domains:
      - '*.demo-web.com'
      - demo-web.com
  commonName: '*.demo-web.com'
  dnsNames:
  - demo-web.com
  issuerRef:
    kind: ClusterIssuer
    name: letsencrypt
  secretName: istio-ingressgateway-certs

```

### Istio ingress

**Istio** 安裝後，預設的ingress名稱是：`istio-ingressgateway`\
此`Ingress resource`實現 **Istio** 管理入口路由、規則、監控等面向的流量流入內部Service。\
**Istio** 實現上分成兩部分：`Gateway`、`VirtualService`

#### Gateway yaml

此部分定義了網站的入口閘道，相關的`域名`與`https`服務。\
有關於`https`服務中，**Istio** 透過 **File Mount** 方式讀取憑證(預設名稱：`istio-ingressgateway-certs`)，實現HTTPS服務入口閘道。

```yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: httpbin-gateway
spec:
  selector:
    istio: ingressgateway # use istio default ingress gateway
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    tls:
      mode: SIMPLE
      serverCertificate: /etc/istio/ingressgateway-certs/tls.crt
      privateKey: /etc/istio/ingressgateway-certs/tls.key
    hosts:
    - "httpbin.example.com"
```

#### VirtualService yaml

此部分定義了網站的入口路由。

```yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: httpbin
spec:
  hosts:
  - "httpbin.example.com"
  gateways:
  - httpbin-gateway
  http:
  - match:
    - uri:
        prefix: /status
    - uri:
        prefix: /delay
    route:
    - destination:
        port:
          number: 8000
        host: httpbin
```

以上兩項`istio ingress resource`賦予了網站入口的使命，缺一不可。
