Kubernetes in Five Minutes
Published: 2020-11-30 13:59:09

> [Also available on my Medium](https://joeir.medium.com/kubernetes-in-five-minutes-18aa4101544f)

## A self-inflicted challenge because I heard people dig Kubernetes in 2020

> “If you can’t explain it simply enough, you don’t understand it well enough”.

Challenge accepted. Here’s my attempt to explain native components of Kubernetes in five minutes with the objective of covering concepts and resource types that will enable you to ~~talk about Kubernetes like you understand it~~ understand Kubernetes and deploy applications at a basic level.

As a pretext, I’ve found it useful to think of Kubernetes-related work not as a collection of verbose YAML files defining strange `kind`s of resources, but as a ***set of abstractions representing common design patterns in application deployment***.

To me, the areas that Kubernetes natively accounts for in logical — *imo* — order are:

1. **Hardware** – Node
2. **Orchestration** – Deployment, Job, CronJob, StatefulSet, DaemonSet
3. **Configuration** – ConfigMap, Secret
4. **Persistence** — PersistentVolumeClaim, PersistentVolume
5. **Execution** – Pod, ReplicaSet
6. **Access Control** – Namespace, ServiceAccount, Role, ClusterRole
7. **Exposure** – Service, Ingress

## Hardware

All software has one fundamental dependency: a machine to run on. We need **hardware** whether it’s our own computers or someone else’s and this abstraction comes in the form of a Node resource type.

1. **[Node](https://kubernetes.io/docs/concepts/architecture/nodes/)**: Represents a logical machine/VM (eg. your computer, ec2 instance, a droplet).

## Orchestration

After we’ve defined our hardware, we need a plan on how to deploy our application. Will it be a single instance? Do we need maybe two? When we need to update our application, should it be done one by one or all at once? Do we want it to be deployed across all our Nodes? Should it be run once every day at 3 AM? We need to **orchestrate** the deployment.

1. **[Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/)**: Long-standing workloads (eg. a server)
1. **[Job](https://kubernetes.io/docs/concepts/workloads/controllers/job/)**: One-off workload (eg. a shell script/database migration task)
1. **[CronJob](https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/)**: Periodically-run one-off workload (eg. data sync task)
1. **[StatefulSet](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/)**: Workloads that require a readable/writable data volume (hard disk) that persists beyond restarts and is not shared amongst other instances of the same application. Use cases include services that implement eventual consistency (eg. transactional databases)
1. **[DaemonSet](https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/)**: Workloads that should be distributed across all targeted Nodes (eg. log collectors, system monitors, security software)

## Configuration

So we’ve got a plan, but applications tend to be a fickle bunch and also need to be told certain things at runtime instead of plan-time; Things such as which network interface to bind to, which port to listen on, which API keys to use *et cetera*. While these things can technically be hard-coded, best-practices of 2020 generally suggest they should not. So we need a way to **configure** our application at runtime.

1. **[ConfigMap](https://kubernetes.io/docs/concepts/configuration/configmap/)**: Stores configuration for mounting as environment variables or read-only files (eg. `SERVER_PORT=8080`)
1. **[Secret](https://kubernetes.io/docs/concepts/configuration/secret/)**: Fundamentally the same but stored in Base64 encoded text and used for values that should not be stored in plaintext (eg. `GITHUB_API_TOKEN=😱`)

## Persistence

Now what if our application handles files and requires a hard disk that it can read and write from across different versions and instances? We need our data to **persist** by providing our application’s system with a hard disk (AKA volume in technical terms).

1. **[PersistentVolume](https://kubernetes.io/docs/concepts/storage/persistent-volumes/)**: A logical “hard drive” (eg. Seagate 2TB, EBS)
1. **[PersistentVolumeClaim](https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#create-a-persistentvolumeclaim)**: Binds a PersistentVolume to a Pod. This is more of a virtual construct: think of it as the act of mounting a hard drive. A PVC defines that intention to mount a hard drive and expose its filesystem for an application to use.

## Execution

So we’ve defined how our application will be deployed and configured, and we’ve given it some hard disk space to use. All that’s left now is to **execute** the application.

1. **[Pod](https://kubernetes.io/docs/concepts/workloads/pods/)**: One instance of your application (eg. npm start, go run ./...)
1. **[ReplicaSet](https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/)**: Maintains the desired count of your application instances.

## Access Control

But what if someone accidentally introduced a nasty virus into our application (somehow)? In an enterprise environment with compliance teams nagging at the whole DevOps thingy, we need to ensure that applications can only access resources that it needs to access. We on the other hand, need **access control**.

1. **[Namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/?spm=a2c65.11461447.0.0.15d87863YiFv0v)**: Defines a virtual boundary within a cluster for access control mechanisms to be implemented on top of. Think about these like browser tabs: an open Facebook tab shouldn’t know what you’re googling in another tab.
1. **[ServiceAccount](https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/)**: Defines a virtual user that can be assigned a Role or ClusterRole which contains a set of permissions scoped to a set of resources (eg. your user login on your machine)
1. **[Role](https://kubernetes.io/docs/reference/access-authn-authz/rbac/)**: Defines namespace-scoped resource access permissions. Linked to a ServiceAccount with namespace-scoped access via a **[RoleBinding](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#rolebinding-v1-rbac-authorization-k8s-io)** resource
1. **[ClusterRole](https://kubernetes.io/docs/reference/access-authn-authz/rbac/)**: Defines cluster-wide resource access permissions. Linked to a ServiceAccount with cluster-wide access via a **[ClusterRoleBinding](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#rolebinding-v1-rbac-authorization-k8s-io)** resource

## Exposure

Finally, there’s no use of an application that no one can access. After we’ve made the application happy and it’s running as expected, we expose it to the rest of the network/public internet for users and other services to access it.

1. **[Service](https://kubernetes.io/docs/concepts/services-networking/service/)**: Exposes a workload to the cluster. Think running npm start on a server application and your application being available on `localhost:3000`. Behind the scenes, your application is binding to a port on your computer. A Service defines that binding.
1. **[Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/)**: Exposes a Service to outside the cluster. Ever tried asking your co-worker to access your `localhost:3000`? Chances are you’d have ended up using `ngrok` or a similar tunneling software. An Ingress is basically an `ngrok` that exposes your `localhost:3000` so it becomes accessible to a larger network.

## Linking It All Up —Some Common Deployment Patterns

They said a picture is worth a thousand words. So here’s some diagrams to visualise how all the above links up.

### An HTTP-based API server

![k8s-in-5-mins-http-server](https://dev-to-uploads.s3.amazonaws.com/i/qtp1wkelheb59zwf5ngs.png) 

### A periodic job

![k8s-in-5-mins-periodic-job](https://dev-to-uploads.s3.amazonaws.com/i/uiacnl4c0jdx45pm4m68.png) 

### An eventually consistent database system

![k8s-in-5-mins-db](https://dev-to-uploads.s3.amazonaws.com/i/td2r0u5s20fmk2bn3l79.png) 

### A cloud-monitoring agent

![k8s-in-5-mins-cloud-monitor](https://dev-to-uploads.s3.amazonaws.com/i/yq26obpezx1p5c5l6f7o.png) 

# Whew!

I hope that was somehow useful for you. If it was, ❤️s are always appreciated for reach, and feel free to follow to get pinged when I publish similar-ish pieces.

> If you would also like to support my writing with actual cash and if you have a paid Medium account, consider reading this article on Medium and giving some 👏s there <3