Multi-Stage Continuous Deployment with GitLab, Helm and Kubernetes
Our goal in this section is to deploy the application to a Kubernetes cluster. Since we are not in a real production environment, we will use the same K3s cluster we set up to run GitLab runners. We will deploy the application to the default namespace and use a NodePort service to expose it to the outside world. If you have been using DigitalOcean, the "dev" machine is where the K3s cluster is running and it has a public IP address. We will use this IP address to access the application using the NodePort service.
ℹ️ In a real production environment, you would use a LoadBalancer or an Ingress controller to expose your application to the outside world. Our focus here is not having a production-ready setup but to understand how to continuously deploy an application to a cluster.
First of all, we already built the Docker image and pushed it to the GitLab Container Registry. This was done in the earlier sections. The image has the following tag:
registry.gitlab.com/$GITLAB_GROUP/$GITLAB_PROJECT:v0
Let's export the variables IMAGE_NAME to our bash environment:
cat <> $HOME/.bashrc && \
source $HOME/.bashrc
export IMAGE_NAME=registry.gitlab.com/$GITLAB_GROUP/$GITLAB_PROJECT:v0
EOF
Now, let's create a Kubernetes deployment and service for our application. We will use the following command to create theses resources:
mkdir -p $HOME/todo/app/manifests/plain && \
cat < $HOME/todo/app/manifests/plain/app.yaml
# Deployment configuration for the todo-app
apiVersion: apps/v1
# Specifies the kind of resource,
# in this case, a Deployment
kind: Deployment
metadata:
# Name of the deployment
name: todo-app
spec:
# Number of pod replicas to maintain
replicas: 1
# Specifies how to identify the pods
# that belong to this Deployment
selector:
matchLabels:
# Pods must have the label 'app: todo-app' to match
app: todo-app
# Defines the pod template
template:
metadata:
labels:
# Labels assigned to the pods created
# by this Deployment
app: todo-app
spec:
# Secret for pulling images
# from a private registry
imagePullSecrets:
- name: gitlab-registry
# List of containers in the pod
containers:
# Image to be used for the container,
# dynamically set by the environment
- image: ${IMAGE_NAME}
# Name of the container
name: todo-app
ports:
- # Exposes port 5000 from the container
containerPort: 5000
# Resource requests and limits for the container
resources:
limits:
# Maximum CPU usage
cpu: 100m
# Maximum memory usage
memory: 128Mi
requests:
# Minimum CPU guaranteed
cpu: 100m
# Minimum memory guaranteed
memory: 128Mi
# Configuration for the readiness probe
# to check pod health
readinessProbe:
httpGet:
# Endpoint checked by the readiness probe
path: /tasks
# Port to access the readiness probe
port: 5000
# Delay before starting the readiness checkCloud Native CI/CD with GitLab
From Commit to Production ReadyEnroll now to unlock all content and receive all future updates for free.
