Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit for a tutorial on getting started with kustomize #4516

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
c5ef8b6
Initial commit for a tutorial on getting started with kustomize
rob8714 Mar 12, 2022
fdcc8a6
add build commands and update comment with name of the file
rob8714 Mar 13, 2022
74806dd
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 13, 2022
2ab44cb
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 13, 2022
3dd35a0
fix typo
rob8714 Mar 13, 2022
2fb4e4d
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 16, 2022
0d361a6
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 16, 2022
c5da901
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 16, 2022
8c79c23
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 16, 2022
c28e7b6
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 16, 2022
6d51ced
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 16, 2022
3e4b83c
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 16, 2022
8499d85
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 17, 2022
3e4bd8c
updates following review
rob8714 Mar 17, 2022
4fb6159
updates to getting started guide
rob8714 Mar 21, 2022
e0d03e1
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 22, 2022
bed0649
Update site/content/en/docs/Getting started/first_kustomization.md
rob8714 Mar 22, 2022
11a6807
updates to consolidate example into one folder
rob8714 Mar 22, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
345 changes: 322 additions & 23 deletions site/content/en/docs/Getting started/first_kustomization.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,98 @@ linkTitle: "Creating Your First Kustomization"
date: 2022-02-27
weight: 20
description: >
A simple project example to get you familiar with the concepts
A step by step tutorial for absolute kustomize beginners
---

We're going to use kustomize to deploy an nginx instance into our Kubernetes cluster.
This page is intended to help you get started with using this amazing tool called kustomize! We will start off with creating a simple nginx deployment manifest and then use it as an example to explore kustomize basics.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a style guide on how we want to write the word kustomize consistently?

  • Kustomize?
  • kustomize?
  • kustomize?

I'm a fan of the last one but things can be a bit cluttered sometimes when you have too many words in backticks flying around.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the second one, the word kustomize will be used a lot and back ticking every single one will start to decrease its emphasis and/or may get annoying to look at.

rob8714 marked this conversation as resolved.
Show resolved Hide resolved

## Creating the directory structure
### Create initial manifests and directory structure

Let's start off by creating our nginx deployment and service manifests in a dedicated folder structure
rob8714 marked this conversation as resolved.
Show resolved Hide resolved

Let's firt create a directory to store our kustomize project.
```bash
mkdir kustomize-nginx && cd kustomize-nginx
```
Create a `base` folder:
```bash
mkdir base
mkdir -p kustomize-example/base kustomize-example/overlays/staging kustomize-example/overlays/production
cd kustomize-example

cat <<'EOF' >base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
EOF

cat <<'EOF' >base/service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
EOF
```
Inside this folder we will create two files:
* `kustomization.yaml` - the configuration file for kustomize
* `deployment.yaml` - the definition for our nginx deployment

`kustomization.yaml`
Now that we have our `deployment.yaml` and `service.yaml` files created, let's create our `kustomization.yaml`:
rob8714 marked this conversation as resolved.
Show resolved Hide resolved

```bash
cat <<'EOF' >base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
metadata:
name: kustomize-nginx

resources:
- deployment.yaml
- service.yaml
EOF
```
The file defines the `apiVersion`, the `kind` and the `resources` it manages.

`deployment.yaml`
```bash
cat <<'EOF' >base/deployment.yaml
In this `kustomization.yaml` file, all that we are doing is telling kustomize to include the `deployment.yaml` and `service.yaml` as resources for it to use. So if we now run `kustomize build base/` from our current working directory, all kustomize will do at this point is to generate a manifest which contains the contents of our `deployment.yaml` and `service.yaml` files with no additional changes.
rob8714 marked this conversation as resolved.
Show resolved Hide resolved

```yaml
$ kustomize build base/
rob8714 marked this conversation as resolved.
Show resolved Hide resolved

apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
Expand All @@ -57,10 +107,259 @@ spec:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
- image: nginx
name: nginx
ports:
- containerPort: 80
```

KnVerey marked this conversation as resolved.
Show resolved Hide resolved
### What are Bases and Overlays?

Let's think of a scenario where we need to deploy the nginx manifests from the previous section to two environments called `Staging` and `Production`. The manifests for these two environments will be mostly identical, with only a few minor changes between them. Traditionally to achieve these changes, we could duplicate the manifests and apply the changes manually or rely on some templating engine. With kustomize we can avoid templating and duplication of our manifests and apply the different changes we need using overlays. With this approach, the `base` would contain the common part of the our manifests and the `overlays` contain our environment specific changes.
rob8714 marked this conversation as resolved.
Show resolved Hide resolved

To continue with this tutorial, create the `kustomization.yaml` files for our two overlays:
rob8714 marked this conversation as resolved.
Show resolved Hide resolved

```bash
cat <<'EOF' >overlays/staging/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- ../../base
EOF

