Skip to content

Instantly share code, notes, and snippets.

@pop
Last active December 19, 2019 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pop/a56a464cd9830a8d34356409149fde2f to your computer and use it in GitHub Desktop.
Save pop/a56a464cd9830a8d34356409149fde2f to your computer and use it in GitHub Desktop.

A crashcourse for Kubernetes

Feel free to clone this Gist to follow along!

Pre-requisites

We will be running a local Kubernetes cluster. If you have an existing Kubernetes cluster you are fond of, feel free to use that instead!

First order of business is to install Minikube.

MacOS

First, install Docker Desktop for Mac.

Then install minikube using Homebrew.

$ brew install minikube

Next start your local cluster using the hyperkit hypervisor.

$ minikube start --vm-driver=hyperkit

Linux

Follow the Installing Minikube on Linux doc to get minikube setup.

Linux has a few hypervisor options, but I prefer KVM. Download the Minikube kvm2 driver after installing Minikube, then run:

$ minikube start --vm-driver=kvm2

Misc

If the minikube start commands fail, check out the Minikube Drivers docs for other hypervisor options.

You may also need to install kubectl.

Verify you have a working cluster

The following command should succeed if you have a working cluster:

$ kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   32m

Deploy an application

Now that you have cluster you can start playing around with it!

First we will deploy some Pods to the cluster.

A Pod is a collection of one or more containers.

A Container is a semi-isolated process running on a shared host.

$ kubectl create -f pods.yaml

This will create a pod hosting a small website.

To access the website you just started running, run the following:

$ kubectl port-forward tinsy-app-pod 8001:8000

In your browser go to https://localhost:8001

This port-forwards an application running in a pod onto your local machine.

Now tear down that pod! We have bigger and better things to deploy.

$ kubectl delete -f pods.yaml

Next we will create a deployment.

A Deployment is a declaratively managed ReplicaSet.

A ReplicaSet is a scalable set of replicated Pods; common for creating Highly Available apps.

$ kubectl create -f deployment.yaml

This deploys three instances of our application (Pods) whose life-cycles are managed by a Kubernetes ReplicaSet.

To access these three instances as one application we need to create a Kubernetes Service.

A Service is a load-balanced interface to an application.

$ kubectl create -f service.yaml

This exposes our application over a randomly assigned port to our local system. Find the IP address of our Kubernetes node, and go to : in your browser. Find the port with kubectl get -f service.yaml.

Note: Reload the application a few times, notice the HOSTNAME variable change value.

apiVersion: apps/v1
kind: Deployment
metadata:
name: tinsy-app-deployment
labels:
app: tinsy-app
spec:
replicas: 3
selector:
matchLabels:
app: tinsy-app
template:
metadata:
labels:
app: tinsy-app
spec:
containers:
- name: tinsy-app-container
image: quay.io/elijahcaine/tinsy-flask-app:envs
ports:
- containerPort: 8000
imagePullPolicy: Always
apiVersion: v1
kind: Pod
metadata:
name: tinsy-app-pod
spec:
containers:
- name: tinsy-app
image: quay.io/elijahcaine/tinsy-flask-app:master
ports:
- containerPort: 8000
imagePullPolicy: Always
apiVersion: v1
kind: Service
metadata:
name: tinsy-app-service
labels:
app: tinsy-app
spec:
selector:
app: tinsy-app
ports:
- name: tinsy-app-port
port: 8000
type: NodePort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment