kubernetes-health 3.11.0 → 3.12.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 588adb5d1b5a937a87c4c71f9b98902faee308d64b679df40784bbf66a7a8cc1
4
- data.tar.gz: ebecbf287f66d584e48b22cbf038dd042cb32bfc7dde3be298c9cc87766ed0fe
3
+ metadata.gz: 3f1527d74312ef1d4c77095b8ee8c87f65dee6308e6340a697cee5bcedfe40f2
4
+ data.tar.gz: 1671089c58d3f7ba73e015a830e2622b324216a21ef25e9a7dc86d1b21527aa5
5
5
  SHA512:
6
- metadata.gz: cd730152d639adbf4402234873b77c00573a61c4c56c8d8ccfd2868518bba40fb3fa0fd7245e38543b52bed32c92647ce4d875af16d9af08bd2f93ae2ee92a4a
7
- data.tar.gz: 9862d03e7c574cdb148473f47ce872f8a83c072fd28f49e7e0fec1c8e4ccd073edfaf1eb4d0b77077299e79ed2f6a56105c7189cc6fdea1f99543062b5886dc5
6
+ metadata.gz: a4b6ad22f24f40d719ab62cf1b14d45c9e4030d899a0370e774d5deface1ebfebfe13feea5047b4c12de09cdf3b4421f05f58165bd086dbc8755f941755c3459
7
+ data.tar.gz: e35f96cbfc2a13a5c49ce2d6e98d1e9c51bdb660e81ce3990d0481a2d7e3a7793dcf349579b1994555d255e22d7f0d63b54f08194bec8d518f662090a26924f1
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # Kubernetes::Health
2
- This gem open a HTTP port for monitoring your rails app while it is running Migrates, Sidekiq and Puma.
2
+ This gem opens a dedicated HTTP port to allow Kubernetes to monitor your Rails app while it is running migrations, Rake tasks, Sidekiq, or Puma.
3
3
 
4
4
  # Features
5
5
  - Puma and Sidekiq metrics for autoscaling.
