Local Cloud-Native Practices for Rust Applications: Colima + K8s + Multi-Architecture CI/CD Deployment Process

Local Cloud-Native Practices for Rust Applications: Colima + K8s + Multi-Architecture CI/CD Deployment Process

The Colima environment naturally supports integration with Kubernetes, allowing for the simulation of a production-grade K8s cluster on local machines. Utilizing the CI capabilities of GitHub Actions, kubectl can easily pull images from GHCR or Docker Hub and run them locally.

Docker

Container runtime, building images, running containers, managing container lifecycle

Kubernetes

Container orchestration platform for managing, scheduling, and scaling applications composed of multiple containers. It handles container scheduling in the cluster, service discovery, load balancing, auto-scaling, and rolling updates.

Colima

Colima (Container on Lima) is a lightweight container runtime management tool that runs containers on macOS through a Linux virtual machine (VM).

1. Stop the current Colima instance and clean up data

# 1. Stop the current Colima instance
colima stop

# 2. Delete old Docker-only configuration
colima delete

colima delete: This will clear all locally built and pulled Docker images, container data, and all packages and configurations installed within the Colima virtual machine.

2. Install kubectl

If kubectl is not installed, running Colima directly will result in an error.

FATA[0000] dependency check failed for kubernetes: kubectl not found, run 'brew install kubectl' to install

Installation

brew install kubectl

3. Start the Colima instance to enable Kubernetes features

# Start Colima and enable Kubernetes
# Add the --kubernetes flag
colima start --kubernetes --vm-type vz --mount-type virtiofs --vz-rosetta -c 4 -m 4

–kubernetes: Core parameter to enable the built-in K3s cluster. Startup logs

INFO[0001] starting colima
INFO[0001] runtime: docker+k3s
INFO[0001] creating and starting ...                     context=vm
INFO[0015] provisioning ...                              context=docker
INFO[0017] starting ...                                  context=docker
INFO[0017] provisioning ...                              context=kubernetes
INFO[0018] downloading and installing ...                context=kubernetes
INFO[0054] loading oci images ...                        context=kubernetes
INFO[0058] starting ...                                  context=kubernetes
INFO[0064] updating config ...                           context=kubernetes
INFO[0065] Switched to context "colima".                 context=kubernetes
INFO[0066] done

runtime: docker+k3s: It can be seen that after Colima starts, it uses K3s (a lightweight K8s distribution) as the Kubernetes cluster.Kubeconfig: Colima will automatically place the K8s configuration (kubeconfig file) in the ~/.kube/config file.

ll ~/.kube/config

4. Verify the status of the Kubernetes cluster

4.1 Verify node status

kubectl get nodes # Displays a Ready node

The output is as follows

NAME     STATUS   ROLES                  AGE     VERSION
colima   Ready    control-plane,master   9m27s   v1.33.3+k3s1

4.1 Verify the status of Pods in the kube-system namespace (K8s core components)

kubectl get pods -n kube-system

The output is as follows

NAME                                      READY   STATUS    RESTARTS   AGE
coredns-5688667fd4-f8gpv                  1/1     Running   0          10m
local-path-provisioner-774c6665dc-8m8gr   1/1     Running   0          10m
metrics-server-6f4c6675d5-b4v4b           1/1     Running   0          10m

kube-system: Namespace where system components reside.

5. Deploy Rust container to K8s

Two YAML files need to be created to describe how the application runs in K8s: Deployment and Service.

Create a dedicated deployment directory named k8s in the project root directory to store Kubernetes-related deployment files, keeping the project structure clear and tidy.

5.1 Deployment (describes how to run Pods)

# servicekit-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: servicekit-deployment
  labels:
    app: servicekit
spec:
  replicas: 2 # Run 2 Pod replicas
  selector:
    matchLabels:
      app: servicekit
  template:
    metadata:
      labels:
        app: servicekit
    spec:
      # Instruct Kubernetes to use the ghcr-auth Secret to pull the image
      imagePullSecrets:
        - name: ghcr-auth
      containers:
        - name: servicekit
          # Use the image pushed to Docker Hub or GHCR
          image: ghcr.io/bobenhome/servicekit-rs:latest
          # Container port
          ports:
            - containerPort: 8084
          # Resource limits (ensure Pod does not exhaust Colima's resources)
          resources:
            limits:
              memory: "512Mi"
              cpu: "500m"
          # Solve timezone issues by configuring environment variables
          env:
            - name: TZ
              value: Asia/Shanghai

Pulling the image from GHCR requires configuring a PAT on GitHub and setting the secret in kubectl.

kubectl create secret docker-registry ghcr-auth \
  --docker-server=ghcr.io \
  --docker-username=<Your GitHub Username> \
  --docker-password=<Your Generated PAT> \
  --docker-email=<Your GitHub Email>

Configure the secret just generated in the servicekit-deployment.yaml file.

imagePullSecrets:
        - name: ghcr-auth

5.2 Service (describes how to expose Deployment)

# servicekit-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: servicekit-service
spec:
  # LoadBalancer will be mapped to the host port in Colima K3s
  type: LoadBalancer
  selector:
    app: servicekit # Matches the labels of Deployment
  ports:
    - protocol: TCP
      port: 8084      # Port exposed by Service
      targetPort: 8084 # Internal port of the container

5.3 Execute the following command in the project root directory to deploy

kubectl apply -f k8s/

kubectl will automatically read all .yaml files in this directory and apply them to your Colima Kubernetes cluster.

5.4 Verify Pods status (check if servicekit Pods are Running)

kubectl get pods -l app=servicekit

The output is as follows

NAME                                     READY   STATUS    RESTARTS   AGE
servicekit-deployment-77469bdb9b-fqsl2   1/1     Running   0          3s
servicekit-deployment-77469bdb9b-xs4nz   1/1     Running   0          3s

5.5 Verify Service status

kubectl get service servicekit-service

The output is as follows

NAME                 TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
servicekit-service   LoadBalancer   10.43.140.144   192.168.5.1   8084:31563/TCP   23s

Access via localhost: http://localhost:8084EXTERNAL-IP: The IP address exposed to the host, can also be accessed via http://192.168.5.1:8084

5.6 Delete Deployment and Service

Delete Deployment

kubectl delete deployment servicekit-deployment

Delete Service

kubectl delete service servicekit-service

5.7 Monitoring Checks

# 1. First check node status
kubectl get nodes
# 2. Check system components
kubectl get pods -n kube-system
# 3. Check business applications
kubectl get pods -n default
#4. In-depth troubleshooting
kubectl describe pod <Pod Name>
kubectl logs -f <Pod Name>

Conclusion

Using Colima to set up a high-performance Kubernetes environment on a local Mac, and implementing automated multi-architecture Docker image builds through GitHub Actions, ultimately allows for the transparent and stable deployment of Rust applications to the local K8s cluster, completing the entire CI/CD loop from code to cluster.

Leave a Comment