I need to know how to host my backend project on oracle cloud infrastructure using…
- 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
- 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
-
Create an OCI Account:
- Visit Oracle Cloud and sign up for an account.
- Choose the appropriate plan that provides dedicated compute resources.
-
Set Up Compartment:
- In the OCI Console, create a compartment to organize your resources.
-
Generate SSH Keys:
- Generate an SSH key pair on your local machine to securely access your instances.
ssh-keygen -t rsa -b 4096
- Generate an SSH key pair on your local machine to securely access your instances.
Step 2: Provision Dedicated Compute Instances
-
Create Compute Instances:
- In the OCI Console, navigate to Compute > Instances.
- Click Create Instance and configure:
- Name: e.g.,
k8s-masterandk8s-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.
- Name: e.g.,
-
Configure Security Lists:
- Ensure necessary ports are open (e.g., SSH
22, Kubernetes API server6443, application ports80,443).
- Ensure necessary ports are open (e.g., SSH
-
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.
-
SSH into Instances:
- SSH into each instance using the public IP and your SSH key.
ssh ubuntu@<instance_public_ip>
- SSH into each instance using the public IP and your SSH key.
-
Install MicroK8s:
- On each node, run:
sudo snap install microk8s --classic --channel=latest
- On each node, run:
-
Add User to MicroK8s Group:
-
sudo usermod -a -G microk8s $(whoami) sudo chown -f -R $(whoami) ~/.kube newgrp microk8s
-
-
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.
-
-
Verify Cluster Setup:
- On the master node:
microk8s kubectl get nodes
- On the master node:
Step 4: Install Kubernetes Dashboard (UI) with K8sLens
-
Install Kubernetes Dashboard:
- Enable the dashboard add-on:
microk8s enable dashboard
- Enable the dashboard add-on:
-
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}}"
- Create a service account and cluster role binding for admin access.
-
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.
- Use port forwarding to access the dashboard:
-
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
kubeconfigfile.
Step 5: Configure Networking with Calico
Calico provides networking and network policy for Kubernetes.
-
Install Calico:
- Enable Calico networking in MicroK8s:
microk8s enable calico
- Enable Calico networking in MicroK8s:
-
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.
-
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
-
-
Configure Traefik Dashboard (Optional):
- Enable the dashboard in the
values.yamlfile or via--setflags.
- Enable the dashboard in the
-
Expose Traefik to the Internet:
- Ensure that the Traefik service is of type
LoadBalancerand is assigned a public IP.
- Ensure that the Traefik service is of type
Step 7: Secure Communication with cert-manager
-
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
- Add cert-manager Helm repo:
-
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
- Create a ClusterIssuer for Let's Encrypt:
Step 8: Build and Containerize Your Application
-
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"] -
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
- Log in to Docker Hub:
Step 9: Set Up GitHub Actions for CI/CD
-
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 }} - In your repository, create
-
Set Up GitHub Secrets:
- Go to your repository settings and add secrets for
DOCKERHUB_USERNAMEandDOCKERHUB_PASSWORD.
- Go to your repository settings and add secrets for
-
Leverage Caching:
- The
actions/cachestep cachespnpmdependencies for faster builds.
- The
Step 10: Deploy Application to Kubernetes with Helm
-
Create Helm Chart:
- Initialize a new Helm chart:
helm create myapp - Update the
values.yamlwith your Docker image and other configurations.
- Initialize a new Helm chart:
-
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
-
-
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
-
-
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
-
-
Deploy with Helm:
- Install or upgrade your Helm release:
helm upgrade --install myapp ./myapp
- Install or upgrade your Helm release:
Step 11: Implement Event-Driven Autoscaling with KEDA
-
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
- Use Helm to install KEDA:
-
Configure KEDA ScaledObject:
-
Create a
ScaledObjectto 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.
-
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
- Follow Kuma's installation guide:
-
Inject Kuma Sidecar:
-
Annotate your namespace or deployment:
metadata: annotations: kuma.io/sidecar-injection: enabled
-
-
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.
-
Install Trust Manager:
- Install via Helm or manifests (depending on availability).
-
Configure Trust Manager:
- Set up trust policies and distribute certificates.
Note: If
cert-managersuffices for your TLS needs, you may omittrust-manager.
Step 14: Monitor Application Health
-
Configure Health Checks:
- Ensure your application exposes the
/_healthendpoint. - Kubernetes readiness and liveness probes are already set up.
- Ensure your application exposes the
-
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
- Install Prometheus and Grafana using Helm for advanced monitoring.
Step 15: Expose Application on Static Route with Security
-
DNS Configuration:
- Point your domain (
yourdomain.com) to the public IP of the Traefik load balancer.
- Point your domain (
-
Verify TLS Certificate:
- Ensure that cert-manager has obtained a TLS certificate from Let's Encrypt.
-
Test the Application:
- Access
https://yourdomain.comfrom your mobile application.
- Access
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
containerdby 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-managercovers your certificate needs, you may not needtrust-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.