6
- - Prometheus and JSON metrics (tested using https://github.com/zalando-incubator/kube-metrics-adapter and JSON format).
6
+ - Prometheus and JSON metrics.
7
7
  - add routes `/_readiness`, `/_liveness` on Rails Stack.
8
8
  - add routes `/_readiness`, `/_liveness` and `/_metrics` as a puma plugin at another port to avoid problems when your app get busy. (code copied from `puma-metrics` gem).
9
9
  - add routes `/_readiness` and `/_liveness` while `rake db:migrate` runs. (optional)
10
- - add routes `/_readiness` and `/_liveness` while `sidekiq` runs. (optional)
10
+ - add routes `/_metrics` while `sidekiq` runs. (optional)
11
11
  - add support to avoid parallel running of `rake db:migrate` while keep kubernetes waiting (PostgreSQL required).
12
12
  - allow custom checks for `/_readiness` and `/_liveness`.
13
13
 
@@ -50,7 +50,7 @@ In Kubernetes you need to configure your deployment `readinessProbe` and `livene
50
50
 
51
51
  Setting `failureThreshold` is import to avoid problems when app finish migrates and is starting the web process.
52
52
 
53
- ## Enabling monitoring while `rake db:migrate` runs
53
+ ## Enabling liveness/readiness routes while `rake db:migrate` runs
54
54
 
55
55
  Your Dockerfile's entry script needs to run migrates before start your web app.
56
56
 
@@ -64,7 +64,26 @@ Kubernetes::Health::Config.enable_rack_on_migrate = true
64
64
  ```
65
65
  The defined port at `config/puma.rb` will be used but can be overrided by `KUBERNETES_HEALTH_METRICS_PORT` env var.
66
66
 
67
- ## Enabling monitoring for `sidekiq`
67
+ ## Enabling liveness/readiness routes while any `rake` tasks runs
68
+
69
+ If you need to run another rake tasks than `db:migrate`, like `assets:precompile`, you can enable the monitoring routes by this way:
70
+
71
+ Add a `rake` file enhancing the original task by using `kubernetes_health:rack_on_rake` task. For example:
72
+
73
+ ```
74
+ # File: lib/tasks/kubernetes_health_enable_rack_on_assets_precompile.rake
75
+ Rake::Task['assets:precompile'].enhance(['kubernetes_health:rack_on_rake'])
76
+ ```
77
+
78
+ ```
79
+ # File: lib/tasks/kubernetes_health_enable_rack_on_assets_clobber.rake
80
+ Rake::Task['assets:clobber'].enhance(['kubernetes_health:rack_on_rake'])
81
+ ```
82
+
83
+ I do recommend doing some checks to make it only enabled in the K8S environment.
84
+ The defined port at `config/puma.rb` will be used but can be overrided by `KUBERNETES_HEALTH_METRICS_PORT` env var.
85
+
86
+ ## Enabling liveness/readiness routes for `sidekiq`
68
87
 
69
88
  Add `KUBERNETES_HEALTH_ENABLE_RACK_ON_SIDEKIQ=true` environment variable.
70
89
 
@@ -76,11 +95,12 @@ Kubernetes::Health::Config.enable_rack_on_sidekiq = true
76
95
  ```
77
96
  The defined port at `config/puma.rb` will be used but can be overrided by `KUBERNETES_HEALTH_METRICS_PORT` env var.
78
97
 
79
- ### How `rake db:migrate` and `sidekiq` monitoring works
80
- It will run a RACK server for `/_readiness`, `/_liveness` and `/_metrics`.
98
+ ### How `rake` and `sidekiq` monitoring works
99
+ It will run a rack server for `/_readiness`, `/_liveness` and `/_metrics` for rake and `/_metrics` for Sidekiq.
100
+ The liveness route will respond using `200` but readiness `503`.
81
101
 
82
102
  ## Avoiding migrations running in parallel and making kubernetes happy.
83
- Rails already avoid migrations running in parallel, but it raise exceptions. This gem will just wait for other migrations without exit.
103
+ Rails already avoid migrations running in parallel, but it raises exceptions. This gem will just wait for other migrations without exit.
84
104
  If you enable `rack_on_migrate` together with this, kubernetes will just wait, avoiding erros.
85
105
 
86
106
 
@@ -97,7 +117,7 @@ Kubernetes::Health::Config.enable_lock_on_migrate = true
97
117
  By default it is working for PostgreSQL, but you can customize it using a lambda:
98
118
  ```
99
119
  Kubernetes::Health::Config.lock_or_wait = lambda {
100
- ActiveRecord::Base.connection.execute 'select pg_advisory_lock(123456789123456789);'
120
+ ActiveRecord::Base.connection.execute "SET lock_timeout TO '3600s'; SELECT pg_advisory_lock(123456789123456789);"
101
121
  }
102
122
 
103
123
  Kubernetes::Health::Config.unlock = lambda {
@@ -17,7 +17,7 @@ module Kubernetes
17
17
  Rails.logger.debug "Kubernetes Health - Request: Path: #{req.path_info} / Params: #{req.params} / HTTP Code: #{http_code}\n#{content}" rescue nil
18
18
  }
19
19
 
20
- @@lock_or_wait = lambda { ActiveRecord::Base.connection.execute 'select pg_advisory_lock(123456789123456789);' }
20
+ @@lock_or_wait = lambda { ActiveRecord::Base.connection.execute "SET lock_timeout TO '3600s'; SELECT pg_advisory_lock(123456789123456789);" }
21
21
  @@unlock = lambda { ActiveRecord::Base.connection.execute 'select pg_advisory_unlock(123456789123456789);' }
22
22
 
23
23
  def self.lock_or_wait
@@ -1,5 +1,5 @@
1
1
  namespace :kubernetes_health do
2
- task :lock_on_migrate do
2
+ task :lock_on_rake do
3
3
  Rails.logger.info "Kubernetes Health: Lock on Migrate - Locking or waiting started."
4
4
  Kubernetes::Health::Config.lock_or_wait.call
5
5
  Rails.logger.info "Kubernetes Health: Lock on Migrate - Locking or waiting finished."
@@ -9,5 +9,4 @@ namespace :kubernetes_health do
9
9
  Rails.logger.info "Kubernetes Health: Lock on Migrate - Unlocking finished."
10
10
  }
11
11
  end
