From 7444f8d71e0998532d153b8a7aa57ea99d9b2e85 Mon Sep 17 00:00:00 2001 From: Jerome Petazzoni Date: Fri, 1 Nov 2019 22:46:43 -0500 Subject: [PATCH] Add cronjobs and YAML catch up instructions --- k8s/dockercoins.yaml | 160 ++++++++++++++++++++++++++++++++++++++ slides/k8s/kubectlrun.md | 66 ++++++++++++++++ slides/k8s/yamldeploy.md | 93 ++++++++++++++++++++++ slides/kube-fullday.yml | 11 +-- slides/kube-selfpaced.yml | 7 +- slides/kube-twodays.yml | 15 ++-- 6 files changed, 337 insertions(+), 15 deletions(-) create mode 100644 k8s/dockercoins.yaml create mode 100644 slides/k8s/yamldeploy.md diff --git a/k8s/dockercoins.yaml b/k8s/dockercoins.yaml new file mode 100644 index 00000000..4e754715 --- /dev/null +++ b/k8s/dockercoins.yaml @@ -0,0 +1,160 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: hasher + name: hasher +spec: + replicas: 1 + selector: + matchLabels: + app: hasher + template: + metadata: + labels: + app: hasher + spec: + containers: + - image: dockercoins/hasher:v0.1 + name: hasher +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: hasher + name: hasher +spec: + ports: + - port: 80 + protocol: TCP + targetPort: 80 + selector: + app: hasher + type: ClusterIP +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: redis + name: redis +spec: + replicas: 1 + selector: + matchLabels: + app: redis + template: + metadata: + labels: + app: redis + spec: + containers: + - image: redis + name: redis +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: redis + name: redis +spec: + ports: + - port: 6379 + protocol: TCP + targetPort: 6379 + selector: + app: redis + type: ClusterIP +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: rng + name: rng +spec: + replicas: 1 + selector: + matchLabels: + app: rng + template: + metadata: + labels: + app: rng + spec: + containers: + - image: dockercoins/rng:v0.1 + name: rng +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: rng + name: rng +spec: + ports: + - port: 80 + protocol: TCP + targetPort: 80 + selector: + app: rng + type: ClusterIP +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: webui + name: webui +spec: + replicas: 1 + selector: + matchLabels: + app: webui + template: + metadata: + labels: + app: webui + spec: + containers: + - image: dockercoins/webui:v0.1 + name: webui +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: webui + name: webui +spec: + ports: + - port: 80 + protocol: TCP + targetPort: 80 + selector: + app: webui + type: NodePort +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: worker + name: worker +spec: + replicas: 1 + selector: + matchLabels: + app: worker + template: + metadata: + labels: + app: worker + spec: + containers: + - image: dockercoins/worker:v0.1 + name: worker diff --git a/slides/k8s/kubectlrun.md b/slides/k8s/kubectlrun.md index 240bb0e7..9b7ae2ec 100644 --- a/slides/k8s/kubectlrun.md +++ b/slides/k8s/kubectlrun.md @@ -278,6 +278,72 @@ Let's leave `kubectl logs` running while we keep exploring. --- +## Scheduling periodic background work + +- A Cron Job is a job that will be executed at specific intervals + + (the name comes from the traditional cronjobs executed by the UNIX crond) + +- It requires a *schedule*, represented as five space-separated fields: + + - minute [0,59] + - hour [0,23] + - day of the month [1,31] + - month of the year [1,12] + - day of the week ([0,6] with 0=Sunday) + +- `*` means "all valid values"; `/N` means "every N" + +- Example: `*/3 * * * *` means "every three minutes" + +--- + +## Creating a Cron Job + +- Let's create a simple job to be executed every three minutes + +- Cron Jobs need to terminate, otherwise they'd run forever + +.exercise[ + +- Create the Cron Job: + ```bash + kubectl run --schedule="*/3 * * * *" --restart=OnFailure --image=alpine sleep 10 + ``` + +- Check the resource that was created: + ```bash + kubectl get cronjobs + ``` + +] + +--- + +## Cron Jobs in action + +- At the specified schedule, the Cron Job will create a Job + +- The Job will create a Pod + +- The Job will make sure that the Pod completes + + (re-creating another one if it fails, for instance if its node fails) + +.exercise[ + +- Check the Jobs that are created: + ```bash + kubectl get jobs + ``` + +] + +(It will take a few minutes before the first job is scheduled.) + +--- + + ## What about that deprecation warning? - As we can see from the previous slide, `kubectl run` can do many things diff --git a/slides/k8s/yamldeploy.md b/slides/k8s/yamldeploy.md new file mode 100644 index 00000000..f9bf9250 --- /dev/null +++ b/slides/k8s/yamldeploy.md @@ -0,0 +1,93 @@ +# Deploying with YAML + +- So far, we created resources with the following commands: + + - `kubectl run` + + - `kubectl create deployment` + + - `kubectl expose` + +- We can also create resources directly with YAML manifests + +--- + +## `kubectl apply` vs `create` + +- `kubectl create -f whatever.yaml` + + - creates resources if they don't exist + + - if resources already exist, don't alter them +
(and display error message) + +- `kubectl apply -f whatever.yaml` + + - creates resources if they don't exist + + - if resources already exist, update them +
(to match the definition provided by the YAML file) + + - stores the manifest as an *annotation* in the resource + +--- + +## Creating multiple resources + +- The manifest can contain multiple resources separated by `---` + +```yaml + kind: ... + apiVersion: ... + metadata: ... + name: ... + ... + --- + kind: ... + apiVersion: ... + metadata: ... + name: ... + ... +``` + +--- + +## Creating multiple resources + +- The manifest can also contain a list of resources + +```yaml + apiVersion: v1 + kind: List + items: + - kind: ... + apiVersion: ... + ... + - kind: ... + apiVersion: ... + ... +``` + +--- + +## Deploying dockercoins with YAML + +- We provide a YAML manifest with all the resources for Dockercoins + + (Deployments and Services) + +- We can use it if we need to deploy or redeploy Dockercoins + +.exercise[ + +- Deploy or redeploy Dockercoins: + ```bash + kubectl apply -f ~/container.training/k8s/dockercoins.yaml + ``` + +] + +(If we deployed Dockercoins earlier, we will see warning messages, +because the resources that we created lack the necessary annotation. +We can safely ignore them.) + diff --git a/slides/kube-fullday.yml b/slides/kube-fullday.yml index 35574248..06049482 100644 --- a/slides/kube-fullday.yml +++ b/slides/kube-fullday.yml @@ -23,7 +23,7 @@ chapters: - shared/prereqs.md #- shared/webssh.md - shared/connecting.md - - k8s/versions-k8s.md + #- k8s/versions-k8s.md - shared/sampleapp.md #- shared/composescale.md #- shared/hastyconclusions.md @@ -42,17 +42,18 @@ chapters: #- k8s/buildshiprun-selfhosted.md - k8s/buildshiprun-dockerhub.md - k8s/ourapponkube.md - #- k8s/kubectlproxy.md - #- k8s/localkubeconfig.md - #- k8s/accessinternal.md - - - k8s/setup-k8s.md + - k8s/yamldeploy.md + #- k8s/setup-k8s.md - k8s/dashboard.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md - k8s/daemonset.md #- k8s/dryrun.md + #- k8s/kubectlproxy.md + #- k8s/localkubeconfig.md + #- k8s/accessinternal.md - k8s/rollout.md #- k8s/healthchecks.md #- k8s/healthchecks-more.md diff --git a/slides/kube-selfpaced.yml b/slides/kube-selfpaced.yml index 9a21f941..e4823caa 100644 --- a/slides/kube-selfpaced.yml +++ b/slides/kube-selfpaced.yml @@ -43,9 +43,7 @@ chapters: - k8s/buildshiprun-dockerhub.md - k8s/ourapponkube.md - - - k8s/kubectlproxy.md - - k8s/localkubeconfig.md - - k8s/accessinternal.md + - k8s/yamldeploy.md - k8s/setup-k8s.md - k8s/dashboard.md #- k8s/kubectlscale.md @@ -53,6 +51,9 @@ chapters: - shared/hastyconclusions.md - k8s/daemonset.md - k8s/dryrun.md + - k8s/kubectlproxy.md + - k8s/localkubeconfig.md + - k8s/accessinternal.md - - k8s/rollout.md - k8s/healthchecks.md diff --git a/slides/kube-twodays.yml b/slides/kube-twodays.yml index eb976900..b8666817 100644 --- a/slides/kube-twodays.yml +++ b/slides/kube-twodays.yml @@ -23,7 +23,7 @@ chapters: - shared/prereqs.md #- shared/webssh.md - shared/connecting.md - - k8s/versions-k8s.md + #- k8s/versions-k8s.md - shared/sampleapp.md #- shared/composescale.md #- shared/hastyconclusions.md @@ -43,20 +43,21 @@ chapters: - k8s/buildshiprun-dockerhub.md - k8s/ourapponkube.md - - - k8s/kubectlproxy.md - - k8s/localkubeconfig.md - - k8s/accessinternal.md - - k8s/setup-k8s.md + - k8s/yamldeploy.md + #- k8s/setup-k8s.md - k8s/dashboard.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md - k8s/daemonset.md -- - k8s/dryrun.md +- + #- k8s/kubectlproxy.md + - k8s/localkubeconfig.md + - k8s/accessinternal.md - k8s/rollout.md - k8s/healthchecks.md - - k8s/healthchecks-more.md + #- k8s/healthchecks-more.md - k8s/record.md - - k8s/namespaces.md