Services, jobs and cronjobs
Deployed and supported. The fields and commands described here are stable within version v1alpha1.
Each service of an App has a kind: deployment (a container that runs continuously, the
default), job (a task that runs once to completion) or cronjob (a scheduled task).
dependsOn orders their startup: a service waits until its dependencies are ready
(deployment) or completed (job).
Prerequisites
- A session and a tenant: see From docker compose, prerequisites.
- Non-root images, or the uid to use (
user).
Steps
1. Write the three kinds
apiVersion: deploy.stackngo.io/v1alpha1
kind: App
metadata:
name: stack
spec:
services:
migrate:
kind: job
image: busybox:1.37
user: "1000"
command: ["sh", "-c", "echo migrate ok"]
size: xxxs
api:
image: nginxinc/nginx-unprivileged:1.27 # implicit kind: deployment
ports: ["8080"]
size: xxxs
dependsOn: [migrate, cache]
cache:
image: busybox:1.37
user: "1000"
command: ["sh", "-c", "mkdir -p /data/x && sleep 3600"]
size: xxxs
volumes:
- { path: /data, size: 1Gi }
tick:
kind: cronjob
schedule: "*/5 * * * *"
image: busybox:1.37
user: "1000"
command: ["date"]
size: xxxs
sng apply stack.yml
apply acme — 1 object(s)
2. Follow the startup order
sng app describe stack
Right after the apply:
App : stack
Statut : en cours
Message : api : attend : migrate
SERVICE KIND IMAGE PHASE URL DETAIL
api deployment nginxinc/nginx-unprivileged:1.27 Waiting - WaitingForDependency attend : migrate
cache deployment busybox:1.37 Progressing - RollingOut 0/1 à jour, 0 disponibles
migrate job busybox:1.37 Progressing - JobRunning job en cours
tick cronjob busybox:1.37 Scheduled - Reconciled planifié : */5 * * * *
When migrate is completed and cache is ready, api is created in turn.
Verification
sng app describe stack displays Statut : prête and 4 services Ready or Scheduled
(SERVICES 4/4 in sng app list). sng app logs stack --service migrate shows migrate ok.
The three kinds in detail
deployment (default) | job | cronjob | |
|---|---|---|---|
| Runs | continuously, restarted if it stops | once, until exit code 0 | according to schedule (5-field cron syntax) |
| Number of instances | replicas (default 1), or autoscale | 1 | 1 per run, never two at the same time |
| On failure | instances restarted indefinitely | 3 attempts, then Failed (JobFailed) | the run is marked as failed, the next one takes place at the scheduled time |
| Restarted when | the image, env or command changes (rolling update), or sng app restart | its spec changes: a new job is created, the old one deleted | — (the schedule is updated) |
| Allowed fields | all | no ports, ingress, replicas, strategy, autoscale, allowFrom, restartedAt | same, plus schedule required |
| Phase when all is well | Ready | Ready (completed) | Scheduled |
| Can be a dependency | yes (must be ready) | yes (must be completed) | no (no "completed" state) |
A job is not rerun by a simple sng apply that does not change its spec. To
replay it, change something in its definition (for example a variable RUN: "2"), or
remove it from the manifest and put it back.
A cronjob that cannot start at the scheduled time has a 5-minute tolerance; beyond that,
the run is skipped. Only one history of a successful run and one of a failed run
are kept: sng app logs stack --service tick shows only the last one.
dependsOn
- Dependencies are names of services of the same App.
- A waiting service is in phase
Waiting, reasonWaitingForDependency; it is not created before. If a dependency fails, it switches toDependencyFailed. - No cycle, no dependency on itself, no dependency on a
cronjob. dependsOnorders creation and updates. If a dependency goes back into deployment, the dependent is markedWaitingand its changes wait, but its existing instances keep running: nothing is stopped.
init: a command before each start
init runs a command with the same image, the same environment and the same size as
the service, before each instance starts. It suits a wait
or a light preparation; a schema migration is better done in a job with dependsOn.
services:
api:
image: registry.example.com/api:1.2
ports: ["8080"]
init: ["sh", "-c", "until nc -z db 5432; do sleep 1; done"]
If it doesn't work
| What you see | What to do |
|---|---|
A service stays in Waiting, WaitingForDependency | Look at the state of the awaited service: it is not ready or completed yet |
DependencyFailed | A dependency failed: fix it (sng app logs stack --service <name>), then sng apply |
A job switches to Failed, JobFailed | All 3 attempts failed: read its logs, fix, and change its definition to replay it |
The manifest is rejected because of a cycle or a cronjob in dependsOn | Remove the offending dependency |
Going further
commandreplaces the image'sCMD,entrypointreplaces theENTRYPOINT: same meaning as in compose. Both are lists (64 items at most).- Containers run with a hardened profile (no administrator, no privilege escalation, no access to the platform). Nothing to configure, nothing to disable.
pullPolicy: always | missing | neverfollows the compose values; without a value, the platform applies its default (an image with thelatesttag is always downloaded again).
See also
- Healthchecks and scaling
- Operations (
sng app restart,sng app logs) - Manifest reference (
Apppage)