Building a Microservices Architecture with Kubernetes: A Complete Example

Introduction

Modern applications demand scalability, flexibility, and resilience. Microservices architecture allows teams to break down monolithic applications into smaller, independent services that can be deployed, scaled, and managed separately.

In this blog, we’ll build a complete microservices-based application on Kubernetes, covering:

  • Defining multiple microservices
  • Exposing them via Kubernetes Services
  • Managing inter-service communication
  • Deploying and scaling them efficiently

Step 1: Define Our Microservices

For this example, we’ll create two services:

  • Product Service: Handles product details.
  • Order Service: Manages order placements and communicates with the Product service.

Deployment for Product Service

apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-service
  labels:
    app: product-service
spec:
  replicas: 2
  selector:
    matchLabels:
      app: product-service
  template:
    metadata:
      labels:
        app: product-service
    spec:
      containers:
      - name: product-service
        image: nginx:latest
        ports:
        - containerPort: 80
Click Here to Copy YAML

Service for Product Service

apiVersion: v1
kind: Service
metadata:
  name: product-service
spec:
  selector:
    app: product-service
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: ClusterIP
Click Here to Copy YAML

Deployment for Order Service

apiVersion: apps/v1
kind: Deployment
metadata:
  name: order-service
  labels:
    app: order-service
spec:
  replicas: 2
  selector:
    matchLabels:
      app: order-service
  template:
    metadata:
      labels:
        app: order-service
    spec:
      containers:
      - name: order-service
        image: httpd:latest
        env:
        - name: PRODUCT_SERVICE_URL
          value: "http://product-service"
        ports:
        - containerPort: 80
Click Here to Copy YAML

Service for Order Service

apiVersion: v1
kind: Service
metadata:
  name: order-service
spec:
  selector:
    app: order-service
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: ClusterIP
Click Here to Copy YAML

Step 2: Deploy and Verify

Apply all YAML files:

kubectl apply -f product-service.yaml
kubectl apply -f order-service.yaml

Check if pods are running:

kubectl get pods

Verify services:

kubectl get svc

Step 3: Expose Services to External Users

To make the services accessible externally, use an Ingress resource.

Ingress Configuration

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: microservices-ingress
spec:
  rules:
  - host: myapp.local
    http:
      paths:
      - path: /products
        pathType: Prefix
        backend:
          service:
            name: product-service
            port:
              number: 80
      - path: /orders
        pathType: Prefix
        backend:
          service:
            name: order-service
            port:
              number: 80
Click Here to Copy YAML

Apply it:

kubectl apply -f ingress.yaml

Step 4: Scaling Microservices

Need to scale services? Just increase the replica count!

kubectl scale deployment product-service --replicas=5
kubectl scale deployment order-service --replicas=5

Verify scaling:

kubectl get deployments

Step 5: Observability and Logging

To monitor microservices performance, use Prometheus and Grafana for metrics and ELK Stack for centralized logging.

Example: Enable logs for a pod

kubectl logs -f <pod-name>

Conclusion

Microservices architecture, combined with Kubernetes, enables scalable, resilient, and manageable applications. By breaking monoliths into independent services, we:
✅ Improve scalability and fault tolerance
✅ Enable faster deployments and updates
✅ Simplify inter-service communication with Kubernetes Services

Start deploying microservices today and scale your applications like a pro! 

What challenges have you faced while working with microservices on Kubernetes? Let’s discuss in the comments!👇

Leave a comment