Istioのルーティングをごにょごにょやってみる。雰囲気で。

昨日はこんな感じでIstioとそのサンプルプロジェクトであるBookinfoをGKE上にデプロイしたので、それを使って今日はIstioのルーティングをごにょごにょやってみる。雰囲気で。

bufferings.hatenablog.com

やること

Istio / Intelligent Routing に書いてある3つのタスクをやる。

  1. Istio / Configuring Request Routing
  2. Istio / Fault Injection
  3. Istio / Traffic Shifting

Bookinfoのつくり

サンプルプロジェクトのBookinfoの画面はこんな感じ。

f:id:bufferings:20180130081528p:plain

つくりは https://istio.io/docs/guides/bookinfo.html に説明があるんだけどこんな感じ。Envoyってのがサイドカーコンテナとしてアプリの隣にデプロイされてて、そいつをプロキシとしてサービス同士が通信することでルートをコントロールする。

f:id:bufferings:20180130081420p:plain

マイクロサービス的なつくりになってて、Reviewsサービスに3つのバージョンが用意されてる。それぞれのバージョンはこんな感じ。

  • v1: レーティングなし
  • v2: 黒い星(★★★)
  • v3: 赤い星(★★★)

今回のチュートリアルでは、このReviewsサービスの周りのルーティングをごにょごにょして遊ぶ。

最初はラウンドロビン

プロジェクトをデプロイした初期の状態では、ProductPageからReviewsサービスのルートがラウンドロビンなので、3つのバージョンが順番にでてくる。

Task 1. Configuring Request Routing

reviews:v1だけ

に流す設定を適用

$ istioctl create -f samples/bookinfo/kube/route-rule-all-v1.yaml

こんな風にして設定を確認すると

$ istioctl get routerule reviews-default -o yaml

こうなってる

apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  creationTimestamp: null
  name: reviews-default
  namespace: default
  resourceVersion: "201910"
spec:
  destination:
    name: reviews
  precedence: 1
  route:
  - labels:
      version: v1
---

各項目の意味は分かってないけど、reviewsサービスへの通信は全てv1になってる雰囲気ある。

f:id:bufferings:20180130203910p:plain

ので何回リロードしてもこの表示(レビューの部分だけ切り取った)。おぉ。

f:id:bufferings:20180130203049p:plain

jasonさんだけv2

にする設定を適用して

$ istioctl create -f samples/bookinfo/kube/route-rule-reviews-test-v2.yaml

取得

$ istioctl get routerule reviews-test-v2 -o yaml

apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  creationTimestamp: null
  name: reviews-test-v2
  namespace: default
  resourceVersion: "275044"
spec:
  destination:
    name: reviews
  match:
    request:
      headers:
        cookie:
          regex: ^(.*?;)?(user=jason)(;.*)?$
  precedence: 2
  route:
  - labels:
      version: v2
---

Cookieuser=jason ってあったらv2に流す雰囲気。こういう感じ。

f:id:bufferings:20180130205015p:plain

普通に表示するとさっきと何も変わらず星が出ないんだけど、右上にあるSign inから"jason"でサインインすると(パスワードはなんでもいい)黒い星になった。おぉ。

f:id:bufferings:20180130205401p:plain

これでTask 1終了!

Task 2. Fault Injection

f:id:bufferings:20180130210034p:plain

Reviews-v2からRatingsサービスの呼び出しのタイムアウトは10sに設定してるので、7sのdelayを挿入して動作を確認してみる。

$ istioctl create -f samples/bookinfo/kube/route-rule-ratings-test-delay.yaml

設定確認

$ istioctl get routerule ratings-test-delay -o yaml

apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  creationTimestamp: null
  name: ratings-test-delay
  namespace: default
  resourceVersion: "277806"
spec:
  destination:
    name: ratings
  httpFault:
    delay:
      fixedDelay: 7.000s
      percent: 100
  match:
    request:
      headers:
        cookie:
          regex: ^(.*?;)?(user=jason)(;.*)?$
  precedence: 2
  route:
  - labels:
      version: v1
---

jasonさんのrating呼び出しが100%の割合で7秒遅延する雰囲気だね。

jasonさんでログインした状態なので、そのままリロードして7秒待つ・・・

f:id:bufferings:20180130211006p:plain

(´・ω・`)ショボーン

どゆこと?

実は、ReviewsサービスからRatingsサービスへの呼び出しは正しく10sが設定されてるんだけど、その前のProductPageからReviewsサービスの呼び出しのタイムアウトが3秒になってるのだった。1回リトライするから合計6秒でエラーメッセージが表示される。

っていうふうに

サービス呼び出しの設定が意図通りの動作をするかを確認したりするのに使えるよってことみたい。ほほー。

Task 2しゅーりょー。

Task 3. Traffic Shifting

始める前に

jasonさん用のルーティング設定を削除して、全部v1に流れるとこまで戻しておくかな。(チュートリアルにはネームスペースオプションがなかったけど、それだと「ネームスペース指定してね」って怒られた)

$ istioctl delete routerule reviews-test-v2 -n default

$ istioctl delete routerule ratings-test-delay -n default

reviews:v1とreviews:v3に50%ずつ流す設定

を適用する

$ istioctl replace -f samples/bookinfo/kube/route-rule-reviews-50-v3.yaml

今回は元々あった設定を上書きするから create じゃなくて replace

f:id:bufferings:20180130213249p:plain

確認

$ istioctl get routerule reviews-default -o yaml

apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  creationTimestamp: null
  name: reviews-default
  namespace: default
  resourceVersion: "279870"
spec:
  destination:
    name: reviews
  precedence: 1
  route:
  - labels:
      version: v1
    weight: 50
  - labels:
      version: v3
    weight: 50
---

いつも通り雰囲気で。そっか50%ずつね。ふむふむ。という感じ。実際にリロードを繰り返してみると、v1(星なし)とv3(赤い星)が半々くらいで出てくる。

f:id:bufferings:20180130212733p:plain

v3大丈夫そうねってなったら

全部v3に流す設定を適用

$ istioctl replace -f samples/bookinfo/kube/route-rule-reviews-v3.yaml

f:id:bufferings:20180130213332p:plain

apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  creationTimestamp: null
  name: reviews-default
  namespace: default
  resourceVersion: "280349"
spec:
  destination:
    name: reviews
  precedence: 1
  route:
  - labels:
      version: v3
    weight: 100
---

リロードしたら常に赤い星になってる。(∩´∀`)∩ワーイ

たのしかった。