12
- end
13
- Rake::Task['db:migrate'].enhance(['kubernetes_health:lock_on_migrate'])
12
+ end
@@ -0,0 +1,10 @@
1
+ require 'rack'
2
+ require "kubernetes/health/rack_on_rake"
3
+
4
+ namespace :kubernetes_health do
5
+ task :rack_on_rake do
6
+ Thread.new do
7
+ Rack::Handler::WEBrick.run Kubernetes::Health::RackOnRake.new, { Port: Kubernetes::Health::Config.metrics_port }
8
+ end
9
+ end
10
+ end
@@ -1,6 +1,6 @@
1
1
  module Kubernetes
2
2
  module Health
3
- class RackOnMigrate
3
+ class RackOnRake
4
4
  def call(env)
5
5
  req = ::Rack::Request.new(env)
6
6
  case req.path_info
@@ -2,8 +2,10 @@ module Kubernetes
2
2
  module Health
3
3
  class Railtie < Rails::Railtie
4
4
  rake_tasks do
5
- load 'kubernetes/health/rack_on_migrate.rake' if Kubernetes::Health::Config.enable_rack_on_migrate
6
- load 'kubernetes/health/lock_on_migrate.rake' if Kubernetes::Health::Config.enable_lock_on_migrate
5
+ load 'kubernetes/health/rack_on_rake.rake'
6
+ load 'kubernetes/health/lock_on_rake.rake'
7
+ Rake::Task['db:migrate'].enhance(['kubernetes_health:rack_on_rake']) if Kubernetes::Health::Config.enable_rack_on_migrate
8
+ Rake::Task['db:migrate'].enhance(['kubernetes_health:lock_on_rake']) if Kubernetes::Health::Config.enable_lock_on_migrate
7
9
  end
8
10
  end
9
11
  end
@@ -1,5 +1,5 @@
1
1
  module Kubernetes
2
2
  module Health
3
- VERSION = '3.11.0'.freeze
3
+ VERSION = '3.12.1'.freeze
4
4
  end
5
5
  end
@@ -22,7 +22,7 @@ module Puma
22
22
  case req.path_info
23
23
  when ::Kubernetes::Health::Config.route_liveness
24
24
  i_am_live = ::Kubernetes::Health::Config.live_if.arity.zero? ? ::Kubernetes::Health::Config.live_if.call : ::Kubernetes::Health::Config.live_if.call(req.params)
25
- http_code = puma_already_started?(extended_puma_stats) && i_am_live ? 200 : 503
25
+ http_code = i_am_live ? 200 : 503
26
26
  when ::Kubernetes::Health::Config.route_readiness
27
27
  i_am_ready = ::Kubernetes::Health::Config.ready_if.arity.zero? ? ::Kubernetes::Health::Config.ready_if.call : ::Kubernetes::Health::Config.ready_if.call(req.params)
28
28
  http_code = puma_already_started?(extended_puma_stats) && i_am_ready ? 200 : 503
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubernetes-health
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.11.0
4
+ version: 3.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wagner Caixeta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-21 00:00:00.000000000 Z
11
+ date: 2024-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -140,9 +140,9 @@ files:
140
140
  - lib/kubernetes/health.rb
141
141
  - lib/kubernetes/health/config.rb
142
142
  - lib/kubernetes/health/engine.rb
143
- - lib/kubernetes/health/lock_on_migrate.rake
144
- - lib/kubernetes/health/rack_on_migrate.rake
145
- - lib/kubernetes/health/rack_on_migrate.rb
143
+ - lib/kubernetes/health/lock_on_rake.rake
144
+ - lib/kubernetes/health/rack_on_rake.rake
145
+ - lib/kubernetes/health/rack_on_rake.rb
146
146
  - lib/kubernetes/health/rack_on_sidekiq.rb
147
147
  - lib/kubernetes/health/railtie.rb
148
148
  - lib/kubernetes/health/sidekiq_options_resolver.rb
@@ -1,11 +0,0 @@
1
- require 'rack'
2
- require "kubernetes/health/rack_on_migrate"
3
-
4
- namespace :kubernetes_health do
5
- task :rack_on_migrate do
6
- Thread.new do
7
- Rack::Handler::WEBrick.run Kubernetes::Health::RackOnMigrate.new, { Port: Kubernetes::Health::Config.metrics_port }
8
- end
9
- end
10
- end
11
- Rake::Task['db:migrate'].enhance(['kubernetes_health:rack_on_migrate'])