🐋 Setup and deploy on Kubernetes 🐳
💬 Be brave and try not to cry, you will probably fail at some steps and this tutorial is likely already outdated. Hopefully this can help a little.
Summary of the steps you need to do:
- Dockerize your app
- Contact the DSI to create namespaces
- Configure your project
🏷️ Namespaces creation
You need to contact the DSI either by email or on Rocket.Chat.
💬 You need one namespace per environment you need. For instance, you want to run both a DEV and PROD environments, you will need to ask for 2 different namespaces (e.g.project-dev
andproject-prod
).
You can give as much information as possible to the team for creation:
- What is your repository link
- Is the service going to be available on Pasteur network only or on the web
- Anything you might think of that could be useful
📂 Configure your project
🔑 Deploy tokens
💬 You can name variables the way you want, of course.
- Create your token (
Settings/Repository/Deploy tokens
)- name: kubernetes
- scopes: read_registry
- Add the new informations to your Variables (
Settings/CI/CD/Variables
)- Add the username (by default
gitlab+deploy-token-xx
) and name itDEPLOY_USER
- Add the token and name it
DEPLOY_TOKEN
- Add the username (by default
📦 Build your image
To run your app on Kubernetes, you first need to have Dockerfile
for your different services.
💬 Let's take the example, for the backend of an application.
First you need to build your image during the CI in gitlab. For this, I recommand to write a script that do this process and call it in the .gitlab-ci.yml
:
- Build script:
#!/bin/sh
echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY
docker build -t "$CI_REGISTRY_IMAGE/backend:${CI_COMMIT_REF_NAME}" backend/
docker tag "$CI_REGISTRY_IMAGE/backend:${CI_COMMIT_REF_NAME}" "$CI_REGISTRY_IMAGE/backend:latest"
docker push "$CI_REGISTRY_IMAGE/backend:${CI_COMMIT_REF_NAME}"
You do not have to set
CI_REGISTRY_PASSWORD
and$CI_REGISTRY_USER
variables in gitlab.
-
.gitlab-ci.yml
:
Add these lines or adapt to your existing .gitlab-ci.yml
.
services:
- docker:18-dind
stages:
- build
build-backend:
image: docker:latest
stage: build
script:
- sh path/to/your/script.sh
tags:
- k8s
🚚 Deploy your image
Once your image is built and available you can use Kubernetes to deploy it.
For this you need:
- Write manifest yaml files for your service for Kubernetes
- Write a deployment script to be called in your
.gitlab-ci.yml
- Complete your
.gitlab-ci.yml
file