【GCP】Qwiklabs の「クラウドアーキテクチャ」を勉強しています~Kubernetes を使った Cloud のオーケストレーション~

Kubernetes を使った Cloud のオーケストレーション GCP
Kubernetes を使った Cloud のオーケストレーション

Qwiklabs の「クラウドアーキテクチャ」を進めていきたいと思います。

Kubernetes を使った Cloud のオーケストレーション

概要

Google Kubernetes Engine(GKE)を使用して、Kubernetes のデプロイや管理を学びます。

コマンド

クラスタの作成

gcloud config set compute/zone us-central1-b
gcloud container clusters create io

サンプルコードを取得する

git clone https://github.com/googlecodelabs/orchestrate-with-kubernetes.git
cd orchestrate-with-kubernetes/kubernetes
ls

Kubernetes のクイックデモ

kubectl create deployment nginx --image=nginx:1.10.0
kubectl get pods
kubectl expose deployment nginx --port 80 --type LoadBalancer
kubectl get services
curl http://<外部IPアドレス>:80

ポッドを作成する

cat pods/monolith.yaml
kubectl create -f pods/monolith.yaml
kubectl get pods
kubectl describe pods monolith

YAMLファイルの内容については、下記のページが参考になります。

Deployment
Deployment はPodとReplicaSetの宣言的なアップデート機能を提供します。 Deploymentにおいて 理想的な状態 を記述すると、Deploymentコントローラーは指定された頻度で現在の状態を理想的な状態に変更します...

ポッドの操作

複数のターミナルを起動します。

第2のターミナル
kubectl port-forward monolith 10080:80
最初のターミナル
curl http://127.0.0.1:10080
curl http://127.0.0.1:10080/secure
curl -u user http://127.0.0.1:10080/login
TOKEN=$(curl http://127.0.0.1:10080/login -u user|jq -r '.token')
echo $TOKEN
curl -H "Authorization: Bearer $TOKEN" http://127.0.0.1:10080/secure
kubectl logs monolith
第3のターミナル
kubectl logs -f monolith
最初のターミナル
curl http://127.0.0.1:10080
kubectl exec monolith --stdin --tty -c monolith /bin/sh
ping -c 3 google.com
exit

サービスの作成

cd ~/orchestrate-with-kubernetes/kubernetes
cat pods/secure-monolith.yaml
kubectl create secret generic tls-certs --from-file tls/
kubectl create configmap nginx-proxy-conf --from-file nginx/proxy.conf
kubectl create -f pods/secure-monolith.yaml
cat services/monolith.yaml
kubectl create -f services/monolith.yaml
gcloud compute firewall-rules create allow-monolith-nodeport --allow=tcp:31000
gcloud compute instances list
curl -k https://<外部IPアドレス>:31000

タイムアウトしてしまいます。
なぜでしょう?

ポッドにラベルを追加する

kubectl get pods -l "app=monolith"
kubectl get pods -l "app=monolith,secure=enabled"
kubectl label pods secure-monolith 'secure=enabled'
kubectl get pods secure-monolith --show-labels
kubectl describe services monolith | grep Endpoints
gcloud compute instances list
curl -k https://<外部IPアドレス>:31000

タイムアウトした原因は、”app=monolith,secure=enabled” のラベルが付いている Pod が存在しなかったためです。

デプロイを作成する

cat deployments/auth.yaml
kubectl create -f deployments/auth.yaml
kubectl create -f services/auth.yaml
kubectl create -f deployments/hello.yaml
kubectl create -f services/hello.yaml
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 -k https://<外部IPアドレス>

まとめ

Google Kubernetes Engine(GKE)を使用して、Kubernetes のデプロイや管理を学びました。
YAMLファイルの書き方をマスターしたいところです。

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

コメント

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