Deploying Astro sites using Kubernetes offers developers a scalable and efficient way to manage modern web applications. This tutorial provides a step-by-step guide to help you set up, configure, and deploy your Astro site in a Kubernetes environment.

Prerequisites

  • Basic knowledge of Astro framework
  • Experience with Kubernetes and Docker
  • Access to a Kubernetes cluster (local or cloud-based)
  • kubectl command-line tool installed
  • Docker installed on your development machine

Step 1: Build Your Astro Site

Start by creating your Astro project or use an existing one. Build the production version of your site with the following command:

npm run build

This generates a static site in the dist directory.

Step 2: Create a Dockerfile

Create a Dockerfile in the root of your project to containerize your Astro site. Example Dockerfile:

FROM nginx:alpine
COPY dist /usr/share/nginx/html
EXPOSE 80

Step 3: Build and Push Docker Image

Build your Docker image and push it to a container registry like Docker Hub:

docker build -t yourusername/astro-site:latest .
docker push yourusername/astro-site:latest

Step 4: Create Kubernetes Deployment

Define a deployment YAML file to deploy your containerized Astro site:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: astro-site-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: astro-site
  template:
    metadata:
      labels:
        app: astro-site
    spec:
      containers:
      - name: astro-site
        image: yourusername/astro-site:latest
        ports:
        - containerPort: 80

Step 5: Expose the Deployment

Create a service YAML to expose your Astro site:

apiVersion: v1
kind: Service
metadata:
  name: astro-site-service
spec:
  type: LoadBalancer
  selector:
    app: astro-site
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

Step 6: Apply Configurations

Deploy your application with kubectl:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

Step 7: Access Your Astro Site

Retrieve the external IP address of your service:

kubectl get services astro-site-service

Open the IP address in your browser to see your deployed Astro site.

Conclusion

Deploying Astro sites with Kubernetes streamlines the process of managing static sites at scale. By containerizing your site and leveraging Kubernetes deployments and services, you ensure high availability and easy scalability for your web applications.