Kubernetes Tutorial
Start with a new repository:
mkdir k8s-tutorial
cd k8s-tutorial
git init
wget http://bottlepy.org/bottle.py
now create a file main.py with the following content:
from bottle import route, run
import socket
@route('/')
def hello():
name = socket.gethostname()
with open("/tmp/mysecret/topsecret", "r") as f:
secret = f.read()
return "Hello! <br/>My name is %s <br/>Let me tell you a secret: %s" % (name, secret)
run(host='0.0.0.0', port=8000, debug=True)
create a Dockerfile
FROM python:2.7
RUN mkdir /opt/tutorial
ADD *.py /opt/tutorial/
EXPOSE 8000
CMD ["python", "/opt/tutorial/main.py"]
Building the Container
create a file called Makefile with this content:
run:
python main.py
docker:
docker build -t example/k8stutorial .
push:
docker push example/k8stutorial
run-docker:
docker run -it --rm example/k8stutorial
Now build the Docker container and push it to the Docker Hub:
$ boot2docker start && eval "$(docker-machine shellinit)" # only on OS X
$ make docker
$ make push
Creating Kubernetes Replication Controller
We want to run the App on three nodes.
So let’s write a replication controller definition:
:1
k8stutorial-rc.yml
apiVersion: v1
kind: ReplicationController
metadata:
name: k8s-tutorial
labels:
app: k8s-tutorial
name: k8s-tutorial
spec:
replicas: 3
template:
metadata:
labels:
app: k8s-tutorial
name: k8s-tutorial
spec:
imagePullSecrets:
- name: registry
containers:
- name: k8s-tutorial
image: example/k8stutorial:latest
ports:
- containerPort: 8000
volumeMounts:
- name: mysecret
mountPath: /tmp/mysecret
readOnly: true
volumes:
- name: mysecret
secret:
secretName: k8stutorial-secret
We still need the secret
:1
k8stutorial-secret.yml
apiVersion: v1
kind: Secret
metadata:
name: k8stutorial-secret
type: Opaque
data:
topsecret: 1234512345
Now let’s deploy the Secret and the Replication Controller on Kubernetes:
$ brew install kubernetes-cli
$ ssh -At example@uri -L 8080:k8s-master.uri:8080
and in a new Terminal let’s have a look at the currently deployed pods and replication controllers:
# show currently deployed pods:
$ kubectl get po
# show currently deployed replication controllers:
$ kubectl get rc
Now let’s deploy our Replication Controller:
# deploy our secret
$ kubectl create -f k8stutorial-secret.yml
# deploy our new replication controller
$ kubectl create -f k8stutorial-rc.yml
Now let’s have a look at the deployed Pods and Replication Controllers again:
# show currently deployed pods:
$ kubectl get po
# show currently deployed replication controllers:
$ kubectl get rc
Create a Kubernetes Service
Write a service definition file
:1
k8stutorial-svc.yml
apiVersion: v1
kind: Service
metadata:
name: k8s-tutorial
annotations:
romulus/host: 'k8stutorial.uri'
romulus/path: '/'
labels:
name: k8s-tutorial
romulus/type: external
spec:
ports:
- port: 80
targetPort: 8000
selector:
app: k8s-tutorial
And let’s deploy the file
$ kubectl create -f k8stutorial-svc.yml
Now let’s have a look at http://k8stutorial.uri Refresh the site a few times and you will notice the different Pod names that are responding to your request. The requests are load balanced between the nodes. So every request is answered by another node.