masayuki14 / worklog

Record working log by issues.
MIT License
0 stars 0 forks source link

compose on kubernetes #18

Closed masayuki14 closed 3 years ago

masayuki14 commented 5 years ago

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設定ファイルに変換してくれるツールもあるらしい。

masayuki14 commented 5 years ago

Compose on Kubernetes: Get startred

masayuki14 commented 5 years ago

Dockerの設定ぱねるから Kubernetes タブの Enable Kuberenetes にチェックを入れて Apply するとインストールが始まる。 しばらく時間がかかる。

スクリーンショット 2019-04-17 12 19 33

Footerの Kubernetes is running がグリーンになる。

masayuki14 commented 5 years ago

kubectl コマンドが使えるようになり、動作を確認できる。

% kubectl api-versions | grep compose
compose.docker.com/v1beta1
compose.docker.com/v1beta2
masayuki14 commented 5 years ago

とりあえずドキュメントどおりに 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
masayuki14 commented 5 years ago
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]

コンソールはこうなった。

masayuki14 commented 5 years ago

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
masayuki14 commented 5 years ago

https://speakerdeck.com/kkoudev/introduction-to-kubernetes-for-docker-compose-user 途中まで読んだけど、まだちょっと早い。

masayuki14 commented 5 years ago

やっぱり公式読む

https://github.com/docker/compose-on-kubernetes/tree/master/docs

ドキュメントは /docs ディレクトリにある。

masayuki14 commented 5 years ago

とりあえず1つずつ読むか。

masayuki14 commented 5 years ago

docker stack コマンドの意味がわからん。どうやって止めるんや?

masayuki14 commented 5 years ago

まずアーキテクチャ https://github.com/docker/compose-on-kubernetes/blob/master/docs/architecture.md

masayuki14 commented 5 years ago

うーむ。わからん。

masayuki14 commented 5 years ago

これ普通に本を買って読んで動かしながらやったほうがいいのかな。

masayuki14 commented 5 years ago

これも読んでみる。 https://www.creationline.com/lab/25164

masayuki14 commented 5 years ago

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
masayuki14 commented 5 years ago

Docker blog の翻訳記事だった。 December 4, 2018 割と最近。

masayuki14 commented 5 years ago

docker-compose.yml => そのまま内部で Kubernetes 設定ファイルに変換して Kubernetes クラスタにコンテナ配置してるっぽい。 pod service etc ... に展開されてる。

masayuki14 commented 5 years ago

この文脈で Kompose という設定ファイルの変換ツールがあるっていうわけか。 https://kubernetes.io/docs/tasks/configure-pod-container/translate-compose-kubernetes/

masayuki14 commented 5 years ago

Compose on Kubernetes by Docker Inc. Kompose by Kubernetes CNCF

masayuki14 commented 5 years ago

Kompose のほうのドキュメント読む。

masayuki14 commented 5 years ago

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.

  1. Go to the directory containing your docker-compose.yml file. If you don’t have one, test using this one.
  2. 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.
  3. To convert the docker-compose.yml file to files that you can use with kubectl, run kompose convert and then kubectl apply -f . Your deployments are running in Kubernetes.
  4. 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.
masayuki14 commented 5 years ago

$ kompose up でそのまま k8s にでぷろいできるらしい。 おなじことできるやーん。

masayuki14 commented 5 years ago

http://localhost:33000/ おぉぉ。おぉ。 とりあえずk8sで動いてる何かにアクセスできた。 うごいている。

masayuki14 commented 5 years ago
% 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 コンテナが動いてるのな。

masayuki14 commented 5 years ago
 % 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>

...
masayuki14 commented 5 years ago

docker stackdocker-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
masayuki14 commented 5 years ago

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
masayuki14 commented 5 years ago
% docker stack rm --orchestrator=kubernetes hellokube
Removing stack: hellokube

とりあえず止められた。良かった。でも確かにこれはお手軽かもしれない。

masayuki14 commented 5 years ago

とりあえずわからなさすぎたので本を買う。 https://amzn.to/2VqussF

masayuki14 commented 5 years ago

買った。 1〜5章まで読んだ。 とりあえず Docker, Docker Swarm, Kubernetes の基本は押さえた。

masayuki14 commented 5 years ago

まずは手元の docker-compose.ymldocker stack deploy できるようにする。

masayuki14 commented 5 years ago

そういえば docker stack rm の記述を追加してくれ、っていうPRを送ろう。

masayuki14 commented 5 years ago

PR 送ってみた。

masayuki14 commented 5 years ago

動かしてみる

% 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]
masayuki14 commented 5 years ago

build できないので、 dashjupyter のコンテナイメージをビルドして DockerHub にpushないといけないぽいけど、いったんレジストリをローカルに用意してみる。

masayuki14 commented 5 years ago
% docker run --rm -it -v $(pwd)/docker-registry:/var/lib/registry -p 5000:5000 registry:latest 

これでレジストリが動いた。

masayuki14 commented 5 years ago

dash 用のコンテナイメージをビルドする

% docker image build --pull=true  -f Dockerfile.dash -t dash_park:latest .

image名とタグはどうすっかな。

masayuki14 commented 5 years ago
% docker image tag dash_park:latest park/dash:0.1.0  
% docker image tag park/dash:0.1.0 park/dash:latest   

park/dash っていう名前にした。

masayuki14 commented 5 years ago

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
masayuki14 commented 5 years ago

できた。いえーい。俺すごい。

masayuki14 commented 5 years ago

jupyterのimageも追加しよう。 buildの時点で localhost:5000 は付けておいた方が良さそうだね。

masayuki14 commented 5 years ago
% 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
masayuki14 commented 5 years ago

dash のimageをpushできたけど、それ自体にはソースコードがないので動かしたところでなんにもならんのでは?という事に気づいた。

masayuki14 commented 5 years ago

% 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#

そりゃなにもない。

masayuki14 commented 5 years ago

% 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して実行するように変える。

masayuki14 commented 5 years ago

いまは各 Dockerfile を一つのディレクトリにまとめていたけどこれからはソースコード郡と一緒にして COPY で含めるようにしないな。 開発の時はVolumeで上書きかな。

masayuki14 commented 5 years ago

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)に置かないといけない仕様(思想?)で出来てるってことね。

masayuki14 commented 5 years ago

% docker image build -t localhost:5000/park/dash:latest . ビルドできた。 けどコンテナ起動したらファイルないのはなんで?

masayuki14 commented 5 years ago

ああ。 -v $(pwd)/dash:/work つけたままだった。馬鹿か。