Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

I need to know how to host my backend project on hetzner cloud infrastructure using…

  1. 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 and I don’t want to use the AWS load balancer. 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.

    microk8s, calico, keda, kuma, traefik and k8slens. Also: cert_manager: Automates TLS certificate management and renewal.

    trust_manager: Centralizes trust management by distributing trusted certificates across cluster.

    helm: Package manager for kubernetes for easy deployment and management of applications.

    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.

    cluster-api-provider-hetzner

    terraform

    I need to be able to scale more machines when the demand rises and delete when the demand falls using the hetzner cluster api

    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

  2. Assistant

    To help you host your backend project on Hetzner Cloud infrastructure with scalable and secure deployment using the latest technologies, we'll outline a step-by-step guide. We'll prioritize tools that are easy to use and have a user interface (UI) to simplify management.

    Overview of the Steps:

    1. Set Up Hetzner Cloud Infrastructure with Terraform
    2. Provision a Kubernetes Cluster Using Cluster API and Hetzner Provider
    3. Install Rancher for Cluster Management (UI-based)
    4. Deploy Traefik Ingress Controller for Load Balancing
    5. Set Up cert-manager for Automatic TLS Certificates
    6. Configure Helm for Application Deployment
    7. Dockerize Your Application with Multi-Stage Builds
    8. Set Up GitHub Actions for CI/CD Pipeline
    9. Implement Kubernetes Deployment with Readiness and Liveness Probes
    10. Enable Horizontal Pod Autoscaling (HPA)
    11. Configure Cluster Autoscaler for Node Scaling
    12. Set Up Monitoring with K8sLens
    13. Optimize GitHub Actions with Caching
    14. Secure Your Application and Communication

    Let's dive into each step.


    Prerequisites

    • Hetzner Cloud Account: Sign up if you haven't already.
    • Domain Name: For exposing your application with TLS.
    • Local Machine Setup:
      • Terraform: Infrastructure as Code tool.
      • kubectl: Kubernetes command-line tool.
      • Helm: Kubernetes package manager.
      • Docker: For building container images.
      • Git: Version control.
      • pnpm: Package manager.
      • Node.js (>20): Runtime environment.
      • GitHub Account: For repository and Actions.
      • Docker Hub Account: For storing Docker images.

    Step 1: Set Up Hetzner Cloud Infrastructure with Terraform

    Firstly, we'll use Terraform along with the cluster-api-provider-hetzner to manage our infrastructure.

    1.1 Install Terraform

    Download and install Terraform from the official website.

    1.2 Set Up Hetzner API Token

    • Log in to your Hetzner Cloud account.
    • Navigate to Security > API Tokens and create a new token.
    • Store this token securely; we'll use it in Terraform.

    1.3 Set Up Terraform Configuration

    Create a directory for your Terraform configurations and create a main.tf file.

    # main.tf
    
    provider "hcloud" {
      token = var.hcloud_token
    }
    
    # Kubernetes Cluster using Hetzner provider
    module "kubernetes_cluster" {
      source  = "your_cluster_api_provider_hetzner_module_source"
      # Module variables and configurations
    }
    

    1.4 Define Variables

    Create a variables.tf file to define variables such as the Hetzner token.

    # variables.tf
    
    variable "hcloud_token" {
      description = "Hetzner Cloud API Token"
      type        = string
    }
    

    Create a terraform.tfvars file to assign values to variables.

    # terraform.tfvars
    
    hcloud_token = "your_hetzner_api_token"
    

    1.5 Initialize and Apply Terraform

    terraform init
    terraform apply
    

    This will provision your infrastructure on Hetzner Cloud.


    Step 2: Provision a Kubernetes Cluster Using Cluster API and Hetzner Provider

    We'll use the Cluster API with the Hetzner provider to set up a scalable Kubernetes cluster.

    2.1 Install Cluster API Tools

    Install clusterctl.

    curl -L https://github.com/kubernetes-sigs/cluster-api/releases/download/v1.4.2/clusterctl-linux-amd64 -o clusterctl
    chmod +x clusterctl
    sudo mv clusterctl /usr/local/bin/
    

    2.2 Initialize Cluster API on Hetzner

    clusterctl init --infrastructure hetzner
    

    This command initializes the Cluster API components and sets up the Hetzner infrastructure provider.

    2.3 Create Cluster Configuration

    Generate the cluster configuration.

    clusterctl generate cluster my-cluster --kubernetes-version v1.26.0 --control-plane-machine-count=1 --worker-machine-count=2 > my-cluster.yaml
    

    2.4 Apply Cluster Configuration

    kubectl apply -f my-cluster.yaml
    

    Wait for the cluster to be provisioned.


    Step 3: Install Rancher for Cluster Management (UI-based)

    Rancher provides an intuitive UI for managing Kubernetes clusters.

    3.1 Install Rancher

    Run Rancher in a Docker container on your local machine or a dedicated node.

    docker run -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher:latest
    

    3.2 Access Rancher UI

    • Navigate to https://your-node-ip in your browser.
    • Set up an admin password.

    3.3 Import Your Kubernetes Cluster

    • In Rancher UI, go to Cluster Management.
    • Click Import Existing Cluster.
    • Follow the instructions to import your Hetzner cluster.

    Step 4: Deploy Traefik Ingress Controller for Load Balancing

    Traefik will handle routing and load balancing for your application.

    4.1 Install Traefik via Rancher UI

    • In Rancher, navigate to Apps & Marketplace.
    • Search for Traefik and install it.
    • Configure as needed, ensuring it's set as the default ingress controller.

    Step 5: Set Up cert-manager for Automatic TLS Certificates

    cert-manager automates the issuance and renewal of TLS certificates.

    5.1 Install cert-manager via Rancher UI

    • Navigate to Apps & Marketplace.
    • Search for cert-manager and install it.

    5.2 Configure cert-manager

    • Set up a ClusterIssuer for Let's Encrypt.
    # cluster-issuer.yaml
    
    apiVersion: cert-manager.io/v1
    kind: ClusterIssuer
    metadata:
      name: letsencrypt-production
    spec:
      acme:
        email: your-email@example.com
        server: https://acme-v02.api.letsencrypt.org/directory
        privateKeySecretRef:
          name: acme-account-key
        solvers:
          - http01:
              ingress:
                class: traefik
    
    • Apply the configuration:
    kubectl apply -f cluster-issuer.yaml
    

    Step 6: Configure Helm for Application Deployment

    Helm simplifies Kubernetes application deployment and management.

    6.1 Create a Helm Chart for Your Application

    In your project repository, create a Helm chart:

    helm create my-backend-app
    

    6.2 Update values.yaml

    Customize the values.yaml file with your application-specific settings.

    # values.yaml
    
    replicaCount: 2
    
    image:
      repository: your-dockerhub-username/your-app-image
      tag: "latest"
      pullPolicy: Always
    
    service:
      type: ClusterIP
      port: 80
    
    ingress:
      enabled: true
      annotations:
        kubernetes.io/ingress.class: traefik
        cert-manager.io/cluster-issuer: letsencrypt-production
      hosts:
        - host: api.your-domain.com
          paths:
            - path: /
              pathType: ImplementationSpecific
      tls:
        - secretName: tls-secret
          hosts:
            - api.your-domain.com
    
    autoscaling:
      enabled: true
      minReplicas: 2
      maxReplicas: 10
      targetCPUUtilizationPercentage: 80
    
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 500m
        memory: 256Mi
    

    6.3 Configure Readiness and Liveness Probes

    Update the deployment template in templates/deployment.yaml:

    # Add under containers:
    
    readinessProbe:
      httpGet:
        path: /_health
        port: 80
      initialDelaySeconds: 5
      periodSeconds: 10
    
    livenessProbe:
      httpGet:
        path: /_health
        port: 80
      initialDelaySeconds: 15
      periodSeconds: 10
    

    Step 7: Dockerize Your Application with Multi-Stage Builds

    Using multi-stage builds reduces the final image size.

    7.1 Create a Multi-Stage Dockerfile

    # Stage 1: Build
    FROM node:20-alpine AS builder
    WORKDIR /app
    COPY package.json pnpm-lock.yaml ./
    RUN npm install -g pnpm && pnpm install
    COPY . .
    RUN pnpm run build
    
    # Stage 2: Run
    FROM node:20-alpine
    WORKDIR /app
    COPY package.json pnpm-lock.yaml ./
    RUN npm install -g pnpm --production && pnpm install --production
    COPY --from=builder /app/dist ./dist
    EXPOSE 80
    CMD ["node", "dist/index.js"]
    

    7.2 Build and Push the Docker Image

    Locally, you can build and push the image to Docker Hub:

    docker build -t your-dockerhub-username/your-app-image:latest .
    docker push your-dockerhub-username/your-app-image:latest
    

    Step 8: Set Up GitHub Actions for CI/CD Pipeline

    Automate testing, building, and deployment.

    8.1 Create GitHub Actions Workflow

    Create .github/workflows/cicd.yml in your repository.

    # cicd.yml
    
    name: CI/CD Pipeline
    
    on:
      push:
        branches: [main]
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v3
    
        - name: Cache pnpm dependencies
          uses: actions/cache@v3
          with:
            path: ~/.pnpm-store
            key: ${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
            restore-keys: |
              ${{ runner.os }}-pnpm-
    
        - name: Install Dependencies
          run: |
            npm install -g pnpm
            pnpm install
    
        - name: Run Tests
          run: pnpm test
    
        - name: Build Docker Image
          run: docker build -t your-dockerhub-username/your-app-image:latest .
    
        - name: Login to Docker Hub
          uses: docker/login-action@v2
          with:
            username: ${{ secrets.DOCKER_USERNAME }}
            password: ${{ secrets.DOCKER_PASSWORD }}
    
        - name: Push Docker Image
          run: docker push your-dockerhub-username/your-app-image:latest
    
        - name: Deploy to Kubernetes
          uses: azure/k8s-deploy@v3
          with:
            manifests: |
              ./deployment.yaml
              ./service.yaml
            images: |
              your-dockerhub-username/your-app-image:latest
            namespace: default
            kubeconfig: ${{ secrets.KUBECONFIG }}
    

    8.2 Set Up GitHub Secrets

    • DOCKER_USERNAME and DOCKER_PASSWORD: Your Docker Hub credentials.
    • KUBECONFIG: Base64 encoded kubeconfig file for your Kubernetes cluster.

    Step 9: Implement Kubernetes Deployment with Readiness and Liveness Probes

    Ensure your application is healthy and can recover from failures.

    9.1 Update Deployment Manifests

    Ensure your Kubernetes deployment includes the probes (already done in Helm chart).


    Step 10: Enable Horizontal Pod Autoscaling (HPA)

    Scale pods based on resource utilization.

    10.1 Ensure Metrics Server is Running

    Install the metrics server:

    kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
    

    10.2 Verify Metrics Server

    kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes"
    

    Step 11: Configure Cluster Autoscaler for Node Scaling

    Scale cluster nodes based on workload.

    11.1 Install Cluster Autoscaler

    • Download the appropriate manifest:
    wget https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/hetzner/cluster-autoscaler-hetzner.yaml
    
    • Modify the manifest:

      • Set your Hetzner Cloud token.
      • Configure resource limits.

    11.2 Apply the Manifest

    kubectl apply -f cluster-autoscaler-hetzner.yaml
    

    Step 12: Set Up Monitoring with K8sLens

    K8sLens provides a rich UI for monitoring your cluster.

    12.1 Install K8sLens

    Download and install from the official website.

    12.2 Connect to Your Cluster

    • Open K8sLens.
    • Add your kubeconfig file.
    • Explore your cluster visually.

    Step 13: Optimize GitHub Actions with Caching

    Speed up your CI builds.

    • Already configured caching for pnpm dependencies in the GitHub Actions workflow.

    Step 14: Secure Your Application and Communication

    Ensure all communication is secure and authenticated.

    14.1 Secure Ingress with TLS

    • Already configured with cert-manager and Let's Encrypt.
    • Ensure your DNS records point to your cluster's ingress controller.

    14.2 Implement Authentication in Your Application

    • Use JWTs or OAuth for securing your APIs.
    • Ensure sensitive data is stored securely (use Kubernetes secrets for environment variables).

    Optional Tool Considerations

    Kuma (Service Mesh)

    • Use Case: Fine-grained traffic control, observability, and security policies.
    • Decision: If you're not deploying multiple microservices requiring complex service-to-service communication, you may omit Kuma for simplicity.

    KEDA (Event-Driven Autoscaling)

    • Use Case: Scaling based on custom metrics or external events (e.g., message queues).
    • Decision: If HPA suffices (scaling based on CPU/memory), you may skip KEDA to reduce complexity.

    Calico (Networking and Network Policies)

    • Use Case: Advanced networking and network policy enforcement.
    • Decision: If basic networking meets your needs, and you've installed Traefik for ingress, Calico can be considered optional.

    Final Notes

    • Monitoring and Alerting: Consider setting up Prometheus and Grafana for in-depth monitoring if needed.
    • Logging: Implement centralized logging with tools like EFK stack (Elasticsearch, Fluentd, Kibana) if necessary.
    • Backup and Recovery: Plan for data backup, especially for stateful components.
    • Testing: Thoroughly test each step in a staging environment before deploying to production.
    • Documentation: Keep documentation updated for future reference and team collaboration.

    By following these steps, you will have a robust, scalable, and secure backend application hosted on Hetzner Cloud infrastructure. The use of Rancher and K8sLens provides user-friendly interfaces for cluster management, aligning with your preference for tools that offer a UI.

    Remember, the key to a successful deployment is iterative testing and incremental implementation. Start by setting up the infrastructure, then gradually add components like CI/CD, autoscaling, and monitoring. This approach reduces complexity and makes troubleshooting easier.