LoginSignup
2
0

More than 3 years have passed since last update.

configをk8sのディレクトリ構成に組み込んでみた

Last updated at Posted at 2020-03-06

はじめに

config以外の構成に関してはkubectl側で出しているDirectory Structureに則っています

configをどのように組み込むのか簡単に調査した所
kustomization.yamlと同じまたはその配下にディレクトリを掘ってconfigを管理する方法をよくみましたが、こちらは以下の点を満たすのが難しいと判断したため、別の方法を模索しました

  • 環境ごとに値が違うconfigと共通で使用したいconfigが存在する
  • ローカル環境のconfigが存在する
    • k8sにlocalディレクトリは存在しない(ローカルはdocker-compose)のでどこに置くのか?
  • アプリケーションエンジニアがk8sのディレクトリ構成を深く潜らなくても更新できるようにしたい

完成形

上記考慮点を解決する1つの案としてconfigsディレクトリをbasesoverlaysと同じ階層にした

ディレクトリ構成
.
├── bases
│   ├── aws-alb-ingress-controller
│   ├── aws-auth
│   ├── metrics-server
│   ├── a # microservice
│   └── b # microservice
├── configs
│   ├── a-nginx
│   │   ├── conf.d
│   │   │   ├── default.conf # 各環境共通
│   │   │   ├── dev.conf
│   │   │   ├── local.conf
│   │   │   └── prod.conf
│   │   └── nginx.conf # 各環境共通
│   ├── b-nginx
└── overlays
    ├── dev
    │   ├── ap-northeast
    │   │   ├── kustomization.yaml
    │   │   ├── patch-aws-alb-ingress-controller.yaml
    │   │   └── patch-aws-auth.yaml
    │   └── bases
    │       ├── kustomization.yaml
    │       ├── namespace.yaml
    │       ├── patch-a.yaml
    │       └── patch-b.yaml
    ├── prod
    └── staging

ちなみにkustomizetion.yamlはこんな感じ

overlays/dev/bases/kustomization.yaml
namespace: development

bases:
  - ../../../bases/a
  - ../../../bases/b

patchesStrategicMerge:
  - patch-a.yaml
  - patch-b.yaml

images:
  - name: nginx
    newTag: 1.15.2
  - name: a
    newTag: 0.0.1
  - name: b
    newTag: 0.0.1

configMapGenerator:
  - name: a-nginx-conf
    files:
      - ../../../configs/a-nginx/nginx.conf
      - ../../../configs/a-nginx/conf.d/default.conf
      - ../../../configs/a-nginx/conf.d/dev.conf
  - name: b-nginx-conf
    files:
      - ../../../configs/b-nginx/nginx.conf
      - ../../../configs/b-nginx/conf.d/default.conf
      - ../../../configs/b-nginx/conf.d/dev.conf

resources:
  - namespace.yaml

注意点

今回の構成だとkustomization.yamlのconfigMapGeneratorからconfig fileをそのまま参照できません

理由として以下です

v2.0 added a security check that prevents kustomizations from reading files outside their own directory root.

https://github.com/kubernetes-sigs/kustomize/blob/master/docs/FAQ.md#security-file-foo-is-not-in-or-below-bar

なので実行コマンドとしてはオプションとして--load_restrictor noneを付与する必要があります

実行コマンド
$ kustomize build --load_restrictor none overlays/dev/ap-northeast | kubectl apply -f -
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0