| 1 | ## Configuration Basics |
| 2 | |
| 3 | ### Synopsis |
| 4 | |
| 5 | `kustomize cfg` provides tools for working with local configuration directories. |
| 6 | |
| 7 | First fetch a bundle of configuration to your local file system from the |
| 8 | Kubernetes examples repository. |
| 9 | |
| 10 | git clone https://github.com/kubernetes/examples/ |
| 11 | cd examples/ |
| 12 | |
| 13 | ### `tree` -- view Resources and directory structure |
| 14 | |
| 15 | `tree` can be used to summarize the collection of Resources in a directory: |
| 16 | |
| 17 | $ kustomize cfg tree mysql-wordpress-pd/ |
| 18 | mysql-wordpress-pd |
| 19 | ├── [gce-volumes.yaml] v1.PersistentVolume wordpress-pv-1 |
| 20 | ├── [gce-volumes.yaml] v1.PersistentVolume wordpress-pv-2 |
| 21 | ├── [local-volumes.yaml] v1.PersistentVolume local-pv-1 |
| 22 | ├── [local-volumes.yaml] v1.PersistentVolume local-pv-2 |
| 23 | ├── [mysql-deployment.yaml] v1.PersistentVolumeClaim mysql-pv-claim |
| 24 | ├── [mysql-deployment.yaml] apps/v1.Deployment wordpress-mysql |
| 25 | ├── [mysql-deployment.yaml] v1.Service wordpress-mysql |
| 26 | ├── [wordpress-deployment.yaml] apps/v1.Deployment wordpress |
| 27 | ├── [wordpress-deployment.yaml] v1.Service wordpress |
| 28 | └── [wordpress-deployment.yaml] v1.PersistentVolumeClaim wp-pv-claim |
| 29 | |
| 30 | `tree` may be provided flags to print the Resource field values. `tree` has a number of built-in |
| 31 | supported fields, and may also print arbitrary values using the `--field` flag to specify a field |
| 32 | path. |
| 33 | |
| 34 | $ kustomize cfg tree mysql-wordpress-pd/ --name --image --replicas --ports |
| 35 | mysql-wordpress-pd |
| 36 | ├── [gce-volumes.yaml] PersistentVolume wordpress-pv-1 |
| 37 | ├── [gce-volumes.yaml] PersistentVolume wordpress-pv-2 |
| 38 | ├── [local-volumes.yaml] PersistentVolume local-pv-1 |
| 39 | ├── [local-volumes.yaml] PersistentVolume local-pv-2 |
| 40 | ├── [mysql-deployment.yaml] PersistentVolumeClaim mysql-pv-claim |
| 41 | ├── [mysql-deployment.yaml] Deployment wordpress-mysql |
| 42 | │ └── spec.template.spec.containers |
| 43 | │ └── 0 |
| 44 | │ ├── name: mysql |
| 45 | │ ├── image: mysql:5.6 |
| 46 | │ └── ports: [{name: mysql, containerPort: 3306}] |
| 47 | ├── [mysql-deployment.yaml] Service wordpress-mysql |
| 48 | │ └── spec.ports: [{port: 3306}] |
| 49 | ├── [wordpress-deployment.yaml] Deployment wordpress |
| 50 | │ └── spec.template.spec.containers |
| 51 | │ └── 0 |
| 52 | │ ├── name: wordpress |
| 53 | │ ├── image: wordpress:4.8-apache |
| 54 | │ └── ports: [{name: wordpress, containerPort: 80}] |
| 55 | ├── [wordpress-deployment.yaml] Service wordpress |
| 56 | │ └── spec.ports: [{port: 80}] |
| 57 | └── [wordpress-deployment.yaml] PersistentVolumeClaim wp-pv-claim |
| 58 | |
| 59 | `tree` can also be used with `kubectl get` to print cluster Resources using OwnersReferences |
| 60 | to build the tree structure. |
| 61 | |
| 62 | kubectl apply -R -f cockroachdb/ |
| 63 | kubectl get all -o yaml | kustomize cfg tree --graph-structure owners --name --image --replicas |
| 64 | . |
| 65 | ├── [Resource] Deployment wp/wordpress |
| 66 | │ ├── spec.replicas: 1 |
| 67 | │ ├── spec.template.spec.containers |
| 68 | │ │ └── 0 |
| 69 | │ │ ├── name: wordpress |
| 70 | │ │ └── image: wordpress:4.8-apache |
| 71 | │ └── [Resource] ReplicaSet wp/wordpress-76b5d9f5c8 |
| 72 | │ ├── spec.replicas: 1 |
| 73 | │ ├── spec.template.spec.containers |
| 74 | │ │ └── 0 |
| 75 | │ │ ├── name: wordpress |
| 76 | │ │ └── image: wordpress:4.8-apache |
| 77 | │ └── [Resource] Pod wp/wordpress-76b5d9f5c8-g656w |
| 78 | │ └── spec.containers |
| 79 | │ └── 0 |
| 80 | │ ├── name: wordpress |
| 81 | │ └── image: wordpress:4.8-apache |
| 82 | ├── [Resource] Service wp/wordpress |
| 83 | ... |
| 84 | |
| 85 | ### `cat` -- view the full collection of Resources |
| 86 | |
| 87 | $ kustomize cfg cat mysql-wordpress-pd/ |
| 88 | apiVersion: v1 |
| 89 | kind: PersistentVolume |
| 90 | metadata: |
| 91 | name: wordpress-pv-1 |
| 92 | annotations: |
| 93 | config.kubernetes.io/path: gce-volumes.yaml |
| 94 | spec: |
| 95 | accessModes: |
| 96 | - ReadWriteOnce |
| 97 | capacity: |
| 98 | storage: 20Gi |
| 99 | gcePersistentDisk: |
| 100 | fsType: ext4 |
| 101 | pdName: wordpress-1 |
| 102 | --- |
| 103 | apiVersion: v1 |
| 104 | ... |
| 105 | |
| 106 | `cat` prints the raw package Resources. This may be used to pipe them to other tools |
| 107 | such as `kubectl apply -f -`. |
| 108 | |
| 109 | ## `fmt` -- format the Resources for a directory (like go fmt for Kubernetes Resources) |
| 110 | |
| 111 | `fmt` formats the Resource Configuration by applying a consistent style, including |
| 112 | ordering of fields and indentation. |
| 113 | |
| 114 | $ kustomize cfg fmt mysql-wordpress-pd/ |
| 115 | |
| 116 | Run `git diff` and see the changes that have been applied. |
| 117 | |
| 118 | ### `grep` -- search for Resources by field values |
| 119 | |
| 120 | `grep` prints Resources matching some field value. The Resources are annotated with their |
| 121 | file source so they can be piped to other commands without losing this information. |
| 122 | |
| 123 | $ kustomize cfg grep "metadata.name=wordpress" wordpress/ |
| 124 | apiVersion: v1 |
| 125 | kind: Service |
| 126 | metadata: |
| 127 | name: wordpress |
| 128 | labels: |
| 129 | app: wordpress |
| 130 | annotations: |
| 131 | config.kubernetes.io/path: wordpress-deployment.yaml |
| 132 | spec: |
| 133 | ports: |
| 134 | - port: 80 |
| 135 | selector: |
| 136 | app: wordpress |
| 137 | tier: frontend |
| 138 | type: LoadBalancer |
| 139 | --- |
| 140 | ... |
| 141 | |
| 142 | - list elements may be indexed by a field value using list[field=value] |
| 143 | - '.' as part of a key or value may be escaped as '\.' |
| 144 | |
| 145 | $ kustomize cfg grep "spec.status.spec.containers[name=nginx].image=mysql:5\.6" wordpress/ |
| 146 | apiVersion: apps/v1 # for k8s versions before 1.9.0 use apps/v1beta2 and before 1.8.0 use extensions/v1beta1 |
| 147 | kind: Deployment |
| 148 | metadata: |
| 149 | name: wordpress-mysql |
| 150 | labels: |
| 151 | app: wordpress |
| 152 | spec: |
| 153 | selector: |
| 154 | matchLabels: |
| 155 | app: wordpress |
| 156 | tier: mysql |
| 157 | template: |
| 158 | metadata: |
| 159 | labels: |
| 160 | app: wordpress |
| 161 | tier: mysql |
| 162 | ... |
| 163 | |
| 164 | `grep` may be used with kubectl to search for Resources in a cluster matching a value. |
| 165 | |
| 166 | kubectl get all -o yaml | kustomize cfg grep "spec.replicas>0" | kustomize cfg tree --replicas |
| 167 | . |
| 168 | └── |
| 169 | ├── [.] Deployment wp/wordpress |
| 170 | │ └── spec.replicas: 1 |
| 171 | ├── [.] ReplicaSet wp/wordpress-76b5d9f5c8 |
| 172 | │ └── spec.replicas: 1 |
| 173 | ├── [.] Deployment wp/wordpress-mysql |
| 174 | │ └── spec.replicas: 1 |
| 175 | └── [.] ReplicaSet wp/wordpress-mysql-f9447f458 |
| 176 | └── spec.replicas: 1 |
| 177 | |
| 178 | ### Error handling |
| 179 | |
| 180 | If there is an error parsing the Resource configuration, kustomize will print an error with the file. |
| 181 | |
| 182 | $ kustomize cfg grep "spec.template.spec.containers[name=\.*].resources.limits.cpu>1.0" ./staging/ | kustomize cfg tree --name --resources |
| 183 | Error: staging/persistent-volume-provisioning/quobyte/quobyte-admin-secret.yaml: [0]: yaml: unmarshal errors: |
| 184 | line 13: mapping key "type" already defined at line 9 |
| 185 | |
| 186 | Here the `staging/persistent-volume-provisioning/quobyte/quobyte-admin-secret.yaml` has a malformed |
| 187 | Resource. Remove the malformed Resources: |
| 188 | |
| 189 | rm staging/persistent-volume-provisioning/quobyte/quobyte-admin-secret.yaml |
| 190 | rm staging/storage/vitess/etcd-service-template.yaml |
| 191 | |
| 192 | When developing -- to get a stack trace for where an error was encountered, |
| 193 | use the `--stack-trace` flag: |
| 194 | |
| 195 | $ kustomize cfg grep "spec.template.spec.containers[name=\.*].resources.limits.cpu>1.0" ./staging/ --stack-trace |
| 196 | go/src/sigs.k8s.io/kustomize/kyaml/yaml/types.go:260 (0x4d35c86) |
| 197 | (*RNode).GetMeta: return m, errors.Wrap(err) |
| 198 | go/src/sigs.k8s.io/kustomize/kyaml/kio/byteio_reader.go:130 (0x4d3e099) |
| 199 | (*ByteReader).Read: meta, err := node.GetMeta() |
| 200 | ... |
| 201 | |
| 202 | |
| 203 | ### Combine `grep` and `tree` |
| 204 | |
| 205 | `grep` and `tree` may be combined to perform queries against configuration. |
| 206 | |
| 207 | Query for `replicas`: |
| 208 | |
| 209 | $ kustomize cfg grep "spec.replicas>5" ./ | kustomize cfg tree --replicas |
| 210 | . |
| 211 | ├── staging/sysdig-cloud |
| 212 | │ └── [sysdig-rc.yaml] ReplicationController sysdig-agent |
| 213 | │ └── spec.replicas: 100 |
| 214 | └── staging/volumes/vsphere |
| 215 | └── [simple-statefulset.yaml] StatefulSet web |
| 216 | └── spec.replicas: 14 |
| 217 | |
| 218 | Query for `resource.limits` |
| 219 | |
| 220 | $ kustomize cfg grep "spec.template.spec.containers[name=\.*].resources.limits.memory>0" ./ | kustomize cfg tree --resources |
| 221 | . |
| 222 | ├── cassandra |
| 223 | │ └── [cassandra-statefulset.yaml] StatefulSet cassandra |
| 224 | │ └── spec.template.spec.containers |
| 225 | │ └── 0 |
| 226 | │ └── resources: {limits: {cpu: "500m", memory: 1Gi}, requests: {cpu: "500m", memory: 1Gi}} |
| 227 | ├── staging/selenium |
| 228 | │ ├── [selenium-hub-deployment.yaml] Deployment selenium-hub |
| 229 | │ │ └── spec.template.spec.containers |
| 230 | │ │ └── 0 |
| 231 | │ │ └── resources: {limits: {memory: 1000Mi, cpu: ".5"}} |
| 232 | │ ├── [selenium-node-chrome-deployment.yaml] Deployment selenium-node-chrome |
| 233 | │ │ └── spec.template.spec.containers |
| 234 | │ │ └── 0 |
| 235 | │ │ └── resources: {limits: {memory: 1000Mi, cpu: ".5"}} |
| 236 | │ └── [selenium-node-firefox-deployment.yaml] Deployment selenium-node-firefox |
| 237 | │ └── spec.template.spec.containers |
| 238 | │ └── 0 |
| 239 | │ └── resources: {limits: {memory: 1000Mi, cpu: ".5"}} |
| 240 | ... |
| 241 | |
| 242 | ### Inverting `grep` |
| 243 | |
| 244 | The `grep` results may be inverted with the `-v` flag and used to find Resources that don't |
| 245 | match a query. |
| 246 | |
| 247 | Find Resources that have an image specified, but the image doesn't have a tag: |
| 248 | |
| 249 | $ kustomize cfg grep "spec.template.spec.containers[name=\.*].name=\.*" ./ | kustomize cfg grep "spec.template.spec.containers[name=\.*].image=\.*:\.*" -v | kustomize cfg tree --image --name |
| 250 | . |
| 251 | ├── staging/newrelic |
| 252 | │ ├── [newrelic-daemonset.yaml] DaemonSet newrelic-agent |
| 253 | │ │ └── spec.template.spec.containers |
| 254 | │ │ └── 0 |
| 255 | │ │ ├── name: newrelic |
| 256 | │ │ └── image: newrelic/nrsysmond |
| 257 | │ └── staging/newrelic-infrastructure |
| 258 | │ └── [newrelic-infra-daemonset.yaml] DaemonSet newrelic-infra-agent |
| 259 | │ └── spec.template.spec.containers |
| 260 | │ └── 0 |
| 261 | │ ├── name: newrelic |
| 262 | │ └── image: newrelic/infrastructure |
| 263 | ├── staging/nodesjs-mongodb |
| 264 | │ ├── [mongo-controller.yaml] ReplicationController mongo-controller |
| 265 | │ │ └── spec.template.spec.containers |
| 266 | │ │ └── 0 |
| 267 | │ │ ├── name: mongo |
| 268 | │ │ └── image: mongo |
| 269 | │ └── [web-controller.yaml] ReplicationController web-controller |
| 270 | │ └── spec.template.spec.containers |
| 271 | │ └── 0 |
| 272 | │ ├── name: web |
| 273 | │ └── image: <YOUR-CONTAINER> |
| 274 | ... |