LoginSignup
6
5

More than 5 years have passed since last update.

はじめてのGKEとkompose

Last updated at Posted at 2018-03-16

GKEにdeployするのにdocker-composeを使いたいが、変換してくれるkomposeそのままでいけるわけじゃないという記録

まず変換

kompose convert -f docker-compose.yml

こんなファイルができる

-rw-r--r-- 1 root root  241 2018-03-16 09:34:24 web-claim0-persistentvolumeclaim.yaml
-rw-r--r-- 1 root root  241 2018-03-16 09:34:24 web-claim1-persistentvolumeclaim.yaml
-rw-r--r-- 1 root root 1.5K 2018-03-16 09:34:24 web-deployment.yaml
-rw-r--r-- 1 root root  361 2018-03-16 09:34:24 web-service.yaml

deploy~がpods、serviceがLB。両方コレで起動できる

kubectl create -f foo.yaml

ダメな点

1. volumes

これがpersistentVolumes に変換?してくれるみたいだけどデータuploadまでしてくれる感じがしない。

今はyamlからcomment outして、image build内でcopyして使ってる。つまり、micro servicesにしかこの手は使えない。

ここは要調査。

2. image name

komposeはcomposeのサービス名をk8sのアプリ名、サービス名、そして まさかのイメージ名 などあらゆるところに使う。

今回自分の場合は web というサービス名だったのでこうなった

$ cat web-deployment.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
...
metadata:
...
  labels:
    io.kompose.service: web                        <=============
  name: web                                        <=============
spec:
...
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: web                  <=============
    spec:
...
        image: web                                <============= ココがガン。

このimage名に合わせて gcr.io/${PROJECT_ID}/ のprefixをつけたimageでpushしないといけないのだが、問題はこのサービス名を yml からパースして取る方法がないところ。

それぽいstackoverflowの回答は0だったので考えることにした(回答書いといた)

https://stackoverflow.com/questions/37249755/can-i-reference-the-service-name-docker-compose-yml

僕がgo/pythonのプロなら今すぐdocker-compose にPRを送るのだが、そんな技術はなかった。

今の回答はこれ

KOMPOSE_SERVICE_NAME=$(docker-compose images | tail -n +3 | awk '{print $2}' | sed s/.*_//)

今回は one image なのでコレで行く(適当)。

で、それを使ってimageを作る

IMAGE=gcr.io/${PROJECT_ID}/${KOMPOSE_SERVICE_NAME}

これをpushするようにすれば、komposeが作ったymlに書かれるimage名と一致するはず。

・・・と思ったがなんかうまくいかなげ。

volumesもダメだった。これはkomposeが書き出したyaml群をシェル芸で消すようにした。

今日のコマンドメモ

途中からhello-clusterとhello-appとかhello-webが混在してるが、同じものにすること

# get prepared
gcloud -v

gcloud components update
gcloud components install kubectl

# info

gcloud compute zones list
gcloud compute regions list

gcloud config set compute/zone asia-northeast1
gcloud config get-value project -q

gcloud info


# project start
PROJECT_ID=my-project-id-123456
export PROJECT_ID="$(gcloud config get-value project -q)"

### create clusters
# not work
gcloud container clusters create hello-cluster --num-nodes=3
gcloud beta container clusters create hello-cluster --num-nodes=3

# works
gcloud container clusters create hello-cluster --num-nodes=3 --zone=asia-northeast1-a

# consoleで作った場合に必要
gcloud container clusters get-credentials hello-app --zone=asia-northeast1-a 
gcloud compute instances list

# image
docker build -t gcr.io/${PROJECT_ID}/hello-app:v1 .
gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v1

# run locally
docker run --rm -p 8080:8080 gcr.io/${PROJECT_ID}/hello-app:v1

# start pods
kubectl run hello-web --image=gcr.io/${PROJECT_ID}/hello-app:v1 --port 8080
kubectl get pods
kubectl describe deployment hello-web

# Load Balancer it is called as a service
kubectl expose deployment hello-web --type=LoadBalancer --port 80 --target-port 8080
kubectl get service
gcloud compute forwarding-rules list
kubectl describe service hello-web

# update image
docker build -t gcr.io/${PROJECT_ID}/hello-app:v2 .
gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v2
kubectl set image deployment/hello-web hello-web=gcr.io/${PROJECT_ID}/hello-app:v2

# delete
#
# pods は deployment にいて
# LB は service にいる。

## firewall called service
kubectl delete service hello-web
## pods
kubectl delete deployment  hello-web 
## cluster
gcloud container clusters delete hello-cluster --zone=asia-northeast1-a

## 動かない
kubectl delete deployment,service --all


## export
kubectl get deployment hello-web -o yaml --export > deployment.yaml
kubectl get service hello-web -o yaml --export > service.yaml

## import/start from yml
kubectl create -f deployment.yaml 
kubectl create -f service.yaml 


kubectl get deployments

# kompose
kompose convert -f docker-compose.yml

時間出来次第書き足す予定。今日はココまで

6
5
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
6
5