🎉

GKE上でIstioを動作させる

2024/02/07に公開

記事の内容

GKE上でIstioを動作させて、Istio経由でPodにアクセスします。

記事の長さ

3分で読めます

GKEの作成

https://zenn.dev/ring_belle/articles/gcp-terraform-gke

こちらでGKEの作成について解説しています。

もし、GKEの作成方法がわからない方がいましたら、上記を参照ください。
本記事では、GKEはすでに作成済みかつ、Credentialも取得済みの想定で進めます。

Istioの実装

Kubernetesクラスターの準備が完了したので、GKE上にIstioを実装していきます。

IstioのInstall

istioctlを使って、KubernetesクラスターにIstioをInstallします。

$ istioctl install
This will install the Istio 1.20.1 "default" profile (with components: Istio core, Istiod, and Ingress gateways) into the cluster. Proceed? (y/N) y
✔ Istio core installed
✔ Istiod installed
✔ Ingress gateways installed
✔ Installation complete
Made this installation the default for injection and validation.

正常にInstallが完了すると、以下のようにistio-systemのnamespaceが追加されているはずです。

$ kubectl get ns                                                                                                                                                           ~/Projects/zenn-gke-istio-ingress
NAME              STATUS   AGE
default           Active   13m
gmp-public        Active   12m
gmp-system        Active   12m
istio-system      Active   3m2s
kube-node-lease   Active   13m
kube-public       Active   13m
kube-system       Active   13m

namespaceにlabelを付与する

namespaceの中のPodがIstioで通信できるように、namespaceにlabelを付与します。

$ kubectl label namespace default istio-injection=enabled

Sample AppをDeployする

公式ドキュメントのサンプルを利用して、サンプリアプリケーションとIngressをデプロイします。

$ kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.20/samples/bookinfo/platform/kube/bookinfo.yaml
service/details created
serviceaccount/bookinfo-details created
deployment.apps/details-v1 created
service/ratings created
serviceaccount/bookinfo-ratings created
deployment.apps/ratings-v1 created
service/reviews created
serviceaccount/bookinfo-reviews created
deployment.apps/reviews-v1 created
deployment.apps/reviews-v2 created
deployment.apps/reviews-v3 created
service/productpage created
serviceaccount/bookinfo-productpage created
deployment.apps/productpage-v1 created

$ kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.20/samples/bookinfo/networking/bookinfo-gateway.yaml
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created

Istioサイドカーを確認する

Istioに必要なリソースをdeployできたので、Istioサイドカーが正常にリリースされていることを確認します。

$ kubectl get pod -n default
NAME                             READY   STATUS    RESTARTS   AGE
details-v1-5f4d584748-vsqbv      2/2     Running   0          48m
productpage-v1-564d4686f-7mz8b   2/2     Running   0          48m
ratings-v1-686ccfb5d8-v4wlq      2/2     Running   0          48m
reviews-v1-86896b7648-fxs72      2/2     Running   0          48m
reviews-v2-b7dcd98fb-cjf4z       2/2     Running   0          48m
reviews-v3-5c5cc7b6d-hzzkm       2/2     Running   0          48m

$ kubectl describe pod ratings-v1-686ccfb5d8-v4wlq
...
Containers:
  ratings:
    Container ID:   containerd://2a7c292f309cac5172d2154902f7dedc9fee6a23217ef5de938a376b02c89c37
    Image:          docker.io/istio/examples-bookinfo-ratings-v1:1.18.0
    Image ID:       docker.io/istio/examples-bookinfo-ratings-v1@sha256:93e8d6f8a7eeac079ef5724d4b20cbc15afcdacb71c8c1402ea1eba0ec0bd86e
    Port:           9080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 07 Feb 2024 09:43:23 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-vclm4 (ro)
  istio-proxy:
    Container ID:  containerd://084d37e85db901103ea5b91cc4ca1142f9355f0b662cd61376e7bfb7f774d5ea
    Image:         docker.io/istio/proxyv2:1.20.1
    Image ID:      docker.io/istio/proxyv2@sha256:d58efa92c283c26765a5cdb2d66c4a26ea6e9cb7042242dd2f8a89fc2a520f9c
    Port:          15090/TCP
    Host Port:     0/TCP
    Args:
...

Podにアクセスする

最後にPodにアクセスします。

アクセスに必要な情報を環境変数に登録します。

$ export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
$ export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')
$ export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].port}')
$ export GATEWAY_URL=$INGRESS_HOST:$INGRESS_PORT

最後に、以下のURLでアクセスできます。

$ curl "http://$GATEWAY_URL/productpage"
...
  </div>
</div>

<!-- Latest compiled and minified JavaScript -->
<script src="static/jquery.min.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="static/bootstrap/js/bootstrap.min.js"></script>

<script type="text/javascript">
  $('#login-modal').on('shown.bs.modal', function () {
    $('#username').focus();
  });
</script>

  </body>
</html>

これでGKEクラスターにIstioをリリースし、Istioが動作するPodにIstioのService経由でアクセスすることができました!

note

勉強法やキャリア構築法など、エンジニアに役立つ記事をnoteで配信しています。

https://note.com/ring_belle/membership

Discussion