From 111ff30c38ba586cd074e89ab35466d50fe5914e Mon Sep 17 00:00:00 2001 From: Jerome Petazzoni Date: Sun, 7 Feb 2021 21:41:08 +0100 Subject: [PATCH 1/2] Add k9s section --- slides/k8s/k9s.md | 141 ++++++++++++++++++++++++++++++++++++++ slides/kube-fullday.yml | 1 + slides/kube-halfday.yml | 1 + slides/kube-selfpaced.yml | 1 + slides/kube-twodays.yml | 1 + 5 files changed, 145 insertions(+) create mode 100644 slides/k8s/k9s.md diff --git a/slides/k8s/k9s.md b/slides/k8s/k9s.md new file mode 100644 index 00000000..9c5174e4 --- /dev/null +++ b/slides/k8s/k9s.md @@ -0,0 +1,141 @@ +# k9s + +- Somewhere in between CLI and GUI (or web UI), we can find the magic land of TUI + + - [Text-based user interfaces](https://en.wikipedia.org/wiki/Text-based_user_interface) + + - often using libraries like [curses](https://en.wikipedia.org/wiki/Curses_%28programming_library%29) and its successors + +- Some folks love them, some folks hate them, some are indifferent ... + +- But it's nice to have different options! + +- Let's see one particular TUI for Kubernetes: [k9s](https://k9scli.io/) + +--- + +## Installing k9s + +- If you are using a training cluster or the [shpod](https://github.com/jpetazzo/shpod) image, k9s is pre-installed + +- Otherwise, it can be installed easily: + + - with [various package managers](https://k9scli.io/topics/install/) + + - or by fetching a [binary release](https://github.com/derailed/k9s/releases) + +- We don't need to set up or configure anything + + (it will use the same configuration as `kubectl` and other well-behaved clients) + +- Just run `k9s` to fire it up! + +--- + +## What kind to we want to see? + +- Press `:` to change the type of resource to view + +- Then type, for instance, `ns` or `namespace` or `nam[TAB]`, then `[ENTER]` + +- Use the arrows to move down to e.g. `kube-system`, and press `[ENTER]` + +- Or, type `/kub` or `/sys` to filter the output, and press `[ENTER]` twice + + (once to exit the filter, once to enter the namespace) + +- We now see the pods in `kube-system`! + +--- + +## Interacting with pods + +- `l` to view logs + +- `d` to describe + +- `s` to get a shell (won't work if `sh` isn't available in the container image) + +- `e` to edit + +- `shift-f` to define port forwarding + +- `ctrl-k` to kill + +- `[ESC]` to get out or get back + +--- + +## Quick navigation between namespaces + +- On top of the screen, we should see shortcuts like this: + ``` + <0> all + <1> kube-system + <2> default + ``` + +- Pressing the corresponding number switches to that namespace + + (or shows resources across all namespaces with `0`) + +- Locate a namespace with a copy of DockerCoins, and go there! + +--- + +## Interacting with Deployments + +- View Deployments (type `:` `deploy` `[ENTER]`) + +- Select e.g. `worker` + +- Scale it with `s` + +- View its aggregated logs with `l` + +--- + +## Exit + +- Exit at any time with `Ctrl-C` + +- k9s will "remember" where you were + + (and go back there next time you run it) + +--- + +## Pros + +- Very convenient to navigate through resources + + (hopping from a deployment, to its pod, to another namespace, etc.) + +- Very convenient to quickly view logs of e.g. init containers + +- Very convenient to get a (quasi) realtime view of resources + + (if we use `watch kubectl get` a lot, we will probably like k9s) + +--- + +## Cons + +- Doesn't promote automation / scripting + + (if you repeat the same things over and over, there is a scripting opportunity) + +- Not all features are available + + (e.g. executing arbitrary commands in containers) + +--- + +## Conclusion + +Try it out, and see if it makes you more productive! + +??? + +:EN:- The k9s TUI +:FR:- L'interface texte k9s diff --git a/slides/kube-fullday.yml b/slides/kube-fullday.yml index 72c9f631..87ba65d0 100644 --- a/slides/kube-fullday.yml +++ b/slides/kube-fullday.yml @@ -58,6 +58,7 @@ content: #- k8s/setup-managed.md #- k8s/setup-selfhosted.md #- k8s/dashboard.md + #- k8s/k9s.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md diff --git a/slides/kube-halfday.yml b/slides/kube-halfday.yml index 0aaea969..6f4ed3c6 100644 --- a/slides/kube-halfday.yml +++ b/slides/kube-halfday.yml @@ -58,6 +58,7 @@ content: #- k8s/accessinternal.md #- k8s/kubectlproxy.md - - k8s/dashboard.md + - k8s/k9s.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md diff --git a/slides/kube-selfpaced.yml b/slides/kube-selfpaced.yml index 0fe9be4b..90b3d5e3 100644 --- a/slides/kube-selfpaced.yml +++ b/slides/kube-selfpaced.yml @@ -59,6 +59,7 @@ content: - k8s/setup-managed.md - k8s/setup-selfhosted.md - k8s/dashboard.md + - k8s/k9s.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md diff --git a/slides/kube-twodays.yml b/slides/kube-twodays.yml index 638bcc26..e52f7eaf 100644 --- a/slides/kube-twodays.yml +++ b/slides/kube-twodays.yml @@ -58,6 +58,7 @@ content: #- k8s/setup-managed.md #- k8s/setup-selfhosted.md - k8s/dashboard.md + - k8s/k9s.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md From 955149e0190e965109854b41c75106f33ce81a20 Mon Sep 17 00:00:00 2001 From: Jerome Petazzoni Date: Sun, 7 Feb 2021 21:44:38 +0100 Subject: [PATCH 2/2] Add Tilt section --- dockercoins/Tiltfile | 49 +++++++ slides/k8s/tilt.md | 302 ++++++++++++++++++++++++++++++++++++++ slides/kube-fullday.yml | 1 + slides/kube-halfday.yml | 3 +- slides/kube-selfpaced.yml | 1 + slides/kube-twodays.yml | 1 + 6 files changed, 356 insertions(+), 1 deletion(-) create mode 100644 dockercoins/Tiltfile create mode 100644 slides/k8s/tilt.md diff --git a/dockercoins/Tiltfile b/dockercoins/Tiltfile new file mode 100644 index 00000000..d06b8120 --- /dev/null +++ b/dockercoins/Tiltfile @@ -0,0 +1,49 @@ +k8s_yaml(blob(''' +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: registry + name: registry +spec: + selector: + matchLabels: + app: registry + template: + metadata: + labels: + app: registry + spec: + containers: + - image: registry + name: registry +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: registry + name: registry +spec: + ports: + - port: 5000 + protocol: TCP + targetPort: 5000 + nodePort: 30555 + selector: + app: registry + type: NodePort +''')) +default_registry('localhost:30555') +docker_build('dockercoins/hasher', 'hasher') +docker_build('dockercoins/rng', 'rng') +docker_build('dockercoins/webui', 'webui') +docker_build('dockercoins/worker', 'worker') +k8s_yaml('../k8s/dockercoins.yaml') + +# Uncomment the following line to let tilt run with the default kubeadm cluster-admin context. +#allow_k8s_contexts('kubernetes-admin@kubernetes') + +# While we're here: if you're controlling a remote cluster, uncomment that line. +# It will create a port forward so that you can access the remote registry. +#k8s_resource(workload='registry', port_forwards='30555:5000') diff --git a/slides/k8s/tilt.md b/slides/k8s/tilt.md new file mode 100644 index 00000000..678f005a --- /dev/null +++ b/slides/k8s/tilt.md @@ -0,0 +1,302 @@ +# Tilt + +- What does a development workflow look like? + + - make changes + + - test / see these changes + + - repeat! + +- What does it look like, with containers? + + 🤔 + +--- + +## Basic Docker workflow + +- Preparation + + - write Dockerfiles + +- Iteration + + - edit code + - `docker build` + - `docker run` + - test + - `docker stop` + +Straightforward when we have a single container. + +--- + +## Docker workflow with volumes + +- Preparation + + - write Dockerfiles + - `docker build` + `docker run` + +- Iteration + + - edit code + - test + +Note: only works with interpreted languages. +
+(Compiled languages require extra work.) + +--- + +## Docker workflow with Compose + +- Preparation + + - write Dockerfiles + Compose file + - `docker-compose up` + +- Iteration + + - edit code + - test + - `docker-compose up` (as needed) + +Simplifies complex scenarios (multiple containers). +
+Facilitates updating images. + +--- + +## Basic Kubernetes workflow + +- Preparation + + - write Dockerfiles + - write Kubernetes YAML + - set up container registry + +- Iteration + + - edit code + - build images + - push images + - update Kubernetes resources + +Seems simple enough, right? + +--- + +## Basic Kubernetes workflow + +- Preparation + + - write Dockerfiles + - write Kubernetes YAML + - **set up container registry** + +- Iteration + + - edit code + - build images + - **push images** + - update Kubernetes resources + +Ah, right ... + +--- + +## We need a registry + +- Remember "build, ship, and run" + +- Registries are involved in the "ship" phase + +- With Docker, we were building and running on the same node + +- We didn't need a registry! + +- With Kubernetes, though ... + +--- + +## Special case of single node clusters + +- If our Kubernetes has only one node ... + +- ... We can build directly on that node ... + +- ... We don't need to push images ... + +- ... We don't need to run a registry! + +- Examples: Docker Desktop, Minikube ... + +--- + +## When we have more than one node + +- Which registry should we use? + + (Docker Hub, Quay, cloud-based, self-hosted ...) + +- Should we use a single registry, or one per cluster or environment? + +- Which tags and credentials should we use? + + (in particular when using a shared registry!) + +- How do we provision that registry and its users? + +- How do we adjust our Kubernetes YAML manifests? + + (e.g. to inject image names and tags) + +--- + +## More questions + +- The whole cycle (build+push+update) is expensive + +- If we have many services, how do we update only the ones we need? + +- Can we take shortcuts? + + (e.g. synchronized files without going through a whole build+push+update cycle) + +--- + +## Tilt + +- Tilt is a tool to address all these questions + +- There are other similar tools (e.g. Skaffold) + +- We arbitrarily decided to focus on that one + +--- + +## Tilt in practice + +- The `dockercoins` directory in our repository has a `Tiltfile` + +- Go to that directory and try `tilt up` + +- Tilt should refuse to start, but it will explain why + +- Edit the `Tiltfile` accordingly and try again + +- Open the Tilt web UI + + (if running Tilt on a remote machine, you will need `tilt up --host 0.0.0.0`) + +- Watch as the Dockercoins app is built, pushed, started + +--- + +## What's in our Tiltfile? + +- Kubernetes manifests for a local registry + +- Kubernetes manifests for DockerCoins + +- Instructions indicating how to build DockerCoins' images + +- A tiny bit of sugar + + (telling Tilt which registry to use) + +--- + + +## How does it work? + +- Tilt keeps track of dependencies between files and resources + + (a bit like a `make` that would run continuously) + +- It automatically alters some resources + + (for instance, it updates the images used in our Kubernetes manifests) + +- That's it! + +(And of course, it provides a great web UI, lots of libraries, etc.) + +--- + +## What happens when we edit a file (1/2) + +- Let's change e.g. `worker/worker.py` + +- Thanks to this line, + ```python + docker_build('dockercoins/worker', 'worker') + ``` + ... Tilt watches the `worker` directory and uses it to build `dockercoins/worker` + +- Thanks to this line, + ```python + default_registry('localhost:30555') + ``` + ... Tilt actually renames `dockercoins/worker` to `localhost:30555/dockercoins_worker` + +- Tilt will tag the image with something like `tilt-xxxxxxxxxx` + +--- + +## What happens when we edit a file (2/2) + +- Thanks to this line, + ```python + k8s_yaml('../k8s/dockercoins.yaml') + ``` + ... Tilt is aware of our Kubernetes resources + +- The `worker` Deployment uses `dockercoins/worker`, so it must be updated + +- `dockercoins/worker` becomes `localhost:30555/dockercoins_worker:tilt-xxx` + +- The `worker` Deployment gets updated on the Kubernetes cluster + +- All these operations (and their log output) are visible in the Tilt UI + +--- + +## Configuration file format + +- The Tiltfile is written in [Starlark](https://github.com/bazelbuild/starlark) + + (essentially a subset of Python) + +- Tilt monitors the Tiltfile too + + (so it reloads it immediately when we change it) + +--- + +## Tilt "killer features" + +- Dependency engine + + (build or run only what's necessary) + +- Ability to watch resources + + (execute actions immediately, without explicitly running a command) + +- Rich library of function and helpers + + (build container images, manipulate YAML manifests...) + +- Convenient UI (web; TUI also available) + + (provides immediate feedback and logs) + +- Extensibility! + +??? + +:EN:- Development workflow with Tilt +:FR:- Développer avec Tilt diff --git a/slides/kube-fullday.yml b/slides/kube-fullday.yml index 87ba65d0..17b93bcd 100644 --- a/slides/kube-fullday.yml +++ b/slides/kube-fullday.yml @@ -59,6 +59,7 @@ content: #- k8s/setup-selfhosted.md #- k8s/dashboard.md #- k8s/k9s.md + #- k8s/tilt.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md diff --git a/slides/kube-halfday.yml b/slides/kube-halfday.yml index 6f4ed3c6..42da801c 100644 --- a/slides/kube-halfday.yml +++ b/slides/kube-halfday.yml @@ -58,7 +58,8 @@ content: #- k8s/accessinternal.md #- k8s/kubectlproxy.md - - k8s/dashboard.md - - k8s/k9s.md + #- k8s/k9s.md + #- k8s/tilt.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md diff --git a/slides/kube-selfpaced.yml b/slides/kube-selfpaced.yml index 90b3d5e3..d725650b 100644 --- a/slides/kube-selfpaced.yml +++ b/slides/kube-selfpaced.yml @@ -60,6 +60,7 @@ content: - k8s/setup-selfhosted.md - k8s/dashboard.md - k8s/k9s.md + - k8s/tilt.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md diff --git a/slides/kube-twodays.yml b/slides/kube-twodays.yml index e52f7eaf..7b8b4de7 100644 --- a/slides/kube-twodays.yml +++ b/slides/kube-twodays.yml @@ -59,6 +59,7 @@ content: #- k8s/setup-selfhosted.md - k8s/dashboard.md - k8s/k9s.md + #- k8s/tilt.md #- k8s/kubectlscale.md - k8s/scalingdockercoins.md - shared/hastyconclusions.md