SidekiqAlive
SidekiqAlive offers a solution to add liveness probe for a Sidekiq instance deployed in Kubernetes.
This library can be used to check sidekiq health outside kubernetes.
How?
A http server is started and on each requests validates that a liveness key is stored in Redis. If it is there means is working.
A Sidekiq worker is the responsible to storing this key. If Sidekiq stops processing workers
this key gets expired by Redis an consequently the http server will return a 500 error.
This worker is responsible to requeue itself for the next liveness probe.
Each instance in kubernetes will be checked based on ENV
variable HOSTNAME
(kubernetes sets this for each replica/pod).
On initialization SidekiqAlive will asign to Sidekiq::Worker a queue with the current host and add this queue to the current instance queues to process.
example:
hostname: foo
Worker queue: sidekiq_alive-foo
instance queues:
- sidekiq_alive-foo
*- your queues
hostname: bar
Worker queue: sidekiq_alive-bar
instance queues:
- sidekiq_alive-bar
*- your queues
Installation
Add this line to your application's Gemfile:
gem 'sidekiq_alive'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sidekiq_alive
Usage
SidekiqAlive will start when running sidekiq
command.
Run Sidekiq
bundle exec sidekiq
curl localhost:7433
#=> Alive!
how to disable?
You can disabled by setting ENV
variable DISABLE_SIDEKIQ_ALIVE
example:
DISABLE_SIDEKIQ_ALIVE=true bundle exec sidekiq
Kubernetes setup
Set livenessProbe
in your Kubernetes deployment
example with recommended setup:
Sidekiq < 6
spec:
containers:
- name: my_app
image: my_app:latest
env:
- name: RAILS_ENV
value: production
command:
- bundle
- exec
- sidekiq
ports:
- containerPort: 7433
livenessProbe:
httpGet:
path: /
port: 7433
initialDelaySeconds: 80
timeoutSeconds: 5
readinessProbe:
httpGet:
path: /
port: 7433
initialDelaySeconds: 80
timeoutSeconds: 5
lifecycle:
preStop:
exec:
command: ['bundle', 'exec', 'sidekiqctl', 'quiet']
terminationGracePeriodSeconds: 60
Sidekiq >= 6
Create file:
kube/sidekiq_quiet
#!/bin/bash
SIDEKIQ_PID=$(ps aux | grep sidekiq | grep busy | awk '{ print $2 }')
kill -SIGTSTP $SIDEKIQ_PID
Make it executable:
$ chmod +x kube/sidekiq_quiet
Execute it in your deployment preStop:
spec:
containers:
- name: my_app
image: my_app:latest
env:
- name: RAILS_ENV
value: production
command:
- bundle
- exec
- sidekiq
ports:
- containerPort: 7433
livenessProbe:
httpGet:
path: /
port: 7433
initialDelaySeconds: 80
timeoutSeconds: 5
readinessProbe:
httpGet:
path: /
port: 7433
initialDelaySeconds: 80
timeoutSeconds: 5
lifecycle:
preStop:
exec:
command: ['kube/sidekiq_quiet']
terminationGracePeriodSeconds: 60
Outside kubernetes
It's just up to you how you want to use it.
An example in local would be:
bundle exec sidekiq
# let it initialize ...
curl localhost:7433
#=> Alive!
Options
SidekiqAlive.setup do |config|
end
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.
Here is an example rails app
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/arturictus/sidekiq_alive. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.