Closed masayuki14 closed 3 years ago
Dockerの設定ぱねるから Kubernetes
タブの Enable Kuberenetes
にチェックを入れて Apply するとインストールが始まる。
しばらく時間がかかる。
Footerの Kubernetes is running
がグリーンになる。
kubectl
コマンドが使えるようになり、動作を確認できる。
% kubectl api-versions | grep compose
compose.docker.com/v1beta1
compose.docker.com/v1beta2
とりあえずドキュメントどおりに docker-compose.yml
を作って
$ docker stack deploy --orchestrator=kubernetes -c docker-compose.yml hellokube
を実行した。
$ cat docker-compose.yml
version: '3.3'
services:
db:
build: db
image: dockersamples/k8s-wordsmith-db
words:
build: words
image: dockersamples/k8s-wordsmith-api
deploy:
replicas: 5
web:
build: web
image: dockersamples/k8s-wordsmith-web
ports:
- "33000:80"
$ docker stack deploy --orchestrator=kubernetes -c docker-compose.yml hellokube
Ignoring unsupported options: build
service "words": build is ignored
service "web": build is ignored
service "db": build is ignored
Waiting for the stack to be stable and running...
db: Ready [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
adminer: Failed [pod status: 0/0 ready, 0/0 pending, 0/0 failed]
web: Ready [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
words: Ready [pod status: 5/5 ready, 0/5 pending, 0/5 failed]
コンソールはこうなった。
Ctrl + c
で抜けて
% docker stack deploy --orchestrator=kubernetes -c docker-compose.yml hellokube
したら Stack hellokube is stable and running
もう動いてた。
Ignoring unsupported options: build
service "web": build is ignored
service "db": build is ignored
service "words": build is ignored
Waiting for the stack to be stable and running...
words: Ready [pod status: 5/5 ready, 0/5 pending, 0/5 failed]
web: Ready [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
db: Ready [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
Stack hellokube is stable and running
とりあえず1つずつ読むか。
docker stack
コマンドの意味がわからん。どうやって止めるんや?
うーむ。わからん。
これ普通に本を買って読んで動かしながらやったほうがいいのかな。
これも読んでみる。 https://www.creationline.com/lab/25164
Local の kubernetes でもう動いてるっぽい。
% kubectl get all
NAME READY STATUS RESTARTS AGE
pod/db-79b45689db-p5bsh 1/1 Running 0 3h
pod/web-6b56cf97b9-9kpwj 1/1 Running 0 3h
pod/words-6d654698d5-5dvcl 1/1 Running 0 3h
pod/words-6d654698d5-jbpvc 1/1 Running 0 3h
pod/words-6d654698d5-k5ffg 1/1 Running 0 3h
pod/words-6d654698d5-kvx2k 1/1 Running 0 3h
pod/words-6d654698d5-ssldw 1/1 Running 0 3h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/db ClusterIP None <none> 55555/TCP 3h
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h
service/web ClusterIP None <none> 55555/TCP 3h
service/web-published LoadBalancer 10.101.191.61 localhost 33000:30836/TCP 3h
service/words ClusterIP None <none> 55555/TCP 3h
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/db 1 1 1 1 3h
deployment.apps/web 1 1 1 1 3h
deployment.apps/words 5 5 5 5 3h
NAME DESIRED CURRENT READY AGE
replicaset.apps/db-79b45689db 1 1 1 3h
replicaset.apps/db-7db5858d84 0 0 0 3h
replicaset.apps/web-6b56cf97b9 1 1 1 3h
replicaset.apps/words-6d654698d5 5 5 5 3h
Docker blog の翻訳記事だった。 December 4, 2018 割と最近。
docker-compose.yml
=> そのまま内部で Kubernetes
設定ファイルに変換して Kubernetes クラスタにコンテナ配置してるっぽい。
pod
service
etc ... に展開されてる。
この文脈で Kompose
という設定ファイルの変換ツールがあるっていうわけか。
https://kubernetes.io/docs/tasks/configure-pod-container/translate-compose-kubernetes/
Compose on Kubernetes by Docker Inc. Kompose by Kubernetes CNCF
Kompose のほうのドキュメント読む。
Use Kompose In just a few steps, we’ll take you from Docker Compose to Kubernetes. All you need is an existing docker-compose.yml file.
- Go to the directory containing your docker-compose.yml file. If you don’t have one, test using this one.
- Run the kompose up command to deploy to Kubernetes directly, or skip to the next step instead to generate a file to use with kubectl.
- To convert the docker-compose.yml file to files that you can use with kubectl, run kompose convert and then kubectl apply -f
- Access your application. If you’re already using minikube for your development process: Otherwise, let’s look up what IP your service is using! If you’re using a cloud provider, your IP will be listed next to LoadBalancer Ingress.
$ kompose up
でそのまま k8s にでぷろいできるらしい。
おなじことできるやーん。
http://localhost:33000/ おぉぉ。おぉ。 とりあえずk8sで動いてる何かにアクセスできた。 うごいている。
% docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf9c7144d5e3 dockersamples/k8s-wordsmith-api "java -Xmx8m -Xms8m …" 4 hours ago Up 4 hours k8s_words_words-6d654698d5-ssldw_default_a0342102-60c0-11e9-90a2-025000000001_0
21cae10a03a6 dockersamples/k8s-wordsmith-api "java -Xmx8m -Xms8m …" 4 hours ago Up 4 hours k8s_words_words-6d654698d5-kvx2k_default_a0342f61-60c0-11e9-90a2-025000000001_0
ad7a5c5f5663 dockersamples/k8s-wordsmith-api "java -Xmx8m -Xms8m …" 4 hours ago Up 4 hours k8s_words_words-6d654698d5-5dvcl_default_a038f9bf-60c0-11e9-90a2-025000000001_0
68963325f2ef dockersamples/k8s-wordsmith-api "java -Xmx8m -Xms8m …" 4 hours ago Up 4 hours k8s_words_words-6d654698d5-jbpvc_default_a0393b47-60c0-11e9-90a2-025000000001_0
44d01e89e49f dockersamples/k8s-wordsmith-db "docker-entrypoint.s…" 4 hours ago Up 4 hours k8s_db_db-79b45689db-p5bsh_default_a02c17eb-60c0-11e9-90a2-025000000001_0
90680d57f27b dockersamples/k8s-wordsmith-api "java -Xmx8m -Xms8m …" 4 hours ago Up 4 hours k8s_words_words-6d654698d5-k5ffg_default_a02f6edf-60c0-11e9-90a2-025000000001_0
e0497caa18e3 dockersamples/k8s-wordsmith-web "/dispatcher" 4 hours ago Up 4 hours k8s_web_web-6b56cf97b9-9kpwj_default_a026f294-60c0-11e9-90a2-025000000001_0
docker コンテナが動いてるのな。
% kubectl describe svc
Name: db
Namespace: default
Labels: com.docker.service.id=hellokube-db
com.docker.service.name=db
com.docker.stack.namespace=hellokube
Annotations: <none>
Selector: com.docker.service.id=hellokube-db,com.docker.service.name=db,com.docker.stack.namespace=hellokube
Type: ClusterIP
IP: None
Port: headless 55555/TCP
TargetPort: 55555/TCP
Endpoints: 10.1.0.8:55555
Session Affinity: None
Events: <none>
Name: kubernetes
Namespace: default
Labels: component=apiserver
provider=kubernetes
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.96.0.1
Port: https 443/TCP
TargetPort: 6443/TCP
Endpoints: 192.168.65.3:6443
Session Affinity: None
Events: <none>
...
docker stack
は docker-swarm
のこまんどぽい。
% docker stack
Usage: docker stack [OPTIONS] COMMAND
Manage Docker stacks
Options:
--orchestrator string Orchestrator to use (swarm|kubernetes|all)
Commands:
deploy Deploy a new stack or update an existing stack
ls List stacks
ps List the tasks in the stack
rm Remove one or more stacks
services List the services in the stack
Optionをちゃんとつけてやれば動く。 これがわからんかった。
% docker stack ls --orchestrator=kubernetes
NAME SERVICES ORCHESTRATOR NAMESPACE
hellokube 3 Kubernetes default
% docker stack ps --orchestrator=kubernetes hellokube
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
a02c17eb-60c hellokube_db-79b45689db-p5bsh dockersamples/k8s-wordsmith-db docker-for-desktop Running Running 21 hours ago
a026f294-60c hellokube_web-6b56cf97b9-9kpwj dockersamples/k8s-wordsmith-web docker-for-desktop Running Running 21 hours ago *:0->80/tcp
a038f9bf-60c hellokube_words-6d654698d5-5dvcl dockersamples/k8s-wordsmith-api docker-for-desktop Running Running 21 hours ago
a0393b47-60c hellokube_words-6d654698d5-jbpvc dockersamples/k8s-wordsmith-api docker-for-desktop Running Running 21 hours ago
a02f6edf-60c hellokube_words-6d654698d5-k5ffg dockersamples/k8s-wordsmith-api docker-for-desktop Running Running 21 hours ago
a0342f61-60c hellokube_words-6d654698d5-kvx2k dockersamples/k8s-wordsmith-api docker-for-desktop Running Running 21 hours ago
a0342102-60c hellokube_words-6d654698d5-ssldw dockersamples/k8s-wordsmith-api docker-for-desktop Running Running 21 hours ago
% docker stack services --orchestrator=kubernetes hellokube
ID NAME MODE REPLICAS IMAGE PORTS
a03132da-60c hellokube_db replicated 0/0 dockersamples/k8s-wordsmith-db
a03132da-60c hellokube_db replicated 0/0 mysql:8.0
a041e664-60c hellokube_web replicated 1/1 dockersamples/k8s-wordsmith-web *:33000->80/tcp
a04bdfa0-60c hellokube_words replicated 5/5 dockersamples/k8s-wordsmith-api
% docker stack rm --orchestrator=kubernetes hellokube
Removing stack: hellokube
とりあえず止められた。良かった。でも確かにこれはお手軽かもしれない。
とりあえずわからなさすぎたので本を買う。 https://amzn.to/2VqussF
買った。 1〜5章まで読んだ。 とりあえず Docker, Docker Swarm, Kubernetes の基本は押さえた。
まずは手元の docker-compose.yml
を docker stack deploy
できるようにする。
そういえば docker stack rm
の記述を追加してくれ、っていうPRを送ろう。
PR 送ってみた。
動かしてみる
% docker stack deploy -c docker-compose.yml --orchestrator=kubernetes disneykube
Ignoring unsupported options: build, restart
Ignoring deprecated options:
container_name: Setting the container name is not supported.
service "adminer": restart is ignored
service "dash": container_name is deprecated
service "dash": build is ignored
service "dash": depends_on are ignored
service "jupyter": build is ignored
service "jupyter": depends_on are ignored
service "db": container_name is deprecated
service "db": restart is ignored
Waiting for the stack to be stable and running...
adminer: Pending [pod status: 0/1 ready, 1/1 pending, 0/1 failed]
adminer: Ready [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
db: Ready [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
build
できないので、 dash
と jupyter
のコンテナイメージをビルドして DockerHub
にpushないといけないぽいけど、いったんレジストリをローカルに用意してみる。
% docker run --rm -it -v $(pwd)/docker-registry:/var/lib/registry -p 5000:5000 registry:latest
これでレジストリが動いた。
dash
用のコンテナイメージをビルドする
% docker image build --pull=true -f Dockerfile.dash -t dash_park:latest .
image名とタグはどうすっかな。
% docker image tag dash_park:latest park/dash:0.1.0
% docker image tag park/dash:0.1.0 park/dash:latest
park/dash
っていう名前にした。
localhost:5000
にpushするにはホスト名を含めたタグ付をする。
% docker image tag park/dash:latest localhost:5000/park/dash:latest
% docker image push localhost:5000/park/dash:latest
The push refers to repository [localhost:5000/park/dash]
The push refers to repository [localhost:5000/park/dash]
e272513162a5: Pushed
...
f94641f1fe1f: Pushed
latest: digest: sha256:931f5f1129e14f6c5f75a198635aa5c209d712ef2b4a2356f4decb06ed3dd79b size: 4119
latest: digest: sha256:931f5f1129e14f6c5f75a198635aa5c209d712ef2b4a2356f4decb06ed3dd79b size: 4119
できた。いえーい。俺すごい。
jupyterのimageも追加しよう。 buildの時点で localhost:5000 は付けておいた方が良さそうだね。
% docker build -f Dockerfile.jupyter -t localhost:5000/park/jupyternotebook:latest .
% docker image tag localhost:5000/park/jupyternotebook:latest localhost:5000/park/jupyternotebook:0.1.0
% docker image push localhost:5000/park/jupyternotebook:latest
% docker image push localhost:5000/park/jupyternotebook:0.1.0
dash
のimageをpushできたけど、それ自体にはソースコードがないので動かしたところでなんにもならんのでは?という事に気づいた。
% docker run --rm localhost:5000/park/dash
応答なしで終わり。
% docker run --rm -it localhost:5000/park/dash bash
root@3fc6730100ba:/work#
root@3fc6730100ba:/work# ls
root@3fc6730100ba:/work# ls -al
total 8
drwxr-xr-x 2 root root 4096 May 10 07:57 .
drwxr-xr-x 1 root root 4096 May 15 01:19 ..
root@3fc6730100ba:/work#
そりゃなにもない。
% docker run --rm -v $(pwd)/dash:/work localhost:5000/park/dash
いつお通りVolume指定してやれば動くはずだけど、うごかない。なぜ?
% docker run --rm -it -p 8050:8050 -v $(pwd)/dash:/work localhost:5000/park/dash python app1.py
コマンドを与えてやるとちゃんと起動できた。 Dockerfile
を確認すると、 CMD ["python", "/work/app.py"]
がコメントアウトされてたからこれ戻してもう一回ビルドし直さないと行けない。
いったん、DBに接続しないapp.pyをCOPYして実行するように変える。
いまは各 Dockerfile
を一つのディレクトリにまとめていたけどこれからはソースコード郡と一緒にして COPY
で含めるようにしないな。
開発の時はVolumeで上書きかな。
COPY failed: Forbidden path outside the build context: ../dash/app1.py ()
COPY は外のディレクトリには行けない
COPY obeys the following rules:
The
path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon. If
is a directory, the entire contents of the directory are copied, including filesystem metadata.
やはり Dockerfile
はソースコードほかと同じディレクトリ(Context)に置かないといけない仕様(思想?)で出来てるってことね。
% docker image build -t localhost:5000/park/dash:latest .
ビルドできた。
けどコンテナ起動したらファイルないのはなんで?
ああ。 -v $(pwd)/dash:/work
つけたままだった。馬鹿か。
Kompose kompose is a tool to help users familiar with docker-compose move to Kubernetes. It takes a Docker Compose file and translates it into Kubernetes resources.
docker-compose.yml をk8s設定ファイルに変換してくれるツールもあるらしい。