apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-api
labels:
app: todo-api
spec:
selector:
matchLabels:
app: todo-api
strategy:
type: Recreate
template:
metadata:
labels:
app: todo-api
spec:
containers:
- image: affixxx/todo-api:latest
name: todo-api
imagePullPolicy: Always
env:
- name: MONGO_HOST
value: todo-api-mongodb
ports:
- containerPort: 8000
name: todo-api

Writing our Ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  namespace: application
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: todo.kube.kfj.io # Valid FQDN
    http:
      paths:
      - path: /
        backend:
          serviceName: todo-api
          servicePort: 8000

Now we have written our application lets deploy it

$ kubectl apply --namespace todo-api -f mongodb.yml
service "todo-api-mongodb" created
persistentvolumeclaim "mongodb-pv-claim" created
deployment "mongodb" created
$ kubectl --namespace todo-api apply -f todo-api.yml
service "todo-api" configured
deployment "todo-api" created
$ kubectl --namespace todo-api apply -f ingress.yml
ingress "ingress" created

Testing the API

Create

$ curl -X POST -d "description=A Todo Item" todo.kube.kfj.io/todo
{"ID":"5adbba7fc876a1000189f705","Date":"2018-04-21T22:26:07.228Z","Description":"A Todo Item","Done":false}

Read

Multiple

$ curl todo.kube.kfj.io/todo
[
  {"ID":"5adbba7fc876a1000189f705","Date":"2018-04-21T22:26:07.228Z","Description":"A Todo Item","Done":false},
  {"ID":"5adbbaf4c876a1000189f706","Date":"2018-04-21T22:28:04.123Z","Description":"Deploy to Kubernetes","Done":false}
]

Single

$ curl todo.kube.kfj.io/todo/5adbbaf4c876a1000189f706
[{"ID":"5adbbaf4c876a1000189f706","Date":"2018-04-21T22:28:04.123Z","Description":"Deploy to Kubernetes","Done":false}]

Update

$ curl -X PATCH todo.kube.kfj.io/todo/5adbbaf4c876a1000189f706
{"updated": true}
$ curl todo.kube.kfj.io/todo/5adbbaf4c876a1000189f706
[{"ID":"5adbbaf4c876a1000189f706","Date":"2018-04-21T22:28:04.123Z","Description":"Deploy to Kubernetes","Done":true}]

Delete

$ curl -X DELETE todo.kube.kfj.io/todo/5adbba7fc876a1000189f705
{result: 'OK'}

SUCCESS!

Thats it our API is deployed and working.

Thanks for coming back for part 3 of this mini series. Today you have learned to build super small docker images and deploy them to a kubernetes cluster. I hope you found it useful!

I will leave my API deployed to http://todo.kube.kfj.io and leave it open for a while if people want to play with it.

Thanks for reading.