> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Securing Internal Microservices

> Layered strategies to secure an internal Kubernetes payments microservice using ClusterIP, NetworkPolicy, mutual TLS and fine grained authorization.

This guide walks through a common interview-style scenario and a practical, layered strategy to secure an internal-only microservice.

Scenario

* You run a payments service that should only be called by trusted internal services (for example, the order service).
* Public users or anything on the internet must never reach the payments service directly.
* When a user places an order, the order service calls the payments service to charge the card. That is the only legitimate caller.

Goal

* Ensure the payments service has no public exposure.
* Prevent lateral attacks inside the cluster.
* Enforce cryptographic identity for services.
* Apply fine-grained authorization so only intended callers reach specific endpoints.

Defense-in-depth: each layer reduces risk and compensates for gaps in others.

1. Network exposure: remove any public address

* First, ensure the payments service does not receive a public IP or direct internet exposure.
* In Kubernetes, use a `ClusterIP` Service (the default) instead of `LoadBalancer` or `NodePort` so the service is not reachable from outside the cluster.

Example Service manifest (explicit `ClusterIP`):

```yaml theme={null}
apiVersion: v1
kind: Service
metadata:
  name: payments
  labels:
    app: payments
spec:
  type: ClusterIP
  selector:
    app: payments
  ports:
    - port: 443
      targetPort: 8443
      protocol: TCP
```

This prevents external traffic from directly reaching the payments pods. However, most cluster overlay networks are "flat by default": any pod can attempt to connect to any other pod unless additional controls are applied.

![A diagram titled "FLAT BY DEFAULT" showing a Kubernetes cluster with pods/services labeled Marketing Web, Order Service, and Payments Service. A red arrow indicates an attacker entering via the Marketing Web pod and moving laterally toward the Order Service.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Kubernetes/Securing-Internal-Microservices/marketing-web-to-order-lateral-attack.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=b93691a1c1b49cb8a4e38fe7e2a07761)

2. NetworkPolicy: pod-level firewall to restrict which pods may connect

* Kubernetes NetworkPolicy is an allow-list model. When you attach an ingress policy to the payments pods, only explicitly allowed sources can reach them.
* NetworkPolicy blocks unwanted pod-to-pod connections at the network layer, reducing the blast radius if a frontend pod is compromised.

Example NetworkPolicy that permits only pods labeled `app=order` to talk to `payments` on port `8443`:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-order-to-payments
  namespace: default
spec:
  podSelector:
    matchLabels:
      app: payments
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: order
      ports:
        - protocol: TCP
          port: 8443
