Skip to content

Instantly share code, notes, and snippets.

@goshlanguage
Last active April 16, 2021 18:11
Show Gist options
  • Save goshlanguage/3ed197239eca0fb7b919fcfb458444e6 to your computer and use it in GitHub Desktop.
Save goshlanguage/3ed197239eca0fb7b919fcfb458444e6 to your computer and use it in GitHub Desktop.
Kubernetes Quickstart

Kubernetes

Kubernetes is a container orchestrator driven by a robust API. It is known for being the basis of resilient systems, "bin packing" or more efficiently colocating workloads to efficiently use system resources, and being extensible.

It does so at the cost of having somewhat of a barrier of entry. This guide will assume some level of familiarity with containers, apis, and some tooling. Feel free to leave comments for clarification.

Command Line Tools

To run through this short lab, you will need to install the following tools. This guide assumes the user is using macos and brew but will also provide links for non brew users:

Tool Brew Install Github Description
kubectl brew install kubectl https://github.com/kubernetes/kubectl kubectl is the command line client for the kubernetes api
kind brew install kind https://github.com/kubernetes-sigs/kind Kind, or kubernetes in docker, is a lightweight cluster bootstrapper that will allow us to quickly bootstrap a lab environment

Bringing up your cluster

To bring up an emulated kubernetes cluster, you can run kind create cluster:

kind create cluster
Creating cluster "kind" ...
 โœ“ Ensuring node image (kindest/node:v1.19.1) ๐Ÿ–ผ
 โœ“ Preparing nodes ๐Ÿ“ฆ
 โœ“ Writing configuration ๐Ÿ“œ
 โœ“ Starting control-plane ๐Ÿ•น๏ธ
 โœ“ Installing CNI ๐Ÿ”Œ
 โœ“ Installing StorageClass ๐Ÿ’พ
Set kubectl context to "kind-kind"
You can now use your cluster with:

kubectl cluster-info --context kind-kind

What's in a cluster?

Kubernetes is made up of the following components:

Component Function Runs On
apiserver The API interface for the cluster, scales horizontally with the control-plane hosts control plane
etcd The state store (key value store) for kubernetes control plane
controller-manager runs the control loops that detect node outages, manages jobs (like cron jobs), populates endpoints and more control plane
scheduler watches for newly created resources and determines which node to assign them too control plane
kubelet per node agent that ensure pods are running and healthy all
kube-proxy network proxy that manages network rules on nodes all

For more information, see https://kubernetes.io/docs/concepts/overview/components/

Now that you have a cluster running, we can take a look at these services in our cluster by looking in the kube-system namespace:

k get po -n kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
coredns-f9fd979d6-l5bxp                      1/1     Running   0          7m3s
coredns-f9fd979d6-rdjwd                      1/1     Running   0          7m3s
etcd-kind-control-plane                      1/1     Running   0          7m14s
kindnet-jxzjw                                1/1     Running   0          7m3s
kube-apiserver-kind-control-plane            1/1     Running   0          7m14s
kube-controller-manager-kind-control-plane   1/1     Running   0          7m14s
kube-proxy-bk96x                             1/1     Running   0          7m3s
kube-scheduler-kind-control-plane            1/1     Running   0          7m14s

Contexts and Authentication

If you already have a kubernetes cluster's credentials on disk (~/.kube/config by default), creating a cluster with kind will automatically switch your context to kind-kind but it's a good idea to confirm. You can do so by looking at the output of kubectl config current-context

kubectl config current-context
kind-kind

You can see all contexts available in your kube config by running kubectl config get-contexts, and set your context with kubectl config use-context <context name>.

Kubernetes uses client certificate authentication. When you ran kind create cluster, it generated a CA, and a series of certificates to be used for this purpose. They were then stored in your ~/.kube/config file for your convenience, along with your user, the host address, and more.

For more about authentication see: https://kubernetes.io/docs/reference/access-authn-authz/authentication/

Common Kubernetes Resources

Here are a list of common kubernetes constructs, and some associated documentation. These fundamental constructs should familiarize you with what you need to get started

Resource Description Doc Link
Node Represents the hosts that make up the cluster, virtualized or otherwise https://kubernetes.io/docs/concepts/architecture/nodes/
Pod A pod is a network namespaced set of containers, or a single container that represent a process https://kubernetes.io/docs/concepts/workloads/pods/
Namespace A network and resource isolated environment where a project or set of processes can be grouped https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
Deployment A definition for pods and how they should be scaled. Also enables blue/green updates https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
DaemonSet A definition for pods that should run on each host, like a daemon would https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/
Other Workload Types There are other ways to define workloads (such as a cron job), see the doc link for more information https://kubernetes.io/docs/concepts/workloads/controllers/
ConfigMap When you need to pass configuration to your container, a configmap can help you do this by loading it onto the disk or into the environment https://kubernetes.io/docs/concepts/configuration/configmap/
Secret A construct for secrets such as tokens and keys https://kubernetes.io/docs/concepts/configuration/secret/
Service Service is a network construct that can include LoadBalancer https://kubernetes.io/docs/concepts/services-networking/service/

There are many more kubernetes resources, however these will be the fundamental resources that you will use. Also note that kubernetes is extensible. There are Custom Resource Definitions, or CRDs, that allow you to create interfaces to map non kubernetes native resources into the kubernetes API.

Lab

This lab aims to quickly touch on tasks you'll be sure to need when deploying applications to kubernetes. For a comprehensive set of self guided labs, please see https://kubernetes.io/docs/tasks/

Let's create a pod with memory and cpu resources configured. To do so, we can reference: https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/ https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/

Create an nginx container requesting "0.5"/"1" CPU cores, and "100Mi"/"200Mi" of RAM. You can apply your manifest once written with: kubectl apply -f ./path/to/manifest.yaml

You should be able then to list your pod with kubectl get pods

Next, proxy to the pod with kubectl port-forward -n nginx pod/nginx 80:8000, using your pod as an example. Ref: https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/

Next, let's define a service that sits in front of our pod. Ref: https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/

Lets now use a configmap, and mount a custom index.html to change our webpage: Ref: https://kubernetes.io/docs/concepts/configuration/configmap/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment