Kubernetes: Adding Cashier & Waiter

Overview

Teaching: 5 min
Exercises: 20 min
Questions
  • How to add other components?
  • What more options can I use?
Objectives
  • Multiple Microservices
  • Limits & Environment Settings

Cashier & Waiter

We’ll now add the Cashier and Waiter to this Café.

This works similar to the Coffee Machine, we create four new files

waiter/k8s/template-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: DEPLOYMENTNAME
  namespace: NAMESPACE
  labels:
    app: DEPLOYMENTNAME
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: DEPLOYMENTNAME
    spec:
      containers:
        - name: DEPLOYMENTNAME
          image: IMAGETAG
          ports:
            - containerPort: 8080
          resources:
            limits:
              memory: "500Mi"
              cpu: "500m"
            requests:
              memory: "500Mi"
              cpu: "10m"
  selector:
    matchLabels:
      app: DEPLOYMENTNAME

waiter/k8s/template-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: DEPLOYMENTNAME
  namespace: NAMESPACE
spec:
  selector:
    app: DEPLOYMENTNAME
  ports:
  - port: 1338
    targetPort: 8080
  type: LoadBalancer

cashier/k8s/template-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: DEPLOYMENTNAME
  namespace: NAMESPACE
  labels:
    app: DEPLOYMENTNAME
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: DEPLOYMENTNAME
    spec:
      containers:
        - name: DEPLOYMENTNAME
          image: IMAGETAG
          ports:
            - containerPort: 80
          resources:
            limits:
              memory: "100Mi"
              cpu: "500m"
            requests:
              memory: "100Mi"
              cpu: "10m"
  selector:
    matchLabels:
      app: DEPLOYMENTNAME

cashier/k8s/template-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: DEPLOYMENTNAME
  namespace: NAMESPACE
spec:
  selector:
    app: DEPLOYMENTNAME
  ports:
  - port: 1336
    targetPort: 80
  type: LoadBalancer

You will notice that the Services are just about identical, with the exception of the ports.

The Deployments are also very similar. The first differences we see here are the resource limits imposed on the the containers. These values enable Kubernetes to better schedule the pods in the cluster.

Excercise: Add sensible resource limits to your Coffee Machine.

Now let’s deploy the components. As we are using the same script as before, which was using some variables, make sure to always set them correctly:

# Cashier
export component_name=cashier
export image_tag=${docker_repo}/cafe-${component_name}:master-latest
bash component-creation.sh

and

# Waiter
export component_name=waiter
export image_tag=${docker_repo}/cafe-${component_name}:master-latest
bash component-creation.sh

and as before

# Coffee Machine
export component_name=coffee-${myname}
export image_tag=${docker_repo}/cafe-${component_name}:master-latest
bash component-creation.sh

Now you might realise, that the components can’t actually talk to one another. We need to inject the correct settings into the Cashier’s and Waiter’s environment by adding these bits

cashier/k8s/template-deployment.yaml

...
      containers:
        - name: DEPLOYMENTNAME
          image: IMAGETAG
          env:
            - name: 'CAFE__DEFAULTCOFFEEMACHINE'
              value: 'http://<svc>:1337'
          ports:
            - containerPort: 80
...

waiter/k8s/template-deployment.yaml

...
      containers:
        - name: DEPLOYMENTNAME
          image: IMAGETAG
          env:
            - name: 'CASHIER_URL'
              value: 'http://<svc>:1336'
          ports:
            - containerPort: 8080
...

In both cases you need to replace <svc> with the name of the respective service in the output of kubectl get services. This should be cafe-<myname>-coffee-<myname> and cafe-<myname>-cashier respectively.