このページの内容は、minikubeで行っています(特に普通のkubernetesと差異はないはず?)。また、利用しているversionは以下の通りです。
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.2", GitCommit:"81753b10df112992bf51bbc2c2f85208aad78335", GitTreeState:"clean", BuildDate:"2018-04-27T09:22:21Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:44:10Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
設定について
Dockerを利用したアプリケーションを作成した際には、設定はどこに配置しますか?おそらく次の2つの内どちらかだと思います。
- 起動時に環境変数としてDockerに渡す
- 起動時にvolume追加して設定ファイルを配置する
Twelve-Factor App的には環境変数で渡す方が筋がよさそうですが、ConfigMapを使えば、どちらも簡単に実現できます。
ConfigMapの作成
ConfigMapの作成方法は以下の4つがあるようです。
- ディレクトリからConfigMap作成
- ファイルからConfigMap作成
- リテラルからConfigMap作成
- kubernetesのyamlファイルを直接作成
上の3つの方法ではkubectl create configmapを利用します。使い方は以下の通りです。
$ kubectl create configmap <map-name> <data-source>
- <map-name> configmapの名前
- <data-source> ディレクトリ・ファイル・リテラル
■ ディレクトリからConfigMap作成
設定ファイルって色々ありますよね。=形式、yaml形式、json形式などなど。
$ ls property/
app.properties default.yml develop.json
$ cat property/app.properties
classname=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/sampledb
username=testuser
password=testpassword
$ cat property/default.yml
mysql1:
master:
host: 'localhost'
port: 3306
dbName: 'testDb'
user: 'master'
password: 'password'
slave:
host: 'localhost'
port: 3306
dbName: 'testDb'
user: 'slave'
password: 'password'
$ cat property/develop.json
{
"server": {
"port": 3000
},
"db": {
"host": "localhost",
"user": "dbuser",
"password": "dbpassword",
"database": "dbname",
"timezone": "utc",
"dateStrings": "date"
},
"mongodb": {
"dsn": "mongodb://localhost:27017/dev-app"
},
"log": {
"appenders": [
{
"category": "access",
"type": "dateFile",
"filename": "/path/to/dev-app/logs/access.log",
"pattern": "-yyyy-MM-dd"
}, {
"category": "app",
"type": "dateFile",
"filename": "/path/to/dev-app/log/system.log",
"pattern": "-yyyy-MM-dd"
}, {
"category": "error",
"type": "dateFile",
"filename": "/path/to/dev-app/log/error.log",
"pattern": "-yyyy-MM-dd"
}, {
"type": "console"
}
]
}
}これらのpropertyディレクトリにあるファイルをすべてConfigMapとして作成にするには以下のコマンドを用います。
$ kubectl create configmap game-config --from-file=./property configmap "game-config" created # 削除はこちら $ kubectl delete configmap game-config configmap "game-config" deleted
createしたConfigMapは以下のコマンドで確認できます。
- kubectl get configma <map-name>
- kubectl describe configmap <map-name>
- kubectl get configmap <map-name> -o yaml
実際に今回createしたファイルは以下のように見えます。お好きなものをご利用ください。
ポイントとしては、「ファイル名がキー(Key)」:「ファイル内容が値(Value)」となってyaml形式で保存されているところです。
$ kubectl get configmap game-config
NAME DATA AGE
game-config 3 56s
#####################################################################
$ kubectl describe configmap game-config
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
app.properties:
----
classname=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/sampledb
username=testuser
password=testpassword
default.yml:
----
mysql1:
master:
host: 'localhost'
port: 3306
dbName: 'testDb'
user: 'master'
password: 'password'
slave:
host: 'localhost'
port: 3306
dbName: 'testDb'
user: 'slave'
password: 'password'
develop.json:
----
{
"server": {
"port": 3000
},
"db": {
"host": "localhost",
"user": "dbuser",
"password": "dbpassword",
"database": "dbname",
"timezone": "utc",
"dateStrings": "date"
},
"mongodb": {
"dsn": "mongodb://localhost:27017/dev-app"
},
"log": {
"appenders": [
{
"category": "access",
"type": "dateFile",
"filename": "/path/to/dev-app/logs/access.log",
"pattern": "-yyyy-MM-dd"
}, {
"category": "app",
"type": "dateFile",
"filename": "/path/to/dev-app/log/system.log",
"pattern": "-yyyy-MM-dd"
}, {
"category": "error",
"type": "dateFile",
"filename": "/path/to/dev-app/dev-app07/log/error.log",
"pattern": "-yyyy-MM-dd"
}, {
"type": "console"
}
]
}
}
#####################################################################
$ kubectl get configmap game-config -o yaml
apiVersion: v1
data:
app.properties: |
classname=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/sampledb
username=testuser
password=testpassword
default.yml: |
mysql1:
master:
host: 'localhost'
port: 3306
dbName: 'testDb'
user: 'master'
password: 'password'
slave:
host: 'localhost'
port: 3306
dbName: 'testDb'
user: 'slave'
password: 'password'
develop.json: "{\n \"server\": {\n \"port\": 3000\n },\n \"db\":
{\n \"host\": \"localhost\",\n \"user\": \"dbuser\",\n \"password\":
\"dbpassword\",\n \"database\": \"dbname\",\n \"timezone\": \"utc\",\n
\ \"dateStrings\": \"date\"\n },\n\n \"mongodb\": {\n \"dsn\":
\"mongodb://localhost:27017/dev-app\"\n },\n \n \"log\": {\n \"appenders\":
[\n {\n \"category\": \"access\",\n \"type\":
\"dateFile\",\n \"filename\": \"/path/to/dev-app/logs/access.log\",\n
\ \"pattern\": \"-yyyy-MM-dd\"\n }, {\n \"category\":
\"app\",\n \"type\": \"dateFile\",\n \"filename\":
\"/path/to/dev-app/log/system.log\",\n \"pattern\": \"-yyyy-MM-dd\"\n
\ }, {\n \"category\": \"error\",\n \"type\":
\"dateFile\",\n \"filename\": \"/path/to/dev-app/dev-app07/log/error.log\",\n
\ \"pattern\": \"-yyyy-MM-dd\"\n }, {\n \"type\":
\"console\"\n }\n ]\n }\n}\n"
kind: ConfigMap
metadata:
creationTimestamp: 2018-05-22T12:30:37Z
name: game-config
namespace: default
resourceVersion: "40807"
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: ee614638-5dbb-11e8-b633-080027936a1f
■ ファイルからConfigMap作成
ディレクトリの中身すべてのファイルををConfigMapに追加するときとほぼ同じです。コマンドは以下の通り
$ kubectl create configmap app-config --from-file=property/app.properties configmap "app-config" created # 削除するときはこちら $ kubectl delete configmap app-config configmap "app-config" deleted
作成したConfigMapの確認方法は同様なので省略しますが、describeで見てみると以下のようにファイル名がそのままキー名になっているのがわかります。
$ kubectl describe configmap app-config Name: app-config Namespace: default Labels: <none> Annotations: <none> Data ==== app.properties: ---- classname=com.mysql.jdbc.Driver url=jdbc:mysql://localhost/sampledb username=testuser password=testpassword Events: <none>
ファイル名をキー名としたくない場合、–from-file=<my-key-name>=<path-to-file> とすることで、作成時にキー名を指定できます。以下の例ではファイル名(app.properties)でなく指定した(game-app-config)がキー名になっていることがわかります。
$ kubectl delete configmap app-config configmap "app-config" deleted $ kubectl create configmap app-config --from-file=game-app-config=property/app.properties configmap "app-config" created $ kubectl describe configmap app-config Name: app-config Namespace: default Labels: <none> Annotations: <none> Data ==== game-app-config: ---- classname=com.mysql.jdbc.Driver url=jdbc:mysql://localhost/sampledb username=testuser password=testpassword Events: <none>
■ リテラルからConfigMap作成
キー名と値(key-value)の文字列を直接入力して、ConfigMapを作成します。作成コマンドは以下の通りです。key-valueのセットはいくらでも入力できます。
kubectl create configmap <map-name> --from-literal=<key1>=<value1> --from-literal=<key2>=<value2>
例えば以下のように作成できます。
$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm configmap "special-config" created # 以下のようにConfigMapが作成できている $ kubectl get configmaps special-config -o yaml apiVersion: v1 data: special.how: very special.type: charm kind: ConfigMap metadata: creationTimestamp: 2018-05-22T13:00:49Z name: special-config namespace: default resourceVersion: "42799" selfLink: /api/v1/namespaces/default/configmaps/special-config uid: 26526935-5dc0-11e8-b633-080027936a1f
■ kubernetesのyamlファイルを直接作成
ここまでの3つは、kubectl create configmapを利用していましたが、「yamlファイルを直接作成」では、kubectl create -f <yamlファイル>を利用して作成します。
$ cat config.yaml apiVersion: v1 kind: ConfigMap metadata: name: special-config data: special.how: very special.type: charm # 作成・および削除は-fオプションでyamlファイルを指定します。 $ kubectl create -f config.yaml configmap "special-config" created $ kubectl delete -f config.yaml configmap "special-config" deleted




コメント