Compare commits

..

No commits in common. "1a9a403b74756264b99c38701479221f4eaea226" and "c9df1812566e37347a4b29c5894aea7ea4d218a4" have entirely different histories.

15 changed files with 33 additions and 140 deletions

View File

@ -1,50 +1,37 @@
# Backend deployment
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.
## Overview
This repository contains the necessary files for deploying the backend application to a kubernetes environment.
## Prerequisites
For the deployment of the **backend** application, the following prerequisites need to be met:
Before deploying the backend application, ensure that you have the following prerequisites:
- On the cluster side: a Kubernetes cluster with the following components:
- Kubernetes cluster with the following components:
- ingress controller
- storage class
- On the local side: for development and local testing:
- `kubectl` installed
- `kustomize` (usually bundled with `kubectl`)
- `kubectl` installed (`kustomize` is usually bundled)
## Deployment Steps
## Manual deployment
#### Initial deployment
### Initial deployment
To deploy the backend application, follow these steps:
1. Clone this repository
1. Apply the desired overly: `kubectl apply -k <path-to-overlay>`
1. Clone this repository: `git clone https://github.com/your-repo.git`
1. Apply the kustomization: `kubectl apply -k .`
#### Rolling updates
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:
### 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:
```bash
kubectl -n <namespace> rollout restart deployment/anyway-backend
kubectl -n anyway-backend rollout restart deployment/nav-backend
```
## 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.
## Configuration
The RBAC configuration for the service included for your reference in the file `rbac.yaml`. To use it, extract the token from the secret `deployment-token-secret`.
Then fill in the token and service account name into the kubeconfig.
The kustomization allows for easy configuration of the backend application. To customize the deployment, modify 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.
### 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.

View File

@ -1,10 +0,0 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
# - namespace.yaml
- pvc.yaml
- deployment.yaml
- service.yaml
- ingress.yaml
- memcached/

View File

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

View File

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

View File

@ -1,19 +1,18 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: anyway-backend
nameSuffix: -prod
resources:
- namespace.yaml
- pvc.yaml
- deployment.yaml
- service.yaml
- ingress.yaml
- memcached/
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
newTag: latest

View File

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

View File

@ -1,18 +0,0 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: anyway-backend
nameSuffix: -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

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

View File

@ -1,59 +0,0 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: anyway-backend
name: deployment-role
rules:
- apiGroups: [""] # "" indicates the core API group
resources:
- pods
- services
- deployments
- ingresses
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
- update
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: deployment-rolebinding
namespace: anyway-backend
subjects:
- kind: ServiceAccount
name: deployment-sa
namespace: anyway-backend
roleRef:
kind: Role
name: deployment-role
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: deployment-sa
namespace: anyway-backend
automountServiceAccountToken: false
---
apiVersion: v1
kind: Secret
metadata:
namespace: anyway-backend
name: deployment-token-secret
annotations:
kubernetes.io/service-account.name: deployment-sa
type: kubernetes.io/service-account-token

View File

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