kubernetes-health 1.0.0 → 2.0.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 +4 -4
- data/.gitignore +1 -1
- data/README.md +51 -13
- data/app/controllers/kubernetes/health_controller.rb +10 -4
- data/config/routes.rb +2 -1
- data/lib/kubernetes/health/config.rb +46 -10
- data/lib/kubernetes/health/rack_on_migrate.rake +22 -0
- data/lib/kubernetes/health/railtie.rb +9 -0
- data/lib/kubernetes/health/version.rb +1 -1
- data/lib/kubernetes/health.rb +2 -1
- 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: 809331bcf24f88f951d92290782878d1e52ae93f789bdeddafe9096fff731402
|
4
|
+
data.tar.gz: 83ca0cbff7856d12eb1fe5ac217cafb8d6c578c9cc891bc626f3645649d92eb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6007ce776ce879c62b57756eaa5e179fb581f9549aa9e323cd252b4f422f6cfb72a67280691028b785d3a609e9a3c7d330bf2b6b86e539ec49b50ce0cb6e1511
|
7
|
+
data.tar.gz: dd4a6316ddbd1272b1cff7b2c79b0dab83125dfe4a4b1cae644e4dcd379a5e43498e4f3667d5ebe339c971e74b594afecf734caf1d1c5f9245d8ddcc3e900f2a
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,40 +1,78 @@
|
|
1
1
|
# Kubernetes::Health
|
2
2
|
|
3
|
-
A
|
3
|
+
A gem that adds `/_readiness` and `/_liveness` and allows Kubernetes monitor your rails using HTTP while migrates are running.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'kubernetes-health', '~>
|
10
|
+
gem 'kubernetes-health', '~> 2.0'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
14
14
|
|
15
|
-
|
15
|
+
$ bundle
|
16
16
|
|
17
|
-
|
17
|
+
## Enable migrates monitoring.
|
18
18
|
|
19
|
-
|
19
|
+
This will run a Rack server for `/_readiness` route and will return `200` and `503` HTTP CODES alternately while your migrates are running.
|
20
|
+
|
21
|
+
If readinessProbe\`s failureThreshold=3 and successThreshold=3 it never will be reach until migrate ends.
|
22
|
+
|
23
|
+
Of course your Dockerfile entry script needs to run migrates before start your web app.
|
24
|
+
|
25
|
+
Add `KUBERNETES_HEALTH_ENABLE_RACK_ON_MIGRATE=true` environment variable.
|
26
|
+
|
27
|
+
or
|
28
|
+
|
29
|
+
```
|
30
|
+
Kubernetes::Health::Config.enable_rack_on_migrate = true # default: false
|
31
|
+
```
|
32
|
+
|
33
|
+
On your app.
|
34
|
+
|
35
|
+
If you need:
|
36
|
+
|
37
|
+
```
|
38
|
+
Kubernetes::Health::Config.rack_on_migrate_rotate_http_codes = [200, 503] # default: [202, 503]
|
39
|
+
```
|
40
|
+
|
41
|
+
Configure your deployment readinessProbe:
|
42
|
+
```
|
43
|
+
readinessProbe:
|
44
|
+
httpGet:
|
45
|
+
path: /_readiness
|
46
|
+
port: 80
|
47
|
+
initialDelaySeconds: 10
|
48
|
+
timeoutSeconds: 5
|
49
|
+
failureThreshold: 3
|
50
|
+
successThreshold: 3
|
51
|
+
```
|
20
52
|
|
21
53
|
## Custom check
|
22
54
|
|
23
|
-
Set Kubernetes::Health::Config.
|
55
|
+
Set Kubernetes::Health::Config.ready_if if you want to check other things.
|
24
56
|
|
25
|
-
Ex. Check if PostgreSQL is working
|
57
|
+
Ex. Check if PostgreSQL is working. `params` is optional.
|
26
58
|
```
|
27
|
-
Kubernetes::Health::Config.
|
28
|
-
return true if params[:wait_migration] == 'true' && File.exists?('migration.lock')
|
59
|
+
Kubernetes::Health::Config.ready_if = lambda { |params|
|
29
60
|
ActiveRecord::Base.connection.execute("SELECT 1").cmd_tuples != 1
|
30
61
|
}
|
31
62
|
```
|
32
63
|
|
33
|
-
|
64
|
+
Set Kubernetes::Health::Config.live_if if you want to check other things.
|
65
|
+
|
66
|
+
Ex. Check if PostgreSQL is working. `params` is optional.
|
67
|
+
```
|
68
|
+
Kubernetes::Health::Config.ready_if = lambda { |params|
|
69
|
+
ActiveRecord::Base.connection.execute("SELECT 1").cmd_tuples != 1
|
70
|
+
}
|
71
|
+
```
|
34
72
|
|
35
|
-
Set Kubernetes::Health::Config.route if you want to use other route.
|
36
73
|
|
37
|
-
|
74
|
+
## Custom routes
|
38
75
|
```
|
39
|
-
Kubernetes::Health::Config.
|
76
|
+
Kubernetes::Health::Config.route_liveness = '/liveness'
|
77
|
+
Kubernetes::Health::Config.route_readiness = '/readiness
|
40
78
|
```
|
@@ -1,9 +1,15 @@
|
|
1
1
|
module Kubernetes
|
2
2
|
class HealthController < ::ActionController::Base
|
3
|
-
def
|
4
|
-
|
5
|
-
return head
|
6
|
-
head
|
3
|
+
def liveness
|
4
|
+
i_am_live = Kubernetes::Health::Config.live_if.arity == 0 ? Kubernetes::Health::Config.live_if.call : Kubernetes::Health::Config.live_if.call(params)
|
5
|
+
return head 200 if i_am_live
|
6
|
+
head 503
|
7
|
+
end
|
8
|
+
|
9
|
+
def readiness
|
10
|
+
i_am_ready = Kubernetes::Health::Config.ready_if.arity == 0 ? Kubernetes::Health::Config.ready_if.call : Kubernetes::Health::Config.ready_if.call(params)
|
11
|
+
return head 200 if i_am_ready
|
12
|
+
head 503
|
7
13
|
end
|
8
14
|
end
|
9
15
|
end
|
data/config/routes.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
-
get Kubernetes::Health::Config.
|
2
|
+
get Kubernetes::Health::Config.route_liveness, to: 'kubernetes/health#liveness'
|
3
|
+
get Kubernetes::Health::Config.route_readiness, to: 'kubernetes/health#readiness'
|
3
4
|
end
|
@@ -1,23 +1,59 @@
|
|
1
1
|
module Kubernetes
|
2
2
|
module Health
|
3
3
|
class Config
|
4
|
-
@@
|
5
|
-
@@
|
4
|
+
@@live_if = lambda { true }
|
5
|
+
@@ready_if = lambda { true }
|
6
|
+
@@enable_rack_on_migrate = ActiveRecord::Type::Boolean.new.cast(ENV['KUBERNETES_HEALTH_ENABLE_RACK_ON_MIGRATE']) || false
|
7
|
+
@@rack_on_migrate_rotate_http_codes = [503, 200]
|
8
|
+
@@route_liveness = '/_liveness'
|
9
|
+
@@route_readiness = '/_readiness'
|
6
10
|
|
7
|
-
def self.
|
8
|
-
@@
|
11
|
+
def self.enable_rack_on_migrate
|
12
|
+
@@enable_rack_on_migrate
|
9
13
|
end
|
10
14
|
|
11
|
-
def self.
|
12
|
-
@@
|
15
|
+
def self.enable_rack_on_migrate=(value)
|
16
|
+
@@enable_rack_on_migrate = value
|
13
17
|
end
|
14
18
|
|
15
|
-
def self.
|
16
|
-
@@
|
19
|
+
def self.rack_on_migrate_rotate_http_codes
|
20
|
+
@@rack_on_migrate_rotate_http_codes
|
17
21
|
end
|
18
22
|
|
19
|
-
def self.
|
20
|
-
@@
|
23
|
+
def self.rack_on_migrate_rotate_http_codes=(value)
|
24
|
+
@@rack_on_migrate_rotate_http_codes = value
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.route_liveness
|
28
|
+
@@route_liveness
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.route_liveness=(value)
|
32
|
+
@@route_liveness = value
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.route_readiness
|
36
|
+
@@route_readiness
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.route_readiness=(value)
|
40
|
+
@@route_readiness = value
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.live_if
|
44
|
+
@@live_if
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.live_if=(value)
|
48
|
+
@@live_if = value
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.ready_if
|
52
|
+
@@ready_if
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.ready_if=(value)
|
56
|
+
@@ready_if = value
|
21
57
|
end
|
22
58
|
end
|
23
59
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
namespace :kubernetes_health do
|
2
|
+
task :before_migrate do
|
3
|
+
Thread.new {
|
4
|
+
require 'rack'
|
5
|
+
@counter=0
|
6
|
+
Rack::Handler.default.run ->(env) {
|
7
|
+
req = Rack::Request.new(env)
|
8
|
+
valid = req.path_info == "#{Kubernetes::Health::Config.route_readiness}"
|
9
|
+
if valid
|
10
|
+
@counter=@counter+1
|
11
|
+
http_codes = Kubernetes::Health::Config.rack_on_migrate_rotate_http_codes
|
12
|
+
http_code = http_codes[(@counter % http_codes.size)]
|
13
|
+
else
|
14
|
+
http_code = 404
|
15
|
+
end
|
16
|
+
Rails.logger.info "Kubernetes Health: Rack on Migrate - Path: #{req.path_info} / Params: #{req.params} / Count: #{@counter} / HTTP Code: #{http_code}"
|
17
|
+
[http_code, {}, []]
|
18
|
+
}
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
Rake::Task['db:migrate'].enhance(['kubernetes_health:before_migrate'])
|
data/lib/kubernetes/health.rb
CHANGED
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:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wagner Caixeta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,6 +73,8 @@ files:
|
|
73
73
|
- lib/kubernetes/health.rb
|
74
74
|
- lib/kubernetes/health/config.rb
|
75
75
|
- lib/kubernetes/health/engine.rb
|
76
|
+
- lib/kubernetes/health/rack_on_migrate.rake
|
77
|
+
- lib/kubernetes/health/railtie.rb
|
76
78
|
- lib/kubernetes/health/version.rb
|
77
79
|
homepage: https://github.com/platbr/kubernetes-health
|
78
80
|
licenses: []
|