kubernetes_helper 1.8.0 → 1.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +73 -2
- data/lib/kubernetes_helper/version.rb +1 -1
- data/lib/templates/_custom_volumes.yml +3 -0
- data/lib/templates/_replicas.yml +1 -1
- data/lib/templates/_volumes.yml +20 -0
- data/lib/templates/cd.sh +9 -6
- data/lib/templates/certificate.yml +1 -1
- data/lib/templates/deployment.yml +5 -19
- data/lib/templates/ingress.yml +2 -2
- data/lib/templates/service.yml +2 -2
- data/lib/templates/settings.rb +4 -3
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 284a91c77c6a5aa796e2593eadbdcecd85a5b52e35ae7bb6dbd53cde80ef7067
|
4
|
+
data.tar.gz: dfe726e21ba3a03de4ad9cb272922d5f253b306f8a16996954f7efa29595a5f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8ccc2bd6661de602e7eb2f21935fa16a8ddb344d3130f3ee7d8331025552675538cf321236b40360f119a58768700d85b9b80cabbc98fa0093e7d5ce337572f
|
7
|
+
data.tar.gz: 4b3185c7eb890ff9acb7e8ecdafbf85176f7a2e2703cd54e8fc1f0f4abe54b9112a00a27c1a2039c5cc5a718b17d1fd3a33eae9212d8b1764c0210babf02be04
|
data/README.md
CHANGED
@@ -9,7 +9,15 @@ cd my_app/
|
|
9
9
|
gem install kubernetes_helper -v '~> 1.0'
|
10
10
|
kubernetes_helper generate_templates
|
11
11
|
```
|
12
|
-
Note: Requires ruby 1.7+
|
12
|
+
Note: Requires ruby 1.7+
|
13
|
+
|
14
|
+
## Configuration
|
15
|
+
- Edit `.kubernetes/settings.rb` and enter or replace all settings with the valid ones
|
16
|
+
- For special applications where default configurations are not enough, you can do the following:
|
17
|
+
- Download the corresponding template from [lib/templates](lib/templates)
|
18
|
+
- Put it inside `.kubernetes` folder
|
19
|
+
- Customize based on your needs (You can add or use your custom variables from `settings.rb`)
|
20
|
+
Note: The local template will be used instead of the default.
|
13
21
|
|
14
22
|
## Deployment
|
15
23
|
Once you generated the basic templates, it comes with the corresponding [readme.md](/lib/templates/README.md) which includes all the steps to deploy your application.
|
@@ -21,7 +29,7 @@ Once you generated the basic templates, it comes with the corresponding [readme.
|
|
21
29
|
- Supports for `- documents` to include multiple documents in a file and share yml variables between them (Sample: `lib/templates/deployment.yml#1`)
|
22
30
|
- Replaces all setting values based on `DEPLOY_ENV`
|
23
31
|
- Supports for secrets auto importer using `import_secrets: ['secrets.yml', '<%=secrets.name%>']` (Sample: `lib/templates/deployment.yml#29`)
|
24
|
-
- Supports for
|
32
|
+
- Supports for sub templates by `include_template 'template_name.yml.erb', { my_local_var: 10 }`
|
25
33
|
Sample: `DEPLOY_ENV=beta kubernetes_helper run_deployment "deployment.yml" "kubectl create"`
|
26
34
|
|
27
35
|
- Run kubernetes commands
|
@@ -44,6 +52,69 @@ Once you generated the basic templates, it comes with the corresponding [readme.
|
|
44
52
|
When performing a script it looks first for file inside .kubernetes folder, if not exist,
|
45
53
|
it looks for the file inside kubernetes_helper template folder.
|
46
54
|
|
55
|
+
## Settings API
|
56
|
+
Below settings are used when running Continuous Deployment
|
57
|
+
- `continuous_deployment.image_name` (String): Partial docker image url. Sample: `gcr.io/my-account/my_app_name`
|
58
|
+
- `continuous_deployment.image_tag` (String, default 'latest'): Image tag to be used for this application
|
59
|
+
- `continuous_deployment.project_name`: Cloud project name. Sample: `my-project-name`
|
60
|
+
- `continuous_deployment.cluster_name`: Cluster cluster name. Sample: `my-cluster-name`
|
61
|
+
- `continuous_deployment.cluster_region`: Cluster region name. Sample: `europe-west4-a`
|
62
|
+
- `continuous_deployment.docker_build_cmd`: Docker command to build the corresponding image. Sample: `build --target production -f Dockerfile `
|
63
|
+
- `continuous_deployment.update_deployment` (Boolean, default: false): If true permits to re-generate and update the k8s deployment(s) before applying the new version (new docker image)
|
64
|
+
|
65
|
+
Below settings are used when configuring the application in the k8s environment
|
66
|
+
- `deployment.name` (String): Web deployment name (Note: Underscores are not accepted). Sample: `my-app`
|
67
|
+
- `deployment.replicas` (Integer): Quantity of replicas. Sample: `1`
|
68
|
+
- `deployment.replicas_range` (Array<min, max, cpu_percentage>, Optional): Defines the minimum and the maximum number of pods that could automatically be created when `CPUUtilizationPercentage` is above than defined. Sample: `[1, 3, 50]`
|
69
|
+
- `deployment.cloud_secret_name` (String, Optional): K8s credentials name where cloud secrets will be saved (includes permission like DB). Sample: `my-app-cloud-secret`
|
70
|
+
- `deployment.cloud_sql_instance` (String, Optional): Cloud sql instance name. Sample: `my-project:europe-west1:my-instance-name=tcp:5432` (5432 => postgres, 3306 => mysql)
|
71
|
+
- `deployment.env_vars` (Hash, optional): List of static env variables (Note: Not recommended for sensitive values). Sample: `{ 'RAILS_ENV' => 'production' }`
|
72
|
+
- `deployment.command` (String, Optional): Bash command to be used for web containers. Sample: `rails s -b 0.0.0.0`
|
73
|
+
- `deployment.liveness_path` (String, Optional): Relative path to be used for readiness and liveness checker of the web app. Sample: `/check_liveness`
|
74
|
+
- `deployment.custom_volumes` (Hash<name: path>, Optional): Custom volumes to be mounted. Sample: `{ my_volume: { kind: 'hostPath', mount_path: '/', settings: { path: '..', type: 'Directory' } } }`
|
75
|
+
|
76
|
+
- `deployment.job_name` (String, optional): Job deployment name (Note: Underscores are not accepted). Sample: `my-app-job`. Note: This deployment is created only if this value is present
|
77
|
+
- `deployment.job_command` (String, optional): Bash command to be used for job container. Sample: `bundle exec sidekiq`
|
78
|
+
- `deployment.job_sidekiq_alive_gem` (Boolean, default false): If true will add liveness checker settings using `sidekiq_alive_gem` (`sidekiq_alive` gem needs to be present in your Gemfile)
|
79
|
+
- `deployment.job_services` (Array, Optional, only `job_sidekiq_alive_gem` or `job_services` is allowed): List of linux service names that are required for a healthy job container. Sample: `['sidekiq', 'cron']`
|
80
|
+
|
81
|
+
|
82
|
+
- `secrets.name` (String): K8s secrets name where env vars will be saved and fetched from. Sample: `my-app-secrets`
|
83
|
+
|
84
|
+
- `service.name`: K8s service name. Sample: `my-app-service`
|
85
|
+
- `service.port_name`: Http port name to connect between k8s ingress and service. Sample: `http-port`. Note: max 15 characters
|
86
|
+
- `service.backend_port_name` (String): Web backend port name to be connected between k8s service and web deployments. Sample: `b-port`. Note: max 15 characters
|
87
|
+
|
88
|
+
- `ingress.name`: Name of k8s ingress for the app: Sample: `my-app-ingress`
|
89
|
+
- `ingress.ip_name` (Optional): Static ip address is not created nor assigned if empty value. Sample: `my-app-static-ip`
|
90
|
+
- `ingress.certificate_name` (Optional): Ssl certificate is not created nor assigned if empty value. Sample: `my-app-lets-encrypt`. Note: requires `certificate_domain`
|
91
|
+
- `ingress.certificate_domain` (Optional): Domain name for the certificate. Sample: `myapp.com`. Note: does not support for willcard domains
|
92
|
+
|
93
|
+
- `cloud.name` (String, optional): Cloud service name. Default `gcloud`.
|
94
|
+
|
95
|
+
### Partials
|
96
|
+
- `_container_extra_settings.yml` Partial template to add custom container settings. Receives `pod` as local variable (`web` | `job` | `cloudsql` | `logs`). Sample:
|
97
|
+
```yaml
|
98
|
+
<% if locals[:pod] == 'job' %>
|
99
|
+
resources:
|
100
|
+
requests:
|
101
|
+
cpu: 50m
|
102
|
+
memory: 256Mi
|
103
|
+
limits:
|
104
|
+
cpu: 500m
|
105
|
+
memory: 1Gi
|
106
|
+
<% end %>
|
107
|
+
```
|
108
|
+
- `_custom_containers.yml` Partial template to add extra containers (Receives `pod` as local variable: `web` | `job`). Sample:
|
109
|
+
```yaml
|
110
|
+
<% if locals[:pod] == 'job' %>
|
111
|
+
- name: scraper
|
112
|
+
image: owencio/easy_scraper
|
113
|
+
...
|
114
|
+
<% end %>
|
115
|
+
```
|
116
|
+
- `_cd_apply_images.sh` Partial template to customize the process to apply the new version (new docker image)
|
117
|
+
|
47
118
|
## Templating
|
48
119
|
When performing a command or script, the setting variables are replaced based on `DEPLOY_ENV`.
|
49
120
|
All these setting variable values are configured in `.kubernetes/settings.rb` which defines the values based on `DEPLOY_ENV`.
|
data/lib/templates/_replicas.yml
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
<% if deployment.cloud_secret_name %>
|
2
|
+
- name: gcloud-creds
|
3
|
+
secret:
|
4
|
+
secretName: '<%=deployment.cloud_secret_name%>'
|
5
|
+
<% end %>
|
6
|
+
|
7
|
+
- name: applog
|
8
|
+
emptyDir: {}
|
9
|
+
|
10
|
+
# Custom volumes
|
11
|
+
# { my_volume: { kind: 'hostPath', mount_path: '/', settings: { path: '..', type: 'Directory' } } }
|
12
|
+
<% (deployment.custom_volumes || {}).each do |name, vol| %>
|
13
|
+
- name: <%= name %>
|
14
|
+
<%= vol[:kind] %>:
|
15
|
+
<% vol[:settings].each do |k, v| %>
|
16
|
+
<%= k %>: <%= v %>
|
17
|
+
<% end %>
|
18
|
+
<% end %>
|
19
|
+
|
20
|
+
<%= include_template "_custom_volumes.yml", { pod: locals[:pod] } %>
|
data/lib/templates/cd.sh
CHANGED
@@ -15,7 +15,7 @@ DOCKER_BUILD_CMD="<%=continuous_deployment.docker_build_cmd || 'build -f Dockerf
|
|
15
15
|
CI_COMMIT_SHA=$(git rev-parse --verify HEAD || :)
|
16
16
|
CI_COMMIT_SHA=${CI_COMMIT_SHA:-$(date +%s) }
|
17
17
|
DEPLOY_NAME="${IMAGE_NAME}:${CI_COMMIT_SHA}"
|
18
|
-
LATEST_NAME="${IMAGE_NAME}
|
18
|
+
LATEST_NAME="${IMAGE_NAME}:<%= continuous_deployment.image_tag || 'latest' %>"
|
19
19
|
|
20
20
|
if [ ! -z "$KB_AUTH_TOKEN" ]
|
21
21
|
then
|
@@ -37,10 +37,14 @@ then
|
|
37
37
|
fi
|
38
38
|
|
39
39
|
|
40
|
-
|
41
|
-
|
40
|
+
ALREADY_DEPLOYED="$(gcloud container images list-tags --format='get(tags)' $IMAGE_NAME | grep $CI_COMMIT_SHA || :;)"
|
41
|
+
if [ -z $ALREADY_DEPLOYED ]
|
42
|
+
then
|
43
|
+
## Build and push containers
|
44
|
+
docker $DOCKER_BUILD_CMD -t $DEPLOY_NAME .
|
45
|
+
docker push $DEPLOY_NAME
|
46
|
+
fi
|
42
47
|
docker tag $DEPLOY_NAME $LATEST_NAME
|
43
|
-
docker push $DEPLOY_NAME
|
44
48
|
docker push $LATEST_NAME
|
45
49
|
|
46
50
|
## Update new secrets defined in secrets.yml as ENV vars for deployments
|
@@ -54,5 +58,4 @@ for deployment in "${deployments[@]}"; do
|
|
54
58
|
[ -z "$deployment" ] && continue # if empty value
|
55
59
|
|
56
60
|
<%= include_template "_cd_apply_images.sh" %>
|
57
|
-
done
|
58
|
-
|
61
|
+
done
|
@@ -21,7 +21,7 @@ documents:
|
|
21
21
|
spec: &template_spec
|
22
22
|
containers:
|
23
23
|
- &app_container
|
24
|
-
image: '<%=continuous_deployment.image_name
|
24
|
+
image: '<%= continuous_deployment.image_name %>:<%= continuous_deployment.image_tag || "latest" %>'
|
25
25
|
<% if deployment.command %>
|
26
26
|
command: ["/bin/bash", "-c", "<%= deployment.command %>"]
|
27
27
|
<% end %>
|
@@ -87,24 +87,7 @@ documents:
|
|
87
87
|
|
88
88
|
terminationGracePeriodSeconds: 120
|
89
89
|
volumes:
|
90
|
-
|
91
|
-
- name: gcloud-creds
|
92
|
-
secret:
|
93
|
-
secretName: '<%=deployment.cloud_secret_name%>'
|
94
|
-
<% end %>
|
95
|
-
|
96
|
-
- name: applog
|
97
|
-
emptyDir: {}
|
98
|
-
|
99
|
-
# Custom volumes
|
100
|
-
# { my_volume: { kind: 'hostPath', mount_path: '/', settings: { path: '..', type: 'Directory' } } }
|
101
|
-
<% (deployment.custom_volumes || {}).each do |name, vol| %>
|
102
|
-
- name: <%= name %>
|
103
|
-
<%= vol[:kind] %>:
|
104
|
-
<% vol[:settings].each do |k, v| %>
|
105
|
-
<%= k %>: <%= v %>
|
106
|
-
<% end %>
|
107
|
-
<% end %>
|
90
|
+
<%= include_template "_volumes.yml", { pod: 'web' } %>
|
108
91
|
|
109
92
|
|
110
93
|
<% if deployment.job_name %>
|
@@ -145,6 +128,9 @@ documents:
|
|
145
128
|
- <<: *logs_container
|
146
129
|
|
147
130
|
<%= include_template "_custom_containers.yml", { pod: 'job' } %>
|
131
|
+
|
132
|
+
volumes:
|
133
|
+
<%= include_template "_volumes.yml", { pod: 'job' } %>
|
148
134
|
<% end %>
|
149
135
|
|
150
136
|
<% if deployment.replicas_range %>
|
data/lib/templates/ingress.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
<% if ingress.
|
1
|
+
<% if ingress.certificate_name %>
|
2
2
|
<%= include_template "certificate.yml" %>
|
3
3
|
<% end %>
|
4
4
|
|
@@ -17,7 +17,7 @@ metadata:
|
|
17
17
|
kubernetes.io/ingress.global-static-ip-name: "<%=ingress.ip_name%>"
|
18
18
|
<% end %>
|
19
19
|
|
20
|
-
<% if ingress.
|
20
|
+
<% if ingress.certificate_name %>
|
21
21
|
networking.gke.io/managed-certificates: '<%=ingress.certificate_name%>'
|
22
22
|
<% end %>
|
23
23
|
|
data/lib/templates/service.yml
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
apiVersion: cloud.google.com/v1beta1
|
5
5
|
kind: BackendConfig
|
6
6
|
metadata:
|
7
|
-
name: '<%=service.
|
7
|
+
name: '<%=service.name%>-bconfig'
|
8
8
|
spec:
|
9
9
|
timeoutSec: 1800
|
10
10
|
---
|
@@ -16,7 +16,7 @@ metadata:
|
|
16
16
|
name: '<%=service.name%>'
|
17
17
|
annotations:
|
18
18
|
<% if cloud.name == 'gcloud' %>
|
19
|
-
beta.cloud.google.com/backend-config: '{"ports": {"80":"<%=service.
|
19
|
+
beta.cloud.google.com/backend-config: '{"ports": {"80":"<%=service.name%>-bconfig"}}'
|
20
20
|
<% end %>
|
21
21
|
spec:
|
22
22
|
selector:
|
data/lib/templates/settings.rb
CHANGED
@@ -6,6 +6,7 @@ settings = {
|
|
6
6
|
deployment: {
|
7
7
|
name: app_name,
|
8
8
|
replicas: is_production ? 2 : 1,
|
9
|
+
replicas_range: nil, # [min, max] or nil to ignore, sample: is_production ? [1, 2] : nil
|
9
10
|
cloud_secret_name: "#{is_production ? 'production' : 'beta'}-cloud-secret",
|
10
11
|
cloud_sql_instance: 'xxx:xxx:xxx=tcp:5432', # 5432 => postgres, 3306 => mysql
|
11
12
|
env_vars: {}, # Sample: { 'CUSTOM_VAR' => 'value' }
|
@@ -20,10 +21,11 @@ settings = {
|
|
20
21
|
name: "#{app_name}-ingress",
|
21
22
|
ip_name: "#{app_name}-static-ip", # nil if static ip is not necessary
|
22
23
|
certificate_name: "#{app_name}-lets-encrypt", # nil if ssl is not required
|
23
|
-
|
24
|
+
certificate_domain: is_production ? 'myapp.com' : 'beta.myapp.com' # nil if domain is not required
|
24
25
|
},
|
25
26
|
continuous_deployment: {
|
26
27
|
image_name: "gcr.io/my-account/#{app_name}",
|
28
|
+
image_tag: 'latest',
|
27
29
|
project_name: 'my-project-name',
|
28
30
|
cluster_name: 'my-cluster-name',
|
29
31
|
cluster_region: 'europe-west4-a',
|
@@ -36,8 +38,7 @@ settings = {
|
|
36
38
|
service: {
|
37
39
|
name: app_name,
|
38
40
|
port_name: 'http-port', # max 15 characters
|
39
|
-
backend_port_name: 'b-port'
|
40
|
-
config_name: "#{app_name}-backend-config"
|
41
|
+
backend_port_name: 'b-port' # max 15 characters
|
41
42
|
}
|
42
43
|
}
|
43
44
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kubernetes_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.11.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- owen2345
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erb
|
@@ -43,9 +43,11 @@ files:
|
|
43
43
|
- lib/templates/_cd_apply_images.sh
|
44
44
|
- lib/templates/_container_extra_settings.yml
|
45
45
|
- lib/templates/_custom_containers.yml
|
46
|
+
- lib/templates/_custom_volumes.yml
|
46
47
|
- lib/templates/_job_liveness.yml
|
47
48
|
- lib/templates/_replicas.yml
|
48
49
|
- lib/templates/_sidekiq_alive_gem.yml
|
50
|
+
- lib/templates/_volumes.yml
|
49
51
|
- lib/templates/cd.sh
|
50
52
|
- lib/templates/certificate.yml
|
51
53
|
- lib/templates/deployment.yml
|