Skip to main content
Where should Bob’s log lines live so they outlast the pod that wrote them? Bob already knows the answer has a name: Elasticsearch. He first tries the obvious approach: run Elasticsearch like any other app—as a Deployment with two replicas. Two pods start, each with a few random letters appended to its name. A “USER1 logged in” line arrives and gets stored. So far, so good. Then what happens when one of those pods is replaced the way the app pod was last night? The Deployment notices, terminates the old pod, and starts a new one. The fresh pod gets a brand-new randomized name.
A diagram titled "A Replaced Pod Starts Empty" showing two blue cartoon pod characters on a platform—one happy pod labeled "elasticsearch-7d9c5-m8rtz" next to a storage icon and one empty/sad pod. Above them is a "Deployment" badge.
Because containers write to ephemeral scratch space by default, the new pod gets a brand-new empty disk. Every document the old pod stored is gone with it. Bob has moved the problem one layer down: the logs now die with the Elasticsearch pod
A dark-themed diagram titled "One Step Down the Line" showing two smiling blue robot icons on separate platforms with an arrow pointing from the left to the right. A label next to the right robot reads "USER1 logged in."
instead of surviving independently. What does Elasticsearch actually need from Kubernetes? Two things a Deployment does not provide:
  1. Durable per-node storage that lives outside a pod so a replacement can mount the same disk and see existing data.
  2. A stable network identity (a predictable hostname) so other Elasticsearch nodes and clients can find a specific pod after it restarts.
How does Bob get a disk that outlives a pod?
A dark-themed diagram titled "A Name That Never Changes" showing Fluentd on the left forwarding data to an Elasticsearch instance labeled "USER1" / "elasticsearch-0." Blue mascot icons sit on server platforms with a central storage cylinder representing persistent storage across nodes.
Kubernetes separates persistent storage into two concepts:
  • A PersistentVolume (PV) is an actual piece of storage (for example, a folder on a node or a network volume).
  • A PersistentVolumeClaim (PVC) is a request for storage—think of it as a ticket that says “I need 5Gi that I can read and write from one node.” When a PV satisfies a PVC, they bind and a pod mounts the claimed volume.
Example PV (a simple hostPath for a lab):
Rather than creating PVCs by hand for each pod, Bob uses a StatefulSet template that creates a PVC for every replica automatically. In a StatefulSet:
  • The container mounts a claim inside the Pod spec:
  • The StatefulSet generates a PVC per replica using volumeClaimTemplates:
Because each pod gets its own PVC, a replacement pod created with the same stable name will mount the same volume and pick up the previous pod’s data. When a replacement happens, Elasticsearch 0 gets recreated as elasticsearch-0, mounts the same claim, and finds the “USER1 logged in” document still present. This is exactly the behavior Bob wanted. Kubernetes calls this pattern a StatefulSet.
A stylized diagram titled "Last Night, Replayed" showing an Elasticsearch pod/instance (elasticsearch-0) linking to an index named es-data-elasticsearch-0 and writing to a data volume (/data/elasticsearch). A user icon labeled "Bob" and a "USER1 logged in" note appear to the right.
One problem remains: pod IP addresses change on restart, so how can anything reliably find a given Elasticsearch pod?
A slide titled "Addresses Keep Changing" showing a Fluentd logo on the left and two stylized blue server/agent icons on the right labeled with IPs 10.244.1.7 and 10.244.2.4.
A Kubernetes Service provides a stable DNS name and (for regular Services) a virtual IP that forwards traffic to the pods matching its selector. For Elasticsearch internal discovery you typically use the StatefulSet-generated stable hostnames (for example elasticsearch-0, elasticsearch-1) combined with a headless Service (clusterIP: None) so each pod has a resolvable DNS name. For external access to HTTP/transport ports you might also add a ClusterIP/NodePort or LoadBalancer service. Bob creates a Service called elasticsearch that opens two ports: 9200 for HTTP (data and queries) and 9300 for node-to-node transport. For quick checks he exposes port 9200 via NodePort so the node maps it to 30200.
A diagram titled "A Service in Front" showing Fluentd on the left connecting to an "elasticsearch" service. The service routes to two "app: elasticsearch" nodes (ports 9200 and 9300) shown as blue mascot hosts, with a KodeKloud logo in the corner.
Example Service spec:
Exposing Elasticsearch with a NodePort is convenient for testing and debugging, but it opens a service port on every node. In production, prefer a LoadBalancer, an Ingress (with secure proxying), or firewall rules and ensure Elasticsearch is secured (authentication, TLS, network policies).
A note on terminology: Kubernetes replicas (how many pod instances a StatefulSet or Deployment runs) are not the same as Elasticsearch replicas (copies of indices/shards). Bob initially runs a single Elasticsearch pod in single-node mode so the cluster does not attempt to form multi-node shards until he scales to multiple pods and configures Elasticsearch cluster settings. Quick reference — Kubernetes objects you used Recommended rule of thumb
  • If losing a pod’s disk loses data you care about, use a StatefulSet with volumeClaimTemplates so each replica gets a durable PVC.
  • If other pods or clients need to discover or connect to the pod, put a Service in front for stable access.
When you need durable storage plus a stable network identity for a pod, use a StatefulSet with volumeClaimTemplates and back it with a Service (headless for internal discovery, ClusterIP/LoadBalancer/Ingress for external access).
Further reading and references

Watch Video