Deploying Swift on Kubernetes: A Basic Practical Guide
Swift is not just a language for mobile applications. Thanks to frameworks like Vapor and Kitura, Swift is actively used for building server-side solutions. However, to run such an application in production, reliable orchestration is required. Kubernetes (K8s) is the ideal tool for managing containerized services. In this guide, we will walk through the complete cycle of deploying a Swift application in a Kubernetes cluster: from containerization to creating working manifests.
1. Preparing the Swift Application and Containerization
Before talking about Kubernetes, you need to package your Swift application into a Docker image. Let's look at an example based on the Vapor framework (but the approach is universal for any Swift server).
Create a minimal Vapor application:
// Sources/App/routes.swiftimport Vapor
func routes(_ app: Application) throws { app.get { req async in "Hello, Kubernetes!" }}Now let's write a Dockerfile. Important: use the official Swift image for building, and a lightweight image (e.g., ubuntu:jammy) for the final container. This will reduce the size and increase security.
# ---- Build Stage ----FROM swift:5.9-jammy AS build
WORKDIR /appCOPY Package.swift Package.resolved ./COPY Sources ./SourcesRUN swift build -c release --static-swift-stdlib
# ---- Run Stage ----FROM ubuntu:jammy
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*WORKDIR /appCOPY --from=build /app/.build/release/Run .
EXPOSE 8080CMD ["./Run"]Build the image and push it to a registry (e.g., Docker Hub):
docker build -t yourdockerhub/swift-k8s-demo:latest .docker push yourdockerhub/swift-k8s-demo:latest2. Kubernetes Basics: Pod and Deployment
Kubernetes manages containers through objects. The simplest is a Pod, but for production it's better to use a Deployment, which provides replication and updates.
Create a file deployment.yaml:
apiVersion: apps/v1kind: Deploymentmetadata: name: swift-demo labels: app: swift-demospec: replicas: 2 selector: matchLabels: app: swift-demo template: metadata: labels: app: swift-demo spec: containers: - name: swift-app image: yourdockerhub/swift-k8s-demo:latest ports: - containerPort: 8080 resources: requests: memory: "128Mi" cpu: "250m" limits: memory: "256Mi" cpu: "500m" livenessProbe: httpGet: path: / port: 8080 initialDelaySeconds: 5 periodSeconds: 10Apply the manifest:
kubectl apply -f deployment.yamlCheck the status:
kubectl get pods -l app=swift-demo3. Service for Accessing the Application
Pods are dynamic (their IPs change upon restart). To allow clients to access the application stably, a Service is needed. Create service.yaml:
apiVersion: v1kind: Servicemetadata: name: swift-servicespec: selector: app: swift-demo ports: - protocol: TCP port: 80 targetPort: 8080 type: ClusterIPFor testing in a local cluster (e.g., Minikube), you can use NodePort or port forwarding:
kubectl port-forward service/swift-service 8080:80Now the application is available at http://localhost:8080.
4. Scaling and Rolling Updates
One of the main advantages of Kubernetes is easy sc