I need to know how to host my backend project on hetzner cloud infrastructure using…
- User
I need to know how to host my backend project on hetzner cloud infrastructure using dedicated machines and the latest technology available and also the latest versions of everything. I'm currently using typescript, babel, github, mongodb atlas, node.js > 20, pnpm and I also want to use github actions, docker hub, kubernetes. Please give me a step by step from beginning to end on how achieve this goal. My health route that should be monitored to restart the container if it errors is /_health, I need this route to be checked once every 10 seconds. I want you to implement a load balancing strategy. I need this application to be exposed to the internet on a static route so my mobile application can communicate with it securely.
These are some of the tools I would like to use: microk8s calico keda kuma traefik cert_manager trust_manager helm
Things that are necessary: Use containerd as the Kubernetes container runtime instead of Docker. Implement multi-stage builds for Docker to reduce image size. Enable Horizontal Pod Autoscaling. Add readiness probes for better pod availability management. Leverage caching in GitHub Actions for faster builds.
If some of this tools can be dropped because of another tool mentioned does the same thing please don’t mind mention it and removing from the explanation. Give priority to the tools that are most easy to use and have UI
Please give me step by step on how to do it and which machine should I install the tools and which machine I need to create the files. Everything needs to be detailed so it can work perfectly! Also explain what the machine will do and explain what the node will do and which machine the node will live
- Assistant
Sure! Below is a detailed step-by-step guide to help you host your backend project on Hetzner Cloud using the latest technologies you've mentioned. We'll prioritize tools that are user-friendly and have UI support. We'll also explain which machines to use for installing tools, creating files, and the roles of each machine and node.
Overview
We'll set up a Kubernetes cluster using MicroK8s with containerd as the container runtime. We'll use Calico for networking, Traefik as the ingress controller, cert-manager for TLS certificates, and Helm for application deployment. We'll implement Horizontal Pod Autoscaling (HPA) with KEDA. We'll use GitHub Actions for CI/CD, leveraging caching for faster builds, and push Docker images to Docker Hub.
Prerequisites
- Hetzner Cloud Account: Sign up for an account if you haven't already.
- Domain Name: For TLS certificates and accessing your application via a static route.
- GitHub Repository: Your backend project hosted on GitHub.
- Docker Hub Account: To store your Docker images.
Step 1: Set Up Hetzner Cloud Infrastructure
1.1. Decide on the Infrastructure Setup
-
Kubernetes Master Node:
- Machine Type: Dedicated machine with sufficient CPU and RAM (e.g., CPX31 with 4 vCPUs, 8 GB RAM).
- Purpose: Controls the Kubernetes cluster.
-
Kubernetes Worker Nodes:
- Machine Type: One or more dedicated machines (e.g., CPX31).
- Purpose: Runs your application pods.
-
Load Balancer (Optional if using Hetzner's LB):
- Purpose: Distributes traffic across worker nodes.
- Note: Since we'll use Traefik as an ingress controller, a separate load balancer is not necessary.
1.2. Create Servers
-
Master Node:
- Create a server named
k8s-master. - OS: Ubuntu 22.04 LTS.
- Create a server named
-
Worker Nodes:
- Create servers named
k8s-worker-1,k8s-worker-2, etc. - OS: Ubuntu 22.04 LTS.
- Create servers named
1.3. Set Up SSH Access
- Ensure you can SSH into all machines using your SSH keys.
Step 2: Install Kubernetes Cluster with MicroK8s
2.1. Install MicroK8s on the Master Node
On
k8s-master:sudo apt update sudo snap install microk8s --classic --channel=1.27/stable- Note: We're using the latest stable version (adjust channel as needed).
2.2. Configure MicroK8s
- Add your user to the
microk8sgroup:
sudo usermod -a -G microk8s $USER sudo chown -f -R $USER ~/.kube- Apply group changes:
newgrp microk8s2.3. Install MicroK8s on Worker Nodes
On each
k8s-worker-X:sudo apt update sudo snap install microk8s --classic --channel=1.27/stable- Add your user to the
microk8sgroup and apply changes as above.
2.4. Join Worker Nodes to the Cluster
On
k8s-master:- Get the join command:
microk8s add-node- This will output a command like:
microk8s join 192.168.0.1:25000/abcdef1234567890On each
k8s-worker-X:- Run the join command provided.
2.5. Verify Cluster Nodes
On
k8s-master:microk8s kubectl get nodes- You should see the master and worker nodes in the Ready state.
2.6. Configure Container Runtime to Use containerd
- MicroK8s uses containerd by default, so no additional configuration is needed.
Step 3: Install Networking with Calico
3.1. Enable Calico
On
k8s-master:microk8s enable helm3 microk8s enable dns storage ingress- Install Calico using Helm:
microk8s helm3 repo add projectcalico https://docs.projectcalico.org/charts microk8s helm3 repo update microk8s helm3 install calico projectcalico/tigera-operator --version v3.25.0- Verify Calico pods are running:
microk8s kubectl get pods -n calico-system
Step 4: Install Traefik Ingress Controller
4.1. Install Traefik via Helm
On
k8s-master:- Add Traefik Helm repo:
microk8s helm3 repo add traefik https://helm.traefik.io/traefik microk8s helm3 repo update- Install Traefik:
microk8s helm3 install traefik traefik/traefik \ --namespace kube-system \ --set additionalArguments="{--providers.kubernetescrd,--providers.kubernetesingress}"- Verify Traefik is running:
microk8s kubectl get pods -n kube-system -l app.kubernetes.io/name=traefik
Step 5: Install cert-manager for TLS Certificates
5.1. Install cert-manager
On
k8s-master:- Add jetstack Helm repo:
microk8s helm3 repo add jetstack https://charts.jetstack.io microk8s helm3 repo update- Install cert-manager:
microk8s helm3 install cert-manager jetstack/cert-manager \ --namespace cert-manager \ --create-namespace \ --version v1.11.0 \ --set installCRDs=true- Verify cert-manager pods:
microk8s kubectl get pods -n cert-manager5.2. Set Up Cluster Issuer with Let's Encrypt
- Create a YAML file
cluster-issuer.yaml:
apiVersion: cert-manager.io/v1 kind: ClusterIssuer metadata: name: letsencrypt-prod spec: acme: email: your-email@example.com server: https://acme-v02.api.letsencrypt.org/directory privateKeySecretRef: name: letsencrypt-prod solvers: - http01: ingress: class: traefik- Apply the ClusterIssuer:
microk8s kubectl apply -f cluster-issuer.yaml
Step 6: Prepare Your Application for Deployment
6.1. Implement Multi-Stage Docker Builds
- Create a
Dockerfilewith multi-stage builds:
# Stage 1: Build FROM node:20-alpine AS build WORKDIR /app COPY package.json pnpm-lock.yaml ./ RUN npm install -g pnpm RUN pnpm install COPY . ./ RUN pnpm build # Stage 2: Production FROM node:20-alpine WORKDIR /app COPY --from=build /app/dist ./dist COPY package.json pnpm-lock.yaml ./ RUN npm install -g pnpm RUN pnpm install --prod EXPOSE 3000 CMD ["node", "dist/index.js"]- Note: Adjust paths and scripts according to your project structure.
6.2. Add Readiness and Liveness Probes
- In your Kubernetes deployment manifest, add probes:
readinessProbe: httpGet: path: /_health port: 3000 initialDelaySeconds: 10 periodSeconds: 10 livenessProbe: httpGet: path: /_health port: 3000 initialDelaySeconds: 15 periodSeconds: 106.3. Write Helm Chart for Application
- Create a Helm chart for your application:
helm create my-backend-
Customize
values.yaml,deployment.yaml,service.yaml, andingress.yaml. -
In
ingress.yaml, configure TLS and usetraefikas the ingress class.
Step 7: Set Up CI/CD with GitHub Actions
7.1. Create GitHub Actions Workflow
- In your GitHub repository, create
.github/workflows/ci-cd.yml:
name: CI/CD Pipeline on: push: branches: - main jobs: build-and-push: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Node.js 20.x uses: actions/setup-node@v3 with: node-version: '20.x' cache: 'pnpm' - name: Install dependencies run: pnpm install - name: Build project run: pnpm build - name: Log in to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push Docker image uses: docker/build-push-action@v4 with: context: . push: true tags: your-dockerhub-username/my-backend:latest - name: Deploy to Kubernetes uses: azure/k8s-deploy@v3 with: namespace: default manifests: | ./deployment.yaml ./service.yaml ./ingress.yaml images: | your-dockerhub-username/my-backend:latest kubectl-version: 'latest' kubeconfig: ${{ secrets.KUBE_CONFIG }}- Note: You'll need to store
DOCKERHUB_USERNAME,DOCKERHUB_TOKEN, andKUBE_CONFIGas secrets in your GitHub repository.
7.2. Set Up Caching
- The
actions/setup-node@v3action withcache: 'pnpm'leverages caching for dependencies.
Step 8: Deploy Application with Helm
8.1. Package and Deploy Helm Chart
- Package your Helm chart:
helm package my-backend- Deploy to the cluster:
microk8s helm3 install my-backend ./my-backend-0.1.0.tgz- Alternatively, use GitHub Actions to automate this step.
Step 9: Configure KEDA for Horizontal Pod Autoscaling
9.1. Install KEDA
On
k8s-master:- Add KEDA Helm repo:
microk8s helm3 repo add kedacore https://kedacore.github.io/charts microk8s helm3 repo update- Install KEDA:
microk8s helm3 install keda kedacore/keda --namespace keda --create-namespace9.2. Configure HPA with KEDA
- In your Helm chart deployment, add annotations:
metadata: annotations: autoscaling.keda.sh/scaledobject: "true"- Create a
ScaledObjectmanifest:
apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: my-backend-scaledobject namespace: default spec: scaleTargetRef: name: my-backend minReplicaCount: 1 maxReplicaCount: 10 triggers: - type: cpu metadata: type: Utilization value: "50"- Apply the ScaledObject:
microk8s kubectl apply -f scaledobject.yaml
Step 10: Expose Application Securely Over the Internet
10.1. Configure DNS
- Point your domain or subdomain (e.g.,
api.yourdomain.com) to the public IP of your master node or the load balancer IP if using one.
10.2. Configure Ingress with TLS
- In your
ingress.yamlof the Helm chart:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-backend-ingress annotations: kubernetes.io/ingress.class: traefik cert-manager.io/cluster-issuer: letsencrypt-prod spec: tls: - hosts: - api.yourdomain.com secretName: my-backend-tls rules: - host: api.yourdomain.com http: paths: - path: / pathType: Prefix backend: service: name: my-backend-service port: number: 3000- Deploy the ingress:
microk8s kubectl apply -f ingress.yaml10.3. Verify TLS Certificate
- Check that cert-manager has issued a certificate:
microk8s kubectl get certificates- Access your application via
https://api.yourdomain.comfrom your mobile application.
Step 11: Monitoring and Health Checks
11.1. Configure Readiness and Liveness Probes
- Already added in Step 6.2 using the
/_healthroute.
11.2. Set Up Monitoring
-
You can use Kubernetes Dashboard or tools like Lens for UI-based cluster management.
-
Enable Kubernetes Dashboard:
microk8s enable dashboard- Access the dashboard:
microk8s dashboard-proxy
Explanation of Machine Roles
Master Node (
k8s-master):- Role: Controls the Kubernetes cluster, schedules pods, manages the cluster state.
- Installed Tools:
- MicroK8s (Kubernetes control plane components)
- Helm (For managing applications)
- Calico (Networking)
- Traefik (Ingress controller)
- cert-manager (TLS certificates)
- KEDA (Autoscaling)
- Kubernetes Dashboard (Cluster monitoring)
Worker Nodes (
k8s-worker-X):- Role: Runs your application pods and services.
- Installed Tools:
- MicroK8s (Kubernetes node components)
- Calico (Networking)
Application Deployment:
- The application containers (pods) are scheduled on Worker Nodes.
- Traefik runs on Master Node and routes external traffic to the pods on Worker Nodes.
Additional Notes
- Container Runtime: We are using containerd, which is the default for MicroK8s; no need for Docker.
- Load Balancing: Handled by Traefik, which balances traffic between multiple pod replicas.
- Autoscaling: Handled by KEDA, scaling pods based on CPU utilization.
- Security: TLS termination is managed by cert-manager with Let's Encrypt certificates.
- CI/CD: GitHub Actions automates building, testing, and deploying your application.
- Caching: Implemented in GitHub Actions for
pnpmdependencies. - Monitoring Health: The
/_healthroute is used for Kubernetes probes to monitor pod health and restart containers if necessary.
Conclusion
By following these steps, you will have a scalable, secure, and modern deployment of your backend application on Hetzner Cloud infrastructure, utilizing Kubernetes with the latest technologies and best practices. The tools chosen are efficient, have good community support, and some offer UI options for easier management.
Optional Simplifications
- Kuma (Service Mesh): Since we are using Traefik and Calico, adding Kuma may not be necessary unless you need advanced service mesh features.
- Trust Manager: If certificate management is adequately handled by cert-manager, additional tools may not be needed.
- UI Tools: You can use tools like Lens (https://k8slens.dev/) for a graphical interface to manage your Kubernetes cluster.
Let me know if you need further clarification on any step!