LoginSignup
1
2

More than 5 years have passed since last update.

Google Kubernetes Engine触ってみたときのコマンド覚書

Last updated at Posted at 2018-07-07

参考

やってること

  • GKE上で対CPUの五目並べのゲームを動かす
  • 最初のCPUのアルゴリズムはただランダムに石を置くだけ
  • ローリングアップデートを使って、アプリを止めずにアルゴリズムを司るコンテナを入れ替える
  • アップデート後はアルゴリズムが賢いものに切り替わる

コマンド(Google Cloud Shellで実行)

ちょっとだけ自分でアレンジ。

  • versionを3桁にした。(慣れ親しんだ形。docker界隈は2桁が主流なのかな?)
  • フロントエンドアクセス用のIPをワンライナーで取得。(目指せshell芸人)

# set env after you create new project
gcloud config set compute/zone asia-northeast1-a
export PROJECT_ID=$(gcloud config list project --format "value(core.project)")

# create kubrnetes cluster
gcloud container clusters create gobang-cluster \
  --num-nodes 3 \
  --scopes "https://www.googleapis.com/auth/datastore" \
  --zone asia-northeast1-a

# set kubernetes config
gcloud container clusters get-credentials gobang-cluster --zone=asia-northeast1-a

# set region of datastore
gcloud app create --region=asia-northeast1

# fetch sample code
git clone https://github.com/asashiho/gke-gobang-app-example

# build docker image
docker build -t frontend:v1.0.0 frontend/
docker build -t backend:v1.0.0 backend-dummy/
docker build -t backend:v1.1.0 backend-smart/

# make tag for upload image
docker tag frontend:v1.0.0 gcr.io/$PROJECT_ID/frontend:v1.0.0
docker tag backend:v1.0.0 gcr.io/$PROJECT_ID/backend:v1.0.0
docker tag backend:v1.1.0 gcr.io/$PROJECT_ID/backend:v1.1.0

# upload image to gcp
gcloud docker -- push gcr.io/$PROJECT_ID/frontend:v1.0.0
gcloud docker -- push gcr.io/$PROJECT_ID/backend:v1.0.0
gcloud docker -- push gcr.io/$PROJECT_ID/backend:v1.1.0

# configure yaml file
sed -i "s/<PROJECT ID>/$PROJECT_ID/" config/frontend-deployment.yaml
sed -i "s/v1.0\$/v1.0.0/" config/frontend-deployment.yaml
sed -i "s/<PROJECT ID>/$PROJECT_ID/" config/backend-deployment.yaml
sed -i "s/v1.0\$/v1.0.0/" config/backend-deployment.yaml

# deploy kubernetes
kubectl create -f config/backend-deployment.yaml
kubectl create -f config/backend-service.yaml
kubectl create -f config/frontend-deployment.yaml
kubectl create -f config/frontend-service.yaml

# check frontend ip
FRONTEND_IP=`kubectl get services | grep frontend | awk '{print $4}'`

# access game
API_URL=http://$FRONTEND_IP/api/v1 client/client.py

# overwrite version
sed -i "s/v1.0.0\$/v1.1.0/" config/backend-deployment.yaml

# update cluster
kubectl apply -f config/backend-deployment.yaml
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2