Enjoy Architecting

Twitter: @taisho6339

【GCP Anthos】Anthos Service Mesh + Ingress For Anthosを組み合わせたk8sのマルチクラスタを構築する

この記事について

前回の記事でIngress For Anthosを使ったマルチクラスタによるk8s冗長化について触れた。 今回はManagedなIstioを提供するAnthos Service Meshも取り入れ、 サービスメッシュを導入したマルチクラスタを実際に構築してみる。

Anthos Service Meshとは?

  • 基本的にはIstioだが、Configuration ProfileがAnthos用にカスタムされて提供されている。

何がマネージドになっているのか?

  • サービスの依存関係、SLOモニタリング、各サービスのメトリクス収集がManagedに集約され、ダッシュボードで統括的にみることができる

    • Istio Proxyのmetrics送信部分がおそらくManagedになっていて、ほぼ何もしなくてもAnthosに自動的に送られるようになっているぽい
  • GKE環境下ではCitadelがManagedになっていて、ルート証明書サーバ証明書周りの管理をGoogleに任せられる

    • ほぼ何も意識せずにmTLS機能を有効にできる
    • Cloud IAPと連携ができる(GCPのサービスアカウントによる認証プロキシをメッシュに挟める)
  • Traffic DirectorがManagedなIstio Pilotとして提供されているらしい(まだ未検証)

    クラスタの要件

  • 4つのvCPUを持つ、n1-standard-4のノードが4つ以上存在すること https://cloud.google.com/service-mesh/docs/gke-install-existing-cluster#requirements
Your GKE cluster must meet the following requirements:

- At least four nodes. If you need to add nodes, see Resizing a cluster

- The minimum machine type is n1-standard-4, which has four vCPUs. If the machine type for your cluster doesn't have at least four vCPUs, change the machine type as described in Migrating workloads to different machine types

- Use a release channel rather than a static version of GKE

本記事で構築するクラスタアーキテクチャ

  • アプリケーションとしては、Istioで用意されているでもアプリのBooknfoを用いる。

f:id:taisho6339:20200508211741p:plain
アーキテクチャ

  • 注意点としては、
    • LBからIngress Gatewayにルーティングする
    • Ingress Gatewayを作ると自動的にLoadBalancerタイプのServiceが作られるが、Ingress For AnthosはLBから直接Ingress GatewayのServiceにルーティングするので、NodePortタイプに書き換える

事前準備

n1-standard-4, 4つのvCPUのインスタンスを余裕を持って5つ程度でノードプールを構築し、クラスタを作っておく

手順について

基本的にはこの手順でIstioをインストールする ドキュメントの通りではあるが、ピックアップした手順を記載しておく。

Anthosの仲間に加える

  • role/gkehub.connectの権限を持ったサービスアカウントを作成する
  • 環境変数の設定
export PROJECT_ID=
export PROJECT_NUMBER=
export CLUSTER_NAME=
export CLUSTER_LOCATION=
export WORKLOAD_POOL=${PROJECT_ID}.svc.id.goog
export MESH_ID="proj-${PROJECT_ID}"
export SERVICE_ACCOUNT_NAME=anthos-connector
export SERVICE_ACCOUNT_KEY_PATH=
gcloud container hub memberships register $CLUSTER_NAME \
   --gke-cluster=${CLUSTER_LOCATION}/${CLUSTER_NAME} \
   --service-account-key-file=$SERVICE_ACCOUNT_KEY_PATH

Anthos Service Meshの設定

  • クラスタのラベルにメッシュIDを設定(モニタリングに用いられる)
gcloud container clusters update ${CLUSTER_NAME} \
  --update-labels=mesh_id=${MESH_ID}
  • Workload Identity(k8sのサービスアカウントをGCPのサービスアカウントとして認証する機能)
gcloud container clusters update ${CLUSTER_NAME} \
   --workload-pool=${WORKLOAD_POOL}
  • Stackdriver MonitoringとLoggingを有効化
gcloud container clusters update ${CLUSTER_NAME} \
   --enable-stackdriver-kubernetes
kubectl create clusterrolebinding cluster-admin-binding \
  --clusterrole=cluster-admin \
  --user="$(gcloud config get-value core/account)"
