なになれ

IT系のことを記録していきます

Kubernetes環境での開発のTips

Kubernetes環境で開発する場合のTipsを紹介します。

Kubernetes環境での開発について

Tipsを紹介する前にKubernetes環境での開発について説明します。
Kubernetes環境で開発したサービスを動かすには、実際に動かすプログラムの他にマニフェストファイルが必要になり、その成果物が正しいかを確認する必要があります。
正しいかを確認するためのポイントは以下になると思います。

  • サービスが動作するか(プログラムを確認する)
  • サービスがデプロイできるか(マニフェストファイルを確認する)

サービスが動作するかという観点について、Kubernetesの環境では複数のサービスが稼働していたり、ネットワーク環境が特別であったり、Kubernetes上で動作確認しないと気づけないことがあるので注意が必要です。
サービスがデプロイできるかという観点について、Kubernetesではデプロイするのにマニフェストファイルを記述することになります。
このマニフェストが正しくないとデプロイがうまく行われません。

Kubernetes上でプログラムを動かすにはコンテナイメージをビルドして、コンテナレジストリにPushして、Kubernetesマニフェストを更新してというように手順が多いです。
このような面倒な手順を行わずになるべく効率的に確認するためのTIpsです。

Tips

動作確認のTips

既に存在するKubernetes環境を使って、ローカルで開発したプログラムの動作確認を行います。
それを実現するのがTelepresenceというツールです。
www.telepresence.io

TelepresenceではKubernetes環境とローカルの環境をネットワーク的に接続して、一時的にローカルのプログラムをKubernetes上のリソースとして動作させることが可能になります。

以下のコマンドでローカルの環境とKubernetes環境とをつなぐDeploymentリソースが作られます。

$ telepresence --new-deployment helloworld --expose 3000

ローカルでプログラムを動かせば、Kubernetes環境上で動作していることと同等になります。
新しくリソースを作るだけでなく、既存のリソースをローカルのプログラムに置き換えることも可能です。
動作確認のためにデプロイする手間が省けます。

マニフェストを作るためのTips

kubectlを使う方法

なるべく間違いがないようにマニフェストをするために雛形を利用します。
kubectlを使うことでKubernetesのリソースの雛形を作成することができます。
-o yaml--dry-runを指定することがポイントです。

Deploymentリソースの雛形は以下のようにして確認できます。

$ kubectl run mydeploy --image nginx -o yaml --dry-run
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: mydeploy
  name: mydeploy
spec:
  replicas: 1
  selector:
    matchLabels:
      run: mydeploy
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: mydeploy
    spec:
      containers:
      - image: nginx
        name: mydeploy
        resources: {}
status: {}

Serviceリソースは以下のようにして確認できます。

$ kubectl create svc clusterip myapp --tcp 80 -o yaml --dry-run

また、kubectl explainで設定値を確認できます。

$ kubectl explain deployment.spec.template.spec

このようなコマンドを活用すれば、マニフェスト作成が楽になります。

komposeを使う方法

既にdocker-composeを使っているなら、komposeが使えます。

kompose.io

kompose convertコマンドを使用するとdocker-composeのyamlkubernetesyamlに変換できます。

docker-compose.yaml

version: '3.7'
services:
  webapp:
    image: hi1280/node-helloworld:0.0.1
    ports:
      - 3000:3000
    environment:
      - NODE_ENV=production

docker-compose.yamlが存在するディレクトリ上で以下のようにコマンドを実行します。

$ kompose convert
INFO Kubernetes file "webapp-service.yaml" created
INFO Kubernetes file "webapp-deployment.yaml" created

Kubernetes用のマニフェストファイルが作成されます。

webapp-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.20.0 ()
  creationTimestamp: null
  labels:
    io.kompose.service: webapp
  name: webapp
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert
        kompose.version: 1.20.0 ()
      creationTimestamp: null
      labels:
        io.kompose.service: webapp
    spec:
      containers:
      - env:
        - name: NODE_ENV
          value: production
        image: hi1280/node-helloworld:0.0.1
        name: webapp
        ports:
        - containerPort: 3000
        resources: {}
      restartPolicy: Always
status: {}

webapp-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.20.0 ()
  creationTimestamp: null
  labels:
    io.kompose.service: webapp
  name: webapp
spec:
  ports:
  - name: "3000"
    port: 3000
    targetPort: 3000
  selector:
    io.kompose.service: webapp
status:
  loadBalancer: {}

マニフェストを確認するためのTips

--dry-runオプションでマニフェストを適用することなく、コマンドを仮に実行することでマニフェストファイルに問題がないか確認できます。

$ kubectl apply -f k8s.yaml --dry-run

問題があればエラーで問題箇所を教えてくれます。

error: error parsing k8s.yaml: error converting YAML to JSON: yaml: line 17: mapping values are not allowed in this context

まとめ

Kubernetesでの開発は手間がかかるので、出来るだけ効率化する必要があります。
Telepresenceや今回紹介したマニフェストの作り方、確認方法が役立つと思います。