cat <<'EOF' >overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- ../../base
KnVerey marked this conversation as resolved.
Show resolved Hide resolved
EOF
```

The `kustomization.yaml` files in the overlays are just including the `base` folder, so if you were to run `kustomize build` on the overlay folders at this point you would get the same output we got when we built `base`. The directory structure you created so far should look like this:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to point out that the the tutorial never explicitly gives the command for building the Staging and Production variants. While it might be obvious to a lot of people what command to run, it's probably best to include them both as some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, I have added this in the build code blocks

rob8714 marked this conversation as resolved.
Show resolved Hide resolved

```
kustomize-example
├── base
│   ├── deployment.yaml
│   ├── kustomization.yaml
│   └── service.yaml
└── overlays
├── production
│   └── kustomization.yaml
└── staging
└── kustomization.yaml
```

### Customising our overlays

For the purposes our example, let's define some requirements of how our deployment should look like in the two environments:

|Requirement| Production | Staging |
|-----------|----------------------------|-------------------------|
|Name |overlay1-nginx-production |overlay2-nginx-staging |
|Namespace |production |staging |
|Replicas |3 |2 |


We can achieve the names required by making use of `namePrefix` and `nameSuffix` as follows:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an actual name for things like namePrefix, nameSuffix, namespace, etc as we define in a kustomization.yaml file? Directives, commands, something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I avoided using a name to refer to these because I was unsure if they had one... @natasha41575 ?

Copy link
Contributor

@natasha41575 natasha41575 Mar 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an actual name for things like namePrefix, nameSuffix, namespace, etc as we define in a kustomization.yaml file? Directives, commands, something else?

I usually call them "fields in the kustomization file", "kustomization fields", etc. We can define a name for them if you'd like. I like the sound of directives, idk if there is a standard for these kinds of things already.

Maybe a Definitions section somewhere near the top of the doc? WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw in some of the docs that they are referred to as fields in some cases and I like the sound of directives for these more than fields, but thinking more about it maybe we have to be a bit careful about introducing new terms since they can also be referred to as transformers? Or define that directives and transformers may be used interchangeably?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My two cents: They can be "fields" when you're talking about the literal key you put into a Kustomization file, "transformers" when you're talking about the code that drives their functionality, or maybe "features" when you're being really abstract. "Directives" is already a (somewhat obscure) feature in this space: patch directives.



_kustomize-example/overlays/production/kustomization.yaml_:
```yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namePrefix: overlay1-
nameSuffix: -production

resources:
- ../../base
```

_kustomize-example/overlays/staging/kustomization.yaml_:
```yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namePrefix: overlay2-
nameSuffix: -staging

resources:
- ../../base
```

The build output for our Production overlay would now be:

```yaml
$ kustomize build overlays/production/

apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: overlay1-nginx-production ### service name changed here
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: overlay1-nginx-production ### deployment name changed here
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
```

It is important to note here that the name for _both_ the `deployment` and the `service` were updated with the `namePrefix` and `nameSuffix` defined. If we had additional kubernetes objects (like an `ingress`) their name would be updated as well.


Moving on to our next requirements, we can set the namespace and the number of replicas we want by using `namespace` and `replicas` respectively:


_kustomize-example/overlays/production/kustomization.yaml_:
```yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namePrefix: overlay1-
nameSuffix: -production

namespace: production

replicas:
- name: nginx
count: 3

resources:
- ../../base
```

_kustomize-example/overlays/staging/kustomization.yaml_:
```yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namePrefix: overlay2-
nameSuffix: -staging

namespace: staging

replicas:
- name: nginx
count: 2

resources:
- ../../base
```

Looking at the output of `kustomize build` we can see that we have met all the requirements we set out to meet:

_Production overlay build_:

```yaml
$ kustomize build overlays/production/

apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: overlay1-nginx-production
namespace: production ### namespace has been set to production
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: overlay1-nginx-production
namespace: production ### namespace has been set to production
spec:
replicas: 3 ### replicas have been updated from 1 to 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
```

_Staging overlay build_:

```yaml
$ kustomize build overlays/staging/

apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: overlay2-nginx-staging
namespace: staging ### namespace has been set to staging
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: overlay2-nginx-staging
namespace: staging ### namespace has been set to staging
spec:
replicas: 2 ### replicas have been from 1 to 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
```
TBC...
Loading