kubernetes_helper 0.3.0 → 1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c95b3029857b4b9793c337abb65f8d983376db3055bd411b8982907cce9b5a1d
4
- data.tar.gz: '08a11127589a60bd3fc340c11a2ddecd635a44347dd77506686d932093a53f4f'
3
+ metadata.gz: 528e61587a3011330c7e474b523ce04079c8e49483bf894d5eb1806927436888
4
+ data.tar.gz: 9df4e7aab70108b75831077d2f4390067e5807664050f70e1557f504835144bd
5
5
  SHA512:
6
- metadata.gz: 05e61bc79a77cbce7894430788ccfdddfceb3ac17041453f55dda0809ba42b8f50ab970dc72a566726f5548b1df3aa9bff068ef98adfd21e867d729b0bed316f
7
- data.tar.gz: 5464337442b676f531ff14f294652ebe43270110f054dcba401045e5e75b92352ac1432ebb7b281bf84cd91c87c0f1ee9984ed0578d535287926b6751f1e446c
6
+ metadata.gz: 9ed371a62084598f9bb4755df54ca059ffbeb729731555e4a6a6cf58dceecc0c347741921b5c46fc11408a937c914ea2d3fddaf3da94bc801dd6cd7f9ecc1ed8
7
+ data.tar.gz: 6248035be5a30995594a39ad56b73b811935b3b55988d067173a530ee0522294f6afb0e6361f664d4f696e7b370ce7b816ed74a53cb3f416c584e12c76ff023d
data/README.md CHANGED
@@ -1,37 +1,61 @@
1
1
  # KubernetesHelper
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kubernetes_helper`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ 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.
4
+ Configuration and customization can be done for multiple environments and at any level which permits to deploy simple and complex applications.
6
5
 
7
6
  ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'kubernetes_helper'
7
+ ```bash
8
+ cd my_app/
9
+ gem install kubernetes_helper -v '~> 1.0'
10
+ kubernetes_helper generate_templates
13
11
  ```
14
-
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install kubernetes_helper
12
+ Note: Requires ruby 1.7+
22
13
 
23
14
  ## Usage
15
+ - Run any kubernetes document
16
+ `DEPLOY_ENV=<env name> kubernetes_helper run_deployment "<document name>" "<bash command>"`
17
+ Evaluates the kubernetes document with the following details:
18
+ - Supports for `- documents` to include multiple documents in a file and share yml variables between them (Sample: `lib/templates/deployment.yml#1`)
19
+ - Replaces all setting values based on `DEPLOY_ENV`
20
+ - Supports for secrets auto importer using `import_secrets: ['secrets.yml', '<%=secrets.name%>']` (Sample: `lib/templates/deployment.yml#29`)
21
+ - Supports for template including by `include_template 'template_name.yml.erb'`
22
+ Sample: `DEPLOY_ENV=beta kubernetes_helper run_deployment "deployment.yml" "kubectl create"`
23
+
24
+ - Run kubernetes commands
25
+ `DEPLOY_ENV=<env name> rake kubernetes_helper:run_command "<bash or k8s commands>"`
26
+ Replaces all setting variables inside command based on `DEPLOY_ENV` and performs it as a normal bash command.
27
+ Sample: `DEPLOY_ENV=beta rake kubernetes_helper:run_command "gcloud compute addresses create \#{ingress.ip_name} --global"'`
28
+
29
+ - Run kubernetes bash scripts
30
+ `DEPLOY_ENV=<env name> kubernetes_helper run_script "<script name>"`
31
+ Performs the script name located inside `.kubernetes` folder or kubernetes_helper template as the second option.
32
+ All setting variables inside the script will be replaced based on `DEPLOY_ENV`.
33
+ Sample: `DEPLOY_ENV=beta kubernetes_helper run_script "cd.sh"`
34
+
35
+ - Generate templates
36
+ `DEPLOY_ENV=<env name> kubernetes_helper generate_templates "<mode_or_template_name>"`
37
+ Copy files based on mode (`basic|advanced`) or a specific file from templates.
38
+ Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "basic"`
39
+ Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "ingress.yml"`
40
+
41
+ When performing a script it looks first for file inside .kubernetes folder, if not exist,
42
+ it looks for the file inside kubernetes_helper template folder.
43
+
44
+ ## Templating
45
+ When performing a command or script, the setting variables are replaced based on `DEPLOY_ENV`.
46
+ All these setting variable values are configured in `.kubernetes/settings.rb` which defines the values based on `DEPLOY_ENV`.
47
+ These setting variables use [erb](https://github.com/ruby/erb) template gem to define variable replacement and conditional blocks, and so on.
48
+ Note: Setting variable values are referenced as an object format instead of a hash format for simplicity.
49
+
50
+
51
+ ## Deployment
52
+ 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.
53
+
54
+ ## Sample
55
+ https://owen2345.github.io/kubernetes_helper/
24
56
 
25
57
  ## TODO
26
- - Documentation
27
- - Include conditional blocks
28
- - Include hardcoded env values
29
- - Tasks
30
- - Add docs to include partials using `include_template 'sample.yml.erb'`
31
- - Rake verify files
32
58
  - Add one_step_configuration.sh
33
- - Fix if/end_if for blocks
34
- - Ability to copy specific template
35
59
 
36
60
  ## Contributing
37
61
 
@@ -9,8 +9,8 @@ case ARGV[0]
9
9
  when 'run_command'
10
10
  KubernetesHelper::Core.new(ENV['DEPLOY_ENV']).run_command(ARGV[1])
11
11
  # Run the deployment script.
12
- # Sample: DEPLOY_ENV=beta kubernetes_helper run_deployment "cd_gcloud.sh"
13
- when 'run_deployment'
12
+ # Sample: DEPLOY_ENV=beta kubernetes_helper run_deployment "cd.sh"
13
+ when 'run_deployment', 'run_script'
14
14
  script_path = KubernetesHelper.settings_path(ARGV[1], use_template: true)
15
15
  KubernetesHelper::Core.new(ENV['DEPLOY_ENV']).run_script(script_path)
16
16
  # Parses kubernetes yml files (supporting multiple documents, Config variables replacement, include secrets).
@@ -22,6 +22,7 @@ when 'run_yml'
22
22
  .parse_yml_file(KubernetesHelper.settings_path(ARGV[1], use_template: true), output_path)
23
23
  KubernetesHelper.run_cmd("#{ARGV[2]} -f #{output_path}")
24
24
  # Generate template files
25
+ # Sample: DEPLOY_ENV=beta kubernetes_helper generate_templates "basic"
25
26
  when 'generate_templates' # Sample: kubernetes_helper generate basic
26
27
  mode = ARGV[1] || 'basic'
27
28
  KubernetesHelper.copy_templates(mode)
@@ -39,11 +39,14 @@ module KubernetesHelper
39
39
  file_name ? File.join(path, file_name) : path
40
40
  end
41
41
 
42
- # @param mode (basic, advanced)
43
- def self.copy_templates(mode)
42
+ # @param mode_or_file (basic, advanced, String) mode name or any specific template name
43
+ def self.copy_templates(mode_or_file)
44
44
  FileUtils.mkdir(settings_path) unless Dir.exist?(settings_path)
45
+ template_path = templates_path(mode_or_file)
46
+ return FileUtils.cp(template_path, settings_path(mode_or_file)) if File.exist?(template_path)
47
+
45
48
  files = %w[README.md secrets.yml settings.rb]
46
- files += %w[deployment.yml cd.sh ingress.yml service.yml] if mode == 'advanced'
49
+ files += %w[deployment.yml cd.sh ingress.yml service.yml] if mode_or_file == 'advanced'
47
50
  files.each do |name|
48
51
  path = settings_path(name)
49
52
  FileUtils.cp(templates_path(name), path) unless File.exist?(path)
@@ -57,7 +57,7 @@ module KubernetesHelper
57
57
  File.write(tmp_file, content)
58
58
  KubernetesHelper.run_cmd("chmod +x #{tmp_file}")
59
59
  KubernetesHelper.run_cmd(tmp_file)
60
- File.delete(tmp_file)
60
+ # File.delete(tmp_file) # keep tmp script for analysis purpose
61
61
  end
62
62
 
63
63
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KubernetesHelper
4
- VERSION = '0.3.0'
4
+ VERSION = '1.0'
5
5
  end
@@ -2,54 +2,56 @@
2
2
 
3
3
  ## Configure a new application environment
4
4
  - Create the project on Gcloud
5
- - Set the project where to work on
5
+ - Set the project where to work on
6
6
  `gcloud config set project my-project`
7
7
 
8
- - Create the cluster (Only if not exist)
8
+ - Create the cluster (Only if not exist)
9
9
  `gcloud container clusters create my-cluster`
10
10
  `# gcloud container clusters list --region europe-west4-a # to list clusters`
11
+ Note: `--preemptible` recommended for non production environments
11
12
 
12
- - Use the cluster/project as default
13
+ - Use the cluster/project as default
13
14
  `gcloud container clusters get-credentials my-cluster --zone europe-west4-a`
14
15
 
15
- - Install helper for the next commands
16
+ - Install helper for the next commands
16
17
  `gem install kubernetes_helper`
17
18
 
18
- - Verify or update k8s settings in .kubernetes/settings.rb
19
+ - Verify or update k8s settings in .kubernetes/settings.rb
20
+ Note: Please do not include sensitive values in this file, ENV values are recommended instead.
19
21
 
20
- - Register shared cloudsql proxy configuration (only if not exists)
22
+ - Register shared cloudsql proxy configuration (only if not exists)
21
23
  ```bash
22
24
  DEPLOY_ENV=beta kubernetes_helper run_command "kubectl create secret generic <%=deployment.cloud_secret_name%> --from-file=credentials.json=<path-to-downloaded/credentials.json>"
23
25
  ```
24
26
 
25
- - Register manually env vars (values must be encrypted using base64)
27
+ - Register manually env vars (values must be encrypted using base64)
26
28
  Open and register secret values in `.kubernetes/secrets.yml`
27
- Note: Enter base64 encoded values
29
+ Note: Enter base64 encoded values
28
30
  ```bash
29
31
  DEPLOY_ENV=beta kubernetes_helper run_yml 'secrets.yml' 'kubectl create'
30
32
  # kubectl get secrets # to list all secrets registered
31
33
  ```
32
34
 
33
- - Create deployment to run application
35
+ - Create deployment to run application
34
36
  ```bash
35
37
  DEPLOY_ENV=beta kubernetes_helper run_yml 'deployment.yml' 'kubectl create'
36
38
  # kubectl get deployment # to list deployments
37
39
  ```
38
40
 
39
- - Create service to connect pods and ingress
41
+ - Create service to connect pods and ingress
40
42
  ```bash
41
43
  DEPLOY_ENV=beta kubernetes_helper run_yml 'service.yml' 'kubectl create'
42
44
  # kubectl get services # to list all registered services
43
45
  ```
44
46
 
45
- - Create the public ip address (only if static ip is required)
47
+ - Create the public ip address (only if static ip is required)
46
48
  ```bash
47
49
  DEPLOY_ENV=beta kubernetes_helper run_command "gcloud compute addresses create <%=ingress.ip_name%> --global"
48
50
  # gcloud compute addresses list # to list static ips generated
49
51
  # Copy new external ip generated by the previous command and point your domain to it
50
52
  ```
51
53
 
52
- - Register ingress to receive external http calls (includes ssl certificates if defined)
54
+ - Register ingress to receive external http calls (includes ssl certificates if defined)
53
55
  ```bash
54
56
  DEPLOY_ENV=beta kubernetes_helper run_yml 'ingress.yml' 'kubectl create'
55
57
  # kubectl get ingress # to list all registered ingresses
@@ -60,18 +62,18 @@
60
62
  ```
61
63
 
62
64
  ## Apply any k8s setting changes
63
- - Secrets
65
+ - Secrets
64
66
  Open kubernetes secrets and add/edit/remove values and then save it
65
67
  `kubectl edit secret ...`
66
68
  Once secrets were updated, then restart all related pods, see: https://medium.com/devops-dudes/how-to-propagate-a-change-in-kubernetes-secrets-by-restarting-dependent-pods-b71231827656
67
69
 
68
- - Other settings
70
+ - Other settings
69
71
  ```bash
70
72
  DEPLOY_ENV=beta kubernetes_helper run_yml 'deployment.yml' 'kubectl apply'
71
73
  ```
72
74
 
73
75
  ## Configure continuous deployment for github actions
74
- * Go to github repository settings
76
+ * Go to github repository settings
75
77
  * Register a new secret variable with content downloaded from https://console.cloud.google.com/iam-admin/serviceaccounts
76
78
  (Make sure to attach a "Editor", "Storage Admin" and "Kubernetes engine cluster admin" role to the service account)
77
79
  ```bash
@@ -79,15 +81,16 @@
79
81
  production: PROD_CLOUD_TOKEN=<secret content here>
80
82
  ```
81
83
 
82
- * Add action to run deployment:
84
+ * Add action to run deployment:
83
85
  ```bash
84
86
  env:
85
87
  KB_AUTH_TOKEN: secrets.BETA_CLOUD_TOKEN
86
88
  run: DEPLOY_ENV=beta kubernetes_helper run_deployment 'cd.sh'
87
89
  ```
88
90
 
89
- * Sample:
91
+ * Sample:
90
92
  ```yml
93
+ - run: sudo gem install kubernetes_helper
91
94
  - name: Staging deployment
92
95
  env: # Env variable saved in github that contains gcloud credential (json format)
93
96
  KB_AUTH_TOKEN: ${{ secrets.BETA_GOOGLE_AUTH }}
@@ -0,0 +1,12 @@
1
+ - apiVersion: autoscaling/v1
2
+ kind: HorizontalPodAutoscaler
3
+ metadata:
4
+ name: <%= "#{deployment.name}-replicas" %>
5
+ spec:
6
+ scaleTargetRef:
7
+ apiVersion: apps/v1
8
+ kind: Deployment
9
+ name: <%= deployment.name %>
10
+ minReplicas: <%= deployment.replicas_range[0] %>
11
+ maxReplicas: <%= deployment.replicas_range[1] %>
12
+ targetCPUUtilizationPercentage: 50
data/lib/templates/cd.sh CHANGED
@@ -2,19 +2,22 @@
2
2
  set -e
3
3
  # expected ENV VAR "KB_AUTH_TOKEN"
4
4
 
5
- SCRIPT_DIR=`dirname "$(realpath -s "$0")"`
5
+ SCRIPT_DIR=`dirname "$(realpath -s "$0")"` # app_dir/.kubernetes/
6
+ cd "$SCRIPT_DIR/../" # project directory
6
7
 
7
- DEPLOYMENTS="<%=[deployment.name, deployment.job_name].join(',')%>"
8
+ DEPLOYMENTS="<%=[deployment.job_name, deployment.name].join(',')%>"
8
9
  IMAGE_NAME="<%=continuous_deployment.image_name%>"
9
10
  CLUSTER_NAME="<%=continuous_deployment.cluster_name%>"
10
11
  PROJECT_NAME="<%=continuous_deployment.project_name%>"
11
12
  CLUSTER_REGION="<%=continuous_deployment.cluster_region%>"
12
- DOCKER_BUILD_CMD="<%=continuous_deployment.docker_build_cmd%>"
13
+ DOCKER_BUILD_CMD="<%=continuous_deployment.docker_build_cmd || 'build -f Dockerfile'%>"
13
14
 
14
15
  CI_COMMIT_SHA=$(git rev-parse --verify HEAD)
15
16
  DEPLOY_NAME="${IMAGE_NAME}:${CI_COMMIT_SHA}"
16
17
  LATEST_NAME="${IMAGE_NAME}:latest"
17
18
  AUTH_PATH="$SCRIPT_DIR/k8s-auth-token.json"
19
+
20
+ rm -f -- $AUTH_PATH
18
21
  echo $KB_AUTH_TOKEN >> $AUTH_PATH
19
22
 
20
23
  ## ***** GOOGLE CONNECTOR
@@ -42,5 +45,5 @@ for deployment in "${deployments[@]}"; do
42
45
  [ -z "$deployment" ] && continue # if empty value
43
46
 
44
47
  kubectl set image deployment/$deployment $deployment=$DEPLOY_NAME
45
- [ "$deployment" = "${deployments[0]}" ] && kubectl rollout status deployment/$deployment
48
+ [ "$deployment" = "${deployments[0]}" ] && kubectl rollout status deployment/$deployment || true
46
49
  done
@@ -46,17 +46,19 @@ documents:
46
46
  - &log_volume
47
47
  name: applog
48
48
  mountPath: /app/log
49
+ - &cloud_credentials_volume
50
+ name: gcloud-creds
51
+ mountPath: /secrets/gcloud
52
+ readOnly: true
49
53
 
50
54
  - &cloudsql_container
51
55
  image: gcr.io/cloudsql-docker/gce-proxy:1.09 # Cloud sql proxy
52
56
  name: cloudsql-proxy
53
57
  command: ["/cloud_sql_proxy", "--dir=/cloudsql",
54
58
  "-instances=<%=deployment.cloud_sql_instance%>",
55
- "-credential_file=/secrets/cloudsql/credentials.json"]
59
+ "-credential_file=/secrets/gcloud/credentials.json"]
56
60
  volumeMounts:
57
- - name: cloudsql-creds
58
- mountPath: /secrets/cloudsql
59
- readOnly: true
61
+ - *cloud_credentials_volume
60
62
 
61
63
  - &logs_container # print to stdout all log files
62
64
  name: print-logs
@@ -65,8 +67,9 @@ documents:
65
67
  volumeMounts:
66
68
  - *log_volume
67
69
 
70
+ terminationGracePeriodSeconds: 120
68
71
  volumes:
69
- - name: cloudsql-creds
72
+ - name: gcloud-creds
70
73
  secret:
71
74
  secretName: '<%=deployment.cloud_secret_name%>'
72
75
  - name: applog
@@ -116,3 +119,7 @@ documents:
116
119
  - *cloudsql_container
117
120
  - <<: *logs_container
118
121
  <% end %>
122
+
123
+ <% if deployment.replicas_range %>
124
+ <%= include_template "_replicas.yml.erb" %>
125
+ <% end %>
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- is_beta = ENV['DEPLOY_ENV'] == 'beta'
4
- app_name = is_beta ? 'my_beta_app' : 'my_app'
3
+ is_production = ENV['DEPLOY_ENV'] == 'production'
4
+ app_name = is_production ? 'my_app' : 'my_beta_app'
5
5
  settings = {
6
6
  deployment: {
7
7
  name: app_name,
8
- replicas: is_beta ? 1 : 2,
9
- cloud_secret_name: "#{is_beta ? 'beta' : 'production'}-cloud-secret",
8
+ replicas: is_production ? 2 : 1,
9
+ cloud_secret_name: "#{is_production ? 'production' : 'beta'}-cloud-secret",
10
10
  cloud_sql_instance: 'xxx:xxx:xxx=tcp:5432', # 5432 => postgres, 3306 => mysql
11
11
  env_vars: {}, # Sample: { 'CUSTOM_VAR' => 'value' }
12
12
  # command: '', # custom container command (default empty to be managed by Dockerfile)
@@ -19,13 +19,14 @@ settings = {
19
19
  name: "#{app_name}-ingress",
20
20
  ip_name: "#{app_name}-static-ip", # nil if static ip is not necessary
21
21
  certificate_name: "#{app_name}-lets-encrypt", # nil if ssl is not required
22
- domain_name: is_beta ? 'beta.myapp.com' : 'myapp.com' # nil if domain is not required
22
+ domain_name: is_production ? 'myapp.com' : 'beta.myapp.com' # nil if domain is not required
23
23
  },
24
24
  continuous_deployment: {
25
25
  image_name: "gcr.io/my-account/#{app_name}",
26
26
  project_name: 'my-project-name',
27
27
  cluster_name: 'my-cluster-name',
28
- cluster_region: 'europe-west4-a'
28
+ cluster_region: 'europe-west4-a',
29
+ docker_build_cmd: 'build -f Dockerfile'
29
30
  },
30
31
  secrets: {
31
32
  name: "#{app_name}-secrets"
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: 0.3.0
4
+ version: '1.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - owen2345
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-03 00:00:00.000000000 Z
11
+ date: 2021-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erb
@@ -40,6 +40,7 @@ files:
40
40
  - lib/kubernetes_helper/railtie.rb
41
41
  - lib/kubernetes_helper/version.rb
42
42
  - lib/templates/README.md
43
+ - lib/templates/_replicas.yml.erb
43
44
  - lib/templates/_sidekiq_alive_gem.yml.erb
44
45
  - lib/templates/cd.sh
45
46
  - lib/templates/deployment.yml