Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?

Beaver Barcamp 17

An introduction to Kubernetes

Pre-requisites

  • [Install Vagrant][vagrant-up]
  • Install virtualbox
  • [Clone the coreos-kubernetes repository][coreos-kubernetes]
  • Install kubectl
  • Clone this repository locally
  • Setup your local cluster

Alternatively, install Minikube.

In a terminal:

$ cd coreos-vagrant/single-node   # Navigate to the coroes-vagrant repository, single-node directory
$ vagrant up                      # Start the virtual machine, this is your 'cluster'
...
$ export KUBECONFIG=`pwd`/kubeconfig   # Tell kubernetes where your node is and how to reach it
$ kubectl get all                 # If this returns a non-error you should be good to go! (be patient)
NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
svc/kubernetes   10.3.0.1     <none>        443/TCP   6s

Deploy an application

Once you've got a kubernetes cluster running, got to your local checkout of this repository and run:

$ kubectl create -f pods.yaml

This will create a pod hosting a small website.

A Pod is essentially a collection of one or more containers.

To access that website, run the following:

$ kubectl port-proxy tinsy-app 8000:8000

and in your browser go to localhost:8000. This port-forwards an application running in a pod onto your local machine.

Now tear down that pod. We're moving onto bigger and better things.

$ kubectl delete -f pods.yaml

Next we will create a deployment.

$ kubectl create -f deployment.yaml

This deploys three instances of our application whose lifecycles are managed by Kubernetes.

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

$ 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 (listed in the Vagrantfile if you're using Vagrant), and goto : 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: extensions/v1beta1
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