istioctl manifest apply --set profile=asm \
  --set values.global.trustDomain=${WORKLOAD_POOL} \
  --set values.global.sds.token.aud=${WORKLOAD_POOL} \
  --set values.nodeagent.env.GKE_CLUSTER_URL=https://container.googleapis.com/v1/projects/${PROJECT_ID}/locations/${CLUSTER_LOCATION}/clusters/${CLUSTER_NAME} \
  --set values.global.meshID=${MESH_ID} \
  --set values.global.proxy.env.GCP_METADATA="${PROJECT_ID}|${PROJECT_NUMBER}|${CLUSTER_NAME}|${CLUSTER_LOCATION}" \
  --set values.global.mtls.enabled=true
  • 任意のnamespaceに対してSidecarのAuto Injectionをdefault有効にする
kubectl label namespace default istio-injection=enabled

アプリケーションのデプロイ

kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
  • VirtualServiceをデプロイ
kubectl apply -f samples/bookinfo/networking/virtual-service-all-v1.yaml
  • DestinationRuleをデプロイ
kubectl apply -f samples/bookinfo/networking/destination-rule-all-mtls.yaml
kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml

Ingress For Anthosと連携する

  • Config用のクラスタを登録(本記事ではアプリケーションと兼用している)
gcloud alpha container hub ingress enable \
  --config-membership=projects/$PROJECT_ID/locations/global/memberships/$CLUSTER_NAME
  • Ingress GatewayのService TypeをNodePortにして不要なLoadBalancerを消す
    • istio-ingressgateway-patch.json
[
  {
    "op": "replace",
    "path": "/spec/type",
    "value": "NodePort"
  },
  {
    "op": "remove",
    "path": "/status"
  }
]
kubectl -n istio-system patch svc istio-ingressgateway \
    --type=json -p="$(cat istio-ingressgateway-patch.json)" \
    --dry-run=true -o yaml | kubectl apply -f -
  • MultiClusterServiceをデプロイ
kubectl apply -f - <<EOF
apiVersion: networking.gke.io/v1
kind: MultiClusterService
metadata:
  name: bookinfo
  namespace: istio-system
  annotations:
    beta.cloud.google.com/backend-config: '{"ports": {"80":"zone-health-check-cfg"}}'
spec:
  template:
    spec:
      selector:
        app: istio-ingressgateway
      ports:
        - name: web
          protocol: TCP
          port: 80
          targetPort: 80
EOF
  • BackendConfigをデプロイ
kubectl apply -f - <<EOF
apiVersion: cloud.google.com/v1beta1
kind: BackendConfig
metadata:
  name: bookinfo-health-check-cfg
  namespace: istio-system
spec:
  healthCheck:
    checkIntervalSec: 30
    timeoutSec: 10
    healthyThreshold: 1
    unhealthyThreshold: 2
    type: HTTP
    port: 80
    requestPath: /productpage
EOF
  • MultiClusterIngressをデプロイ
kubectl apply -f - <<EOF
apiVersion: networking.gke.io/v1
kind: MultiClusterIngress
metadata:
  name: bookinfo-ingress
  namespace: istio-system
spec:
  template:
    spec:
      backend:
        serviceName: bookinfo
        servicePort: 80
EOF

結果

  • AnthosによってGlobalなGCLBが作成され、Ingress GatewayにルーティングされるNEGがバックエンドとして登録されている f:id:taisho6339:20200508211903p:plain

  • GCLBのVIPにアクセスすることでアプリケーションが表示される f:id:taisho6339:20200508211934p:plain

  • サービスメッシュのダッシュボードはこんな感じになっている(SLOモニタリングの機能もある) f:id:taisho6339:20200508212043p:plainf:id:taisho6339:20200508212049p:plainf:id:taisho6339:20200508212053p:plainf:id:taisho6339:20200508212100p:plainf:id:taisho6339:20200508212111p:plain

試しにIstioでルーティングを変えてみる

Istioの代表的な機能である、Traffic Managementを試してみる。 今回は試しにreviewsサービスのv1に50%、v3に50%の割合でルーティングされるようにしてみる。

kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-50-v3.yaml

何度かリロードするとReviewの部分が切り替わることが分かる。 各クラスタごとにIstioのコントロールプレーンを持っているので、 マルチクラスタによって冗長化しているケースはどちらにもapplyしてあげる必要がある。 (v3は赤い星、v1は星無しで出る) f:id:taisho6339:20200508212505p:plain

まとめ

Anthosを活用することでGKEに関しては、 マルチクラスタ構成による冗長化、ManagedなIstioの導入を組み合わせてかんたんに構築できることが分かった。 まだまだAnthosに関しては発展途上で情報も全然ない印象ではあるが、機能としては便利なので今後に期待したい。

参考

https://speakerdeck.com/jukuwa/gcpug-tokyo-istio-1-dot-5-day-anthos-service-mesh