Kubernetes Basics

Why Kubernetes?

Docker Compose runs containers on one machine. In production ShopEase runs 30+ containers on many machines, and someone must restart crashed containers, spread them across machines, scale them, and roll out new versions without downtime. Kubernetes (K8s) does this: you describe the desired state in YAML, and Kubernetes keeps the cluster in that state.

kubectl apply -f order-service.yaml
                                  |
                       +----------v-----------+
                       |    Control plane     |   API server, scheduler,
                       |  (desired state)     |   controllers, etcd
                       +----------+-----------+
               +------------------+------------------+
               v                  v                  v
          +---------+        +---------+        +---------+
          | Node 1  |        | Node 2  |        | Node 3  |
          | order-1 |        | order-2 |        | order-3 |   <- Pods
          | pay-1   |        | gateway |        | pay-2   |
          +---------+        +---------+        +---------+

Core Objects

ObjectWhat it isShopEase example
PodOne or more containers that run together; the smallest unitone order-service container
DeploymentKeeps N identical Pods running and rolls out new versionsorder-service, replicas: 3
ServiceA stable DNS name and virtual IP that load-balances to Podshttp://order-service
ConfigMapNon-secret configuration as key/values or filesKafka address, feature flags
SecretSensitive values (passwords, keys)DB password, Razorpay secret
IngressHTTP(S) entry from the internet, routes by host/path, TLSapi.shopease.in → api-gateway
NamespaceA folder that groups and isolates objectsshopease, observability
HorizontalPodAutoscalerChanges replicas based on CPU or custom metrics3 → 10 order Pods during a sale

What Changes for Spring Cloud?

  • Service discovery: Kubernetes Services + DNS replace Eureka. order-service calls http://payment-service and the Service load-balances. (Spring Cloud Kubernetes can also plug into DiscoveryClient.)
  • Configuration: ConfigMaps and Secrets (as environment variables or files) often replace Config Server.
  • Health: Actuator liveness and readiness groups become Kubernetes probes.
  • Still useful: Spring Cloud Gateway (routing, auth, rate limits), OpenFeign, Resilience4j, Micrometer Tracing.

Example

Everyday kubectl commands:

kubectl get pods -n shopease
kubectl logs -f deploy/order-service -n shopease
kubectl describe pod order-service-6d8f7c9b5-x2kqp -n shopease
kubectl exec -it deploy/order-service -n shopease -- sh
kubectl port-forward svc/order-service 8082:80 -n shopease

Example output

NAME                               READY   STATUS    RESTARTS   AGE
api-gateway-7b9c6d5f4-hk2lm        1/1     Running   0          2d
order-service-6d8f7c9b5-x2kqp      1/1     Running   0          3h
order-service-6d8f7c9b5-q8wzr      1/1     Running   0          3h
order-service-6d8f7c9b5-m4tnv      1/1     Running   0          3h
payment-service-5c4b8a7d6-7rjpd    1/1     Running   1          1d
product-service-84f9d6c7b-zt5wq    1/1     Running   0          1d