From 082448d4af5f7ec90a0c6d56b6b95493092c8819 Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Tue, 1 May 2018 15:01:28 -0700 Subject: [PATCH] Modify mysql and springboot demo to download their data from the code repo, not the kinflate repo --- demos/mySql.md | 31 +++++----- demos/springboot.md | 126 +++++++++++++++++----------------------- docs/kustomization.yaml | 2 +- 3 files changed, 72 insertions(+), 87 deletions(-) diff --git a/demos/mySql.md b/demos/mySql.md index 62c3dfef16..e9504a054a 100644 --- a/demos/mySql.md +++ b/demos/mySql.md @@ -10,25 +10,28 @@ In the production environment we want: - MySQL resources to have 'env: prod' labels. - MySQL to use persistent disk for storing data. -### Download resources - -Download `deployment.yaml`, `service.yaml` and -`secret.yaml`. These are plain k8s resources files one -could add to a k8s cluster to run MySql. - - +First make a place to work: + ``` DEMO_HOME=$(mktemp -d) -cd $DEMO_HOME +``` -CONTENT=https://raw.githubusercontent.com/kinflate +### Download resources -# Get MySQL configs -for f in service secret deployment ; do \ - wget -q $CONTENT/mysql/master/emptyDir/$f.yaml ; \ -done -``` +To keep this document shorter, the base resources +needed to run MySql on a k8s cluster are off in a +supplemental data directory rather than declared here +as HERE documents. + +Download them: + +``` +curl -s -o "$DEMO_HOME/#1.yaml" \ + "https://raw.githubusercontent.com/kubernetes/kubectl\ +/master/cmd/kustomize/demos/data/mySql\ +/{deployment,secret,service}.yaml" +``` ### Initialize kustomization.yaml diff --git a/demos/springboot.md b/demos/springboot.md index 15bdc6cf9f..cac5fc2564 100644 --- a/demos/springboot.md +++ b/demos/springboot.md @@ -12,23 +12,28 @@ In the production environment we want to customize the following: - JVM memory to be properly set. - health check and readiness check. +First make a place to work: + +``` +DEMO_HOME=$(mktemp -d) +``` + ### Download resources -Download `deployment.yaml`, `service.yaml`. These are plain k8s resources files one -could add to a k8s cluster to run sbdemo. +To keep this document shorter, the base resources +needed to run springboot on a k8s cluster are off in a +supplemental data directory rather than declared here +as HERE documents. - -``` +Download them: -DEMO_HOME=$(mktemp -d) -cd $DEMO_HOME - -CONTENT=https://raw.githubusercontent.com/kinflate + +``` +CONTENT="https://raw.githubusercontent.com/kubernetes/kubectl\ +/master/cmd/kustomize/demos/data/springboot" -# Get SpringBoot configs -for f in service deployment; do \ - wget -q $CONTENT/example-springboot/master/$f.yaml ; \ -done +curl -s -o "$DEMO_HOME/#1.yaml" \ + "$CONTENT/base/{deployment,service}.yaml" ``` ### Initialize kustomization.yaml @@ -63,13 +68,14 @@ cat kustomization.yaml > - deployment.yaml > ``` -### Add configmap generator +### Add configMap generator ``` -cd $DEMO_HOME -wget -q $CONTENT/example-springboot/master/application.properties -kustomize edit add configmap demo-configmap --from-file application.properties +echo "app.name=Kustomize Demo" >$DEMO_HOME/application.properties + +kustomize edit add configmap demo-configmap \ + --from-file application.properties cat kustomization.yaml ``` @@ -83,7 +89,8 @@ cat kustomization.yaml > name: demo-configmap > ``` -### Customize configmap +### Customize configMap + We want to add database credentials for the prod environment. In general, these credentials can be put into the file `application.properties`. However, for some cases, we want to keep the credentials in a different file and keep application specific configs in `application.properties`. With this clear separation, the credentials and application specific things can be managed and maintained flexibly by different teams. @@ -91,8 +98,8 @@ For example, application developers only tune the application configs in `applic only care about the credentials. For Spring Boot application, we can set an active profile through the environment variable `spring.profiles.active`. Then -the application will pick up an extra `application-.properties` file. With this, we can customize the configmap in two -steps. Add an environment variable through the patch and add a file to the configmap. +the application will pick up an extra `application-.properties` file. With this, we can customize the configMap in two +steps. Add an environment variable through the patch and add a file to the configMap. ``` @@ -120,7 +127,8 @@ spring.datasource.username=root spring.datasource.password=admin EOF -kustomize edit add configmap demo-configmap --from-file application-prod.properties +kustomize edit add configmap \ + demo-configmap --from-file application-prod.properties cat kustomization.yaml ``` @@ -143,78 +151,52 @@ environment): ``` cd $DEMO_HOME - kustomize edit set nameprefix 'prod-' - -cat kustomization.yaml ``` `kustomization.yaml` should have updated value of namePrefix field: > ``` > namePrefix: prod- -> commonAnnotations: -> note: This is a example annotation > ``` This `namePrefix` directive adds _prod-_ to all -resource names. +resource names, as can be seen by building the +resources: - + ``` -kustomize build $DEMO_HOME +kustomize build $DEMO_HOME | grep prod- ``` -The output should contain: - -> ``` -> apiVersion: v1 -> data: -> .... -> kind: ConfigMap -> metadata: -> .... -> name: prod-demo-configmap-7746248cmc -> --- -> apiVersion: v1 -> kind: Service -> metadata: -> .... -> name: prod-sbdemo -> spec: -> .... -> --- -> apiVersion: apps/v1beta2 -> kind: Deployment -> metadata: -> .... -> name: prod-sbdemo -> spec: -> selector: -> .... -> ``` - ### Label Customization We want resources in production environment to have certain labels so that we can query them by label selector. -`kustomize` does not have `edit set label` command to add -a label, but one can always edit `kustomization.yaml` directly: +`kustomize` does not have `edit set label` command to +add a label, but one can always edit +`kustomization.yaml` directly: ``` -sed -i 's/app: helloworld/app: prod/' \ - $DEMO_HOME/kustomization.yaml +cat <>$DEMO_HOME/kustomization.yaml +commonLabels: + env: prod +EOF ``` -At this point, running `kustomize build` will -generate MySQL configs with name-prefix 'prod-' and -labels `env:prod`. +Confirm that the resources now all have names prefixed +by `prod-` and the label tuple `env:prod`: + +``` +kustomize build $DEMO_HOME | grep -C 3 env +``` ### Download Patch for JVM memory + When a Spring Boot application is deployed in a k8s cluster, the JVM is running inside a container. We want to set memory limit for the container and make sure the JVM is aware of that limit. In K8s deployment, we can set the resource limits for containers and inject these limits to some environment variables by downward API. When the container starts to run, it can pick up the environment variables and @@ -224,10 +206,10 @@ Download the patch `memorylimit_patch.yaml`. It contains the memory limits setup ``` -cd $DEMO_HOME -wget -q $CONTENT/example-springboot-instances/master/production/memorylimit_patch.yaml +curl -s -o "$DEMO_HOME/#1.yaml" \ + "$CONTENT/overlays/production/{memorylimit_patch}.yaml" -cat memorylimit_patch.yaml +cat $DEMO_HOME/memorylimit_patch.yaml ``` The output contains @@ -262,10 +244,10 @@ Download the patch `healthcheck_patch.yaml`. It contains the liveness probes and ``` -cd $DEMO_HOME -wget -q $CONTENT/example-springboot-instances/master/production/healthcheck_patch.yaml +curl -s -o "$DEMO_HOME/#1.yaml" \ + "$CONTENT/overlays/production/{healthcheck_patch}.yaml" -cat healthcheck_patch.yaml +cat $DEMO_HOME/healthcheck_patch.yaml ``` The output contains @@ -296,11 +278,11 @@ The output contains ### Add patches -Currently `kustomize` doesn't provide a command to add a file as a patch, but we can edit the file `kustomization.yaml` to -include this patch. +Add these patches to the kustomization: ``` +cd $DEMO_HOME kustomize edit add patch memorylimit_patch.yaml kustomize edit add patch healthcheck_patch.yaml ``` diff --git a/docs/kustomization.yaml b/docs/kustomization.yaml index 1caee88ba9..b0ad1c1b5c 100644 --- a/docs/kustomization.yaml +++ b/docs/kustomization.yaml @@ -1,5 +1,5 @@ # ---------------------------------------------------- -# Example kustomize.yaml content. +# Example kustomization.yaml content. # # This file declares the customization provided by # the kustomize program.