iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 24
0
DevOps

Kubernetes and Istio 三十天系列 第 24

[Day24] Istio Example BookInfo - Traffic Management IV

Istio Example BookInfo Architecture

BookInfo-Architecture

前言

Istio Service Mesh可以使用ingress and egress當作Service Mesh的出入口,統一且有效的管理。

Istio ingress

在 Kubernetes Cluster已經有Kubernetes Ingress用於Cluster外部公開的服務。在 Istio Service Mesh中,更好的方法(也適用於 Kubernetes 和其他環境)是使用Istio-Gateway。Istio-Gateway允許將 Istio 功能(例如,監控和路由規則)應用於進入Service Mesh的流量。

此任務描述如何配置 Istio-Gateway並且在Istio Service Mesh Expose Service。

Install

kubectl apply -f samples/httpbin/httpbin.yaml

確認Istio-ingressgateway Kubernetes Service

kubectl get svc istio-ingressgateway -n istio-system
NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                                                                                                                                      AGE
istio-ingressgateway     NodePort    10.43.239.45    <none>        15020:30701/TCP,80:31380/TCP,443:31390/TCP,31400:31400/TCP,15029:32421/TCP,15030:30215/TCP,15031:31403/TCP,15032:30366/TCP,15443:32113/TCP   5d8h

這邊是採用NodePort的方式設定,可以參考[Day18] Istio Example BookInfo

Configuring ingress using an Istio Gateway

Ingress Gateway用於接收Service Mesh(Istio)的 HTTP/TCP 連接、以及各種通訊協議等,但與 Kubernetes Ingress Resources 不同,它不包括任何流量路由配置。流入流量的流量路由使用 Istio 進行配置。

Setting a Gateway

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: httpbin-gateway
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "httpbin.example.com"
EOF

通過Gateway的流量設定Route Rule

kubectl apply -f - <<EOF
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
EOF

我們創建了一個VirtualService httpbin ,其中包含兩條Route Rule,允許 /status 和 /delay。

只有真正符合 httpbin-gateway的流量才能成功真正訪問。所有其他外部請求將被拒絕,並返回 404。

在這個配置中Service Mesh中其他服務的內部請求不受這些規則約束,而是簡單地默認為round-robin routing。要將這些規則適用於內部服務之間的溝通,我們可以透過添加特殊的設定在httpbin-gateway讓許多規則也能適用於內部服務。

Verifying

有代入istio-gateway設定的host會得到正常的httpstatus 200

curl -I -HHost:httpbin.example.com http://$INGRESS_HOST:$INGRESS_PORT/status/200
HTTP/1.1 200 OK
server: envoy
content-type: text/html; charset=utf-8
access-control-allow-origin: *
access-control-allow-credentials: true
content-length: 0
x-envoy-upstream-service-time: 48

若訪問不存在的Route rule,則會得到httpstatus 404

curl -I -HHost:httpbin.example.com http://$INGRESS_HOST:$INGRESS_PORT/headers
HTTP/1.1 404 Not Found
server: envoy
content-length: 0

Accessing ingress services using a browser

在瀏覽器中輸入 httpbin Service的hostname是不會生效的,這是因為因為我們沒有辦法讓瀏覽器像 curl 訪問httpbin.example.com。而在現實世界中,因為有正常配置的主機和 DNS 記錄,這種做法就能夠成功了——只要簡單的在瀏覽器中訪問由域名構成的 URL 即可,例如 https://httpbin.example.com/status/200

要解決此問題以進行簡單的測試和演示,我們可以在 Gateway 和 VirtualService 配置中為通配符值 *。例如,如果我們將 Ingress 配置更改為以下內容:

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: httpbin-gateway
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: httpbin
spec:
  hosts:
  - "*"
  gateways:
  - httpbin-gateway
  http:
  - match:
    - uri:
        prefix: /headers
    route:
    - destination:
        port:
          number: 8000
        host: httpbin
EOF

接下來就可以在瀏覽器訪問 $INGRESS_HOST:$INGRESS_PORT(也就是 127.0.0.1:31380)進行訪問,輸入 http://127.0.0.1:31380/headers 網址之後,應該會顯示瀏覽器發送的請求 Header。

結語

今天主要是把之前提到的Istio-gateway一起說明,明天會針對egress做更深度的討論

圖片來源

Istio BookInfo


上一篇
[Day23] Istio Example BookInfo - Traffic Management III
下一篇
[Day25] Istio Example BookInfo - Traffic Management V
系列文
Kubernetes and Istio 三十天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言