
Create a dedicated namespace to keep Elastic stack resources separate from other workloads. This simplifies management and prevents accidental cross-resource interference.
-n on every command:
- 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 namedpv-elasticsearch that offers 5 GiB and supports ReadWriteOnce (single-node access). It maps to /data/elasticsearch on the host node.
es-pvolume.yaml:
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 namedelasticsearch exposes two ports:
- 9200 → NodePort
30200(HTTP / REST API) - 9300 → NodePort
30300(Elasticsearch transport between nodes)
app=elasticsearch appear.
3) StatefulSet (the database)
A StatefulSet provides stable network identities and persistent storage for Elasticsearch pods. Vijin creates a StatefulSet namedelasticsearch 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:
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.
elasticsearch-0. The PVC will bind to pv-elasticsearch, the init container will run and exit, then the main Elasticsearch container will start.

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):

localhost:30200 in the lab):
- 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
Links and references
- Kubernetes documentation: https://kubernetes.io/docs/
- StatefulSet overview: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
- Elasticsearch Docker images: https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html
- kubectl reference: https://kubernetes.io/docs/reference/kubectl/