9.2. ConfigMap プロパティーソースのチュートリアルの実行
以下のチュートリアルでは、Kubernetes の Secret および ConfigMap の設定を試すことができます。Kubernetes インテグレーションでの Spring Boot の有効化 の説明どおりに、Spring Cloud Kubernetes プラグインを有効にして、Kubernetes 設定オブジェクトを Spring Boot 外部化設定と統合します。
9.2.1. Spring Boot Camel Config クイックスタートの実行
以下のチュートリアルは、Kubernetes の Secret と ConfigMap の設定を可能にする spring-boot-camel-config-archetype
Maven archetype を基にしています。
手順
新しいシェルプロンプトを開き、以下の Maven コマンドを入力して簡単な Camel Spring Boot プロジェクトを作成します。
mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate \ -DarchetypeCatalog=https://maven.repository.redhat.com/ga/io/fabric8/archetypes/archetypes-catalog/2.2.0.fuse-740017-redhat-00003/archetypes-catalog-2.2.0.fuse-740017-redhat-00003-archetype-catalog.xml \ -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \ -DarchetypeArtifactId=spring-boot-camel-config-archetype \ -DarchetypeVersion=2.2.0.fuse-740017-redhat-00003
mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate \ -DarchetypeCatalog=https://maven.repository.redhat.com/ga/io/fabric8/archetypes/archetypes-catalog/2.2.0.fuse-740017-redhat-00003/archetypes-catalog-2.2.0.fuse-740017-redhat-00003-archetype-catalog.xml \ -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \ -DarchetypeArtifactId=spring-boot-camel-config-archetype \ -DarchetypeVersion=2.2.0.fuse-740017-redhat-00003
Copy to Clipboard Copied! archetype プラグインは対話モードに切り替わり、残りのフィールドを追加するよう要求します。
Define value for property 'groupId': : org.example.fis Define value for property 'artifactId': : fuse74-configmap Define value for property 'version': 1.0-SNAPSHOT: : Define value for property 'package': org.example.fis: : [INFO] Using property: spring-boot-version = 1.5.17.RELEASE Confirm properties configuration: groupId: org.example.fis artifactId: fuse74-configmap version: 1.0-SNAPSHOT package: org.example.fis spring-boot-version: 1.5.17.RELEASE Y: :
Define value for property 'groupId': : org.example.fis Define value for property 'artifactId': : fuse74-configmap Define value for property 'version': 1.0-SNAPSHOT: : Define value for property 'package': org.example.fis: : [INFO] Using property: spring-boot-version = 1.5.17.RELEASE Confirm properties configuration: groupId: org.example.fis artifactId: fuse74-configmap version: 1.0-SNAPSHOT package: org.example.fis spring-boot-version: 1.5.17.RELEASE Y: :
Copy to Clipboard Copied! プロンプトが表示されたら、
groupId
値にorg.example.fis
を入力し、artifactId
値にfuse74-configmap
を入力します。残りのフィールドにはデフォルト値を使用します。OpenShift にログインし、アプリケーションをデプロイする OpenShift プロジェクトに切り替えます。たとえば、
developer
ユーザーとしてログインし、test
プロジェクトにデプロイする場合は、以下のコマンドを入力します。oc login -u developer -p developer oc project test
oc login -u developer -p developer oc project test
Copy to Clipboard Copied! コマンドラインで、新しい
fuse74-configmap
プロジェクトのディレクトリーに変更し、このアプリケーションの Secret オブジェクトを作成します。oc create -f sample-secret.yml
oc create -f sample-secret.yml
Copy to Clipboard Copied! 注記アプリケーションをデプロイする 前 に Secret オブジェクトを作成する必要があります。そうでないと、Secret が利用できるようになるまでデプロイされたコンテナーが待機状態になります。続けて Secret を作成すると、コンテナーは待機状態ではなくなります。Secret オブジェクトの設定方法に関する詳細は Secret の設定 を参照してください。
クイックスタートアプリケーションをビルドおよびデプロイします。
fuse74-configmap
プロジェクトの最上位レベルから、次のように入力します。mvn fabric8:deploy -Popenshift
mvn fabric8:deploy -Popenshift
Copy to Clipboard Copied! 次のようにアプリケーションログを確認します。
-
ブラウザーで OpenShift コンソールを開き、該当するプロジェクト namespace を選択します (例:
test
)。 -
fuse74-configmap
サービスの円形 Pod アイコンの中央をクリックします。 - Pods ビューで Name Pod をクリックし、実行中の Pod の詳細を表示します (1 つの Pod のみが実行中である場合は直接詳細ページが表示されます)。
- Logs タグをクリックしてアプリケーションログを表示し、下方向にスクロールして Camel アプリケーションによって生成されたログメッセージを見つけます。
-
ブラウザーで OpenShift コンソールを開き、該当するプロジェクト namespace を選択します (例:
src/main/resources/application.properties
で設定されるデフォルトの受信者リストは、生成されたメッセージを 2 つのダミーエンドポイントであるdirect:async-queue
およびdirect:file
に送信します。これにより、以下のようなメッセージがアプリケーションログに書き込まれます。5:44:57.376 [Camel (camel) thread #0 - timer://order] INFO generate-order-route - Generating message message-44, sending to the recipient list 15:44:57.378 [Camel (camel) thread #0 - timer://order] INFO target-route-queue - ----> message-44 pushed to an async queue (simulation) 15:44:57.379 [Camel (camel) thread #0 - timer://order] INFO target-route-queue - ----> Using username 'myuser' for the async queue 15:44:57.380 [Camel (camel) thread #0 - timer://order] INFO target-route--file - ----> message-44 written to a file
5:44:57.376 [Camel (camel) thread #0 - timer://order] INFO generate-order-route - Generating message message-44, sending to the recipient list 15:44:57.378 [Camel (camel) thread #0 - timer://order] INFO target-route-queue - ----> message-44 pushed to an async queue (simulation) 15:44:57.379 [Camel (camel) thread #0 - timer://order] INFO target-route-queue - ----> Using username 'myuser' for the async queue 15:44:57.380 [Camel (camel) thread #0 - timer://order] INFO target-route--file - ----> message-44 written to a file
Copy to Clipboard Copied! ConfigMap オブジェクトを使用して
fuse74-configmap
アプリケーションの設定を更新する前に、OpenShift ApiServer からのデータを表示する権限をfuse74-configmap
アプリケーションに付与する必要があります。次のコマンドを入力して、fuse74-configmap
アプリケーションのサービスアカウントに表示
権限を付与します。oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config
oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config
Copy to Clipboard Copied! 注記構文
system:serviceaccount:PROJECT_NAME:SERVICE_ACCOUNT_NAME
を使用してサービスアカウントが指定されます。fis-config
デプロイメント記述子は、SERVICE_ACCOUNT_NAME
をqs-camel-config
に定義します。ライブリロード機能の動作を確認するには、以下のように ConfigMap オブジェクトを作成します。
oc create -f sample-configmap.yml
oc create -f sample-configmap.yml
Copy to Clipboard Copied! 新しい ConfigMap は、実行中のアプリケーションにある Camel ルートの受信者リストをオーバーライドし、生成されたメッセージを 3 つ のダミーエンドポイントである
direct:async-queue
、direct:file
、およびdirect:mail
に送信するよう設定します。ConfigMap オブジェクトの詳細は、ConfigMap の設定 を参照してください。これにより、以下のようなメッセージがアプリケーションログに書き込まれます。16:25:24.121 [Camel (camel) thread #0 - timer://order] INFO generate-order-route - Generating message message-9, sending to the recipient list 16:25:24.124 [Camel (camel) thread #0 - timer://order] INFO target-route-queue - ----> message-9 pushed to an async queue (simulation) 16:25:24.125 [Camel (camel) thread #0 - timer://order] INFO target-route-queue - ----> Using username 'myuser' for the async queue 16:25:24.125 [Camel (camel) thread #0 - timer://order] INFO target-route--file - ----> message-9 written to a file (simulation) 16:25:24.126 [Camel (camel) thread #0 - timer://order] INFO target-route--mail - ----> message-9 sent via mail
16:25:24.121 [Camel (camel) thread #0 - timer://order] INFO generate-order-route - Generating message message-9, sending to the recipient list 16:25:24.124 [Camel (camel) thread #0 - timer://order] INFO target-route-queue - ----> message-9 pushed to an async queue (simulation) 16:25:24.125 [Camel (camel) thread #0 - timer://order] INFO target-route-queue - ----> Using username 'myuser' for the async queue 16:25:24.125 [Camel (camel) thread #0 - timer://order] INFO target-route--file - ----> message-9 written to a file (simulation) 16:25:24.126 [Camel (camel) thread #0 - timer://order] INFO target-route--mail - ----> message-9 sent via mail
Copy to Clipboard Copied!
9.2.2. 設定プロパティー Bean
設定プロパティー Bean は、インジェクションによって設定を受け取ることができる標準の Java Bean です。Java コードと外部設定メカニズムの間の基本的なインターフェイスを提供します。
外部化設定および Bean レジストリー
以下のイメージは、spring-boot-camel-config
クイックスタートで Spring Boot の外部化設定がどのように動作するかを示しています。
設定メカニズムには以下の 2 つの主要部分があります。
- プロパティーソース
-
設定へのインジェクションのプロパティー設定を提供します。デフォルトのプロパティーソースはアプリケーションの
application.properties
ファイルで、任意で ConfigMap オブジェクトまたは Secret オブジェクトによるオーバーライドが可能です。 - 設定プロパティー Bean
-
プロパティーソースから設定の更新を受け取ります。設定プロパティー Bean は、
@Configuration
または@ConfigurationProperties
アノテーションが付けられた Java Bean です。 - Spring Bean レジストリー
- 必要なアノテーションが付けられた設定プロパティー Bean は、Spring Bean レジストリーに登録されます。
- Camel Bean レジストリー
- Camel Bean レジストリーは、自動的に Spring Bean レジストリーと統合されるため、登録された Spring Bean を Camel ルートで参照できます。
QuickstartConfiguration クラス
fuse74-configmap
プロジェクトの設定プロパティー Bean は、次のように QuickstartConfiguration
Java クラス (src/main/java/org/example/fis/
ディレクトリーの下) として定義されます。
package org.example.fis; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @Configuration @ConfigurationProperties(prefix = "quickstart") public class QuickstartConfiguration { /** * A comma-separated list of routes to use as recipients for messages. */ private String recipients; /** * The username to use when connecting to the async queue (simulation) */ private String queueUsername; /** * The password to use when connecting to the async queue (simulation) */ private String queuePassword; // Setters and Getters for Bean properties // NOT SHOWN ... }
package org.example.fis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "quickstart")
public class QuickstartConfiguration {
/**
* A comma-separated list of routes to use as recipients for messages.
*/
private String recipients;
/**
* The username to use when connecting to the async queue (simulation)
*/
private String queueUsername;
/**
* The password to use when connecting to the async queue (simulation)
*/
private String queuePassword;
// Setters and Getters for Bean properties
// NOT SHOWN
...
}
- 1
@Configuration
アノテーションによって、QuickstartConfiguration
クラスはインスタンス化され、quickstartConfiguration
を ID として持つ Bean として Spring に登録されます。これにより、Bean は自動的に Camel からアクセスできるようになります。たとえば、Camel 構文 ${bean:quickstartConfiguration?method=getQueueUsername}
を使用すると、target-route-queue
ルートはqueueUserName
プロパティーにアクセスできるようになります。- 2
@ConfigurationProperties
アノテーションは、プロパティーソースのプロパティー値を定義するときに使用する必要がある接頭辞quickstart
を定義します。たとえば、プロパティーファイルはrecipients
プロパティーをquickstart.recipients
として参照します。- 3
recipient
プロパティーはプロパティーソースからインジェクトできます。- 4
queueUsername
プロパティーはプロパティーソースからインジェクトできます。- 5
queuePassword
プロパティーはプロパティーソースからインジェクトできます。
9.2.3. Secret の設定
このクイックスタートの Kubernetes Secret は、必要な 1 つの追加ステップ以外は標準の方法でセットアップされています。追加ステップとして、Spring Cloud Kubernetes プラグインを Secret のマウントパスで設定し、起動時に Secret が読み取れるようにする必要があります。Secret を設定するには、以下を行います。
- サンプル Secret オブジェクトの作成
- Secret のボリュームマウントの設定
- Secret プロパティーを読み取るため spring-cloud-kubernetes を設定
サンプル Secret オブジェクト
クイックスタートプロジェクトによって、以下のようなサンプル Secret sample-secret.yml
が提供されます。Secret オブジェクトのプロパティー値は常に base64 でエンコードされます (base64
コマンドラインユーティリティーを使用)。Secret が Pod のファイルシステムにマウントされると、値は自動的に元のプレーンテキストにデコードされます。
sample-secret.yml ファイル
apiVersion: v1 kind: Secret metadata: name: camel-config type: Opaque data: # The username is 'myuser' quickstart.queue-username: bXl1c2VyCg== quickstart.queue-password: MWYyZDFlMmU2N2Rm
apiVersion: v1
kind: Secret
metadata:
name: camel-config
type: Opaque
data:
# The username is 'myuser'
quickstart.queue-username: bXl1c2VyCg==
quickstart.queue-password: MWYyZDFlMmU2N2Rm
- 1
- metadata.name: Secret を識別します。OpenShift システムの他の部分はこの識別子を使用して Secret を参照します。
- 2
- quickstart.queue-username:
quickstartConfiguration Bean
のqueueUsername
プロパティーにインジェクトすることを目的とします。値は base64 でエンコードする 必要 があります。 - 3
- quickstart.queue-password:
quickstartConfiguration
Bean のqueuePassword
プロパティーにインジェクトすることを目的とします。値は base64 でエンコードする 必要 があります。
Kubernetes では、プロパティー名を CamelCase で定義できません (プロパティー名をすべて小文字にする必要があります)。この制限を回避するには、ハイフンを使用した形式 queue-username
を使用します。Spring はこれを queueUsername
と一致します。これは、外部化設定に対して Spring Boot の リラックスバインディング ルールを利用します。
Secret のボリュームマウントの設定
Secret をボリュームマウントとして設定し、起動時に Secret がロードされるようにアプリケーションを設定する必要があります。アプリケーションの起動後、Secret プロパティーはファイルシステムの指定の場所で利用可能になります。アプリケーションの deployment.yml
ファイルは、Secret のボリュームマウントを定義する src/main/fabric8/
ディレクトリー下にあります。
deployment.yml ファイル
spec: template: spec: serviceAccountName: "qs-camel-config" volumes: - name: "camel-config" secret: # The secret must be created before deploying this application secretName: "camel-config" containers: - volumeMounts: - name: "camel-config" readOnly: true # Mount the secret where spring-cloud-kubernetes is configured to read it # see src/main/resources/bootstrap.yml mountPath: "/etc/secrets/camel-config" resources: # requests: # cpu: "0.2" # memory: 256Mi # limits: # cpu: "1.0" # memory: 256Mi env: - name: SPRING_APPLICATION_JSON value: '{"server":{"undertow":{"io-threads":1, "worker-threads":2 }}}'
spec:
template:
spec:
serviceAccountName: "qs-camel-config"
volumes:
- name: "camel-config"
secret:
# The secret must be created before deploying this application
secretName: "camel-config"
containers:
-
volumeMounts:
- name: "camel-config"
readOnly: true
# Mount the secret where spring-cloud-kubernetes is configured to read it
# see src/main/resources/bootstrap.yml
mountPath: "/etc/secrets/camel-config"
resources:
# requests:
# cpu: "0.2"
# memory: 256Mi
# limits:
# cpu: "1.0"
# memory: 256Mi
env:
- name: SPRING_APPLICATION_JSON
value: '{"server":{"undertow":{"io-threads":1, "worker-threads":2 }}}'
Secret プロパティーを読み取るため spring-cloud-kubernetes を設定
Secret を Spring Boot の外部化設定と統合するには、Spring Cloud Kubernetes プラグインを Secret のマウントパスで設定する必要があります。Spring Cloud Kubernetes は指定の場所から Secret を読み取り、Spring Boot はそれをプロパティーソースとして利用できるようになります。Spring Cloud Kubernetes プラグインを設定するには、クイックスタートプロジェクトの src/main/resources
下にある bootstrap.yml
ファイルを設定します。
bootstrap.yml ファイル
# Startup configuration of Spring-cloud-kubernetes spring: application: name: camel-config cloud: kubernetes: reload: # Enable live reload on ConfigMap change (disabled for Secrets by default) enabled: true secrets: paths: /etc/secrets/camel-config
# Startup configuration of Spring-cloud-kubernetes
spring:
application:
name: camel-config
cloud:
kubernetes:
reload:
# Enable live reload on ConfigMap change (disabled for Secrets by default)
enabled: true
secrets:
paths: /etc/secrets/camel-config
spring.cloud.kubernetes.secrets.paths
プロパティーは、Pod の Secret ボリュームマウントのパスリストを指定します。
bootstrap.properties
ファイル (または bootstrap.yml
ファイル) は、application.properties
ファイルと同様に動作しますが、アプリケーションの起動時により前の段階でロードされます。bootstrap.properties
ファイルの Spring Cloud Kubernetes プラグインに関連するプロパティーを設定した方が信頼性が高くなります。
9.2.4. ConfigMap の設定
Spring Cloud Kubernetes との統合には、ConfigMap オブジェクトの作成と適切な view パーミッションの設定の他に、ConfigMap の metadata.name
がプロジェクトの bootstrap.yml
ファイルに設定された spring.application.name
プロパティーの値と一致するようにする必要があります。ConfigMap を設定するには、以下を行います。
- サンプル ConfigMap オブジェクトの作成
- view パーミッションのセットアップ
- Spring Cloud Kubernetes プラグインの設定
サンプル ConfigMap オブジェクト
クイックスタートオブジェクトは、サンプル ConfigMap sample-configmap.yml
を提供します。
kind: ConfigMap apiVersion: v1 metadata: # Must match the 'spring.application.name' property of the application name: camel-config data: application.properties: | # Override the configuration properties here quickstart.recipients=direct:async-queue,direct:file,direct:mail
kind: ConfigMap
apiVersion: v1
metadata:
# Must match the 'spring.application.name' property of the application
name: camel-config
data:
application.properties: |
# Override the configuration properties here
quickstart.recipients=direct:async-queue,direct:file,direct:mail
view パーミッションの設定
Secret の deployment.yml ファイルで説明したとおり、serviceAccountName
はプロジェクトの deployment.yml
ファイルで qs-camel-config
に設定されます。そのため、以下のコマンドを実行して、クイックスタートアプリケーションで view
パーミッションを有効にします (test
プロジェクト namespace にデプロイされることを仮定します)。
oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config
oc policy add-role-to-user view system:serviceaccount:test:qs-camel-config
Spring Cloud Kubernetes プラグインの設定
Spring Cloud Kubernetes プラグインは、bootstrap.yml
ファイルの以下の設定によって指定されます。
- spring.application.name
-
この値は、ConfigMap オブジェクトの
metadata.name
と一致する必要があります (たとえば、クイックスタートのsample-configmap.yml
で定義された値)。デフォルトはapplication
です。 - spring.cloud.kubernetes.reload.enabled
-
これを
true
に設定すると、ConfigMap オブジェクトの動的リロードが有効になります。
サポートされるプロパティーに関する詳細は PropertySource リロード設定プロパティー を参照してください。