Skip to main content
Time to turn the deployment plan into real Kubernetes objects. This guide follows the exact sequence used in the lab so objects are created in the right order and bind together predictably. Vijin Palazhi starts from the lab terminal and creates a home for the Elastic stack resources so they don’t get mixed with application resources.
A dark-themed diagram titled "A Home for the Stack" showing a user avatar labeled "Bob" on the left and three circular icons for Fluentd, Elasticsearch, and Kibana across the top. Below them is a dashed outline of a folder.
Why use a namespace? A namespace is a named folder for Kubernetes objects. Creating one isolates the stack’s objects and makes cleanup easier.
Create a dedicated namespace to keep Elastic stack resources separate from other workloads. This simplifies management and prevents accidental cross-resource interference.
Create the namespace and set it as the default context so you don’t need -n on every command:
Summary of the deployment plan:
  • Create persistent storage (PersistentVolume).
  • Create a Service that will front Elasticsearch.
  • Create the StatefulSet that runs Elasticsearch and claims the storage.
  • Ensure filesystem permissions are correct with an init container.
  • Verify pods, endpoints, and query Elasticsearch.

1) PersistentVolume (the disk)

The storage must exist before the StatefulSet requests it. Vijin creates a hostPath PersistentVolume named pv-elasticsearch that offers 5 GiB and supports ReadWriteOnce (single-node access). It maps to /data/elasticsearch on the host node. es-pvolume.yaml:
Apply the PV:
After applying, the volume will appear unclaimed until a PersistentVolumeClaim requests it.

2) Service (the address)

Create the Service before pods exist — a Service selects pods by labels, so it will start forwarding traffic once matching pods are available. This NodePort Service named elasticsearch exposes two ports:
  • 9200 → NodePort 30200 (HTTP / REST API)
  • 9300 → NodePort 30300 (Elasticsearch transport between nodes)
Relevant ports fragment:
Apply the Service:
The Service will remain ready to route traffic as soon as pods with app=elasticsearch appear.

3) StatefulSet (the database)

A StatefulSet provides stable network identities and persistent storage for Elasticsearch pods. Vijin creates a StatefulSet named elasticsearch that uses the official image docker.elastic.co/elasticsearch/elasticsearch:7.1.0. The manifest sets discovery to single-node for a single-node cluster. Key StatefulSet fragments: Metadata, serviceName, selector, and pod labels:
Container spec with ports and single-node discovery:
Persistent storage setup with a PersistentVolumeClaim template. This claim (es-data) requests 5 GiB with ReadWriteOnce and will bind to pv-elasticsearch created earlier:

Filesystem permissions and init container

Elasticsearch runs as UID 1000. If the host directory (e.g. /data/elasticsearch) is owned by root, the container will fail to write. To avoid this, add an init container that fixes ownership before the main container starts.
Ensure the data directory is writable by Elasticsearch’s runtime user (UID 1000). Without this step the main container will fail to start due to permission errors.
Init container example:
Apply the StatefulSet manifest and watch the pods:
Kubernetes will create a pod named elasticsearch-0. The PVC will bind to pv-elasticsearch, the init container will run and exit, then the main Elasticsearch container will start.
A diagram titled "Be Patient While It Starts" showing a sleepy blue Elasticsearch pod labeled "elasticsearch-0" connected by an arrow to a cylindrical storage icon labeled "pv-elasticsearch." The image also shows the status "Bound" below and a KodeKloud logo in the corner.

4) Verify the deployment

Check readiness and endpoints to confirm the pod is actually serving traffic. Useful checks:
  • Wait for the pod to be READY:
  • Inspect Service endpoints (there should be exactly one address — elasticsearch-0):
An illustration on a dark teal background reads "One Address Behind It" above a blue "elasticsearch" badge. Below, a small green robot-like mascot stands on a platform holding the Elasticsearch logo, with a KodeKloud logo in the corner.
Query Elasticsearch directly using the NodePort exposed on the node (HTTP on localhost:30200 in the lab):
Example response:
You can also request the full cluster state:
At this point:
  • The Elasticsearch database is running and reachable.
  • Its data is stored on a PersistentVolume that outlives the pod.
  • The application may still need to be configured to send events to this Elasticsearch instance — the index will remain empty until data is pushed.

Quick reference: resources created

This sequence ensures storage, networking, permissions, and pod identity are all handled in the correct order for a reliable single-node Elasticsearch deployment on Kubernetes.

Watch Video

Practice Lab