DEV Community

Nishant Raj
Nishant Raj

Posted on

Exposing and Managing Applications in Kubernetes: A Step-by-Step Guide

In this blog, we will explore how to expose applications using Kubernetes Services and manage Deployments for scaling and accessibility. We'll create Services of type NodePort and LoadBalancer, test the application’s accessibility, and discuss Service types and their use cases.


Task 1: Create a Service of Type NodePort

Objective:

Create a NodePort Service named myapp-service to expose your application on port 80, accessible externally via a Node's IP and NodePort.

Solution

Step 1: Define the Service YAML

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: NodePort
  selector:
    app: myapp
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080
Enter fullscreen mode Exit fullscreen mode

Step 2: Apply the YAML

kubectl apply -f nodeport-service.yml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the Service

kubectl get svc myapp-service
Enter fullscreen mode Exit fullscreen mode

Expected output:

NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
myapp-service    NodePort    10.96.100.123    <none>        80:30080/TCP     5s
Enter fullscreen mode Exit fullscreen mode

Step 4: Access the Service

Use a Node's IP and the NodePort to test access:

wget http://<Node-IP>:30080
Enter fullscreen mode Exit fullscreen mode

Task 2: Create a Deployment

Objective:

Deploy an application using the nginx:1.23.4-alpine image, expose port 80, and ensure 1 replica is running.

Solution

Step 1: Define the Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: nginx
        image: nginx:1.23.4-alpine
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Step 2: Apply the YAML

kubectl apply -f deployment.yml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the Deployment

kubectl get deploy myapp
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Task 3: Scale the Deployment

Objective:

Scale the Deployment to run 2 replicas.

Solution

Step 1: Scale the Deployment

kubectl scale deploy myapp --replicas=2
Enter fullscreen mode Exit fullscreen mode

Step 2: Verify the Scale

kubectl get deploy myapp
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Task 4: Test the Service Internally (Not necessary but anyway..)

Objective:

Create a temporary Pod using the busybox image and test the internal accessibility of the Service.

Solution

Step 1: Run a Temporary Pod

kubectl run busybox --rm -it --image=busybox -- /bin/sh
Enter fullscreen mode Exit fullscreen mode

Step 2: Test the Service

Inside the busybox shell:

wget -O- http://myapp-service
exit
Enter fullscreen mode Exit fullscreen mode

Task 5: Test the Service Externally

Objective:

Verify that the Service is accessible outside the cluster.

Solution

From a host outside the cluster, use a Node's IP and the NodePort to test access:

wget http://<Node-IP>:30080
Enter fullscreen mode Exit fullscreen mode

Task 6: Expose the Service via LoadBalancer

Objective:

Modify the Service to use the LoadBalancer type, making it accessible with an external IP.

Solution

Step 1: Update the Service Type

kubectl edit svc myapp-service
Enter fullscreen mode Exit fullscreen mode

Change the type field to LoadBalancer:

type: LoadBalancer
Enter fullscreen mode Exit fullscreen mode

Step 2: Verify the External IP

kubectl get svc myapp-service
Enter fullscreen mode Exit fullscreen mode

Check the EXTERNAL-IP field for the assigned IP address.

Step 3: Test the Service

wget http://<EXTERNAL-IP>
Enter fullscreen mode Exit fullscreen mode

Task 7: Can Pods Be Exposed Without a Deployment?

Discussion:

Yes, Pods can be exposed without a Deployment by creating a Service with a label selector matching the Pods. However, this approach lacks features like:

  • Automatic restart of failed Pods.
  • Scaling capabilities.
  • Rolling updates or rollbacks.

Best Practice: Use Deployments for most scenarios to leverage Kubernetes’ scaling and fault-tolerance features.


Task 8: When to Use Different Service Types

ClusterIP (Default)

  • Purpose: Internal-only communication between services.
  • Use Case: Backend services accessed by frontend Pods.

NodePort

  • Purpose: Expose a Service on each Node's IP and a specific port.
  • Use Case: Testing applications locally or in environments without a LoadBalancer.

LoadBalancer

  • Purpose: Provide external access using a cloud provider’s load balancer.
  • Use Case: Production environments hosted on cloud platforms.

ExternalName

  • Purpose: Map a Service to an external DNS name.
  • Use Case: Redirect traffic to legacy systems or external APIs.

Conclusion

In this guide, we explored:

  1. Creating and using NodePort and LoadBalancer Services.
  2. Managing Deployments for scaling and reliability.
  3. Testing Service accessibility internally and externally.
  4. Understanding Service types and their appropriate use cases.

These practices are essential for building and exposing scalable, accessible Kubernetes applications. Stay tuned for more Kubernetes insights! 🚀

Top comments (0)