Getting Started with Python
検証環境
- macOS Catalina 10.15.4
- Docker for Mac Ver 2.3.0.2
cdk8s のインストール
$ 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
$ 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で行う
$ cdk8s synth
dist/hello.k8s.yaml
$ 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サービスの構成を部品化した場合の記述例
$ 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
参考