Deploying an NGINX Server in Kubernetes Using GitHub Codespaces: An 8-Step Guide
Deploying applications in a Kubernetes environment can be streamlined with GitHub Codespaces. In this guide, I will walk you through how to deploy an NGINX server in a Kubernetes cluster using Codespaces. By following these steps, you’ll set up the environment, deploy NGINX, and optionally automate the process using GitHub Actions.
Step 1: Set Up GitHub Codespaces Environment
First, create a repository on GitHub to host your Kubernetes manifests and NGINX configuration files. After setting up the repository, you can enable GitHub Codespaces and open a new Codespace instance directly from your repository.
Once inside Codespaces, install essential tools like kubectl, docker, helm, and a Kubernetes tool like k3d or kind to create a local Kubernetes cluster.
Install kubectl and k3d or kind
sudo apt-get update
sudo apt-get install -y kubectl
curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
Step 2: Set Up Local Kubernetes Cluster in Codespaces
Next, use k3d or kind to run a local Kubernetes cluster. In this example, we will use k3d, a lightweight wrapper to run Kubernetes clusters in Docker.
Create a local Kubernetes cluster using k3d
k3d cluster create my-cluster --agents 2 --port 8080:80@loadbalancer
This command creates a cluster with two worker nodes (--agents 2) and exposes port 8080 for NGINX access.
Step 3: Write NGINX Deployment Manifest
Now, create the NGINX deployment manifest. This YAML file defines the configuration for deploying 3 replicas of NGINX pods in your Kubernetes cluster.
nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Save this manifest as nginx-deployment.yaml in your repository.
Step 4: Create NGINX Service YAML
To expose the NGINX pods externally, create a Service that will forward traffic to the NGINX pods. Here’s the YAML configuration for a LoadBalancer service.
nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Save this manifest as nginx-service.yaml.
Step 5: Apply the Manifests to the Cluster
With the manifests ready, apply them to the Kubernetes cluster using kubectl.
Apply deployment and service manifests
kubectl apply -f nginx-deployment.yaml
kubectl apply -f nginx-service.yaml
This will start the NGINX pods and expose the service.
Step 6: Verify NGINX Deployment and Service
To verify that the NGINX pods and services are running correctly, you can check their status using the following commands:
Verify pods are running
kubectl get pods
Verify service is created
kubectl get service nginx-service
If everything is running correctly, you should see your NGINX pods in the Running state, and the service should be available with an external IP or localhost, depending on your cluster setup.
Step 7: Access NGINX Application
You can now access your NGINX application through the load balancer. If you are using k3d, the service will be available at localhost:8080.
curl http://localhost:8080
This should return the default NGINX welcome page.
Step 8: Set Up CI/CD with GitHub Actions (Optional)
To automate the deployment process, you can set up a GitHub Actions workflow. This workflow will deploy the NGINX server whenever changes are pushed to your repository. Create a file called .github/workflows/deploy.yml in your repository.
deploy.yml
name: Deploy to k3d
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up k3d
run: curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
- name: Deploy to k3d
run: |
k3d cluster create my-cluster
kubectl apply -f nginx-deployment.yaml
kubectl apply -f nginx-service.yaml
With this workflow, every push to the main branch will trigger a deployment in the local Kubernetes cluster.
Conclusion
By following these eight steps, you can deploy an NGINX server in a Kubernetes environment using GitHub Codespaces. This guide also includes an optional step to automate the deployment process with GitHub Actions, making it easy to manage and update your application. This approach simplifies the development workflow, particularly for developers looking to leverage cloud-based development environments like Codespaces.
Imported from rifaterdemsahin.com · 2025