update deployment for multi-environment setup

This commit is contained in:
Remy Moll 2024-09-20 22:09:48 +02:00
parent c9df181256
commit d9d8aa8e8e
14 changed files with 78 additions and 32 deletions

View File

@ -1,37 +1,49 @@
# Backend deployment # Backend deployment
## Overview The container is built using the Dockerfile and deployed to our own kubernetes cluster.
Both steps are handled by the CI/CD pipeline:
- First the container is built in the workflow `workflow_build-image.yaml` and pushed to the (local) container registry.
- Then the deployment is handled in the workflow `workflow_deploy-container.yaml` which deploys the container to the kubernetes cluster. Depending on the branch, the deployment is done to the staging or production environment.
This repository contains the necessary files for deploying the backend application to a kubernetes environment.
## Prerequisites ## Prerequisites
Before deploying the backend application, ensure that you have the following prerequisites: For the deployment of the **backend** application, the following prerequisites need to be met:
- Kubernetes cluster with the following components: - On the cluster side: a Kubernetes cluster with the following components:
- ingress controller - ingress controller
- storage class - storage class
- `kubectl` installed (`kustomize` is usually bundled) - On the local side: for development and local testing:
- `kubectl` installed
- `kustomize` (usually bundled with `kubectl`)
## Deployment Steps
### Initial deployment ## Manual deployment
#### Initial deployment
To deploy the backend application, follow these steps: To deploy the backend application, follow these steps:
1. Clone this repository: `git clone https://github.com/your-repo.git` 1. Clone this repository
1. Apply the kustomization: `kubectl apply -k .` 1. Apply the desired overly: `kubectl apply -k <path-to-overlay>`
### Rolling updates #### Rolling updates
Since the deployment uses the `latest` tag for the backend application, we simply need to trigger a rolling update which will pull the latest image from the registry. To do this, run the following command: Since the deployment uses a single tag (along with the `always` pull policy) for the backend application, we simply need to trigger a rolling update which will pull the latest image from the registry. To do this, run the following command:
```bash ```bash
kubectl -n anyway-backend rollout restart deployment/nav-backend kubectl -n <namespace> rollout restart deployment/anyway-backend
``` ```
## Configuration ## Automated deployment
### CI/CD and credentials
For the deployment to work, the CI runner needs to authenticate against the kubernetes cluster. This is done by creating a service account in the cluster and providing the CI runner with the necessary credentials through a kubeconfig file. This file is stored as a repository secret `KUBE_CONFIG` and is used by the `kubectl` command in the CI pipeline.
The kustomization allows for easy configuration of the backend application. To customize the deployment, modify the `kustomization.yaml` file. The RBAC configuration for the service included for your reference in the file `rbac.yaml`.
### Memcached
The backend application requires a memcached instance. By default, the kustomization deploys this instance, as configured under `memcached/`. To disable this, comment out the `memcached` overlay in the `kustomization.yaml` file. ### Deployment environments
The deployment is done to two environments:
- Staging: All builds from forks and pull requests are deployed to the staging environment. This is done to test the changes before merging them to the main branch.
- Production: Only builds from the main branch are deployed to the production environment. This is the live environment that is used by the users. The main branch is protected and can only be merged to through pull requests.

View File

@ -1,16 +1,16 @@
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: nav-backend name: anyway-backend
spec: spec:
replicas: 1 replicas: 1
selector: selector:
matchLabels: matchLabels:
app: nav-backend app: anyway-backend
template: template:
metadata: metadata:
labels: labels:
app: nav-backend app: anyway-backend
spec: spec:
containers: containers:
- name: worker - name: worker

View File

@ -1,15 +1,15 @@
kind: IngressRoute kind: IngressRoute
apiVersion: traefik.io/v1alpha1 apiVersion: traefik.io/v1alpha1
metadata: metadata:
name: nav-ingress name: anyway-ingress
spec: spec:
entryPoints: entryPoints:
- websecure - websecure
routes: routes:
- match: Host(`anyway.kluster.moll.re`) - match: Host(`dummy`)
kind: Rule kind: Rule
services: services:
- name: nav-service - name: anyway-backend
port: 8000 port: 8000
tls: tls:
certResolver: default-tls certResolver: default-tls

View File

@ -1,8 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
namespace: anyway-backend
resources: resources:
- namespace.yaml - namespace.yaml
- pvc.yaml - pvc.yaml
@ -10,9 +8,3 @@ resources:
- service.yaml - service.yaml
- ingress.yaml - ingress.yaml
- memcached/ - memcached/
images:
- name: backend-image
newName: git.kluster.moll.re/anydev/anyway-backend
newTag: latest

View File

@ -1,10 +1,10 @@
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: nav-service name: anyway-backend
spec: spec:
selector: selector:
app: nav-backend app: anyway-backend
ports: ports:
- protocol: TCP - protocol: TCP
port: 8000 port: 8000

View File

@ -0,0 +1,18 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: anyway-prod
images:
- name: backend-image
newName: git.kluster.moll.re/anydev/anyway-backend
newTag: stable
patches:
- path: patch-ingress.yaml
target:
kind: IngressRoute
name: anyway-ingress

View File

@ -0,0 +1,3 @@
- op: replace #action
path: /spec/routes/0/match
value: Host(`anyway.anydev.info`) || Host(`anyway.kluster.moll.re`)

View File

@ -0,0 +1,18 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: anyway-stg
images:
- name: backend-image
newName: git.kluster.moll.re/anydev/anyway-backend
newTag: unstable
patches:
- path: patch-ingress.yaml
target:
kind: IngressRoute
name: anyway-ingress

View File

@ -0,0 +1,3 @@
- op: replace #action
path: /spec/routes/0/match
value: Host(`anyway-stg.anydev.info`) || Host(`anyway-stg.kluster.moll.re`)