【GCP】Qwiklabs の「Google Cloud の Kubernetes」を勉強しています~Kubernetes Engine によるデプロイの管理~

Kubernetes Engine によるデプロイの管理 GCP
Kubernetes Engine によるデプロイの管理

Qwiklabs の「Google Cloud の Kubernetes」を進めていきたいと思います。

Kubernetes Engine によるデプロイの管理

概要

Kubernetes Engine を使用して、コンテナのスケーリングと管理の演習を行います。

コマンド

設定

ゾーンを設定する
gcloud config set compute/zone us-central1-a
このラボのサンプルコードを入手する
git clone https://github.com/googlecodelabs/orchestrate-with-kubernetes.git
cd orchestrate-with-kubernetes/kubernetes
gcloud container clusters create bootcamp --num-nodes 5 --scopes "https://www.googleapis.com/auth/projecthosting,storage-rw"

Deployment オブジェクトの詳細

kubectl explain deployment
kubectl explain deployment --recursive
kubectl explain deployment.metadata.name

Deployment の作成

sed -i -e 's/kelseyhightower\/auth:2.0.0/kelseyhightower\/auth:1.0.0/' deployments/auth.yaml
cat deployments/auth.yaml
kubectl create -f deployments/auth.yaml
kubectl get deployments
kubectl get replicasets
kubectl get pods
kubectl create -f services/auth.yaml
kubectl create -f deployments/hello.yaml
kubectl create -f services/hello.yaml
kubectl create secret generic tls-certs --from-file tls/
kubectl create configmap nginx-frontend-conf --from-file=nginx/frontend.conf
kubectl create -f deployments/frontend.yaml
kubectl create -f services/frontend.yaml
kubectl get services frontend
curl -ks https://<外部IPアドレス>
curl -ks https://`kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"`
Deployment をスケールする
kubectl explain deployment.spec.replicas
kubectl scale deployment hello --replicas=5
kubectl get pods | grep hello- | wc -l
kubectl scale deployment hello --replicas=3
kubectl get pods | grep hello- | wc -l

ローリング アップデート

ローリング アップデートをトリガーする
kubectl edit deployment hello

Deployment の containers セクションにある image を次のように変更します。

containers:
- name: hello
  image: kelseyhightower/hello:2.0.0
kubectl get replicaset
kubectl rollout history deployment/hello
ローリング アップデートを一時停止する
kubectl rollout pause deployment/hello
kubectl rollout status deployment/hello
kubectl get pods -o jsonpath --template='{range .items[*]}{.metadata.name}{"\t"}{"\t"}{.spec.containers[0].image}{"\n"}{end}'
ローリング アップデートを再開する
kubectl rollout resume deployment/hello
kubectl rollout status deployment/hello
アップデートをロールバックする
kubectl rollout undo deployment/hello
kubectl rollout history deployment/hello
kubectl get pods -o jsonpath --template='{range .items[*]}{.metadata.name}{"\t"}{"\t"}{.spec.containers[0].image}{"\n"}{end}'

カナリア デプロイ

カナリア デプロイを作成する
cat deployments/hello-canary.yaml
kubectl create -f deployments/hello-canary.yaml
kubectl get deployments
カナリア デプロイを確認する
curl -ks https://`kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"`/version
本番環境でのカナリア デプロイメント – セッション アフィニティ
kind: Service
apiVersion: v1
metadata:
  name: "hello"
spec:
  sessionAffinity: ClientIP
  selector:
    app: "hello"
  ports:
    - protocol: "TCP"
      port: 80
      targetPort: 80

Blue / Green デプロイ

サービス
kubectl apply -f services/hello-blue.yaml
Blue / Green デプロイを使用したアップデート
kubectl create -f deployments/hello-green.yaml
curl -ks https://`kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"`/version

新しいバージョンを指定するようにサービスを更新します。

kubectl apply -f services/hello-green.yaml
curl -ks https://`kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"`/version
Blue / Green ロールバック
kubectl apply -f services/hello-blue.yaml
curl -ks https://`kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"`/version

まとめ

Kubernetes Engine を使用して、コンテナのスケーリングと管理の演習を行いました。
Blue / Green デプロイは、実際に試してみたいデプロイ方法です。

https://www.papacoder.net/gcp-training-free-campaign/

コメント

タイトルとURLをコピーしました