kubernetes_helper 1.13.1 → 1.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +84 -64
- data/lib/kubernetes_helper/version.rb +1 -1
- data/lib/templates/README.md +68 -33
- data/lib/templates/_cd_digital.sh +24 -0
- data/lib/templates/_cd_google.sh +37 -0
- data/lib/templates/_resources.yml +15 -0
- data/lib/templates/cd.sh +2 -36
- data/lib/templates/certificate.yml +8 -3
- data/lib/templates/deployment.yml +9 -1
- data/lib/templates/ingress.yml +8 -4
- data/lib/templates/service.yml +1 -1
- data/lib/templates/settings.rb +1 -2
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 150f172f156e7ee7061f651caec568f443526c231b88322f15909fb14db1d928
|
4
|
+
data.tar.gz: 3a9b2d35c152f5cb4feb9b526fba6caa8c1264679ebb0f80b93b3f6a6f49f711
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1991a65e8a61d8a4fc44a34a6987c9c3b82f7f19ff746c84f4fdd120487e674957538a3e780f99a5518300cb5230528662e7efb54bde78144d53acd743bad0f
|
7
|
+
data.tar.gz: b511e26550c54ea1dddcf156526bc0972d45a612eb60cde67a14a67f3b41c0a0bede568684ec45de54c8cac24cd57b615bb464936eff8093bccfb797501c3097
|
data/README.md
CHANGED
@@ -1,70 +1,37 @@
|
|
1
1
|
# KubernetesHelper
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
TODO:
|
4
|
+
- create a release if Business app is running well with the limitations, perhaps test with Buddy app by using the gem from github
|
5
|
+
- Publish the gem + release new version
|
5
6
|
|
6
|
-
## Installation
|
7
|
-
```bash
|
8
|
-
cd my_app/
|
9
|
-
gem install kubernetes_helper -v '~> 1.0'
|
10
|
-
kubernetes_helper generate_templates
|
11
|
-
```
|
12
|
-
Note: Requires ruby 1.7+
|
13
7
|
|
14
|
-
|
15
|
-
|
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.
|
8
|
+
This gem is a helper to manage easily Kubernetes settings for GCloud (easy customization for other cloud services) where configuring and deploying a new application can be done in a couple of minutes.
|
9
|
+
Configuration and customization can be done for multiple environments and at any level which permits to deploy simple and complex applications.
|
21
10
|
|
22
|
-
##
|
23
|
-
|
11
|
+
## Steps
|
12
|
+
1. Install the `kubernetes_helper` gem
|
13
|
+
```bash
|
14
|
+
cd my_app/
|
15
|
+
gem install kubernetes_helper -v '~> 1.0'
|
16
|
+
```
|
17
|
+
Note: Requires ruby 1.7+
|
24
18
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
- Run kubernetes commands
|
36
|
-
`DEPLOY_ENV=<env name> rake kubernetes_helper:run_command "<bash or k8s commands>"`
|
37
|
-
Replaces all setting variables inside command based on `DEPLOY_ENV` and performs it as a normal bash command.
|
38
|
-
Sample: `DEPLOY_ENV=beta rake kubernetes_helper:run_command "gcloud compute addresses create \#{ingress.ip_name} --global"'`
|
39
|
-
|
40
|
-
- Run kubernetes bash scripts
|
41
|
-
`DEPLOY_ENV=<env name> kubernetes_helper run_script "<script name>"`
|
42
|
-
Performs the script name located inside `.kubernetes` folder or kubernetes_helper template as the second option.
|
43
|
-
All setting variables inside the script will be replaced based on `DEPLOY_ENV`.
|
44
|
-
Sample: `DEPLOY_ENV=beta kubernetes_helper run_script "cd.sh"`
|
19
|
+
2. Generate helper settings
|
20
|
+
```bash
|
21
|
+
kubernetes_helper generate_templates
|
22
|
+
```
|
23
|
+
Note: `.kubernetes` folder was added. For special applications where default configurations are not enough, you can do the following:
|
24
|
+
- Download the required template from [lib/templates](lib/templates)
|
25
|
+
- Put it inside `.kubernetes` folder
|
26
|
+
- Customize based on your needs (You can add or use your custom variables from `settings.rb`)
|
27
|
+
Note: The local template will be used instead of the default one.
|
45
28
|
|
46
|
-
|
47
|
-
|
48
|
-
Copy files based on mode (`basic|advanced`) or a specific file from templates.
|
49
|
-
Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "basic"`
|
50
|
-
Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "ingress.yml"`
|
29
|
+
3. Install/setup the application on kubernetes
|
30
|
+
Open [.kubernetes/README.md](lib/templates/README.md) to see the instructions (customize the file according to your project and keep it updated)
|
51
31
|
|
52
|
-
When performing a script it looks first for file inside .kubernetes folder, if not exist,
|
53
|
-
it looks for the file inside kubernetes_helper template folder.
|
54
32
|
|
55
33
|
## Settings API
|
56
|
-
|
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
|
-
- `continuous_deployment.before_building_image` (String, default: empty): Add the ability to enter commands before building docker image
|
65
|
-
- `continuous_deployment.after_building_image` (String, default: empty): Add the ability to enter commands after building docker image
|
66
|
-
|
67
|
-
Below settings are used when configuring the application in the k8s environment
|
34
|
+
### Application deployment.yml
|
68
35
|
- `deployment.name` (String): Web deployment name (Note: Underscores are not accepted). Sample: `my-app`
|
69
36
|
- `deployment.replicas` (Integer): Quantity of replicas. Sample: `1`
|
70
37
|
- `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]`
|
@@ -73,7 +40,6 @@ Below settings are used when configuring the application in the k8s environment
|
|
73
40
|
- `deployment.env_vars` (Hash, optional): List of static or external env variables (Note: Not recommended for sensitive values).
|
74
41
|
Sample: `{ 'RAILS_ENV' => 'production' }`
|
75
42
|
Example for external secrets: `{ PAPERTRAIL_PORT: { name: 'common_secrets', key: 'paper_trail_port' }` will import `paper_trail_port` value from `common_secrets` yml as `PAPERTRAIL_PORT`
|
76
|
-
|
77
43
|
- `deployment.command` (String, Optional): Bash command to be used for web containers. Sample: `rails s -b 0.0.0.0`
|
78
44
|
- `deployment.liveness_path` (String, Optional): Relative path to be used for readiness and liveness checker of the web app. Sample: `/check_liveness`
|
79
45
|
- `deployment.custom_volumes` (Hash<name: path>, Optional): Custom volumes to be mounted.
|
@@ -82,27 +48,49 @@ Below settings are used when configuring the application in the k8s environment
|
|
82
48
|
- `deployment.log_container` (Boolean, default true): Permits to auto include logs container to print all logs from logs/*.log to stdout (required for papertrail using fluentd)
|
83
49
|
- `deployment.log_folder` (String, default `/app/log`): Logs to be printed from
|
84
50
|
- `deployment.app_port` (Integer, default 3000): Application port number
|
51
|
+
- `deployment.resources` (Hash, optional): Configure depending on the web app requirements. Sample: `{ cpu: { max: '1', min: '500m' }, mem: { max: '1Gi', min: '500Mi' } }`
|
85
52
|
|
53
|
+
- `deployment.cloudsql_resources` (Hash, optional): Configure depending on the app requirements. Default: `{ cpu: { max: '300m', min: '100m' }, mem: { max: '500Mi', min: '200Mi' } }`
|
54
|
+
- `deployment.logs_resources` (Hash, optional): Configure depending on the app requirements. Default: `{ cpu: { max: '200m', min: '50m' }, mem: { max: '200Mi', min: '50Mi' } }`
|
55
|
+
|
56
|
+
### Application deployment.yml for jobs or services without internet interaction (Optional)
|
86
57
|
- `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
|
87
58
|
- `deployment.job_command` (String, optional): Bash command to be used for job container. Sample: `bundle exec sidekiq`
|
88
59
|
- `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)
|
89
60
|
- `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']`
|
61
|
+
- `deployment.job_resources` (Hash, optional): Configure depending on the job app requirements. Sample: `{ cpu: { max: '1', min: '500m' }, mem: { max: '1Gi', min: '500Mi' } }`
|
90
62
|
|
91
|
-
|
63
|
+
### Applications secrets.yml (Optional)
|
92
64
|
- `secrets.name` (String): K8s secrets name where env vars will be saved and fetched from. Sample: `my-app-secrets`
|
93
65
|
|
66
|
+
### Application service.yml (Optional)
|
94
67
|
- `service.name`: K8s service name. Sample: `my-app-service`
|
95
68
|
- `service.port_name` (String, default `http-port`): Http port name to connect between k8s ingress and service. Sample: `http-port`. Note: max 15 characters
|
96
69
|
- `service.backend_port_name` (String, default `b-port`): Web backend port name to be connected between k8s service and web deployments. Sample: `b-port`. Note: max 15 characters
|
70
|
+
- `service.type`: K8s service type. By default `NodePort`
|
97
71
|
|
72
|
+
### Application ingress.yml (Optional)
|
98
73
|
- `ingress.name`: Name of k8s ingress for the app: Sample: `my-app-ingress`
|
99
74
|
- `ingress.ip_name` (Optional): Static ip address is not created nor assigned if empty value. Sample: `my-app-static-ip`
|
100
|
-
- `ingress.certificate_name` (
|
101
|
-
- `ingress.certificate_domain` (Optional): Domain name for the certificate. Sample: `myapp.com`. Note: does not support for willcard domains
|
75
|
+
- `ingress.certificate_name` (Deprecated): Ssl certificate is not created nor assigned if empty value. Sample: `my-app-lets-encrypt`. Note: requires `certificate_domain`
|
76
|
+
- `ingress.certificate_domain` (Optional): Domain name for the certificate. Sample: `myapp.com`. Note: does not support for willcard domains
|
77
|
+
To register multiple domains (Certificate names will be auto-generated like `mysite-com-lets-encrypt`): `certificate_domain: ['mysite.com', 'mysite.de', 'mysite.uk']`
|
102
78
|
|
103
79
|
- `cloud.name` (String, optional): Cloud service name. Default `gcloud`.
|
104
80
|
|
105
|
-
###
|
81
|
+
### Application CD (continuous deployment)
|
82
|
+
- `continuous_deployment.image_name` (String): Partial docker image url. Sample: `gcr.io/my-account/my_app_name`
|
83
|
+
- `continuous_deployment.image_tag` (String, default 'latest'): Image tag to be used for this application
|
84
|
+
- `continuous_deployment.project_name`: Cloud project name. Sample: `my-project-name`
|
85
|
+
- `continuous_deployment.cluster_name`: Cluster cluster name. Sample: `my-cluster-name`
|
86
|
+
- `continuous_deployment.cluster_region`: Cluster region name. Sample: `europe-west4-a`
|
87
|
+
- `continuous_deployment.docker_build_cmd` (deprecated): Docker command to build the corresponding image. Sample: `build --target production -f Dockerfile `
|
88
|
+
- `continuous_deployment.docker_cmd` (String): Docker command to build the corresponding image.
|
89
|
+
Simple docker image: `docker build -f Dockerfile -t $DEPLOY_NAME .`
|
90
|
+
Docker image with target: `docker build --target production -f Dockerfile -t $DEPLOY_NAME .`
|
91
|
+
- `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)
|
92
|
+
|
93
|
+
### Gem templating partials
|
106
94
|
- `_container_extra_settings.yml` Partial template to add custom container settings. Receives `pod` as local variable (`web` | `job` | `cloudsql` | `logs`). Sample:
|
107
95
|
```yaml
|
108
96
|
<% if locals[:pod] == 'job' %>
|
@@ -125,18 +113,50 @@ Below settings are used when configuring the application in the k8s environment
|
|
125
113
|
```
|
126
114
|
- `_cd_apply_images.sh` Partial template to customize the process to apply the new version (new docker image)
|
127
115
|
|
128
|
-
|
116
|
+
### Gem templating
|
129
117
|
When performing a command or script, the setting variables are replaced based on `DEPLOY_ENV`.
|
130
118
|
All these setting variable values are configured in `.kubernetes/settings.rb` which defines the values based on `DEPLOY_ENV`.
|
131
119
|
These setting variables use [erb](https://github.com/ruby/erb) template gem to define variable replacement and conditional blocks, and so on.
|
132
120
|
Note: Setting variable values are referenced as an object format instead of a hash format for simplicity.
|
133
121
|
|
134
122
|
|
135
|
-
|
123
|
+
### Sample
|
136
124
|
https://owen2345.github.io/kubernetes_helper/
|
137
125
|
|
126
|
+
|
127
|
+
## API
|
128
|
+
- Run any kubernetes document
|
129
|
+
`DEPLOY_ENV=<env name> kubernetes_helper run_deployment "<document name>" "<bash command>"`
|
130
|
+
Evaluates the kubernetes document with the following details:
|
131
|
+
- Supports for `- documents` to include multiple documents in a file and share yml variables between them (Sample: `lib/templates/deployment.yml#1`)
|
132
|
+
- Replaces all setting values based on `DEPLOY_ENV`
|
133
|
+
- Supports for secrets auto importer using `import_secrets: ['secrets.yml', '<%=secrets.name%>']` (Sample: `lib/templates/deployment.yml#29`)
|
134
|
+
- Supports for sub templates by `include_template 'template_name.yml.erb', { my_local_var: 10 }`
|
135
|
+
Sample: `DEPLOY_ENV=beta kubernetes_helper run_deployment "deployment.yml" "kubectl create"`
|
136
|
+
|
137
|
+
- Run kubernetes commands
|
138
|
+
`DEPLOY_ENV=<env name> rake kubernetes_helper:run_command "<bash or k8s commands>"`
|
139
|
+
Replaces all setting variables inside command based on `DEPLOY_ENV` and performs it as a normal bash command.
|
140
|
+
Sample: `DEPLOY_ENV=beta rake kubernetes_helper:run_command "gcloud compute addresses create \#{ingress.ip_name} --global"'`
|
141
|
+
|
142
|
+
- Run kubernetes bash scripts
|
143
|
+
`DEPLOY_ENV=<env name> kubernetes_helper run_script "<script name>"`
|
144
|
+
Performs the script name located inside `.kubernetes` folder or kubernetes_helper template as the second option.
|
145
|
+
All setting variables inside the script will be replaced based on `DEPLOY_ENV`.
|
146
|
+
Sample: `DEPLOY_ENV=beta kubernetes_helper run_script "cd.sh"`
|
147
|
+
|
148
|
+
- Generate templates
|
149
|
+
`DEPLOY_ENV=<env name> kubernetes_helper generate_templates "<mode_or_template_name>"`
|
150
|
+
Copy files based on mode (`basic|advanced`) or a specific file from templates.
|
151
|
+
Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "basic"`
|
152
|
+
Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "ingress.yml"`
|
153
|
+
|
154
|
+
When performing a script it looks first for file inside .kubernetes folder, if not exist,
|
155
|
+
it looks for the file inside kubernetes_helper template folder.
|
156
|
+
|
138
157
|
## TODO
|
139
158
|
- Add one_step_configuration.sh
|
159
|
+
- Change `include_template` into `ERB render partial`
|
140
160
|
|
141
161
|
## Contributing
|
142
162
|
|
data/lib/templates/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Kubernetes app configuration
|
2
2
|
|
3
|
-
##
|
3
|
+
## Cluster connection
|
4
4
|
- Create the project on Gcloud
|
5
5
|
- Set the project where to work on
|
6
6
|
`gcloud config set project my-project`
|
@@ -13,13 +13,14 @@
|
|
13
13
|
- Use the cluster/project as default
|
14
14
|
`gcloud container clusters get-credentials my-cluster --zone europe-west4-a`
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
## App setup
|
17
|
+
- Install kubernetes-helper (if not installed yet)
|
18
|
+
`gem install kubernetes_helper`
|
18
19
|
|
19
|
-
- Verify or update k8s settings in
|
20
|
-
Note: Please do not include sensitive values in this file,
|
20
|
+
- Verify or update k8s settings in `.kubernetes/settings.rb`
|
21
|
+
Note: Please do not include sensitive values in this file, secrets are recommended instead.
|
21
22
|
|
22
|
-
- Register shared cloudsql proxy configuration (only if not
|
23
|
+
- Register shared cloudsql proxy configuration to connect application to the Database/Pubsub (only if it does not exist yet)
|
23
24
|
```bash
|
24
25
|
DEPLOY_ENV=beta kubernetes_helper run_command "kubectl create secret generic <%=deployment.cloud_secret_name%> --from-file=credentials.json=<path-to-downloaded/credentials.json>"
|
25
26
|
```
|
@@ -60,7 +61,67 @@
|
|
60
61
|
# You can start accessing to the app using the generated ip address
|
61
62
|
# `kubectl get ManagedCertificate` # to see the status of ssl provisionning
|
62
63
|
```
|
64
|
+
- Update your domain to point to the new generated ip address (if required) and visit the domain or the ip address to see your application.
|
65
|
+
Note: The domain name propagation can take some time before pointing to the new ip address.
|
66
|
+
Note2: If the application shows "404 not found", check the deployment/pods status by: `kubectl get pods`
|
67
|
+
Note3: If the pod error is `ImagePullBackOff`, it is because the application docker image is missing.
|
68
|
+
You can deploy your application via github actions or similar (see #Configure-continuous-deployment-for-github-actions) or do it manually (see #Deploy-application-manually)
|
63
69
|
|
70
|
+
## Deploy application manually
|
71
|
+
Run the deployment manually with:
|
72
|
+
```bash
|
73
|
+
DEPLOY_ENV=beta kubernetes_helper run_deployment 'cd.sh'
|
74
|
+
```
|
75
|
+
The application image will be create and uploaded to the configured container registry (application pods should be restarted with the new docker image).
|
76
|
+
Visit the application url to see changes.
|
77
|
+
|
78
|
+
## Configure continuous deployment for github actions
|
79
|
+
This gem comes with continuous deployment script out of the box which can be executed with a single line of code.
|
80
|
+
* Go to github repository settings
|
81
|
+
* Register a new secret variable with content downloaded from (for google cloud) https://console.cloud.google.com/iam-admin/serviceaccounts
|
82
|
+
(Make sure to attach a "Editor", "Storage Admin" and "Kubernetes engine cluster admin" role to the service account)
|
83
|
+
```bash
|
84
|
+
BETA_CLOUD_TOKEN=<secret content here>
|
85
|
+
PROD_CLOUD_TOKEN=<secret content here>
|
86
|
+
```
|
87
|
+
|
88
|
+
* Add github workflow to automatically run deployment when merged into master or staging, something like:
|
89
|
+
```yml
|
90
|
+
name: "Continuous Deployment"
|
91
|
+
on:
|
92
|
+
push:
|
93
|
+
branches:
|
94
|
+
- master
|
95
|
+
- staging
|
96
|
+
|
97
|
+
deployment:
|
98
|
+
runs-on: ubuntu-latest
|
99
|
+
jobs:
|
100
|
+
steps:
|
101
|
+
- uses: actions/checkout@v2
|
102
|
+
with:
|
103
|
+
ref: ${{ env.DEPLOY_BRANCH }}
|
104
|
+
- name: Cancel previous Workflow Actions
|
105
|
+
uses: styfle/cancel-workflow-action@0.6.0
|
106
|
+
with:
|
107
|
+
access_token: ${{ github.token }}
|
108
|
+
|
109
|
+
- name: Set up Cloud SDK
|
110
|
+
uses: google-github-actions/setup-gcloud@master
|
111
|
+
- uses: satackey/action-docker-layer-caching@v0.0.11
|
112
|
+
continue-on-error: true
|
113
|
+
with:
|
114
|
+
key: CD-docker-cache-${{ hashFiles('Dockerfile', 'Gemfile.lock') }}
|
115
|
+
|
116
|
+
#### App deployment
|
117
|
+
- run: sudo gem install kubernetes_helper
|
118
|
+
- name: App deployment
|
119
|
+
env:
|
120
|
+
KB_AUTH_TOKEN: ${{ github.ref_name == 'master' && secrets.PROD_CLOUD_TOKEN || secrets.BETA_CLOUD_TOKEN }}
|
121
|
+
DEPLOY_ENV: ${{ github.ref_name == 'master' && 'production' || 'beta' }}
|
122
|
+
run: kubernetes_helper run_deployment 'cd.sh'
|
123
|
+
```
|
124
|
+
|
64
125
|
## Apply any k8s setting changes
|
65
126
|
- Secrets
|
66
127
|
Open kubernetes secrets and add/edit/remove values and then save it
|
@@ -70,30 +131,4 @@
|
|
70
131
|
- Other settings
|
71
132
|
```bash
|
72
133
|
DEPLOY_ENV=beta kubernetes_helper run_yml 'deployment.yml' 'kubectl apply'
|
73
|
-
```
|
74
|
-
|
75
|
-
## Configure continuous deployment for github actions
|
76
|
-
* Go to github repository settings
|
77
|
-
* Register a new secret variable with content downloaded from https://console.cloud.google.com/iam-admin/serviceaccounts
|
78
|
-
(Make sure to attach a "Editor", "Storage Admin" and "Kubernetes engine cluster admin" role to the service account)
|
79
|
-
```bash
|
80
|
-
beta: BETA_CLOUD_TOKEN=<secret content here>
|
81
|
-
production: PROD_CLOUD_TOKEN=<secret content here>
|
82
|
-
```
|
83
|
-
|
84
|
-
* Add action to run deployment:
|
85
|
-
```bash
|
86
|
-
env:
|
87
|
-
KB_AUTH_TOKEN: secrets.BETA_CLOUD_TOKEN
|
88
|
-
run: DEPLOY_ENV=beta kubernetes_helper run_deployment 'cd.sh'
|
89
|
-
```
|
90
|
-
|
91
|
-
* Sample:
|
92
|
-
```yml
|
93
|
-
- run: sudo gem install kubernetes_helper
|
94
|
-
- name: Staging deployment
|
95
|
-
env: # Env variable saved in github that contains gcloud credential (json format)
|
96
|
-
KB_AUTH_TOKEN: ${{ secrets.BETA_GOOGLE_AUTH }}
|
97
|
-
run: DEPLOY_ENV=beta kubernetes_helper run_deployment 'cd.sh'
|
98
|
-
if: ${{ !contains(fromJson('["main", "master"]'), env.DEPLOY_BRANCH) }}
|
99
|
-
```
|
134
|
+
```
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Download and install doctl
|
2
|
+
if [ -z "$(which doctl)" ]; then
|
3
|
+
wget https://github.com/digitalocean/doctl/releases/download/v1.72.0/doctl-1.72.0-linux-amd64.tar.gz
|
4
|
+
tar xf ~/doctl-1.72.0-linux-amd64.tar.gz
|
5
|
+
sudo mv ~/doctl /usr/local/bin
|
6
|
+
fi
|
7
|
+
|
8
|
+
if [ ! -z "$KB_AUTH_TOKEN" ]
|
9
|
+
then
|
10
|
+
doctl auth init --access-token $KB_AUTH_TOKEN
|
11
|
+
fi
|
12
|
+
|
13
|
+
## Build and push containers
|
14
|
+
echo "****** building image..."
|
15
|
+
<% if continuous_deployment.docker_cmd %>
|
16
|
+
<%= continuous_deployment.docker_cmd %>
|
17
|
+
<% else %>
|
18
|
+
docker <%=continuous_deployment.docker_build_cmd || 'build -f Dockerfile'%> -t $DEPLOY_NAME .
|
19
|
+
<% end %>
|
20
|
+
docker push $DEPLOY_NAME
|
21
|
+
|
22
|
+
echo "****** tagging image $DEPLOY_NAME as $LATEST_NAME"
|
23
|
+
docker tag $DEPLOY_NAME $LATEST_NAME
|
24
|
+
docker push $LATEST_NAME
|
@@ -0,0 +1,37 @@
|
|
1
|
+
if [ ! -z "$KB_AUTH_TOKEN" ]
|
2
|
+
then
|
3
|
+
AUTH_PATH="$SCRIPT_DIR/k8s-auth-token.json"
|
4
|
+
rm -f -- $AUTH_PATH
|
5
|
+
echo $KB_AUTH_TOKEN >> $AUTH_PATH
|
6
|
+
|
7
|
+
## ***** GOOGLE CONNECTOR
|
8
|
+
# Download and install Google Cloud SDK
|
9
|
+
if [ -z "$(which gcloud)" ]; then
|
10
|
+
export CLOUDSDK_CORE_DISABLE_PROMPTS=1; curl https://sdk.cloud.google.com | bash && source /home/runner/google-cloud-sdk/path.bash.inc && gcloud --quiet components update kubectl
|
11
|
+
fi
|
12
|
+
|
13
|
+
# Connect to cluster
|
14
|
+
gcloud auth activate-service-account --key-file $AUTH_PATH --project $PROJECT_NAME
|
15
|
+
gcloud docker --authorize-only --project $PROJECT_NAME
|
16
|
+
gcloud container clusters get-credentials $CLUSTER_NAME --region $CLUSTER_REGION
|
17
|
+
## ***** END GOOGLE CONNECTOR
|
18
|
+
fi
|
19
|
+
|
20
|
+
|
21
|
+
ALREADY_DEPLOYED="$(gcloud container images list-tags --format='get(tags)' $IMAGE_NAME | grep $CI_COMMIT_SHA || :;)"
|
22
|
+
if [ -z $ALREADY_DEPLOYED ]
|
23
|
+
then
|
24
|
+
## Build and push containers
|
25
|
+
echo "****** image not created yet, building image..."
|
26
|
+
<% if continuous_deployment.docker_cmd %>
|
27
|
+
<%= continuous_deployment.docker_cmd %>
|
28
|
+
<% else %>
|
29
|
+
docker <%=continuous_deployment.docker_build_cmd || 'build -f Dockerfile'%> -t $DEPLOY_NAME .
|
30
|
+
<% end %>
|
31
|
+
docker push $DEPLOY_NAME
|
32
|
+
else
|
33
|
+
echo "****** image was already created: $ALREADY_DEPLOYED"
|
34
|
+
fi
|
35
|
+
|
36
|
+
echo "****** tagging image $DEPLOY_NAME as $LATEST_NAME"
|
37
|
+
gcloud container images add-tag --quiet $DEPLOY_NAME $LATEST_NAME
|
@@ -0,0 +1,15 @@
|
|
1
|
+
resources:
|
2
|
+
limits:
|
3
|
+
<% if locals[:cpu] && locals[:cpu][:max] %>
|
4
|
+
cpu: <%= locals[:cpu][:max] %>
|
5
|
+
<% end %>
|
6
|
+
<% if locals[:mem] && locals[:mem][:max] %>
|
7
|
+
memory: <%= locals[:mem][:max] %>
|
8
|
+
<% end %>
|
9
|
+
requests:
|
10
|
+
<% if locals[:cpu] && locals[:cpu][:min] %>
|
11
|
+
cpu: <%= locals[:cpu][:min] %>
|
12
|
+
<% end %>
|
13
|
+
<% if locals[:mem] && locals[:mem][:min] %>
|
14
|
+
memory: <%= locals[:mem][:min] %>
|
15
|
+
<% end %>
|
data/lib/templates/cd.sh
CHANGED
@@ -10,48 +10,14 @@ IMAGE_NAME="<%=continuous_deployment.image_name%>"
|
|
10
10
|
CLUSTER_NAME="<%=continuous_deployment.cluster_name%>"
|
11
11
|
PROJECT_NAME="<%=continuous_deployment.project_name%>"
|
12
12
|
CLUSTER_REGION="<%=continuous_deployment.cluster_region%>"
|
13
|
-
DOCKER_BUILD_CMD="<%=continuous_deployment.docker_build_cmd || 'build -f Dockerfile'%>"
|
14
13
|
|
15
14
|
CI_COMMIT_SHA=$(git rev-parse --verify HEAD || :)
|
16
15
|
CI_COMMIT_SHA=${CI_COMMIT_SHA:-$(date +%s) }
|
17
16
|
DEPLOY_NAME="${IMAGE_NAME}:${CI_COMMIT_SHA}"
|
18
17
|
LATEST_NAME="${IMAGE_NAME}:<%= continuous_deployment.image_tag || 'latest' %>"
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
AUTH_PATH="$SCRIPT_DIR/k8s-auth-token.json"
|
23
|
-
rm -f -- $AUTH_PATH
|
24
|
-
echo $KB_AUTH_TOKEN >> $AUTH_PATH
|
25
|
-
|
26
|
-
## ***** GOOGLE CONNECTOR
|
27
|
-
# Download and install Google Cloud SDK
|
28
|
-
if [ -z "$(which gcloud)" ]; then
|
29
|
-
export CLOUDSDK_CORE_DISABLE_PROMPTS=1; curl https://sdk.cloud.google.com | bash && source /home/runner/google-cloud-sdk/path.bash.inc && gcloud --quiet components update kubectl
|
30
|
-
fi
|
31
|
-
|
32
|
-
# Connect to cluster
|
33
|
-
gcloud auth activate-service-account --key-file $AUTH_PATH --project $PROJECT_NAME
|
34
|
-
gcloud docker --authorize-only --project $PROJECT_NAME
|
35
|
-
gcloud container clusters get-credentials $CLUSTER_NAME --region $CLUSTER_REGION
|
36
|
-
## ***** END GOOGLE CONNECTOR
|
37
|
-
fi
|
38
|
-
|
39
|
-
|
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
|
-
echo "****** image not created yet, building image..."
|
45
|
-
<%=continuous_deployment.before_building_image || ''%>
|
46
|
-
docker $DOCKER_BUILD_CMD -t $DEPLOY_NAME .
|
47
|
-
<%=continuous_deployment.after_building_image || ''%>
|
48
|
-
docker push $DEPLOY_NAME
|
49
|
-
else
|
50
|
-
echo "****** image was already created: $ALREADY_DEPLOYED"
|
51
|
-
fi
|
52
|
-
|
53
|
-
echo "****** tagging image $DEPLOY_NAME as $LATEST_NAME"
|
54
|
-
gcloud container images add-tag --quiet $DEPLOY_NAME $LATEST_NAME
|
19
|
+
<%= include_template "_cd_google.sh" if continuous_deployment.image_name.include?('gcr.io/') %>
|
20
|
+
<%= include_template "_cd_digital.sh" if continuous_deployment.image_name.include?('digitalocean.com/') %>
|
55
21
|
|
56
22
|
## Update new secrets defined in secrets.yml as ENV vars for deployments
|
57
23
|
<% if continuous_deployment.update_deployment %>
|
@@ -1,7 +1,12 @@
|
|
1
|
+
# locals: { domains: Hash(domain => cert_name) }
|
2
|
+
<% locals[:domains].each do |domain, cert_name| %>
|
1
3
|
apiVersion: networking.gke.io/v1beta1
|
2
4
|
kind: ManagedCertificate
|
3
5
|
metadata:
|
4
|
-
name: '<%=
|
6
|
+
name: '<%= cert_name %>'
|
5
7
|
spec:
|
6
|
-
domains: # does not support for willcard domains
|
7
|
-
- '<%=
|
8
|
+
domains: # does not support for willcard domains nor multiple domains
|
9
|
+
- '<%= domain %>'
|
10
|
+
|
11
|
+
---
|
12
|
+
<% end %>
|
@@ -42,6 +42,9 @@ documents:
|
|
42
42
|
periodSeconds: 15
|
43
43
|
readinessProbe: *liveness_probe
|
44
44
|
<% end %>
|
45
|
+
<% if deployment.resources %>
|
46
|
+
<%= include_template "_resources.yml", deployment.resources %>
|
47
|
+
<% end %>
|
45
48
|
<%= include_template "_container_extra_settings.yml", { pod: 'web' } %>
|
46
49
|
|
47
50
|
volumeMounts:
|
@@ -73,6 +76,7 @@ documents:
|
|
73
76
|
env: []
|
74
77
|
volumeMounts:
|
75
78
|
- *cloud_credentials_volume
|
79
|
+
<%= include_template "_resources.yml", deployment.cloudsql_resources || { cpu: { max: '300m', min: '100m' }, mem: { max: '500Mi', min: '200Mi' } } %>
|
76
80
|
<%= include_template "_container_extra_settings.yml", { pod: 'cloudsql' } %>
|
77
81
|
<% end %>
|
78
82
|
|
@@ -80,10 +84,11 @@ documents:
|
|
80
84
|
- &logs_container # print to stdout all log files
|
81
85
|
name: print-logs
|
82
86
|
image: busybox
|
83
|
-
command: [/bin/sh, -c, 'until find <%= deployment.log_folder %>/*.log; do sleep
|
87
|
+
command: [/bin/sh, -c, 'until find <%= deployment.log_folder %>/*.log; do sleep 5; done; tail -n+1 -f <%= deployment.log_folder %>/*.log']
|
84
88
|
env: []
|
85
89
|
volumeMounts:
|
86
90
|
- *log_volume
|
91
|
+
<%= include_template "_resources.yml", deployment.logs_resources || { cpu: { max: '200m', min: '50m' }, mem: { max: '200Mi', min: '50Mi' } } %>
|
87
92
|
<%= include_template "_container_extra_settings.yml", { pod: 'logs' } %>
|
88
93
|
<% end %>
|
89
94
|
|
@@ -117,6 +122,9 @@ documents:
|
|
117
122
|
<% if deployment.job_command %>
|
118
123
|
command: [ "/bin/bash", "-c", "<%= deployment.job_command %>" ]
|
119
124
|
<% end %>
|
125
|
+
<% if deployment.job_resources %>
|
126
|
+
<%= include_template "_resources.yml", deployment.job_resources %>
|
127
|
+
<% end %>
|
120
128
|
<%= include_template "_container_extra_settings.yml", { pod: 'job' } %>
|
121
129
|
|
122
130
|
<% if deployment.job_sidekiq_alive_gem %>
|
data/lib/templates/ingress.yml
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
-
<%
|
2
|
-
|
1
|
+
<%
|
2
|
+
domains = Array(ingress.domain_name || ingress.certificate_domain)
|
3
|
+
domains = domains.map { |domain| cert_name = "#{domain.downcase.gsub(/[^0-9A-Za-z]/, '-')}-lets-encrypt"; domains.count > 1 ? [domain, cert_name] : [domain, ingress.certificate_name || cert_name] }.to_h
|
4
|
+
%>
|
5
|
+
<% if ingress.certificate_domain %>
|
6
|
+
<%= include_template "certificate.yml", { domains: domains } %>
|
3
7
|
<% end %>
|
4
8
|
|
5
9
|
---
|
@@ -17,8 +21,8 @@ metadata:
|
|
17
21
|
kubernetes.io/ingress.global-static-ip-name: "<%=ingress.ip_name%>"
|
18
22
|
<% end %>
|
19
23
|
|
20
|
-
<% if ingress.
|
21
|
-
networking.gke.io/managed-certificates: '<%=
|
24
|
+
<% if ingress.certificate_domain %>
|
25
|
+
networking.gke.io/managed-certificates: '<%= domains.values.join(", ") %>'
|
22
26
|
<% end %>
|
23
27
|
|
24
28
|
spec:
|
data/lib/templates/service.yml
CHANGED
data/lib/templates/settings.rb
CHANGED
@@ -26,7 +26,6 @@ settings = {
|
|
26
26
|
ingress: {
|
27
27
|
name: "#{app_name}-ingress",
|
28
28
|
ip_name: "#{app_name}-static-ip", # nil if static ip is not necessary
|
29
|
-
certificate_name: "#{app_name}-lets-encrypt", # nil if ssl is not required
|
30
29
|
certificate_domain: is_production ? 'myapp.com' : 'beta.myapp.com' # nil if domain is not required
|
31
30
|
},
|
32
31
|
continuous_deployment: {
|
@@ -35,7 +34,7 @@ settings = {
|
|
35
34
|
project_name: 'my-project-name',
|
36
35
|
cluster_name: 'my-cluster-name',
|
37
36
|
cluster_region: 'europe-west4-a',
|
38
|
-
|
37
|
+
docker_cmd: 'docker build -f Dockerfile -t $DEPLOY_NAME .', # using target: 'docker build --target production -f Dockerfile -t $DEPLOY_NAME .'
|
39
38
|
update_deployment: false # permits to reload secrets and re-generate/update deployment yaml
|
40
39
|
},
|
41
40
|
}
|
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.15.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- owen2345
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erb
|
@@ -41,11 +41,14 @@ files:
|
|
41
41
|
- lib/kubernetes_helper/version.rb
|
42
42
|
- lib/templates/README.md
|
43
43
|
- lib/templates/_cd_apply_images.sh
|
44
|
+
- lib/templates/_cd_digital.sh
|
45
|
+
- lib/templates/_cd_google.sh
|
44
46
|
- lib/templates/_container_extra_settings.yml
|
45
47
|
- lib/templates/_custom_containers.yml
|
46
48
|
- lib/templates/_custom_volumes.yml
|
47
49
|
- lib/templates/_job_liveness.yml
|
48
50
|
- lib/templates/_replicas.yml
|
51
|
+
- lib/templates/_resources.yml
|
49
52
|
- lib/templates/_sidekiq_alive_gem.yml
|
50
53
|
- lib/templates/_volumes.yml
|
51
54
|
- lib/templates/cd.sh
|