```

With this policy, a compromised Marketing pod will have its traffic dropped at the cluster network layer—provided your CNI plugin enforces NetworkPolicy.

![A dark-themed diagram showing a Kubernetes cluster with pods labeled "Marketing Web", "Order Service", and "Payments Service" and a network policy (netpol) icon. The title reads "Layer 2 · Network Policy" and a rule box on the right shows "ALLOW label = order-service."](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Kubernetes/Securing-Internal-Microservices/kubernetes-network-policy-order-service-allow.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=97359eaa1e6a2c67588ddf8c3c68c796)

NetworkPolicy limitations

* NetworkPolicy enforces connectivity based on selectors that resolve to pod IPs; IPs are not strong cryptographic identities.
* A network-level allow-list does not verify the caller at the application level (who is making the request and whether they are authorized to call a specific API).

![A dark-themed diagram of a Kubernetes cluster showing pods (Marketing Web, Order Service, Payments Service) and a network policy (netpol) icon. The graphic emphasizes “IP is not identity,” illustrating an example IP (10.4.2.7) that can belong to different pods over time.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Kubernetes/Securing-Internal-Microservices/k8s-pods-ip-not-identity-netpol.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=6dc6291c1eb7a03c4f78bfa780e7c0d5)

3. Mutual TLS (mTLS): cryptographic service identity and transport security

* mTLS ensures both client and server present and verify certificates. Every connection is authenticated and encrypted.
* Use short-lived certificates issued to each service instance. With mTLS, attackers inside the cluster without valid certs cannot impersonate a legitimate service.

In practice, many teams use a service mesh to implement mTLS (examples: Istio, Linkerd). The mesh injects a sidecar proxy next to each pod. The sidecar handles certificate issuance, rotation, verification, and encryption so application code remains unchanged.

![A stylized diagram titled "Layer 3 · Mutual TLS" showing a Kubernetes cluster with an Order Service (caller) and Payments Service (receiver) exchanging secured traffic. The graphic also depicts TLS certificates/locks and an attacker trying to intercept or spoof messages.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Kubernetes/Securing-Internal-Microservices/mutual-tls-k8s-order-payments-diagram.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=856417c6995087a6d503e01fdd9502d5)

Who issues certificates?

* The mesh control plane typically operates as a certificate authority (CA) or integrates with an internal CA. It issues short-lived identities (SPIFFE/SPIRE or an equivalent) to proxies and enforces mTLS automatically for service-to-service traffic.
* Because certs rotate frequently and every connection validates identity, attackers cannot trivially replay or forge identities.

![A stylized architecture diagram titled "Layer 3 · Service Mesh" showing a Kubernetes cluster with microservices (Order, Payments, Marketing Web) each paired with proxy sidecars, a trusted authority, network policies, and icons for user/internet; a note reads "App code unchanged."](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Kubernetes/Securing-Internal-Microservices/layer-3-service-mesh-k8s-sidecars.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=b276e5015d1ac965be0f149b74f5cae1)

4. Authorization: fine-grained access control at the application layer

* Even with mTLS proving a caller's identity, you should limit which service identities can call which API endpoints.
* Implement authorization policies in the mesh (or in the application) so the proxy checks identity and intended operation on each request.

Example Istio AuthorizationPolicy that allows only the `order` service account to POST to the `/charge` endpoint:

```yaml theme={null}
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: payments-policy
  namespace: default
spec:
  selector:
    matchLabels:
      app: payments
  action: ALLOW
  rules:
    - from:
        - source:
            principals:
              - "spiffe://cluster.local/ns/default/sa/order-sa"
      to:
        - operation:
            methods: ["POST"]
            paths: ["/charge"]
```

With this policy, the payments proxy enforces:

* Who is calling (mTLS principal).
* What operation they are allowed to perform (HTTP method + path).

If a request is from any other principal or targets an unauthorized endpoint (for example `/refund`), the proxy blocks it.

![A stylized architecture diagram titled "Layer 4 · Authorization" showing a Kubernetes cluster with service pods (Marketing Web, Order Service, Payments Service, Refund, Support) and proxy icons. Arrows and badges indicate allowed and blocked connections, with a callout that explicitly allows "order-service → payments."](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Kubernetes/Securing-Internal-Microservices/layer4-authorization-k8s-order-payments-allow.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=ee0379384dbd28259ace15660826b180)

Checklist: two questions every request should answer

* Is the caller who it claims to be? (mTLS identity)
* Is that caller allowed to invoke this API/endpoint? (authorization policy)

Layered summary table

| Layer | Purpose | Example configuration |
| - | - | - |
| Network exposure | Prevent public access to service | `Service` type: `ClusterIP` |
| Pod-level network control | Block unauthorized pod-to-pod connections | `NetworkPolicy` allow-list (ingress from `app=order`) |
| Cryptographic identity & transport | Authenticate services and encrypt traffic | mTLS via service mesh (Istio, Linkerd) |
| Authorization | Fine-grained API access control | Istio `AuthorizationPolicy` (principals, methods, paths) |

References

* [Kubernetes Services](https://kubernetes.io/docs/concepts/services-networking/service/)
* [Kubernetes NetworkPolicy](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
* [Istio Security (mTLS & Authorization)](https://istio.io/latest/docs/concepts/security/)

Summary: Use a ClusterIP (no public endpoint), restrict ingress with Kubernetes NetworkPolicy, enforce cryptographic identities using mutual TLS (typically via a service mesh), and apply fine-grained authorization policies so only the intended service accounts can call specific endpoints.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/60905e58-3a8e-4423-9743-081b4959f0a0/lesson/91187ac4-256e-4aa9-96d8-e32738867bd9)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.