DEV Community

Calvin Tran
Calvin Tran

Posted on

DataOps part 3: deploy Titanic to kubernetes

Alt Text
Image Source: Blue Star Line

This is the last part of dataops series, if you haven’t gone through the first and second ones, here is the link: part1, part2


We have to make sure that our kubernetes cluster has an ingress installed. If you have configured, here is the way how to do it

  • For vagrant kubernetes
$ kubectl apply -f

$ kubectl apply -f
  • For microk8s
$ kubectl apply -f

$ microk8s.enable ingress


Before starting with deployment process, let’s go through the process flow.

Kubernetes process flow

That are the steps for deploy machine learning model on kubernete the easy way.

  1. Create a deployment (Deployment runs multiple replicas of your application and automatically replaces any instances that fail or become unresponsive — google k8s).

  2. Expose deployment to services (Services for expose your pods into network, there are different type of services like NodePort or LoadBalancer).

  3. Create ingress (Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster, on another way, ingress help outside traffic access to our API).

Create Deployment

Deployment is an object in kubernetes that let you manage a set of identical pods. Without the deployment, you have to create, update, delete the pod manually

apiVersion: apps/v1
kind: Deployment
  name: titanic-app
    app: titanic-app
    tier: backend
    version: v1
      app: titanic-app
  replicas: 2
        app: titanic-app
      - name: titanic-app
        image: canhtran/titanic-app-demo:latest
        - containerPort: 5000

I create two replicas (2 pods) for our api, the default port is flask port 5000.

# Apply deployment
$ kubectl apply -f deployment-titanic.yaml
deployment.apps/titanic-app created

We can check the pods status.

$ kubectl get pod
NAME                           READY   STATUS              RESTARTS   AGE
titanic-app-6f4fdf8668-7nctk   1/1     Running             0          64s
titanic-app-6f4fdf8668-zhghw   0/1     ContainerCreating   0          64s

Expose services

Unlike deployment is used to keep a set of pods running by creating pods from a template, service is used to allow network access to a set of pods.

$ kubectl expose deployment titanic-app --type=LoadBalancer --port=5000
service/titanic-app exposed

Also get our service status

$ kubectl get svc
NAME          TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
titanic-app   LoadBalancer   <pending>     5000:32755/TCP   14

Create ingress

Finally, an ingress so that our service can be accessible from outside.

apiVersion: extensions/v1beta1
kind: Ingress
  name: titanic-ingress
  annotations: / "false"
  - http:
        - path: /
            serviceName: titanic-app
            servicePort: 5000
$ kubectl apply -f ingress-titanic.yaml
ingress.extensions/titanic-ingress created

Let's wrap it up and test our API

$ curl -i -X POST -H "Content-Type:application/json" -d '{"Pclass":3,"Sex":1,"Age":2,"Fare":7,"Embarked":0,"Title":1,"FamilySize":2,"IsAlone":0,"Age_Class":6, "FarePerPerson":3}'

HTTP/1.1 200 OK
Server: openresty/
Date: Mon, 29 Jul 2019 12:00:10 GMT
Content-Type: application/json
Content-Length: 13
Connection: keep-alive


The API have been up and running as expected :)

Top comments (0)