Skip to content

Cloud Native Adapterの拡張

本章では、Qmonus Value Streamを使って、Cloud Native Adapterを編集し、ステージング環境にデプロイしたシンプルなAPIサーバの構成を変更する手順を解説します。

Getting StartedでのHTTP APIサーバの構築を前提とします。

以降のステップでは下記の作業を通じて、Cloud Native Adapterを拡張できることを確認していきます。

  • Cloud Native Adapter のComposite機能を用いて、Kubernetesのヘルスチェック機能であるreadinessProbe、livenessProbeを追加します。
  • CI/CDパイプラインを実行して新たにデリバリされるリソースを確認します。

1. Cloud Native Adapterの編集

本チュートリアルではGetting Startedのチュートリアル で作成した持ち込みのプライベートリポジトリと、getting-started ブランチをそのまま利用します。 ここでは、Cloud Native Adapterの特徴の一つである、Composite機能を用いて、Cloud Native AdapterにreadinessProbe、livenessProbeの機能を追加します。

以下のようにCloud Native Adapterを編集し、アプリケーションの構成を変更します。 まずはComposite元となるhealthcheck.cueを新たに作成します。

bash
# localディレクトリに移動
cd .valuestream/local

# Composite元となるhealthcheck.cueを作成する
vim healthcheck.cue
go
package healthcheck

DesignPattern: {
	name: "healthcheck"

	parameters: {
		port:      int
		probePath: string
	}

	resources: app: {
		deployment: _portConfig
	}

	_portConfig: spec: template: spec: containers: [{
		readinessProbe: {
			httpGet: {
				path: parameters.probePath
				port: parameters.port
			}
			timeoutSeconds:   5
			successThreshold: 2
			failureThreshold: 2
			periodSeconds:    10
		}
		livenessProbe: {
			httpGet: {
				path:   parameters.probePath
				port:   parameters.port
				scheme: "HTTP"
			}
			initialDelaySeconds: 60
			periodSeconds:       10
			successThreshold:    1
			timeoutSeconds:      5
		}
	}, ...]
}

前回チュートリアルで使用していたmain.cueを編集して、作成したhealthcheck.cueをCompositeします。

bash
# Composite機能を使ってDesgin Patternを編集する
vim main.cue # 以下のdiffのように変更する

2. Cloud Native Adapterの動作確認

本手順では、動作確認に利用するパラメータファイルとして params.json を作成します。

params.json

実際にQmonus Value Streamを通してCI/CDを実行する際は、AssemblyLineの動作の中でパラメータが自動的に渡されるため、params.jsonというファイルを意識する必要はありません。 今回はコンパイルが正しく動作することを確認するために、params.jsonというパラメータファイルを一時的に作成します。

bash
# .valuestream ディレクトリに移動
cd ../
# params.jsonを作成する(実際にはQmonus Value Streamの中で自動的に作成される)
vim params.json
json
{
    "params": [
        {
            "name": "k8sNamespace",
            "value": "dummyns"
        },
        {
            "name": "imageName",
            "value": "dummyimage"
        }
    ]
}

Cloud Native Adapterが正しくコンパイルできるかどうかを確認します。

bash
# qvsctlでApplication Manifestにコンパイル
qvsctl manifest compile -m . -c qvs.yaml -p params.json
less output/manifests.yml
YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-demo
  namespace: dummyns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-demo
  template:
    metadata:
      labels:
        app: nginx-demo
    spec:
      containers:
        - name: nginx-demo
          image: dummyimage
          ports:
            - containerPort: 80
          readinessProbe:
            httpGet:
              path: /
              port: 80
            timeoutSeconds: 5
            successThreshold: 2
            failureThreshold: 2
            periodSeconds: 10
          resources:
            requests:
              cpu: 5m
              memory: 32Mi
            limits:
              cpu: 10m
              memory: 64Mi
          livenessProbe:
            httpGet:
              path: /
              port: 80
              scheme: HTTP
            initialDelaySeconds: 60
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-demo
  namespace: dummyns
spec:
  type: LoadBalancer
  ports:
    - port: 8080
      name: http
      targetPort: 80
  selector:
    app: nginx-demo

git add、 git commit、 git pushを行い、リモートリポジトリに変更を反映します。

bash
# リモートリポジトリにpushする
git add local/healthcheck.cue local/main.cue
git commit -m "Cloud Native Adapter Extension QVS Tutorial"
git push origin getting-started
git log -n 1 # commit番号を控えておいて後の手順3で入力する

3. AssemblyLineの実行 (GUI)

Getting Startedで実施したのと同様に、登録したAssemblyLineを実行し、再度、Simple APIアプリケーションをKubernetesにデプロイします。

  1. 左メニューより、AssemblyLineを選択します。
  2. 画面中央に表示される staging-deploy を選択します。
  3. Pipelines (Stages) に表示されている deploy カードを選択し、全てのパラメータが埋まっていることを確認します。
  4. 以下のInput Parameterを入力し、 RUN ボタンを押下してAssemblyLineを実行します。
    • gitRevision: (手順2でのgit commit時のコミット番号)
    • imageName: nginx:latest

4. デプロイされたサンプルアプリケーションの確認 (CLI)

Kubernetesにアクセスして、デプロイに成功したアプリケーションを確認します。

  • Serviceリソースを確認することで、サンプルアプリケーションに割り当てられたグローバルIPアドレスを確認できます。
  • そのIPアドレスを指定して、HTTPリクエストを送ることで、サンプルアプリケーションのAPIを確認します。
  • describeコマンドを使用することで、追加したreadinessProbe、livenessProbeを確認することが出来ます。

以下の手順では、Kubeconfigをoutput.kubeconfig.yamlとしていますが、適宜ご利用しているKubeconfig名に読み替えて実行してください。

bash
# Podの確認 (Runningになっていることを確認)
kubectl --kubeconfig output.kubeconfig.yaml get pod

# 新たにCompositeされたreadinessProbe、livenessProbeを確認する
kubectl --kubeconfig output.kubeconfig.yaml describe pod nginx-demo

--略--
    Liveness:     http-get http://:80/ delay=60s timeout=5s period=10s #success=1 #failure=3
    Readiness:    http-get http://:80/ delay=0s  timeout=5s period=10s #success=2 #failure=2

AssemblyLine Resultsに表示されているIPアドレスへ自身のWEBブラウザからアクセスします。 http://${ipAddress}:8080 WEBブラウザ上からNginxのwelcomeページにアクセスできれば完了です。

リソースの削除

チュートリアルで作成したリソースは、Getting Startedで作成したQVS ConfigのInfrastructure Adapterの定義をコメントアウトし、リモートリポジトリに変更を反映した後に、再度AssemblyLineを実行することで削除できます。詳しくは、デプロイしたリソースの削除を参照してください。

解説

本チュートリアルではGetting Startedで使用したCloud Native Adapterを拡張して新たにreadinessProbe、livenessProbeによるヘルスチェック機能を追加しました。
このように、Qmonus Value Streamでは、Cloud Native AdapterのComposite機能を用いて、異なるCloud Native Adapterを再利用できます。また、Cloud Native Adapterを適切なサイズでモジュール化し、再利用することで、マニフェストを拡張できます。