mkit 0.10.5 → 0.11.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/Makefile +42 -0
- data/README.md +4 -1
- data/config/mkit_config.yml +3 -0
- data/docs/architecture.md +102 -0
- data/lib/mkit/app/controllers/pods_controller.rb +2 -2
- data/lib/mkit/app/controllers/services_controller.rb +4 -4
- data/lib/mkit/app/model/ingress.rb +1 -1
- data/lib/mkit/app/model/mkit_job.rb +0 -1
- data/lib/mkit/app/model/pod.rb +51 -36
- data/lib/mkit/app/model/resource.rb +7 -4
- data/lib/mkit/app/model/service.rb +51 -23
- data/lib/mkit/app/model/volume.rb +3 -3
- data/lib/mkit/app/templates/haproxy/app_haproxy.cfg.erb +1 -1
- data/lib/mkit/container/abstract.rb +16 -0
- data/lib/mkit/container/api.rb +22 -0
- data/lib/mkit/container/containers.rb +22 -0
- data/lib/mkit/container/docker/api.rb +25 -0
- data/lib/mkit/container/docker/containers.rb +178 -0
- data/lib/mkit/container/docker/events.rb +22 -0
- data/lib/mkit/container/docker/mapper.rb +54 -0
- data/lib/mkit/container/docker/networks.rb +37 -0
- data/lib/mkit/container/docker/payload.rb +41 -0
- data/lib/mkit/container/docker/stream_demux.rb +51 -0
- data/lib/mkit/container/docker/volumes.rb +29 -0
- data/lib/mkit/container/errors.rb +21 -0
- data/lib/mkit/container/event.rb +29 -0
- data/lib/mkit/container/events.rb +15 -0
- data/lib/mkit/container/http/unix_client.rb +139 -0
- data/lib/mkit/container/info.rb +41 -0
- data/lib/mkit/container/networks.rb +19 -0
- data/lib/mkit/container/registry.rb +37 -0
- data/lib/mkit/container/spec.rb +67 -0
- data/lib/mkit/container/utf8_buffer.rb +62 -0
- data/lib/mkit/container/volumes.rb +15 -0
- data/lib/mkit/ctypes.rb +0 -1
- data/lib/mkit/exceptions.rb +0 -2
- data/lib/mkit/pods/container_exec_command.rb +50 -0
- data/lib/mkit/pods/container_listener.rb +187 -0
- data/lib/mkit/pods/container_log_listener.rb +68 -0
- data/lib/mkit/pods/pod_reconciler.rb +48 -0
- data/lib/mkit/sagas/create_pod_saga.rb +22 -5
- data/lib/mkit/sagas/destroy_pod_saga.rb +6 -3
- data/lib/mkit/version.rb +1 -1
- data/lib/mkit/workers/haproxy_worker.rb +16 -10
- data/lib/mkit.rb +6 -4
- data/mkit.gemspec +11 -6
- metadata +121 -58
- data/lib/mkit/app/helpers/docker_helper.rb +0 -106
- data/lib/mkit/app/templates/docker/docker_run.sh.erb +0 -10
- data/lib/mkit/cmd/shell_client.rb +0 -57
- data/lib/mkit/cmd_runner.rb +0 -27
- data/lib/mkit/pods/docker_exec_command.rb +0 -38
- data/lib/mkit/pods/docker_listener.rb +0 -123
- data/lib/mkit/pods/docker_log_listener.rb +0 -40
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0e37f1412819400bee6377e7c0690309a62ab64d70f54401779dd0a81d0010ba
|
|
4
|
+
data.tar.gz: '058c0e2d3f7dfda50685ef819a3244ae56e5742cb9b27217e841d5151e989255'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7fc7119ea933771ef817f2d816d32caf967e24bb17d654a3d68801bda87a853fb87afd7ce787f8d5cb9edfb19123593695f3f2976b1525bac3c371d795a62293
|
|
7
|
+
data.tar.gz: a3bbdc312efc5fe7bcd3a6e4a87fc114a67eff40b9d7da88286ea14fc48a8611d29b610502d5b8cb0b80c30d2f6482556aceccd6adb9ad273efc3617cb63862d
|
data/Makefile
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
.PHONY: help build install test lint console server clean all
|
|
2
|
+
|
|
3
|
+
help:
|
|
4
|
+
@echo "MKIt - available targets:"
|
|
5
|
+
@echo ""
|
|
6
|
+
@echo " make build - Build the gem (rake package)"
|
|
7
|
+
@echo " make install - Build and install the gem locally"
|
|
8
|
+
@echo " make test - Run specs in spec/ (Minitest)"
|
|
9
|
+
@echo " make lint - Run RuboCop on lib/ and bin/"
|
|
10
|
+
@echo " make console - Open a Pry console (rake console)"
|
|
11
|
+
@echo " make server - Run mkitd in development (no ssl, ./config)"
|
|
12
|
+
@echo " make clean - Remove built gems"
|
|
13
|
+
@echo " make all - lint, test and build"
|
|
14
|
+
@echo ""
|
|
15
|
+
|
|
16
|
+
build:
|
|
17
|
+
bundle exec rake package
|
|
18
|
+
|
|
19
|
+
install:
|
|
20
|
+
bundle exec rake install
|
|
21
|
+
|
|
22
|
+
test:
|
|
23
|
+
@for f in $$(find spec -name '*_spec.rb' | sort); do \
|
|
24
|
+
echo "== $$f"; \
|
|
25
|
+
RACK_ENV=test bundle exec ruby -Ilib -Ispec $$f || exit 1; \
|
|
26
|
+
done
|
|
27
|
+
|
|
28
|
+
lint:
|
|
29
|
+
bundle exec rubocop lib/ bin/
|
|
30
|
+
|
|
31
|
+
console:
|
|
32
|
+
bundle exec rake console
|
|
33
|
+
|
|
34
|
+
server:
|
|
35
|
+
bundle exec bin/mkitd --no-ssl -e development
|
|
36
|
+
|
|
37
|
+
clean:
|
|
38
|
+
rm -f mkit-*.gem
|
|
39
|
+
|
|
40
|
+
all: lint test build
|
|
41
|
+
|
|
42
|
+
.DEFAULT_GOAL := help
|
data/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
This is micro kubernetes(tm) on Ruby(tm), a simple tool to deploy containers to mimic a (very) minimalistic k8 cluster with a nice REST API.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
MKIt removes the port conflicts of running containers locally: every service gets its own IP and DNS name, so you can run any services on their default ports without remapping anything by hand.
|
|
6
6
|
|
|
7
7
|
It contains an internal DNS and uses HAProxy as ingress for Pod access.
|
|
8
8
|
The database is a simple sqlite3 db and the server is a Sinatra based application.
|
|
@@ -60,6 +60,9 @@ mkit:
|
|
|
60
60
|
reload: systemctl reload haproxy
|
|
61
61
|
restart: systemctl restart haproxy
|
|
62
62
|
status: systemctl status haproxy
|
|
63
|
+
container:
|
|
64
|
+
runtime: docker # container runtime - default: docker
|
|
65
|
+
socket: /var/run/docker.sock # runtime API unix socket (default: the runtime's default socket)
|
|
63
66
|
database:
|
|
64
67
|
env: development
|
|
65
68
|
clients:
|
data/config/mkit_config.yml
CHANGED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Architecture
|
|
2
|
+
|
|
3
|
+
MKIt is a Sinatra REST API (`mkitd`) that deploys containers as services/pods, with HAProxy as ingress, an internal
|
|
4
|
+
DNS and a SQLite database (ActiveRecord). `mkit` is the command line client of that API.
|
|
5
|
+
|
|
6
|
+
## Processes and entry points
|
|
7
|
+
|
|
8
|
+
* `bin/mkitd`: the server. Mounts `ServicesController`, `MkitJobsController` and `MkitController` as Rack
|
|
9
|
+
middleware around `Sinatra::Application`.
|
|
10
|
+
* `bin/mkit`: the client, in `lib/mkit/client/*` (HTTP, unix socket and websocket clients). Commands are declared in
|
|
11
|
+
`lib/mkit/client/commands.yaml`.
|
|
12
|
+
|
|
13
|
+
## Startup
|
|
14
|
+
|
|
15
|
+
`MKIt.startup` (`lib/mkit.rb`):
|
|
16
|
+
|
|
17
|
+
1. `configure`: config dir, SSL, default config copy, self-signed certificate, HAProxy dir; the container runtime is
|
|
18
|
+
configured here (`Container::Registry.configure_from`).
|
|
19
|
+
2. DB connection and migrations, default configs.
|
|
20
|
+
3. Brings up the host interface (`MKIt::Interface`).
|
|
21
|
+
4. Registers the singletons in a `Dry::Container` (`MKIt::System`: `job_manager`, `mkit_dns`, `container_listener`),
|
|
22
|
+
then the workers and sagas.
|
|
23
|
+
5. Starts the job manager and the container event listener, then `restore_operation` (recreates the service
|
|
24
|
+
networks, recomputes the services status and restarts HAProxy).
|
|
25
|
+
|
|
26
|
+
## Async job pipeline
|
|
27
|
+
|
|
28
|
+
API controllers and the container event listener enqueue `MkitJob` rows (DB backed) with a `topic`. `JobManager`
|
|
29
|
+
polls `MkitJob.take` in a thread and hands each job to every worker registered for its topic (`do_the(job)`).
|
|
30
|
+
|
|
31
|
+
* Workers (`lib/mkit/workers/`, subclasses of `AWorker`) and sagas (`lib/mkit/sagas/`, subclasses of `ASaga`, e.g.
|
|
32
|
+
create/destroy pod) declare their topics in `topics` and register themselves in `initialize`.
|
|
33
|
+
* New ones are added in `WorkerManager` / `SagaManager.register_workers`.
|
|
34
|
+
|
|
35
|
+
## Models
|
|
36
|
+
|
|
37
|
+
`lib/mkit/app/model/`: `Service` (owns ports, config, resources, ingress with frontends/backends), `Pod`, `Volume`,
|
|
38
|
+
`Lease`/`Pool` (IP allocation in the MKIt network, `10.210.198.0/24` by default), `DnsHost`...
|
|
39
|
+
|
|
40
|
+
Every service gets a dedicated IP on a host interface. The HAProxy configuration is generated from the ERB templates in
|
|
41
|
+
`lib/mkit/app/templates/haproxy` into `haproxy.config_dir` (helpers in `app/helpers/haproxy.rb`).
|
|
42
|
+
|
|
43
|
+
## Container runtime
|
|
44
|
+
|
|
45
|
+
`lib/mkit/container/`. Everything talks to the runtime through `MKIt::Container::Registry.api`:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
Container::Api groups the resources below
|
|
49
|
+
├─ Containers run/start/stop/remove/find/exec/logs/log_path/follow_logs/attach_exec
|
|
50
|
+
├─ Networks create/find/exists?/attach/detach/remove
|
|
51
|
+
├─ Volumes create/find/remove
|
|
52
|
+
└─ Events each { |event| }
|
|
53
|
+
Container::Docker::* implementation over the Docker Engine API
|
|
54
|
+
Container::Http::UnixClient generic JSON/HTTP client over a unix socket
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
* The contracts speak **runtime neutral value objects**, never raw runtime payloads: `containers.find` returns a
|
|
58
|
+
`Container::Info` (id, name, `running?`, `tty?`, `ip_on(network)`, exit code, oom, error), `events.each` yields
|
|
59
|
+
`Container::Event`s (kind, action, container id/name, exit code) and pods are described by a `Container::Spec`.
|
|
60
|
+
`Docker::Mapper` and `Docker::Payload` translate from/to the Engine API.
|
|
61
|
+
* Runtime and socket come from `mkit.container.{runtime,socket}` (default `docker`; the legacy `mkit.docker.socket`
|
|
62
|
+
is still read).
|
|
63
|
+
* Errors are `Container::Error` (with the HTTP `status` when there is one), `NotFound` and `ExecFailed`.
|
|
64
|
+
* Listeners (`lib/mkit/pods/`) and sagas receive the containers api by constructor injection, models use
|
|
65
|
+
`Registry.api`.
|
|
66
|
+
* To add a runtime (e.g. Podman): implement the four contracts, add a builder to `Registry::BUILDERS` and run
|
|
67
|
+
`spec/container/contracts_spec.rb` against the new classes.
|
|
68
|
+
|
|
69
|
+
## Pod state is event driven
|
|
70
|
+
|
|
71
|
+
`ContainerListener` consumes the runtime events in **a single consumer thread**: a handler must never block or sleep.
|
|
72
|
+
|
|
73
|
+
* `container start` and `network connect` call `Pod#sync_ip(info)`: it stores the ip, updates the DNS and publishes
|
|
74
|
+
`pod_ip_updated`, which makes the `PodWorker` request a new proxy config. There is no polling.
|
|
75
|
+
* a container that no longer exists clears the pod ip (and its DNS record, the DNS server cannot answer with an
|
|
76
|
+
empty address);
|
|
77
|
+
* a container that exists without an ip (stopped, not started yet) keeps the last ip: a stopped pod still counts;
|
|
78
|
+
* the pod is **saved before** the job is published, the worker reads the ip from the database.
|
|
79
|
+
* `container die` and `oom` log why the container ended (exit code, oom, runtime error, last output lines).
|
|
80
|
+
`CreatePodSaga` logs why a container could not be created or started.
|
|
81
|
+
* `PodReconciler` re-syncs every pod (container id, ip) with the runtime before each subscription to the event
|
|
82
|
+
stream, that is on startup and after every reconnect, and publishes a single `update_proxy_config` per changed
|
|
83
|
+
service. It recovers the events lost while the stream was down.
|
|
84
|
+
|
|
85
|
+
## HAProxy configuration
|
|
86
|
+
|
|
87
|
+
A service only has a HAProxy config file while it has **at least one pod with an ip**. `Service#proxy_pods` are the
|
|
88
|
+
pods the template renders, `Service#routable?` tells whether there is any; `HAProxyWorker` writes the file and
|
|
89
|
+
reloads, or deletes the file when the service is not routable (an invalid file would make HAProxy reject the whole
|
|
90
|
+
configuration).
|
|
91
|
+
|
|
92
|
+
## Repairing a service
|
|
93
|
+
|
|
94
|
+
`mkit start` and `mkit restart` (stop + start) repair a service: `Service#start` recreates the network and the pods
|
|
95
|
+
missing to reach `min_replicas` (`spawn_pods`), and `Pod#start` recreates a container that was removed from outside
|
|
96
|
+
(publishing `create_pod_saga`) instead of doing nothing.
|
|
97
|
+
|
|
98
|
+
## Configuration
|
|
99
|
+
|
|
100
|
+
Server: `mkit_config.yml` (HAProxy control commands, network, container runtime, DB env, authorized `clients` ids),
|
|
101
|
+
`database.yml`, `mkitd_config.sh`; defaults in `config/` and `lib/mkit/config/`. Client: `~/.mkit/mkitc_config.yml`
|
|
102
|
+
(`mkit init` registers a client id). Service definitions are YAML, see `samples/apps/`.
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
require 'mkit/app/model/pod'
|
|
4
4
|
require 'mkit/app/helpers/pods_helper'
|
|
5
5
|
require 'mkit/app/helpers/params_helper'
|
|
6
|
-
require 'mkit/pods/
|
|
6
|
+
require 'mkit/pods/container_exec_command'
|
|
7
7
|
require 'mkit/exceptions'
|
|
8
8
|
|
|
9
9
|
class PodsController < MKIt::Server
|
|
@@ -20,7 +20,7 @@ class PodsController < MKIt::Server
|
|
|
20
20
|
listener = nil
|
|
21
21
|
ws.onopen do
|
|
22
22
|
settings.sockets << ws
|
|
23
|
-
listener = MKIt::
|
|
23
|
+
listener = MKIt::ContainerExecCommand.new(pod, ws, options: options_parameter)
|
|
24
24
|
settings.listeners << listener
|
|
25
25
|
listener.register
|
|
26
26
|
end
|
|
@@ -4,8 +4,8 @@ require 'mkit/app/model/service'
|
|
|
4
4
|
require 'mkit/app/helpers/services_helper'
|
|
5
5
|
require 'mkit/app/helpers/params_helper'
|
|
6
6
|
require 'mkit/app/helpers/migrations_helper'
|
|
7
|
-
require 'mkit/pods/
|
|
8
|
-
require 'mkit/pods/
|
|
7
|
+
require 'mkit/pods/container_log_listener'
|
|
8
|
+
require 'mkit/pods/container_exec_command'
|
|
9
9
|
|
|
10
10
|
class ServicesController < MKIt::Server
|
|
11
11
|
helpers MKIt::ServicesHelper
|
|
@@ -53,7 +53,7 @@ class ServicesController < MKIt::Server
|
|
|
53
53
|
ws.onopen do
|
|
54
54
|
settings.sockets << ws
|
|
55
55
|
ws.send("<<<< %s | %s >>>>\n" % [srv.name, pod.name])
|
|
56
|
-
listener = MKIt::
|
|
56
|
+
listener = MKIt::ContainerLogListener.new(pod, ws, options: options_parameter)
|
|
57
57
|
settings.listeners << listener
|
|
58
58
|
listener.register
|
|
59
59
|
end
|
|
@@ -147,7 +147,7 @@ class ServicesController < MKIt::Server
|
|
|
147
147
|
listener = nil
|
|
148
148
|
ws.onopen do
|
|
149
149
|
settings.sockets << ws
|
|
150
|
-
listener = MKIt::
|
|
150
|
+
listener = MKIt::ContainerExecCommand.new(pod, ws, options: options_parameter)
|
|
151
151
|
settings.listeners << listener
|
|
152
152
|
listener.register
|
|
153
153
|
end
|
|
@@ -80,7 +80,7 @@ class Ingress < ActiveRecord::Base
|
|
|
80
80
|
|
|
81
81
|
# when frontend port is range - e.g. 1200-1220, referred backend port must be empty
|
|
82
82
|
yaml["frontend"].each do |front|
|
|
83
|
-
if front["bind"]["port"] =~ /^\d+-\d+$/
|
|
83
|
+
if front["bind"]["port"].to_s =~ /^\d+-\d+$/
|
|
84
84
|
referred_backend = yaml["backend"].find { |back| back["name"] == front["default_backend"] }
|
|
85
85
|
raise_bad_configuration "Frontend port range '#{front["bind"]["port"]}' must have an empty backend port" unless referred_backend && (referred_backend["bind"]["port"].nil?)
|
|
86
86
|
end
|
data/lib/mkit/app/model/pod.rb
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
require 'mkit/
|
|
2
|
-
require 'mkit/
|
|
1
|
+
require 'mkit/container/registry'
|
|
2
|
+
require 'mkit/container/spec'
|
|
3
3
|
require 'mkit/app/model/service'
|
|
4
4
|
require 'mkit/status'
|
|
5
5
|
|
|
@@ -9,26 +9,32 @@ class Pod < ActiveRecord::Base
|
|
|
9
9
|
|
|
10
10
|
before_destroy :clean_up
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
# Updates the pod ip (and its dns) from the container info, publishing pod_ip_updated (which
|
|
13
|
+
# refreshes the proxy config) when it changes. Does not wait.
|
|
14
|
+
# - container gone (info nil): the ip is cleared, the pod leaves the proxy config
|
|
15
|
+
# - container exists without ip in the service network (not started, stopped): the last ip is kept,
|
|
16
|
+
# a stopped pod still counts; the next container event brings the new one
|
|
17
|
+
# The pod is saved before the job is published: the worker reads the ip from the database.
|
|
18
|
+
# notify: false leaves the job to the caller (it must publish it, e.g. once per service)
|
|
19
|
+
def sync_ip(info, notify: true)
|
|
20
|
+
return self.ip if info && info.ip_on(self.service.pods_network).nil?
|
|
14
21
|
|
|
15
|
-
|
|
16
|
-
new_ip
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if self.ip != new_ip
|
|
24
|
-
self.ip = new_ip
|
|
25
|
-
self.update_dns
|
|
26
|
-
MkitJob.publish(topic: :pod_ip_updated, service_id: self.service.id, pod_id: self.id)
|
|
27
|
-
end
|
|
28
|
-
MKItLogger.info("docker ip updated [#{self.ip}]")
|
|
22
|
+
new_ip = info&.ip_on(self.service.pods_network)
|
|
23
|
+
return self.ip if new_ip == self.ip
|
|
24
|
+
|
|
25
|
+
self.ip = new_ip
|
|
26
|
+
self.save
|
|
27
|
+
new_ip.nil? ? self.clear_dns : self.update_dns
|
|
28
|
+
MkitJob.publish(topic: :pod_ip_updated, service_id: self.service.id, pod_id: self.id) if notify
|
|
29
|
+
MKItLogger.info("pod #{self.name} ip updated [#{self.ip.inspect}]")
|
|
29
30
|
self.ip
|
|
30
31
|
end
|
|
31
32
|
|
|
33
|
+
# the dns server cannot answer with an empty address
|
|
34
|
+
def clear_dns
|
|
35
|
+
self.dns_host = nil
|
|
36
|
+
end
|
|
37
|
+
|
|
32
38
|
def update_dns
|
|
33
39
|
self.dns_host ||= DnsHost.new(
|
|
34
40
|
service: self.service,
|
|
@@ -39,36 +45,45 @@ class Pod < ActiveRecord::Base
|
|
|
39
45
|
self.dns_host.save
|
|
40
46
|
end
|
|
41
47
|
|
|
42
|
-
def
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
end
|
|
48
|
+
def set_status_from_container
|
|
49
|
+
info = container
|
|
50
|
+
self.status = if info.nil?
|
|
51
|
+
MKIt::Status::PENDING
|
|
52
|
+
elsif info.running?
|
|
53
|
+
MKIt::Status::RUNNING
|
|
54
|
+
else
|
|
55
|
+
MKIt::Status::STOPPED
|
|
56
|
+
end
|
|
52
57
|
self.save
|
|
53
58
|
self.status
|
|
54
59
|
end
|
|
55
60
|
|
|
56
|
-
def
|
|
57
|
-
|
|
61
|
+
def container_spec
|
|
62
|
+
MKIt::Container::Spec.new(name: self.name, service: self.service)
|
|
58
63
|
end
|
|
59
64
|
|
|
65
|
+
# Starts the container; recreates it when it does not exist (e.g. removed with `docker rm`).
|
|
60
66
|
def start
|
|
61
|
-
|
|
67
|
+
info = container
|
|
68
|
+
if info.nil?
|
|
69
|
+
MKItLogger.warn("pod #{self.name} has no container, creating it")
|
|
70
|
+
MkitJob.publish(topic: :create_pod_saga, data: { pod_name: self.name })
|
|
71
|
+
elsif !info.running?
|
|
72
|
+
containers.start(self.name)
|
|
73
|
+
end
|
|
62
74
|
end
|
|
63
75
|
|
|
64
76
|
def stop
|
|
65
|
-
|
|
77
|
+
containers.stop(self.name) if container&.running?
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# MKIt::Container::Info of the pod's container, nil when it does not exist
|
|
81
|
+
def container
|
|
82
|
+
containers.find(self.name)
|
|
66
83
|
end
|
|
67
84
|
|
|
68
|
-
def
|
|
69
|
-
|
|
70
|
-
return properties.to_o unless properties.nil?
|
|
71
|
-
nil
|
|
85
|
+
def containers
|
|
86
|
+
MKIt::Container::Registry.api.containers
|
|
72
87
|
end
|
|
73
88
|
|
|
74
89
|
def clean_up
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Resource < ActiveRecord::Base
|
|
4
|
+
LIMITS = %w[cpu memory memory_swap].freeze
|
|
5
|
+
LIMIT_FORMAT = /\A\d+m\z/.freeze
|
|
6
|
+
|
|
4
7
|
belongs_to :service
|
|
5
8
|
|
|
6
9
|
def self.create(yaml)
|
|
@@ -37,10 +40,10 @@ class Resource < ActiveRecord::Base
|
|
|
37
40
|
end
|
|
38
41
|
# validate limits
|
|
39
42
|
unless yaml.nil? || yaml["limits"].nil?
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
LIMITS.each do |name|
|
|
44
|
+
value = yaml["limits"][name]
|
|
45
|
+
raise_bad_configuration "resource #{name} limits must match '\\d+m'" if value && value.to_s !~ LIMIT_FORMAT
|
|
46
|
+
end
|
|
44
47
|
end
|
|
45
48
|
true
|
|
46
49
|
end
|
|
@@ -5,7 +5,7 @@ require 'mkit/app/model/service_port'
|
|
|
5
5
|
require 'mkit/app/model/service_config'
|
|
6
6
|
require 'mkit/app/model/pod'
|
|
7
7
|
require 'mkit/app/helpers/erb_helper'
|
|
8
|
-
require 'mkit/
|
|
8
|
+
require 'mkit/container/registry'
|
|
9
9
|
require 'mkit/mkit_interface'
|
|
10
10
|
require 'mkit/status'
|
|
11
11
|
require 'mkit/utils'
|
|
@@ -32,7 +32,6 @@ class Service < ActiveRecord::Base
|
|
|
32
32
|
validates :name, presence: true
|
|
33
33
|
|
|
34
34
|
include MKIt::ERBHelper
|
|
35
|
-
include MKIt::DockerHelper
|
|
36
35
|
|
|
37
36
|
def self.create(yaml)
|
|
38
37
|
config = yaml["service"]
|
|
@@ -65,12 +64,8 @@ class Service < ActiveRecord::Base
|
|
|
65
64
|
srv.status = MKIt::Status::CREATED
|
|
66
65
|
srv.save!
|
|
67
66
|
data = { service_id: srv.id, version: srv.version }
|
|
68
|
-
# create
|
|
69
|
-
(
|
|
70
|
-
pd = Pod.new( status: MKIt::Status::CREATED, name: SecureRandom.uuid.gsub('-','')[0..11])
|
|
71
|
-
srv.pod << pd
|
|
72
|
-
MkitJob.publish(topic: :create_pod_saga, data: {pod_name: pd.name})
|
|
73
|
-
}
|
|
67
|
+
# create pods
|
|
68
|
+
srv.spawn_pods(srv.resource.min_replicas)
|
|
74
69
|
srv
|
|
75
70
|
end
|
|
76
71
|
end
|
|
@@ -116,18 +111,15 @@ class Service < ActiveRecord::Base
|
|
|
116
111
|
|
|
117
112
|
# destroy old pods...
|
|
118
113
|
self.pod.destroy_all
|
|
119
|
-
# create
|
|
120
|
-
(
|
|
121
|
-
pd = Pod.new( status: MKIt::Status::CREATED, name: SecureRandom.uuid.gsub('-','')[0..11])
|
|
122
|
-
self.pod << pd
|
|
123
|
-
MkitJob.publish(topic: :create_pod_saga, data: {pod_name: pd.name})
|
|
124
|
-
}
|
|
114
|
+
# create pods
|
|
115
|
+
self.spawn_pods(self.resource.min_replicas)
|
|
125
116
|
self.save
|
|
126
117
|
end
|
|
127
118
|
end
|
|
128
119
|
|
|
129
120
|
def create_pods_network
|
|
130
|
-
|
|
121
|
+
networks = MKIt::Container::Registry.api.networks
|
|
122
|
+
networks.create(self.pods_network) unless networks.exists?(self.pods_network)
|
|
131
123
|
end
|
|
132
124
|
|
|
133
125
|
def deploy_network
|
|
@@ -166,7 +158,7 @@ class Service < ActiveRecord::Base
|
|
|
166
158
|
def update_status!
|
|
167
159
|
combined_status = nil
|
|
168
160
|
self.pod.each { |pod|
|
|
169
|
-
child_status = pod.
|
|
161
|
+
child_status = pod.set_status_from_container
|
|
170
162
|
if combined_status
|
|
171
163
|
case combined_status
|
|
172
164
|
when MKIt::Status::RUNNING
|
|
@@ -202,11 +194,25 @@ class Service < ActiveRecord::Base
|
|
|
202
194
|
MkitJob.publish(topic: :update_proxy_config, application_id: self.id, data: proxy_config)
|
|
203
195
|
end
|
|
204
196
|
|
|
197
|
+
# pods that can receive traffic: the ones with a known ip (a stopped pod keeps its last ip)
|
|
198
|
+
def proxy_pods
|
|
199
|
+
self.pod.reject { |p| p.ip.to_s.empty? }
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# a proxy config only makes sense when there is a pod to route to
|
|
203
|
+
def routable?
|
|
204
|
+
proxy_pods.any?
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def proxy_filename
|
|
208
|
+
my_addr = self.lease.ip.split('.')[3]
|
|
209
|
+
"#{'%04i' % my_addr.to_i}_#{self.name}.cfg"
|
|
210
|
+
end
|
|
211
|
+
|
|
205
212
|
def proxy_config
|
|
206
213
|
# config
|
|
207
214
|
haproxy = parse
|
|
208
|
-
|
|
209
|
-
filename = "#{'%04i' % my_addr.to_i}_#{self.name}.cfg"
|
|
215
|
+
filename = proxy_filename
|
|
210
216
|
MKItLogger.debug("haproxy config file: #{filename}")
|
|
211
217
|
{filename: filename, data: haproxy}
|
|
212
218
|
end
|
|
@@ -216,9 +222,7 @@ class Service < ActiveRecord::Base
|
|
|
216
222
|
end
|
|
217
223
|
|
|
218
224
|
def clean_up
|
|
219
|
-
|
|
220
|
-
filename = "#{'%04i' % my_addr.to_i}_#{self.name}.cfg"
|
|
221
|
-
MkitJob.publish(topic: :destroy_proxy_config, data: {filename: filename})
|
|
225
|
+
MkitJob.publish(topic: :destroy_proxy_config, data: {filename: proxy_filename})
|
|
222
226
|
end
|
|
223
227
|
|
|
224
228
|
def find_pod_by_id_or_name(pod_id)
|
|
@@ -229,12 +233,36 @@ class Service < ActiveRecord::Base
|
|
|
229
233
|
#
|
|
230
234
|
# ctrl
|
|
231
235
|
#
|
|
236
|
+
# Starts the service, repairing it first: the pods missing to reach min_replicas are created and so is
|
|
237
|
+
# the network (both can be gone, e.g. after a `docker rm` / `docker network rm`). The pods that already
|
|
238
|
+
# exist are started; one without container is recreated by Pod#start.
|
|
232
239
|
def start
|
|
233
|
-
self.pod.
|
|
240
|
+
existing = self.pod.to_a
|
|
241
|
+
self.create_pods_network
|
|
242
|
+
self.spawn_pods(missing_pods)
|
|
243
|
+
existing.each { |p|
|
|
234
244
|
MkitJob.publish(topic: :start_pod, service_id: self.id, pod_id: p.id)
|
|
235
245
|
}
|
|
236
246
|
end
|
|
237
247
|
|
|
248
|
+
def min_replicas
|
|
249
|
+
self.resource&.min_replicas || 1
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# pods needed to reach min_replicas
|
|
253
|
+
def missing_pods
|
|
254
|
+
[min_replicas - self.pod.size, 0].max
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Creates `count` pods and publishes the saga that creates (and starts) their containers.
|
|
258
|
+
def spawn_pods(count)
|
|
259
|
+
count.times do
|
|
260
|
+
pd = Pod.new(status: MKIt::Status::CREATED, name: SecureRandom.uuid.gsub('-', '')[0..11])
|
|
261
|
+
self.pod << pd
|
|
262
|
+
MkitJob.publish(topic: :create_pod_saga, data: { pod_name: pd.name })
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
238
266
|
def stop
|
|
239
267
|
self.pod.each { |p|
|
|
240
268
|
MkitJob.publish(topic: :stop_pod, service_id: self.id, pod_id: p.id)
|
|
@@ -245,7 +273,7 @@ class Service < ActiveRecord::Base
|
|
|
245
273
|
out = ""
|
|
246
274
|
self.pod.each { |p|
|
|
247
275
|
out << "<<<< %s | %s >>>>\n" % [self.name, p.name]
|
|
248
|
-
out << logs(p.name)
|
|
276
|
+
out << MKIt::Container::Registry.api.containers.logs(p.name)
|
|
249
277
|
}
|
|
250
278
|
out
|
|
251
279
|
end
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
require 'mkit/ctypes'
|
|
2
|
-
require 'mkit/
|
|
2
|
+
require 'mkit/container/registry'
|
|
3
3
|
require 'fileutils'
|
|
4
4
|
|
|
5
5
|
class Volume < ActiveRecord::Base
|
|
@@ -33,7 +33,7 @@ class Volume < ActiveRecord::Base
|
|
|
33
33
|
def create_volume
|
|
34
34
|
case self.ctype
|
|
35
35
|
when MKIt::CType::DOCKER_STORAGE
|
|
36
|
-
MKIt::
|
|
36
|
+
MKIt::Container::Registry.api.volumes.create(self.name)
|
|
37
37
|
when MKIt::CType::LOCAL_STORAGE
|
|
38
38
|
# nop
|
|
39
39
|
end
|
|
@@ -42,7 +42,7 @@ class Volume < ActiveRecord::Base
|
|
|
42
42
|
def delete_volume
|
|
43
43
|
case self.ctype
|
|
44
44
|
when MKIt::CType::DOCKER_STORAGE
|
|
45
|
-
MKIt::
|
|
45
|
+
MKIt::Container::Registry.api.volumes.remove(self.name)
|
|
46
46
|
end
|
|
47
47
|
end
|
|
48
48
|
|
|
@@ -22,7 +22,7 @@ backend <%=name%>-<%=be.name%>-back
|
|
|
22
22
|
mode <%=be.mode%>
|
|
23
23
|
balance <%=be.load_balance%>
|
|
24
24
|
<%=be.options.join("\n ")%>
|
|
25
|
-
<%
|
|
25
|
+
<%proxy_pods.each { | pod | %>
|
|
26
26
|
server <%=pod.name%> <%=pod.ip%><%unless be.port.nil? || be.port.empty? then%><%=":#{be.port}"%> <%=be.bind_options.join(' ')%> <%end%>
|
|
27
27
|
<%}%>
|
|
28
28
|
<%}%>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MKIt
|
|
4
|
+
module Container
|
|
5
|
+
# Declares the methods a runtime implementation must provide.
|
|
6
|
+
module Abstract
|
|
7
|
+
def abstract_methods(*names)
|
|
8
|
+
names.each do |name|
|
|
9
|
+
define_method(name) do |*_args, **_opts, &_block|
|
|
10
|
+
raise NotImplementedError, "#{self.class} must implement ##{name}"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'mkit/container/containers'
|
|
4
|
+
require 'mkit/container/networks'
|
|
5
|
+
require 'mkit/container/volumes'
|
|
6
|
+
require 'mkit/container/events'
|
|
7
|
+
|
|
8
|
+
module MKIt
|
|
9
|
+
module Container
|
|
10
|
+
# Entry point of a container runtime: groups its resources.
|
|
11
|
+
class Api
|
|
12
|
+
attr_reader :containers, :networks, :volumes, :events
|
|
13
|
+
|
|
14
|
+
def initialize(containers:, networks:, volumes:, events:)
|
|
15
|
+
@containers = containers
|
|
16
|
+
@networks = networks
|
|
17
|
+
@volumes = volumes
|
|
18
|
+
@events = events
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'mkit/container/abstract'
|
|
4
|
+
|
|
5
|
+
module MKIt
|
|
6
|
+
module Container
|
|
7
|
+
# Contract for container lifecycle operations.
|
|
8
|
+
class Containers
|
|
9
|
+
extend Abstract
|
|
10
|
+
|
|
11
|
+
# run(spec): creates (pulling the image if needed) and starts a container, returns its id
|
|
12
|
+
# find(id): Container::Info, nil when the container does not exist
|
|
13
|
+
# exec(id, cmd): runs cmd inside the container and returns its output (raises ExecFailed)
|
|
14
|
+
# logs(id, tail:): last log lines as text
|
|
15
|
+
# log_path(id): log file in the host, nil when the log driver has none
|
|
16
|
+
# follow_logs(id, **query) { |text| }: streams log text as it is produced
|
|
17
|
+
# attach_exec(id, cmd, detached:): starts an interactive exec, returns a bidirectional IO
|
|
18
|
+
abstract_methods :run, :start, :stop, :remove, :find, :exec, :logs, :log_path,
|
|
19
|
+
:follow_logs, :attach_exec
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|