15
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kustomizeの基本的な使い方

Last updated at Posted at 2018-09-09

基本的な使いかた

Kustiomizeを使ってk8sのyamlを楽にしたいの続きです

0. kustomizeをinstallする

mac

$ brew install kustomize

それ以外

$ opsys=linux
$ curl -s https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest |\
  grep browser_download |\
  grep $opsys |\
  cut -d '"' -f 4 |\
  xargs curl -O -L
$ mv kustomize_*_${opsys}_amd64 kustomize
$ chmod u+x kustomize

1. ベースとなるフォルダを作成する

2. overlayするフォルダを作成する

フォルダー構造は下記のようになる

.
├── base
└── overlay

3. base以下にkustomization.yaml,service.yaml,pod.yamlを作成する

├── base
│   ├── kustomization.yaml
│   ├── pod.yaml
│   └── service.yaml
└── overlay

kustomization.yaml

resources:
  - pod.yaml
  - service.yaml

base/pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: kube
  labels:
    app: webphp
spec:
  containers:
    - name: nginx
      image: sasenomura/nginx:latest
      ports:
        - containerPort: 80

base/service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginxtest
spec:
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: webphp
  type: LoadBalancer

4. baseフォルダをkustomize buildする

baseでビルドできなければoverlayを編集してビルドすることはできない

$ kustomize build base/
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: webphp
  name: kube
spec:
  containers:
  - image: sasenomura/nginx:latest
    name: nginx
    ports:
    - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginxtest
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: webphp
  type: LoadBalancer

5. overlayのマニフェストを書く

ここではbaseに対してv1.1のパッチを書いていく

└── overlay
    ├── v1.1
    │   ├── kustomization.yaml
    │   └── pod.yaml

overlay/v1.1/pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: kube
spec:
  containers:
    - name: nginx
      image: sasenomura/nginx:1.1 # latestから1.1に変更する

overlay/v1.1/kustomization.yaml

bases:
  - ../../base #baseフォルダを指定する
patches:
  - ./pod.yaml #パッチとしてpod.yamlを指定する

6. overlayの方でkustomize buildする

$ kustomize build overlay/
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: webphp
  name: kube
spec:
  containers:
  - image: sasenomura/nginx:1.1 # 変更されている
    name: nginx
    ports:
    - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginxtest
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: webphp
  type: LoadBalancer

kustomizeで差分を見る

$ kustomize diff overlay/v1.1/
diff -u -N /var/folders/6c/n8tr0cq13b5bxx4dm3m5w_xh0000gn/T/noop-599200664/v1_Pod_kube.yaml /var/folders/6c/n8tr0cq13b5bxx4dm3m5w_xh0000gn/T/transformed-747587549/v1_Pod_kube.yaml
--- /var/folders/6c/n8tr0cq13b5bxx4dm3m5w_xh0000gn/T/noop-599200664/v1_Pod_kube.yaml	2018-09-06 11:37:44.000000000 +0900
+++ /var/folders/6c/n8tr0cq13b5bxx4dm3m5w_xh0000gn/T/transformed-747587549/v1_Pod_kube.yaml	2018-09-06 11:37:44.000000000 +0900
@@ -6,7 +6,7 @@
   name: kube
 spec:
   containers:
-  - image: sasenomura/nginx:latest
+  - image: sasenomura/nginx:1.1
     name: nginx
     ports:
     - containerPort: 80

imageが sasenomura/nginx:latest -> sasenomura/nginx:1.1に変更されていることがわかる

-  - image: sasenomura/nginx:latest
+  - image: sasenomura/nginx:1.1
15
8
1

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
15
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?