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 oracle cloud infrastructure using…

  1. User

    I need to know how to host my backend project on oracle 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.

    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

    Introduction

    Hosting your backend project on Oracle Cloud Infrastructure (OCI) using dedicated machines and the latest technologies involves several steps, from setting up your cloud environment to deploying and managing your application with Kubernetes. This guide will walk you through the process step-by-step, integrating tools like MicroK8s, Calico, Keda, Kuma, Traefik, and K8sLens, among others. We'll prioritize tools that are easy to use and have a user interface (UI) to simplify management.


    Prerequisites:

    • An Oracle Cloud Infrastructure account.
    • Basic knowledge of Kubernetes and Docker.
    • Access to your project's source code on GitHub.
    • A domain name for your application.

    Step 1: Set Up Oracle Cloud Infrastructure (OCI) Account

    1. Create an OCI Account:

      • Visit Oracle Cloud and sign up for an account.
      • Choose the appropriate plan that provides dedicated compute resources.
    2. Set Up Compartment:

      • In the OCI Console, create a compartment to organize your resources.
    3. Generate SSH Keys:

      • Generate an SSH key pair on your local machine to securely access your instances.
        ssh-keygen -t rsa -b 4096
        

    Step 2: Provision Dedicated Compute Instances

    1. Create Compute Instances:

      • In the OCI Console, navigate to Compute > Instances.
      • Click Create Instance and configure:
        • Name: e.g., k8s-master and k8s-worker-1, k8s-worker-2, etc.
        • Shape: Choose the appropriate shape (e.g., VM.Standard.E3.Flex).
        • Image: Use the latest Ubuntu Server 22.04 LTS.
        • Networking: Place instances in the same Virtual Cloud Network (VCN) and subnet.
        • SSH Keys: Upload your public SSH key.
    2. Configure Security Lists:

      • Ensure necessary ports are open (e.g., SSH 22, Kubernetes API server 6443, application ports 80, 443).
    3. Allocate Public IPs:

      • Assign public IP addresses to access the nodes remotely.
      • Alternatively, set up a bastion host for secure access.

    Step 3: Install and Configure MicroK8s

    MicroK8s is a lightweight, production-grade Kubernetes distribution.

    1. SSH into Instances:

      • SSH into each instance using the public IP and your SSH key.
        ssh ubuntu@<instance_public_ip>
        
    2. Install MicroK8s:

      • On each node, run:
        sudo snap install microk8s --classic --channel=latest
        
    3. Add User to MicroK8s Group:

      • sudo usermod -a -G microk8s $(whoami)
        sudo chown -f -R $(whoami) ~/.kube
        newgrp microk8s
        
    4. Initialize the Cluster:

      • On the master node:

        sudo microk8s add-node
        
        • This will output a command with a token to join worker nodes.
      • On each worker node:

        • Run the join command provided by the master node.
    5. Verify Cluster Setup:

      • On the master node:
        microk8s kubectl get nodes
        

    Step 4: Install Kubernetes Dashboard (UI) with K8sLens

    1. Install Kubernetes Dashboard:

      • Enable the dashboard add-on:
        microk8s enable dashboard
        
    2. Set Up Access Credentials:

      • Create a service account and cluster role binding for admin access.
        microk8s kubectl create serviceaccount dashboard-admin-sa
        microk8s kubectl create clusterrolebinding dashboard-admin-sa --clusterrole=cluster-admin --serviceaccount=default:dashboard-admin-sa
        
      • Get the token:
        microk8s kubectl get secret $(microk8s kubectl get serviceaccount dashboard-admin-sa -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
        
    3. Access the Dashboard:

      • Use port forwarding to access the dashboard:
        microk8s kubectl port-forward -n kube-system service/kubernetes-dashboard 10443:443
        
      • Open your browser and navigate to https://localhost:10443.
      • Log in using the token you retrieved.
    4. Install K8sLens:

      • Download and install Lens Desktop, a powerful Kubernetes IDE with a user-friendly interface.
      • Connect Lens to your cluster by importing the kubeconfig file from MicroK8s.
        microk8s config > kubeconfig
        
      • In Lens, add your cluster using the kubeconfig file.

    Step 5: Configure Networking with Calico

    Calico provides networking and network policy for Kubernetes.

    1. Install Calico:

      • Enable Calico networking in MicroK8s:
        microk8s enable calico
        
    2. Verify Calico Pods:

      • microk8s kubectl get pods -n kube-system
        
      • Ensure Calico pods are running.

    Step 6: Install Traefik Ingress Controller

    Traefik will serve as the ingress controller and load balancer.

    1. Install Traefik using Helm:

      • First, install Helm:

        sudo snap install helm --classic
        
      • Add Traefik Helm repo:

        helm repo add traefik https://traefik.github.io/charts
        
      • Create a namespace for Traefik:

        microk8s kubectl create namespace traefik
        
      • Install Traefik:

        helm install traefik traefik/traefik --namespace=traefik --set kubernetes.ingressClass=traefik --set service.type=LoadBalancer
        
    2. Configure Traefik Dashboard (Optional):

      • Enable the dashboard in the values.yaml file or via --set flags.
    3. Expose Traefik to the Internet:

      • Ensure that the Traefik service is of type LoadBalancer and is assigned a public IP.

    Step 7: Secure Communication with cert-manager

    1. Install cert-manager:

      • Add cert-manager Helm repo:
        helm repo add jetstack https://charts.jetstack.io
        helm repo update
        
      • Install cert-manager:
        helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --version v1.12.0 --set installCRDs=true
        
    2. Configure Let's Encrypt Issuer:

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

    Step 8: Build and Containerize Your Application

    1. Create Multi-Stage Dockerfile:

      • This reduces the image size by compiling in one stage and copying the necessary files to a minimal runtime image.
      # Stage 1: Build
      FROM node:20-alpine AS builder
      WORKDIR /app
      COPY package.json pnpm-lock.yaml ./
      RUN npm install -g pnpm
      RUN pnpm install
      COPY . .
      RUN pnpm run build
      
      # Stage 2: Production
      FROM node:20-alpine
      WORKDIR /app
      COPY --from=builder /app/dist ./dist
      COPY --from=builder /app/node_modules ./node_modules
      COPY package.json ./
      EXPOSE 3000
      CMD ["node", "dist/index.js"]
      
    2. Build and Push Docker Image to Docker Hub:

      • Log in to Docker Hub:
        docker login
        
      • Build the image:
        docker build -t yourusername/yourapp:latest .
        
      • Push the image:
        docker push yourusername/yourapp:latest
        

    Step 9: Set Up GitHub Actions for CI/CD

    1. Create GitHub Actions Workflow:

      • In your repository, create .github/workflows/ci-cd.yml.
      name: CI/CD Pipeline
      
      on:
        push:
          branches:
            - main
      
      jobs:
        build:
          runs-on: ubuntu-latest
          steps:
            - uses: actions/checkout@v3
              with:
                fetch-depth: 0
            - name: Set up Node.js
              uses: actions/setup-node@v3
              with:
                node-version: '20'
      
            - 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 pnpm & dependencies
              run: |
                npm install -g pnpm
                pnpm install
      
            - name: Build
              run: pnpm run build
      
            - name: Docker Build and Push
              uses: docker/build-push-action@v4
              with:
                context: .
                push: true
                tags: yourusername/yourapp:latest
                secrets:
                  DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
                  DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
      
    2. Set Up GitHub Secrets:

      • Go to your repository settings and add secrets for DOCKERHUB_USERNAME and DOCKERHUB_PASSWORD.
    3. Leverage Caching:

      • The actions/cache step caches pnpm dependencies for faster builds.

    Step 10: Deploy Application to Kubernetes with Helm

    1. Create Helm Chart:

      • Initialize a new Helm chart:
        helm create myapp
        
      • Update the values.yaml with your Docker image and other configurations.
    2. Configure Ingress with Traefik and TLS:

      • In templates/ingress.yaml, configure ingress rules:

        apiVersion: networking.k8s.io/v1
        kind: Ingress
        metadata:
          name: myapp-ingress
          annotations:
            kubernetes.io/ingress.class: traefik
            cert-manager.io/cluster-issuer: letsencrypt-prod
        spec:
          tls:
          - hosts:
            - yourdomain.com
            secretName: myapp-tls
          rules:
          - host: yourdomain.com
            http:
              paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: myapp-service
                    port:
                      number: 3000
        
    3. Add Readiness and Liveness Probes:

      • In your deployment template, add:

        readinessProbe:
          httpGet:
            path: /_health
            port: 3000
          initialDelaySeconds: 5
          periodSeconds: 10
        livenessProbe:
          httpGet:
            path: /_health
            port: 3000
          initialDelaySeconds: 15
          periodSeconds: 20
        
    4. Enable Horizontal Pod Autoscaling:

      • Define an HPA resource:

        apiVersion: autoscaling/v2beta2
        kind: HorizontalPodAutoscaler
        metadata:
          name: myapp-hpa
        spec:
          scaleTargetRef:
            apiVersion: apps/v1
            kind: Deployment
            name: myapp
          minReplicas: 2
          maxReplicas: 10
          metrics:
          - type: Resource
            resource:
              name: cpu
              target:
                type: Utilization
                averageUtilization: 50
        
    5. Deploy with Helm:

      • Install or upgrade your Helm release:
        helm upgrade --install myapp ./myapp
        

    Step 11: Implement Event-Driven Autoscaling with KEDA

    1. Install KEDA:

      • Use Helm to install KEDA:
        helm repo add kedacore https://kedacore.github.io/charts
        helm repo update
        helm install keda kedacore/keda --namespace keda --create-namespace
        
    2. Configure KEDA ScaledObject:

      • Create a ScaledObject to scale based on custom metrics (e.g., queue length in RabbitMQ, Kafka, etc.).

        apiVersion: keda.sh/v1alpha1
        kind: ScaledObject
        metadata:
          name: myapp-scaledobject
          namespace: default
        spec:
          scaleTargetRef:
            name: myapp
          triggers:
          - type: cpu
            metadata:
              type: Utilization
              value: "50"
        

    Step 12: Implement Service Mesh with Kuma (Optional)

    If advanced service mesh features are required.

    1. Install Kuma:

      • Follow Kuma's installation guide:
        # Install Kuma control plane
        kumactl install control-plane | microk8s kubectl apply -f -
        
      • Verify installation:
        microk8s kubectl get pods -n kuma-system
        
    2. Inject Kuma Sidecar:

      • Annotate your namespace or deployment:

        metadata:
          annotations:
            kuma.io/sidecar-injection: enabled
        
    3. Configure Policies:

      • Define traffic policies, mTLS, etc.

    Note: Kuma adds complexity. If your application doesn't require service mesh capabilities, you can omit this step.


    Step 13: Set Up Trust Manager (Optional)

    If you need centralized trust management.

    1. Install Trust Manager:

      • Install via Helm or manifests (depending on availability).
    2. Configure Trust Manager:

      • Set up trust policies and distribute certificates.

    Note: If cert-manager suffices for your TLS needs, you may omit trust-manager.


    Step 14: Monitor Application Health

    1. Configure Health Checks:

      • Ensure your application exposes the /_health endpoint.
      • Kubernetes readiness and liveness probes are already set up.
    2. Set Up Monitoring with Prometheus and Grafana (Optional):

      • Install Prometheus and Grafana using Helm for advanced monitoring.
        helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
        helm install prometheus prometheus-community/kube-prometheus-stack
        

    Step 15: Expose Application on Static Route with Security

    1. DNS Configuration:

      • Point your domain (yourdomain.com) to the public IP of the Traefik load balancer.
    2. Verify TLS Certificate:

      • Ensure that cert-manager has obtained a TLS certificate from Let's Encrypt.
    3. Test the Application:

      • Access https://yourdomain.com from your mobile application.

    Conclusion

    You've successfully set up your backend project on Oracle Cloud Infrastructure using dedicated machines and the latest technologies. By integrating Kubernetes with tools like MicroK8s, Traefik, cert-manager, and leveraging CI/CD with GitHub Actions, your application is scalable, secure, and ready for production use.


    Notes and Recommendations:

    • Container Runtime: MicroK8s uses containerd by default, satisfying your requirement to use it instead of Docker.

    • Tool Overlaps:

      • Kuma and Service Mesh: Unless you need advanced service mesh features like traffic shifting, mTLS, and policy management, you can omit Kuma to reduce complexity.
      • Trust Manager: If cert-manager covers your certificate needs, you may not need trust-manager.
    • Ease of Use and UI:

      • Lens Desktop (K8sLens) provides a comprehensive UI for managing your Kubernetes cluster.
      • Traefik offers a dashboard for monitoring ingress routes.
      • Kubernetes Dashboard is also available for cluster management.
    • Load Balancing Strategy:

      • Traefik serves as an effective ingress controller and load balancer without relying on AWS services.
    • Security:

      • Ensure all sensitive data and secrets are securely managed using Kubernetes secrets and GitHub encrypted secrets.
      • Regularly update your systems and dependencies to the latest versions for security patches.

    By following this guide, you should have a robust, scalable, and secure backend infrastructure that leverages modern DevOps practices and cloud technologies. Remember to monitor your application and infrastructure regularly to maintain optimal performance and security.