From b6efde13eb50f2560969258454fc535cad26706b Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Mon, 11 Jun 2018 10:24:22 -0700 Subject: [PATCH 1/2] Add README.md for wordpress to show variable reference usage --- examples/README.md | 2 + examples/wordpress/README.md | 145 +++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 examples/wordpress/README.md diff --git a/examples/README.md b/examples/README.md index d5f12fc64..33b08aa91 100644 --- a/examples/README.md +++ b/examples/README.md @@ -29,3 +29,5 @@ go get github.com/kubernetes-sigs/kustomize * [breakfast](breakfast.md) - Customize breakfast for Alice and Bob. + + * [wordpress](wordpress/README.md) - Create wordpress with mysql diff --git a/examples/wordpress/README.md b/examples/wordpress/README.md new file mode 100644 index 000000000..ebc4f1c98 --- /dev/null +++ b/examples/wordpress/README.md @@ -0,0 +1,145 @@ +# Demo: WordPress + +In this tutorial, you will learn - how to use `kustomize` to declare a variable reference and substitute it in container's command. + +In the WordPress example, we want + +- to connect WordPress with a MySQL database +- access the service name of MySQL database from WordPress container + +First make a place to work: + +``` +DEMO_HOME=$(mktemp -d) +MYSQL_HOME=$DEMO_HOME/mysql +mkdir -p $MYSQL_HOME +WORDPRESS_HOME=$DEMO_HOME/wordpress +mkdir -p $WORDPRESS_HOME +``` + +### Download resources + +Download the resources and `kustomization.yaml` for WordPress. + + +``` +CONTENT="https://raw.githubusercontent.com\ +/kubernetes-sigs/kustomize\ +/master/examples/wordpress/wordpress" + +curl -s -o "$WORDPRESS_HOME/#1.yaml" \ + "$CONTENT/{deployment,service,kustomization}.yaml" +``` + +Download the resources and `kustomization.yaml` for MySQL. + + +``` +CONTENT="https://raw.githubusercontent.com\ +/kubernetes-sigs/kustomize\ +/master/examples/wordpress/mysql" + +curl -s -o "$MYSQL_HOME/#1.yaml" \ + "$CONTENT/{deployment,service,secret,kustomization}.yaml" +``` + +### Create kustomization.yaml +We create a new kustomization, which has two bases: mysql and wordpress. + + +``` +cat <$DEMO_HOME/kustomization.yaml +bases: + - wordpress + - mysql +namePrefix: demo- +EOF +``` + +### Download patch for WordPress +In the new kustomization, we apply a patch for wordpress deployment. The patch does two things +- Add an initial container to show the mysql service name +- Add environment variable for mysql database so that wordpress can connect to it + + +``` +CONTENT="https://raw.githubusercontent.com\ +/kubernetes-sigs/kustomize\ +/master/examples/patch.yaml" + +curl -s -o "$DEMO_HOME/#1.yaml" \ + "$CONTENT/{patch}.yaml" +``` +The patch has following content +> ``` +> apiVersion: apps/v1beta2 +> kind: Deployment +> metadata: +> name: wordpress +> spec: +> template: +> spec: +> initContainers: +> - name: init-command +> image: debian +> command: +> - "echo $(WORDPRESS_SERVICE)" +> - "echo $(MYSQL_SERVICE)" +> containers: +> - name: wordpress +> env: +> - name: WORDPRESS_DB_HOST +> value: mysql +> - name: WORDPRESS_DB_PASSWORD +> valueFrom: +> secretKeyRef: +> name: mysql-pass +> key: password +> ``` +In the init container's command, there are two variables as `$(WORDPRESS_SERVICE)` and `$(MYSQL_SERVICE)`, which are for the service names of wordpress and mysql. If we define those variables in `kustomization.yaml`, they will be substituted in `kustomize build` to the correct names. + + +### Add Variable Reference +Let us define the variables needed in the init container's command. + + +``` +cat <>$DEMO_HOME/kustomization.yaml +vars: + - name: WORDPRESS_SERVICE + objref: + kind: Service + name: wordpress + apiVersion: v1 + fieldref: + fieldpath: metadata.name + - name: MYSQL_SERVICE + objref: + kind: Service + name: mysql + apiVersion: v1 +EOF +``` +`WORDPRESS_SERVICE` is from the field `metadata.name` of Service `wordpress`. If we don't specify `fieldref`, the default is `metadata.name`. So `MYSQL_SERVICE` is from the field `metadata.name` of Service `mysql`. + +### Substitution +We can see the substitution by `kustomize build` + + +``` +kustomize build $DEMO_HOME +``` + +We will see the init Container's command as + +> ``` +> (truncated) +> ... +> initContainers: +> - command: +> - echo demo-wordpress +> - echo demo-mysql +> image: debian +> name: init-command +> +> ``` \ No newline at end of file From ab0c9b41184efbcef8293645edd3495db23bbe84 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Mon, 11 Jun 2018 13:52:18 -0700 Subject: [PATCH 2/2] address comments --- examples/README.md | 2 +- examples/wordpress/README.md | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/examples/README.md b/examples/README.md index 33b08aa91..6ac0dc8ed 100644 --- a/examples/README.md +++ b/examples/README.md @@ -30,4 +30,4 @@ go get github.com/kubernetes-sigs/kustomize * [breakfast](breakfast.md) - Customize breakfast for Alice and Bob. - * [wordpress](wordpress/README.md) - Create wordpress with mysql + * [container args](wordpress/README.md) - Injecting k8s runtime data into container arguments (e.g. to point wordpress to a SQL service). diff --git a/examples/wordpress/README.md b/examples/wordpress/README.md index ebc4f1c98..1a438f33c 100644 --- a/examples/wordpress/README.md +++ b/examples/wordpress/README.md @@ -1,10 +1,10 @@ -# Demo: WordPress +# Demo: Injecting k8s runtime data into containers -In this tutorial, you will learn - how to use `kustomize` to declare a variable reference and substitute it in container's command. +In this tutorial, you will learn how to use `kustomize` to declare a variable reference and substitute it in container's command. -In the WordPress example, we want +To run WordPress, it's necessary to -- to connect WordPress with a MySQL database +- connect WordPress with a MySQL database - access the service name of MySQL database from WordPress container First make a place to work: @@ -44,7 +44,7 @@ curl -s -o "$MYSQL_HOME/#1.yaml" \ ``` ### Create kustomization.yaml -We create a new kustomization, which has two bases: mysql and wordpress. +Create a new kustomization with two bases: ``` @@ -57,9 +57,9 @@ EOF ``` ### Download patch for WordPress -In the new kustomization, we apply a patch for wordpress deployment. The patch does two things +In the new kustomization, apply a patch for wordpress deployment. The patch does two things - Add an initial container to show the mysql service name -- Add environment variable for mysql database so that wordpress can connect to it +- Add environment variable that allow wordpress to find the mysql database ``` @@ -96,11 +96,10 @@ The patch has following content > name: mysql-pass > key: password > ``` -In the init container's command, there are two variables as `$(WORDPRESS_SERVICE)` and `$(MYSQL_SERVICE)`, which are for the service names of wordpress and mysql. If we define those variables in `kustomization.yaml`, they will be substituted in `kustomize build` to the correct names. +The init container's command requires information that depends on k8s resource object fields, represented by the placeholder variables +$(WORDPRESS_SERVICE) and $(MYSQL_SERVICE). - -### Add Variable Reference -Let us define the variables needed in the init container's command. +### Bind the Variables to k8s Object Fields ``` @@ -123,14 +122,14 @@ EOF `WORDPRESS_SERVICE` is from the field `metadata.name` of Service `wordpress`. If we don't specify `fieldref`, the default is `metadata.name`. So `MYSQL_SERVICE` is from the field `metadata.name` of Service `mysql`. ### Substitution -We can see the substitution by `kustomize build` +Confirm the variable substitution: ``` kustomize build $DEMO_HOME ``` -We will see the init Container's command as +Expect this in the output: > ``` > (truncated)