• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..05-Mar-2019-

README.mdH A D05-Mar-20194.2 KiB206159

deployment.yamlH A D05-Mar-2019747 3635

secret.yamlH A D05-Mar-2019156 109

service.yamlH A D05-Mar-2019137 1211

README.md

1# Demo: MySql
2
3This example takes some off-the-shelf k8s resources
4designed for MySQL, and customizes them to suit a
5production scenario.
6
7In the production environment we want:
8
9- MySQL resource names to be prefixed by 'prod-'.
10- MySQL resources to have 'env: prod' labels.
11- MySQL to use persistent disk for storing data.
12
13First make a place to work:
14<!-- @makeDemoHome @test -->
15```
16DEMO_HOME=$(mktemp -d)
17```
18
19### Download resources
20
21To keep this document shorter, the base resources
22needed to run MySql on a k8s cluster are off in a
23supplemental data directory rather than declared here
24as HERE documents.
25
26Download them:
27
28<!-- @downloadResources @test -->
29```
30curl -s  -o "$DEMO_HOME/#1.yaml" "https://raw.githubusercontent.com\
31/kubernetes-sigs/kustomize\
32/master/examples/mySql\
33/{deployment,secret,service}.yaml"
34```
35
36### Initialize kustomization.yaml
37
38The `kustomize` program gets its instructions from
39a file called `kustomization.yaml`.
40
41Start this file:
42
43<!-- @kustomizeYaml @test -->
44```
45touch $DEMO_HOME/kustomization.yaml
46```
47
48### Add the resources
49
50<!-- @addResources @test -->
51```
52cd $DEMO_HOME
53
54kustomize edit add resource secret.yaml
55kustomize edit add resource service.yaml
56kustomize edit add resource deployment.yaml
57
58cat kustomization.yaml
59```
60
61`kustomization.yaml`'s resources section should contain:
62
63> ```
64> resources:
65> - secret.yaml
66> - service.yaml
67> - deployment.yaml
68> ```
69
70### Name Customization
71
72Arrange for the MySQL resources to begin with prefix
73_prod-_ (since they are meant for the _production_
74environment):
75
76<!-- @customizeLabel @test -->
77```
78cd $DEMO_HOME
79
80kustomize edit set nameprefix 'prod-'
81
82cat kustomization.yaml
83```
84
85`kustomization.yaml` should have updated value of namePrefix field:
86
87> ```
88> namePrefix: prod-
89> ```
90
91This `namePrefix` directive adds _prod-_ to all
92resource names.
93
94<!-- @genNamePrefixConfig @test -->
95```
96kustomize build $DEMO_HOME
97```
98
99The output should contain:
100
101> ```
102> apiVersion: v1
103> data:
104>   password: YWRtaW4=
105> kind: Secret
106> metadata:
107>   ....
108>   name: prod-mysql-pass-d2gtcm2t2k
109> ---
110> apiVersion: v1
111> kind: Service
112> metadata:
113>   ....
114>   name: prod-mysql
115> spec:
116>   ....
117> ---
118> apiVersion: apps/v1beta2
119> kind: Deployment
120> metadata:
121>   ....
122>   name: prod-mysql
123> spec:
124>   selector:
125>     ....
126> ```
127
128### Label Customization
129
130We want resources in production environment to have
131certain labels so that we can query them by label
132selector.
133
134`kustomize` does not have `edit set label` command to add
135a label, but one can always edit `kustomization.yaml` directly:
136
137<!-- @customizeLabels @test -->
138```
139sed -i.bak 's/app: helloworld/app: prod/' \
140    $DEMO_HOME/kustomization.yaml
141```
142
143At this point, running `kustomize build` will
144generate MySQL configs with name-prefix 'prod-' and
145labels `env:prod`.
146
147### Storage customization
148
149Off the shelf MySQL uses `emptyDir` type volume, which
150gets wiped away if the MySQL Pod is recreated, and that
151is certainly not desirable for production
152environment. So we want to use Persistent Disk in
153production. kustomize lets you apply `patchesStrategicMerge` to the
154resources.
155
156<!-- @createPatchFile @test -->
157```
158cat <<'EOF' > $DEMO_HOME/persistent-disk.yaml
159apiVersion: apps/v1beta2 # for versions before 1.9.0 use apps/v1beta2
160kind: Deployment
161metadata:
162  name: mysql
163spec:
164  template:
165    spec:
166      volumes:
167      - name: mysql-persistent-storage
168        emptyDir: null
169        gcePersistentDisk:
170          pdName: mysql-persistent-storage
171EOF
172```
173
174Add the patch file to `kustomization.yaml`:
175
176<!-- @specifyPatch @test -->
177```
178cat <<'EOF' >> $DEMO_HOME/kustomization.yaml
179patchesStrategicMerge:
180- persistent-disk.yaml
181EOF
182```
183
184A `mysql-persistent-storage` persistent disk needs to exist for it to run successfully.
185
186Lets break this down:
187
188- In the first step, we created a YAML file named
189  `persistent-disk.yaml` to patch the resource defined
190  in deployment.yaml
191
192- Then we added `persistent-disk.yaml` to list of
193  `patchesStrategicMerge` in `kustomization.yaml`. `kustomize build`
194  will apply this patch to the deployment resource with
195  the name `mysql` as defined in the patch.
196
197
198The output of the following command can now be applied
199to the cluster (i.e. piped to `kubectl apply`) to
200create the production environment.
201
202<!-- @finalInflation @test -->
203```
204kustomize build $DEMO_HOME  # | kubectl apply -f -
205```
206