Your first Kubernetes deployment
Posted on August 17, 2021 by Adrian Wyssmann ‐ 4 min read
Kubernetes is complex and at first overwhelming if you never did something with it. In this post I want to focus on a simple deployment and putting some pieces (Pod, Deployment, ConfigMap, Ingress) together to get a better understanding for newbies.
I assume you are already familiar with the Kubernetes Basics and you have setup your own cluster.
Deploy an nginx container
As a developer you usually have your own application you want to deploy. However, for this post I go a different approach which maybe also more understanding from the people coming from operations. Let’s assume we have a static website we want to deploy:
For this you need something which can serve the website, so we could use an nginx and deploy it as follows:
This will deploy a single nginx
pod:
You can then access the website using kubectl proxy to forward port 80 of the pod to port 5000 of your localhost:
Then in your browser you can access the port via https://localhost:5000, which serves the default nginx welcome page:
Use a ConfigMap to map your custom page
As next, you want to show the page mentioned above, rather than the default one. Here is where you can use a ConfigMap. The ConfigMap defines a file index.html
with some custom HTML:
The index.html
from the ConfigMap shall replace the default /usr/share/nginx/html/index.html
within the container. This is done by mapping it into the container, by extending the nginx-demo.yaml
with volumeMounts
and volumes
, telling kubernetes to map the file from withom zhe ConfigMap to the desired path in the pod:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-demo
namespace: nginx-demo
labels:
app: nginx-demo
spec:
replicas: 1
selector:
matchLabels:
app: nginx-demo
template:
metadata:
labels:
app: nginx-demo
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/share/nginx/html/index.html
name: nginx-indexhtml-vol
subPath: index.html
volumes:
- ConfigMap:
defaultMode: 420
items:
- key: index.html
path: index.html
name: nginx-demo-indexhtml
name: nginx-indexhtml-vol
This will restart the pod and then you issue again the same kubectl proxy command as above:
This should give you an idea of what a ConfigMap offers
Ingress
kubectl proxy works ok but it’s not how we want to access our website, we prefer something like https://nginx-demo.intra, Thus we need two things
expose the application as a network service
an Ingress which exposes HTTP and HTTPS routes from outside the cluster to services
You must have an Ingress controller to satisfy an Ingress. Only creating an Ingress resource has no effect.
See Ingress controller for details. This is the configuration required:
That’s it, we deployed a custom static website using a ConfigMap via an Ingress. As a next step it might be worth to look at Ingress controller, specifically also Bare-metal considerations in case you run your cluster on bare metal.