Overview
Teaching: 5 min
Exercises: 20 minQuestionsObjectives
- How do I get my Coffee Machine into the Namespace?
- Deployments and Services
- Adding a service to a Kubernetes Cluster
Now that we have the Namespace, we need to get something into it.
For this, you’ll need your component name:
export component_name=coffee-${myname}
Let’s create the directories
mkdir -p $component_name/k8s
First we have $component_name/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: 1337
selector:
matchLabels:
app: DEPLOYMENTNAME
We will replace the upper case words again using a script, but let’s take a look first.
This is a “Deployment” for our DEPLOYMENTNAME (Hint: we’ll use a combination of product and component) to the Namespace we set up. It will be one replica (instance or copy) based our image that specifies the coffee machine port. You also see a lot of labels being thrown around. This is used to link different parts and helps us figuring out what’s what when looking at different Kubernetes views later.
Now we add the $component_name/k8s/template-service.yaml
apiVersion: v1
kind: Service
metadata:
name: DEPLOYMENTNAME
namespace: NAMESPACE
spec:
selector:
app: DEPLOYMENTNAME
ports:
- port: 1337
targetPort: 1337
type: LoadBalancer
This is a “Service” definition linking back to the Deployment above by use of the label and exposing the container’s port to the cluster. In fact, by making it a LoadBalancer we are exposing it publicly.
Now we need the script to do the transformation component-creation.sh:
#!/bin/bash
set -eu
### Kubernetes configuration generation ###
sed "s#DEPLOYMENTNAME#${product_name}-${component_name}#g; s#NAMESPACE#${product_name}#g; s#IMAGETAG#${image_tag}#g" ${component_name}/k8s/template-deployment.yaml > ${component_name}/k8s/$product_name-$component_name-deployment.yaml
sed "s/DEPLOYMENTNAME/${product_name}-${component_name}/g; s/NAMESPACE/${product_name}/g" ${component_name}/k8s/template-service.yaml > ${component_name}/k8s/$product_name-$component_name-service.yaml
# Deployment
kubectl apply -f ${component_name}/k8s/${product_name}-${component_name}-deployment.yaml
# Service
kubectl apply -f ${component_name}/k8s/${product_name}-${component_name}-service.yaml
The script works similar to what we did before, replacing some strings and applying the results. Notice, that we need to set the $image_tag.
export docker_repo=eu.gcr.io/cessda-sandbox
export image_tag=${docker_repo}/cafe-${component_name}:master-latest
Note: The cafe in the $image_tag is the Café’s product name as we are re-using the existing images from the first session. In the real setting, this should of course be replace by ${product_name}.
Now let’s go
bash component-creation.sh
Check what has been created:
kubectl get pods
kubectl get deployments
kubectl get services
You can now talk to your Coffee Machine!