cdk8s / Python のGetting Started を試した

Getting Started with Python

検証環境

  • macOS Catalina 10.15.4
    • メモリ: 8GB
  • Docker for Mac Ver 2.3.0.2

cdk8s のインストール

  • Homebrew でのインストール実施
$ brew install cdk8s
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
...()
==> Installing dependencies for cdk8s: icu4c and node
==> Installing cdk8s dependency: icu4c
...()
==> node
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
  • cdk8s インストールの確認
$ brew list cdk8s
/usr/local/Cellar/cdk8s/0.21.0/bin/cdk8s
/usr/local/Cellar/cdk8s/0.21.0/libexec/bin/cdk8s
/usr/local/Cellar/cdk8s/0.21.0/libexec/lib/ (4450 files)

新しいプロジェクトの作成

  • 新しいフォルダを作成し移動
$ mkdir hello
$ cd hello
  • 初期化コマンドを実行(プロジェクトの作成)
    • 注意点:2020/5/16現在、cdk8s が対象とするPythonバージョンは3.7 です。私は、3.8 が動くようにしていたため、以下の初期化でエラーとなりました。pyenv でPython を3.7 に変更してから実行してください。
$ cdk8s init python-app
Initializing a project from the python-app template
Creating a virtualenv for this project…

✔ Successfully created virtual environment! 
✔ Installation Succeeded 
✔ Success! 
Updated Pipfile.lock (e6988c)!
Installing dependencies from Pipfile.lock (e6988c)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 9/9 — 00:00:02
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
========================================================================================================

 Your cdk8s Python project is ready!

   cat help      Prints this message  
   cdk8s synth   Synthesize k8s manifests to dist/
   cdk8s import  Imports k8s API objects to "imports/k8s"

  Deploy:
   kubectl apply -f dist/*.k8s.yaml

========================================================================================================

HelloWorld アプリの定義をPythonで行う

  • 参照ページ に記載されているPython でのチャートを初期化した際に生成されたmain.py に書き込む(置き換える)
  • 以下のコマンドを実行してk8s マニフェストを生成する
$ cdk8s synth
dist/hello.k8s.yaml

生成されたk8s マニフェストを使ってk8s へデプロイする

$ kubectl apply -f dist/hello.k8s.yaml
$ kubectl get all
NAME                                             READY   STATUS    RESTARTS   AGE
pod/hello-deployment-c51e9e6b-5f6d956ffd-ccvl9   1/1     Running   0          2m
pod/hello-deployment-c51e9e6b-5f6d956ffd-gdbwm   1/1     Running   0          2m

NAME                             TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/hello-service-9878228b   LoadBalancer   10.100.76.132   localhost     80:32271/TCP   2m
service/kubernetes               ClusterIP      10.96.0.1       <none>        443/TCP        145m

NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-deployment-c51e9e6b   2/2     2            2           2m

NAME                                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-deployment-c51e9e6b-5f6d956ffd   2         2         2       2m
  • Web ブラウザでアプリ起動を確認

f:id:san-tak:20200926191537p:plain

Webサービスの構成を部品化した場合の記述例

$ cdk8s synth
dist/hello.k8s.yaml

$ kubectl apply -f dist/hello.k8s.yaml
$ kubectl get all
NAME                                                   READY   STATUS              RESTARTS   AGE
pod/hello-ghost-deployment-f639d16c-65466bdfbc-7h4q6   0/1     ContainerCreating   0          58s
pod/hello-hello-deployment-83984d2d-75c65c967c-dz9rk   1/1     Running             0          58s
pod/hello-hello-deployment-83984d2d-75c65c967c-x2qsr   1/1     Running             0          58s

NAME                                   TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/hello-ghost-service-cbacde01   LoadBalancer   10.109.142.8     <pending>     80:31226/TCP   58s
service/hello-hello-service-cf7c398a   LoadBalancer   10.104.232.133   localhost     80:31210/TCP   58s
service/kubernetes                     ClusterIP      10.96.0.1        <none>        443/TCP        162m

NAME                                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-ghost-deployment-f639d16c   0/1     1            0           58s
deployment.apps/hello-hello-deployment-83984d2d   2/2     2            2           58s

NAME                                                         DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-ghost-deployment-f639d16c-65466bdfbc   1         1         0       58s
replicaset.apps/hello-hello-deployment-83984d2d-75c65c967c   2         2         2       58s

f:id:san-tak:20200926191426p:plain

参考