DEV Community

Cover image for 3.Deploy Nginx Web Server on Kubernetes Cluster
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

3.Deploy Nginx Web Server on Kubernetes Cluster

Lab Information

Some of the Nautilus team developers are developing a static website and they want to deploy it on Kubernetes cluster. They want it to be highly available and scalable. Therefore, based on the requirements, the DevOps team has decided to create a deployment for it with multiple replicas. Below you can find more details about it:

Create a deployment using nginx image with latest tag only and remember to mention the tag i.e nginx:latest. Name it as nginx-deployment. The container should be named as nginx-container, also make sure replica counts are 3.

Create a NodePort type service named nginx-service. The nodePort should be 30011.
Enter fullscreen mode Exit fullscreen mode

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.

Lab Solutions

Step 1: Create the Deployment YAML configuration

Create a file named nginx-deployment.yaml:

cat > nginx-deployment.yaml << EOF
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-container
        image: nginx:latest
        ports:
        - containerPort: 80
EOF
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Deployment

Apply the deployment configuration:

kubectl apply -f nginx-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the Deployment

Check if the deployment is created and all replicas are running:

kubectl get deployment nginx-deployment
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify the Pods

Check the pods created by the deployment:

kubectl get pods -l app=nginx
Enter fullscreen mode Exit fullscreen mode

Step 5: Create the Service YAML configuration

Create a file named nginx-service.yaml:

cat > nginx-service.yaml << EOF
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30011
EOF
Enter fullscreen mode Exit fullscreen mode

Step 6: Create the Service

Apply the service configuration:

kubectl apply -f nginx-service.yaml
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify the Service

Check if the service is created correctly:

kubectl get service nginx-service
Enter fullscreen mode Exit fullscreen mode

Step 8: Detailed Service Verification

Get more details about the service:

kubectl describe service nginx-service
Enter fullscreen mode Exit fullscreen mode

Step 9: Test the Setup

Test if the service is working by accessing it. Since this is a Kubernetes cluster, you can test using:

Using curl from inside the cluster

Get the Cluster IP of the service

CLUSTER_IP=$(kubectl get service nginx-service -o jsonpath='{.spec.clusterIP}')
Enter fullscreen mode Exit fullscreen mode

Test access using Cluster IP

kubectl run test-pod --image=nginx:latest --rm -it --restart=Never -- bash -c "curl http://$CLUSTER_IP"
Enter fullscreen mode Exit fullscreen mode

Step 10: Verify Endpoints

Check that the service is correctly targeting all 3 pods:

kubectl get endpoints nginx-service
Enter fullscreen mode Exit fullscreen mode


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)