kubes 0.4.6 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -0
- data/README.md +54 -1
- data/docs/_docs/config/reference.md +1 -0
- data/docs/_docs/config/skip.md +1 -1
- data/docs/_docs/dsl/multiple-resources.md +3 -3
- data/docs/_docs/dsl/resources/job.md +62 -0
- data/docs/_docs/dsl/resources/secret.md +19 -2
- data/docs/_docs/extra-env/dsl.md +2 -2
- data/docs/_docs/extra-env/yaml.md +1 -1
- data/docs/_docs/generators.md +41 -0
- data/docs/_docs/helpers.md +17 -3
- data/docs/_docs/helpers/aws.md +1 -0
- data/docs/_docs/helpers/aws/advanced.md +10 -0
- data/docs/_docs/helpers/aws/advanced/secrets.md +131 -0
- data/docs/_docs/helpers/aws/advanced/ssm.md +78 -0
- data/docs/_docs/helpers/aws/secrets.md +18 -88
- data/docs/_docs/helpers/aws/ssm.md +20 -38
- data/docs/_docs/helpers/google.md +1 -1
- data/docs/_docs/helpers/google/advanced.md +10 -0
- data/docs/_docs/helpers/google/advanced/secrets.md +78 -0
- data/docs/_docs/helpers/google/secrets.md +18 -27
- data/docs/_docs/helpers/google/service-account.md +8 -0
- data/docs/_docs/intro.md +2 -9
- data/docs/_docs/intro/docker-image.md +66 -0
- data/docs/_docs/intro/how-kubes-works.md +7 -11
- data/docs/_docs/layering.md +2 -0
- data/docs/_docs/layering/merge.md +1 -1
- data/docs/_docs/layering/mix.md +99 -0
- data/docs/_docs/learn/dsl/delete.md +10 -2
- data/docs/_docs/learn/dsl/review-project.md +2 -2
- data/docs/_docs/learn/yaml/delete.md +10 -2
- data/docs/_docs/learn/yaml/review-project.md +2 -2
- data/docs/_docs/patterns/clock-web-worker.md +3 -3
- data/docs/_docs/patterns/migrations.md +1 -1
- data/docs/_docs/patterns/multiple-envs.md +55 -0
- data/docs/_docs/patterns/secrets.md +0 -2
- data/docs/_docs/variables.md +23 -0
- data/docs/_docs/variables/advanced.md +62 -0
- data/docs/_docs/variables/basic.md +137 -0
- data/docs/_docs/vs.md +10 -0
- data/docs/_docs/vs/custom.md +109 -0
- data/docs/_docs/vs/helm.md +243 -0
- data/docs/_docs/vs/kustomize.md +167 -0
- data/docs/_docs/yaml.md +2 -2
- data/docs/_includes/commands.html +2 -2
- data/docs/_includes/intro/features.md +11 -0
- data/docs/_includes/layering/layers.md +2 -4
- data/docs/_includes/sidebar.html +41 -0
- data/docs/_includes/vs/article.md +1 -0
- data/docs/_includes/vs/kubes/layering.md +10 -0
- data/docs/_includes/vs/kubes/structure.md +24 -0
- data/docs/_reference/kubes-delete.md +1 -1
- data/docs/_reference/kubes-exec.md +17 -1
- data/docs/_reference/kubes-init.md +2 -2
- data/docs/_reference/kubes-logs.md +2 -1
- data/docs/_reference/kubes-new-help.md +15 -0
- data/docs/_reference/kubes-new-helper.md +25 -0
- data/docs/_reference/kubes-new-resource.md +56 -0
- data/docs/_reference/kubes-new-variable.md +20 -0
- data/docs/_reference/kubes-new.md +26 -0
- data/docs/_reference/kubes-prune.md +22 -0
- data/docs/reference.md +2 -0
- data/kubes.gemspec +2 -2
- data/lib/kubes.rb +5 -3
- data/lib/kubes/auth.rb +4 -1
- data/lib/kubes/auth/base.rb +21 -0
- data/lib/kubes/auth/ecr.rb +1 -15
- data/lib/kubes/auth/gcr.rb +24 -0
- data/lib/kubes/cli.rb +9 -1
- data/lib/kubes/cli/build.rb +6 -0
- data/lib/kubes/cli/compile.rb +7 -0
- data/lib/kubes/cli/deploy.rb +1 -6
- data/lib/kubes/cli/exec.rb +5 -1
- data/lib/kubes/cli/help/exec.md +15 -0
- data/lib/kubes/cli/help/new/helper.md +4 -0
- data/lib/kubes/cli/help/new/resource.md +30 -0
- data/lib/kubes/cli/init.rb +1 -1
- data/lib/kubes/cli/new.rb +15 -0
- data/lib/kubes/cli/new/helper.rb +24 -0
- data/lib/kubes/cli/new/resource.rb +97 -0
- data/lib/kubes/cli/new/variable.rb +16 -0
- data/lib/kubes/cli/sequence.rb +1 -0
- data/lib/kubes/command.rb +7 -0
- data/lib/kubes/compiler.rb +19 -21
- data/lib/kubes/compiler/decorator/base.rb +1 -1
- data/lib/kubes/compiler/dsl/core/base.rb +6 -9
- data/lib/kubes/compiler/dsl/syntax/job.rb +217 -0
- data/lib/kubes/compiler/layering.rb +21 -7
- data/lib/kubes/compiler/shared/custom_variables.rb +38 -0
- data/lib/kubes/compiler/shared/helpers.rb +11 -2
- data/lib/kubes/compiler/shared/helpers/deprecated.rb +37 -0
- data/lib/kubes/compiler/shared/plugin_helpers.rb +14 -0
- data/lib/kubes/compiler/strategy.rb +7 -6
- data/lib/kubes/compiler/strategy/base.rb +59 -2
- data/lib/kubes/compiler/strategy/dsl.rb +0 -29
- data/lib/kubes/compiler/strategy/erb.rb +10 -22
- data/lib/kubes/compiler/util/normalize.rb +6 -3
- data/lib/kubes/compiler/util/yaml_dump.rb +4 -4
- data/lib/kubes/config.rb +1 -1
- data/lib/kubes/core.rb +6 -0
- data/lib/kubes/docker/strategy/image_name.rb +1 -1
- data/lib/kubes/kubectl.rb +5 -23
- data/lib/kubes/kubectl/batch.rb +27 -33
- data/lib/kubes/kubectl/ordering.rb +42 -0
- data/lib/kubes/plugin.rb +14 -0
- data/lib/kubes/util/sh.rb +1 -1
- data/lib/kubes/version.rb +1 -1
- data/lib/templates/base/.kubes/config.rb.tt +1 -1
- data/lib/templates/base/.kubes/config/env/dev.rb +1 -1
- data/lib/templates/base/.kubes/config/env/prod.rb +1 -1
- data/lib/templates/dsl/.kubes/resources/web/deployment.rb +1 -1
- data/lib/templates/new/helper/file.rb +2 -0
- data/lib/templates/new/resource/dsl/backend_config.rb +10 -0
- data/lib/templates/new/resource/dsl/config_map.rb +5 -0
- data/lib/templates/new/resource/dsl/daemon_set.rb +11 -0
- data/lib/templates/new/resource/dsl/deployment.rb +4 -0
- data/lib/templates/new/resource/dsl/ingress.rb +3 -0
- data/lib/templates/new/resource/dsl/job.rb +2 -0
- data/lib/templates/new/resource/dsl/managed_certificate.rb +2 -0
- data/lib/templates/new/resource/dsl/namespace.rb +2 -0
- data/lib/templates/new/resource/dsl/network_policy.rb +7 -0
- data/lib/templates/new/resource/dsl/pod.rb +6 -0
- data/lib/templates/new/resource/dsl/role.rb +4 -0
- data/lib/templates/new/resource/dsl/role_binding.rb +7 -0
- data/lib/templates/new/resource/dsl/secret.rb +5 -0
- data/lib/templates/new/resource/dsl/service.rb +2 -0
- data/lib/templates/new/resource/dsl/service_account.rb +1 -0
- data/lib/templates/new/resource/yaml/backend_config.yaml +10 -0
- data/lib/templates/new/resource/yaml/config_map.yaml +9 -0
- data/lib/templates/new/resource/yaml/daemon_set.yaml +11 -0
- data/lib/templates/new/resource/yaml/deployment.yaml +19 -0
- data/lib/templates/new/resource/yaml/ingress.yaml +12 -0
- data/lib/templates/new/resource/yaml/job.yaml +19 -0
- data/lib/templates/new/resource/yaml/managed_certificate.yaml +7 -0
- data/lib/templates/new/resource/yaml/namespace.yaml +6 -0
- data/lib/templates/new/resource/yaml/network_policy.yaml +20 -0
- data/lib/templates/new/resource/yaml/pod.yaml +11 -0
- data/lib/templates/new/resource/yaml/role.yaml +13 -0
- data/lib/templates/new/resource/yaml/role_binding.yaml +11 -0
- data/lib/templates/new/resource/yaml/secret.yaml +9 -0
- data/lib/templates/new/resource/yaml/service.yaml +14 -0
- data/lib/templates/new/resource/yaml/service_account.yaml +4 -0
- data/lib/templates/new/variable/file.rb +1 -0
- data/lib/templates/yaml/.kubes/resources/base/all.yaml.tt +2 -0
- data/lib/templates/yaml/.kubes/resources/web/deployment.yaml.tt +1 -1
- data/spec/fixtures/multiple-files/{deployment-1.rb → .kubes/resources/web/deployment-1.rb} +0 -0
- data/spec/fixtures/multiple-files/{deployment-2.rb → .kubes/resources/web/deployment-2.rb} +0 -0
- data/spec/fixtures/project/.kubes/resources/{deployment.rb → web/deployment.rb} +0 -0
- data/spec/fixtures/project/.kubes/resources/{foobar.rb → web/empty.rb} +0 -0
- data/spec/fixtures/project/.kubes/resources/{service.rb → web/service.rb} +1 -1
- data/spec/fixtures/syntax/{network_policy.rb → .kubes/resources/web/network_policy.rb} +0 -0
- data/spec/fixtures/syntax/{pod.rb → .kubes/resources/web/pod.rb} +0 -0
- data/spec/kubes/cli/prune_spec.rb +1 -0
- data/spec/kubes/compiler/strategy/dsl_spec.rb +2 -2
- data/spec/kubes/compiler_spec.rb +6 -2
- data/spec/kubes/dsl/network_policy_spec.rb +1 -1
- data/spec/kubes/dsl/pod_spec.rb +1 -1
- metadata +97 -25
@@ -0,0 +1,137 @@
|
|
1
|
+
---
|
2
|
+
title: Basic Variables
|
3
|
+
nav_text: Basic
|
4
|
+
categories: variables
|
5
|
+
---
|
6
|
+
|
7
|
+
## Basic Layering Example
|
8
|
+
|
9
|
+
Here's an example variables directory structure:
|
10
|
+
|
11
|
+
.kubes/variables
|
12
|
+
├── base.rb
|
13
|
+
├── dev.rb
|
14
|
+
└── prod.rb
|
15
|
+
|
16
|
+
base.rb
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
@endpoint = "base-endpoint" # overriden by ENV specific variable files.
|
20
|
+
```
|
21
|
+
|
22
|
+
dev.rb
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
@endpoint = "dev-endpoint"
|
26
|
+
```
|
27
|
+
|
28
|
+
prod.rb
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
@endpoint = "prod-endpoint"
|
32
|
+
```
|
33
|
+
|
34
|
+
The `@endpoint` value will be overriden by the ENV specific variable files.
|
35
|
+
|
36
|
+
## Deployment YAML
|
37
|
+
|
38
|
+
Here's an example deployment.yaml
|
39
|
+
|
40
|
+
.kubes/resources/web/deployment.yaml
|
41
|
+
|
42
|
+
```yaml
|
43
|
+
apiVersion: apps/v1
|
44
|
+
kind: Deployment
|
45
|
+
metadata:
|
46
|
+
name: web
|
47
|
+
labels:
|
48
|
+
role: web
|
49
|
+
spec:
|
50
|
+
replicas: 1 # overridden on a env basis
|
51
|
+
selector:
|
52
|
+
matchLabels:
|
53
|
+
role: web
|
54
|
+
template:
|
55
|
+
metadata:
|
56
|
+
labels:
|
57
|
+
role: web
|
58
|
+
spec:
|
59
|
+
containers:
|
60
|
+
- name: web
|
61
|
+
image: <%= docker_image %>
|
62
|
+
env:
|
63
|
+
- name: endpoint
|
64
|
+
value: <%= @endpoint %>
|
65
|
+
```
|
66
|
+
|
67
|
+
## Deploy
|
68
|
+
|
69
|
+
When you deploy you can use `KUBES_ENV` to and the ENV specific variables will be used:
|
70
|
+
|
71
|
+
KUBES_ENV=dev kubes deploy
|
72
|
+
|
73
|
+
Results in:
|
74
|
+
|
75
|
+
```yaml
|
76
|
+
metadata:
|
77
|
+
namespace: demo-dev
|
78
|
+
labels:
|
79
|
+
app: demo
|
80
|
+
role: web
|
81
|
+
name: web
|
82
|
+
spec:
|
83
|
+
selector:
|
84
|
+
matchLabels:
|
85
|
+
app: demo
|
86
|
+
role: web
|
87
|
+
template:
|
88
|
+
metadata:
|
89
|
+
labels:
|
90
|
+
app: demo
|
91
|
+
role: web
|
92
|
+
spec:
|
93
|
+
containers:
|
94
|
+
- name: web
|
95
|
+
image: gcr.io/GOOGLE_PROJECT/demo:kubes-2020-11-07T22-29-02
|
96
|
+
env:
|
97
|
+
- name: endpoint
|
98
|
+
value: dev-endpoint
|
99
|
+
replicas: 1
|
100
|
+
apiVersion: apps/v1
|
101
|
+
kind: Deployment
|
102
|
+
```
|
103
|
+
|
104
|
+
When using `KUBES_ENV=prod` the endpoint will use the `variables/prod.rb` values.
|
105
|
+
|
106
|
+
KUBES_ENV=prod kubes deploy
|
107
|
+
|
108
|
+
Results in:
|
109
|
+
|
110
|
+
```yaml
|
111
|
+
metadata:
|
112
|
+
namespace: demo-prod
|
113
|
+
labels:
|
114
|
+
app: demo
|
115
|
+
role: web
|
116
|
+
name: web
|
117
|
+
spec:
|
118
|
+
selector:
|
119
|
+
matchLabels:
|
120
|
+
app: demo
|
121
|
+
role: web
|
122
|
+
template:
|
123
|
+
metadata:
|
124
|
+
labels:
|
125
|
+
app: demo
|
126
|
+
role: web
|
127
|
+
spec:
|
128
|
+
containers:
|
129
|
+
- name: web
|
130
|
+
image: gcr.io/GOOGLE_PROJECT/demo:kubes-2020-11-07T22-29-02
|
131
|
+
env:
|
132
|
+
- name: endpoint
|
133
|
+
value: prod-endpoint
|
134
|
+
replicas: 1
|
135
|
+
apiVersion: apps/v1
|
136
|
+
kind: Deployment
|
137
|
+
```
|
data/docs/_docs/vs.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
---
|
2
|
+
title: Kubes vs. Other Tools
|
3
|
+
---
|
4
|
+
|
5
|
+
Here are some useful comparisons to help you compare Kubes vs other tools in the ecosystem:
|
6
|
+
|
7
|
+
{% assign docs = site.docs | where: "categories","vs" %}
|
8
|
+
{% for doc in docs -%}
|
9
|
+
* [{{ doc.nav_text }}]({{ doc.url }})
|
10
|
+
{% endfor %}
|
@@ -0,0 +1,109 @@
|
|
1
|
+
---
|
2
|
+
title: Kubes vs Custom Solutions
|
3
|
+
nav_text: Custom Solutions
|
4
|
+
categories: vs
|
5
|
+
---
|
6
|
+
|
7
|
+
Kubernetes provide a great platform to run and manage Docker containers. The `kubectl` command how you usually interact with a Kubernetes cluster. It does its job well and is quite a powerful tool.
|
8
|
+
|
9
|
+
{% include vs/article.md %}
|
10
|
+
|
11
|
+
## Kubernetes Questions
|
12
|
+
|
13
|
+
With Kubernetes, you usually use `kubectl` commands to deploy Docker images and run them on a Kubernetes cluster. As you get your Kubernetes applications production-ready, you'll have to answer many questions:
|
14
|
+
|
15
|
+
* How do you create multiple environments like dev and prod with the same code and not duplicate the YAML?
|
16
|
+
* How you handle creating service accounts and managing cloud permissions like AWS IAM, Google Service Accounts, etc?
|
17
|
+
* How will we build the Docker image and update Docker image?
|
18
|
+
* How do you deploy updated Kubernetes YAML settings in a controlled manner?
|
19
|
+
|
20
|
+
## Kubectl with Simple Wrappers
|
21
|
+
|
22
|
+
Most folks start off with `kubectl` commands to create their Kubernetes resources. It's simple. It's also important to learn how to use `kubectl` commands to establish fundamentals. Eventually, you grow tired of typing the same commands repeatedly, though. So you write a wrapper bash script. Example:
|
23
|
+
|
24
|
+
kubectl-wrapper.sh
|
25
|
+
|
26
|
+
kubectl apply -f service.yaml
|
27
|
+
kubectl apply -f deployment.yaml
|
28
|
+
|
29
|
+
Bash shines for simple scripts and light glue, but it can quickly get messy as the script takes on more things to do.
|
30
|
+
|
31
|
+
## Multiple Envs Duplication
|
32
|
+
|
33
|
+
One way to create different env like dev and prod is to copy their YAML files. Here's a naive example structure:
|
34
|
+
|
35
|
+
├── dev
|
36
|
+
│ ├── deployment.yaml
|
37
|
+
│ └── service.yaml
|
38
|
+
└── prod
|
39
|
+
├── deployment.yaml
|
40
|
+
└── service.yaml
|
41
|
+
|
42
|
+
We then write a wrapper script that selects the folder:
|
43
|
+
|
44
|
+
kubectl-wrapper.sh
|
45
|
+
|
46
|
+
KUBE_ENV=${1:-dev}
|
47
|
+
kubectl apply -f $KUBE_ENV/service.yaml
|
48
|
+
kubectl apply -f $KUBE_ENV/deployment.yaml
|
49
|
+
|
50
|
+
We've duplicated `service.yaml` and `deployment.yaml`, though. Instead, it'll be nice if we use the same YAML and create a different env like dev and prod with it. Things like `envsubst` to replace variables from the same "template" YAML files can help. As requirements increases, the simple bash glue scripts end up getting messy.
|
51
|
+
|
52
|
+
## PreBuilt Docker Image
|
53
|
+
|
54
|
+
Additionally, the Docker image is expected to be prebuilt. Because you must first build the Docker image, folks will usually write bash script that perform these additional steps and then glue things together.
|
55
|
+
|
56
|
+
## Kubernetes Resources Galore
|
57
|
+
|
58
|
+
Kubernetes has a large service area, and there are so many resource Kinds that we can create that it's difficult for a simple wrapper script to handle enough control for your needs.
|
59
|
+
|
60
|
+
## Kubes Makes It Easier
|
61
|
+
|
62
|
+
Kubes is a Kubernetes Deployment Tool that automates the following:
|
63
|
+
|
64
|
+
1. It builds the docker image
|
65
|
+
2. Creates the Kubernetes YAML
|
66
|
+
3. Runs kubectl apply
|
67
|
+
|
68
|
+
Kubes works transparently and straightforwardly. The deploy command simply do all 3 steps: build, compile, and apply.
|
69
|
+
|
70
|
+
kubes deploy
|
71
|
+
|
72
|
+
### Layering: Multiple Environments like dev and prod
|
73
|
+
|
74
|
+
To deploy and create multiple environments like dev and prod with the same YAML, we use a different KUBES_ENV setting:
|
75
|
+
|
76
|
+
KUBES_ENV=dev kubes deploy
|
77
|
+
KUBES_ENV=prod kubes deploy
|
78
|
+
|
79
|
+
The same code is used to create different environments. Kubes achieves this with a feature called Layering. The concept is similar to Kustomize overlays. Here's the general layering processing order that Kubes takes.
|
80
|
+
|
81
|
+
* [Layering Docs]({% link _docs/layering.md %})
|
82
|
+
|
83
|
+
## Hooks
|
84
|
+
|
85
|
+
Kubes support a variety of hooks run scripts at any part of the `kubectl` commands. This allows you customize and add app-specific logic needed. Example:
|
86
|
+
|
87
|
+
.kubes/config/hooks/kubectl.rb
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
before("apply",
|
91
|
+
on: "web/deployment",
|
92
|
+
execute: "echo 'before apply hook test'",
|
93
|
+
)
|
94
|
+
|
95
|
+
after("delete",
|
96
|
+
on: "web/deployment",
|
97
|
+
execute: "echo 'after delete hook test'",
|
98
|
+
)
|
99
|
+
```
|
100
|
+
|
101
|
+
There are also [cloud helpers]({% link _docs/helpers.md %}) that will handle things like Secrets and IAM Account creation.
|
102
|
+
|
103
|
+
* [Kubes Kubectl Hooks Docs]({% link _docs/config/hooks/kubectl.md %})
|
104
|
+
|
105
|
+
## Summary
|
106
|
+
|
107
|
+
Many companies roll their own custom solutions. Chances are that the `kubectl` wrapper scripts eventually grow into messy glue. Every time you go to another company, you must relearn and figure out the home-grown solution's particularities. Even within companies, going from team to team, there may be different scripts that are their own unique beasts. It's a science project.
|
108
|
+
|
109
|
+
Kubes provides a tool that streamlines the `kubectl` deployment already. Kubes also works in a transparent and straightforward manner. You know what's going on. It's also extendable and customizable. You can add business logic that you wish. Kubes provides convenient tooling and helps you get things done quickly.
|
@@ -0,0 +1,243 @@
|
|
1
|
+
---
|
2
|
+
title: Kubes vs Helm
|
3
|
+
nav_text: Helm
|
4
|
+
categories: vs
|
5
|
+
---
|
6
|
+
|
7
|
+
Though both Kubes and Helm can be used to deploy applications to Kubernetes, they work quite differently. Kubernetes is more focused on deploying your application. Helm is more like a package manager.
|
8
|
+
|
9
|
+
{% include vs/article.md %}
|
10
|
+
|
11
|
+
## Project Structures
|
12
|
+
|
13
|
+
### Helm Project Structure
|
14
|
+
|
15
|
+
Here's an example of Helm project structure:
|
16
|
+
|
17
|
+
├── Chart.yaml
|
18
|
+
├── templates
|
19
|
+
│ ├── _helpers.tpl
|
20
|
+
│ ├── deployment.yaml
|
21
|
+
│ └── service.yaml
|
22
|
+
└── values.yaml
|
23
|
+
|
24
|
+
The Kubernetes YAML files reside in the templates folder. The `values.yaml` contains the default configuration values for the YAML files.
|
25
|
+
|
26
|
+
{% include vs/kubes/structure.md %}
|
27
|
+
|
28
|
+
## Multiple Environments: Variables vs Layering
|
29
|
+
|
30
|
+
Both Helm and Kubes allow you to use the same code to create multiple environments. They take different approaches, though.
|
31
|
+
|
32
|
+
### Helm Variables
|
33
|
+
|
34
|
+
Helm supports creating multiple environments like dev and prod by using different variables files. Here are example commands:
|
35
|
+
|
36
|
+
helm install chart-dev . --namespace chart-dev --create-namespace -f values/dev.yaml
|
37
|
+
helm install chart-prod . --namespace chart-prod --create-namespace -f values/prod.yaml
|
38
|
+
|
39
|
+
To create different environments in different namespaces with helm, you use the namespace CLI options. Helm creates the namespace outside of YAML, so it's lifecycle is not managed. The `--create-namespace` option is only necessary once. To use different variable values, you use the `-f` option. You can specify as many variables files as you wish.
|
40
|
+
|
41
|
+
The commands can become verbose, as you have to remember to type the CLI options.
|
42
|
+
|
43
|
+
{% include vs/kubes/layering.md %}
|
44
|
+
|
45
|
+
## Templating Support
|
46
|
+
|
47
|
+
Both Helm and Kubes support templating logic.
|
48
|
+
|
49
|
+
### Helm Templating
|
50
|
+
|
51
|
+
The templating language is a mixture of the [Go template language](https://godoc.org/text/template) and the [Sprig template library](https://masterminds.github.io/sprig/). Here's an example of Helm templating.
|
52
|
+
|
53
|
+
templates/deployment.yaml
|
54
|
+
|
55
|
+
```yaml
|
56
|
+
{% raw %}apiVersion: apps/v1
|
57
|
+
kind: Deployment
|
58
|
+
metadata:
|
59
|
+
name: {{ include "mychart.fullname" . }}
|
60
|
+
labels:
|
61
|
+
{{- include "mychart.labels" . | nindent 4 }}
|
62
|
+
spec:
|
63
|
+
{{- if not .Values.autoscaling.enabled }}
|
64
|
+
replicas: {{ .Values.replicaCount }}
|
65
|
+
{{- end }}
|
66
|
+
selector:
|
67
|
+
matchLabels:
|
68
|
+
{{- include "mychart.selectorLabels" . | nindent 6 }}
|
69
|
+
template:
|
70
|
+
metadata:
|
71
|
+
{{- with .Values.podAnnotations }}
|
72
|
+
annotations:
|
73
|
+
{{- toYaml . | nindent 8 }}
|
74
|
+
{{- end }}
|
75
|
+
labels:
|
76
|
+
{{- include "mychart.selectorLabels" . | nindent 8 }}
|
77
|
+
spec:
|
78
|
+
containers:
|
79
|
+
- name: {{ .Chart.Name }}
|
80
|
+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
|
81
|
+
imagePullPolicy: {{ .Values.image.pullPolicy }}
|
82
|
+
ports:
|
83
|
+
- name: http
|
84
|
+
containerPort: 80
|
85
|
+
protocol: TCP{% endraw %}
|
86
|
+
```
|
87
|
+
|
88
|
+
### Kubes Templating
|
89
|
+
|
90
|
+
Kubes uses ERB Ruby for templating. Here's an example.
|
91
|
+
|
92
|
+
.kubes/resources/web/deployment.yaml
|
93
|
+
|
94
|
+
```yaml
|
95
|
+
apiVersion: apps/v1
|
96
|
+
kind: Deployment
|
97
|
+
metadata:
|
98
|
+
name: web
|
99
|
+
labels:
|
100
|
+
role: web
|
101
|
+
spec:
|
102
|
+
replicas: 1 # overridden on a env basis
|
103
|
+
selector:
|
104
|
+
matchLabels:
|
105
|
+
role: web
|
106
|
+
template:
|
107
|
+
metadata:
|
108
|
+
labels:
|
109
|
+
role: web
|
110
|
+
spec:
|
111
|
+
containers:
|
112
|
+
- name: web
|
113
|
+
image: <%= docker_image %>
|
114
|
+
```
|
115
|
+
|
116
|
+
The `docker_image` method is a built-in helper. It returns the Docker image built from your Dockerfile or a configured pre-built image. See: [Docker Image Docs]({% link _docs/intro/docker-image.md %}).
|
117
|
+
|
118
|
+
One of the reasons why the Kubes YAML template is more straightforward is because Kubes also supports layering. So logic can be moved to different layered YAML files that get merged.
|
119
|
+
|
120
|
+
## Define Custom Helpers
|
121
|
+
|
122
|
+
Both Helm and Kubes support custom user-defined helpers. We'll take a look at examples from each tool.
|
123
|
+
|
124
|
+
### Helm Helpers
|
125
|
+
|
126
|
+
With Helm, you can define custom helpers in `templates/_helpers.tpl`. Example:
|
127
|
+
|
128
|
+
templates/_helpers.tpl
|
129
|
+
|
130
|
+
```go{% raw %}
|
131
|
+
{{- define "demo.serviceAccountName" -}}
|
132
|
+
{{- if .Values.serviceAccount.create }}
|
133
|
+
{{- default (include "demo.fullname" .) .Values.serviceAccount.name }}
|
134
|
+
{{- else }}
|
135
|
+
{{- default "default" .Values.serviceAccount.name }}
|
136
|
+
{{- end }}
|
137
|
+
{{- end }}{% endraw %}
|
138
|
+
```
|
139
|
+
|
140
|
+
The helper methods need to be defined in the go-lang templating language, hence the need for curly brackets.
|
141
|
+
|
142
|
+
### Kubes Helpers
|
143
|
+
|
144
|
+
With Kubes, to define template helper methods, it's just Ruby code. Example:
|
145
|
+
|
146
|
+
.kubes/helpers/my_helpers.rb
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
module MyHelpers
|
150
|
+
def database_endpoint
|
151
|
+
case Kubes.env
|
152
|
+
when "dev"
|
153
|
+
"dev-db.cbuqdmc3nqvb.us-west-2.rds.amazonaws.com"
|
154
|
+
when "prod"
|
155
|
+
"prod-db.cbuqdmc3nqvb.us-west-2.rds.amazonaws.com"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
```
|
160
|
+
|
161
|
+
The custom helper definitions are a lot more natural.
|
162
|
+
|
163
|
+
## Custom Hooks
|
164
|
+
|
165
|
+
Both Helm and Kubes support hooks. This allows you to hook into the deploy lifecycle and add your own custom business logic.
|
166
|
+
|
167
|
+
Helm supports a wide variety of hooks for the install, delete, upgrade, and rollback. The hooks run at the helm-level.
|
168
|
+
|
169
|
+
Kubes also supports hook as the kubes-level; this is similar to the helm-level. Kubes also provide finer-grain control hooks at the kubectl-level.
|
170
|
+
|
171
|
+
Overally, Helm and Kubes hooks work quite differently. We'll take a look at **some** examples:
|
172
|
+
|
173
|
+
### Helm Hook Example
|
174
|
+
|
175
|
+
Helm hooks are Kubernetes Job resources with a special `helm.sh/hook` annotation. Example:
|
176
|
+
|
177
|
+
templates/job.yaml
|
178
|
+
|
179
|
+
```yaml
|
180
|
+
{% raw %}apiVersion: batch/v1
|
181
|
+
kind: Job
|
182
|
+
metadata:
|
183
|
+
name: "{{ .Release.Name }}"
|
184
|
+
annotations:
|
185
|
+
"helm.sh/hook": post-install # This is what defines this resource as a hook.
|
186
|
+
spec:
|
187
|
+
template:
|
188
|
+
metadata:
|
189
|
+
name: "{{ .Release.Name }}"
|
190
|
+
spec:
|
191
|
+
restartPolicy: Never
|
192
|
+
containers:
|
193
|
+
- name: post-install-job
|
194
|
+
image: "alpine:3.3"
|
195
|
+
command: ["/bin/sleep","{{ default "10" .Values.sleepyTime }}"]{% endraw %}
|
196
|
+
```
|
197
|
+
|
198
|
+
So Helm hooks are just Kubernetes jobs and run on the cluster.
|
199
|
+
|
200
|
+
### Kubes Hook Example
|
201
|
+
|
202
|
+
Kube hooks are scripts that run on the same machine that kubes is running on. Here's an example:
|
203
|
+
|
204
|
+
.kubes/config/hooks/kubectl.rb
|
205
|
+
|
206
|
+
```ruby
|
207
|
+
before("apply",
|
208
|
+
on: "web/deployment",
|
209
|
+
execute: "echo 'before apply hook test'",
|
210
|
+
)
|
211
|
+
```
|
212
|
+
|
213
|
+
The hook will simply run the `echo` command on the same machine as what kubes is running on. The scope is much more fine-grain. We can target any role and resource kind. For example:
|
214
|
+
|
215
|
+
# hook can run here
|
216
|
+
kubectl apply -f .kubes/output/shared/namespace.yaml
|
217
|
+
# hook can run here
|
218
|
+
kubectl apply -f .kubes/output/web/service.yaml
|
219
|
+
# hook can run here
|
220
|
+
kubectl apply -f .kubes/output/web/deployment.yaml
|
221
|
+
# hook can run here
|
222
|
+
|
223
|
+
### Hook Differences
|
224
|
+
|
225
|
+
We showed examples of hooks with both Helm and Kubes. We already covered one of the differences:
|
226
|
+
|
227
|
+
* Code: Helm hooks are written as Kubernetes resources. Kubes hooks are written as Ruby code that can shell out to scripts or call an inline [Ruby code]({% link _docs/config/hooks/ruby.md %}).
|
228
|
+
* Context: Helm runs as a Kubernetes job. Kubes hooks run on the same machine as kubes itself.
|
229
|
+
* Fine-Grain Control: With Kubes we can target the hook at each kubectl resource. Kubes also supports the coarser-grain hooks at the kubes-level. These are like helm hooks.
|
230
|
+
|
231
|
+
For more info on hooks, check out the [Kubes Hooks Docs](https://kubes.guru/docs/config/hooks/).
|
232
|
+
|
233
|
+
## Additional Features
|
234
|
+
|
235
|
+
Helm and Kubes are quite different tools. Helm is more like a package manager. Kubes is more focused on deploying your specific application and adds additional convenient tooling.
|
236
|
+
|
237
|
+
Helm can package up your applications and then helps distribute them via a helm server. It's a full-fledge package management system.
|
238
|
+
|
239
|
+
Kubes supports the deployment workflow. It can build the docker image from your Dockerfile use it to deploy to Kubernetes. Kubes also has additional convenience CLI commands like [exec]({% link _reference/kubes-exec.md %}) and [logs]({% link _reference/kubes-logs.md %}) to work with Kubernetes containers.
|
240
|
+
|
241
|
+
## Summary
|
242
|
+
|
243
|
+
Helm and Kubes are quite different. Helm is a full-fledge package management. Kubes is a more focused on deployment. Both tools support templating logic to help keep your code DRY. Additionally, Kubes supports layering to merge YAML files together for DRYness. Kubes also helps you build your Docker images.
|