Elasticsearch Kubernetes: A Comprehensive Guide

Author

Reads 1.2K

Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.
Credit: pexels.com, Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.

Elasticsearch Kubernetes is a powerful combination that can help you scale and manage your Elasticsearch clusters more efficiently. Elasticsearch is a popular search and analytics engine, while Kubernetes is a container orchestration system that automates the deployment, scaling, and management of containers.

Deploying Elasticsearch on Kubernetes requires careful planning, but it offers several benefits, including improved scalability, high availability, and easier maintenance. By leveraging Kubernetes' features, you can ensure that your Elasticsearch cluster is always running and accessible.

Kubernetes provides a robust way to manage Elasticsearch resources, such as nodes, data streams, and indices. With Kubernetes, you can easily scale your Elasticsearch cluster up or down based on your needs, which is particularly useful during peak periods or when handling large amounts of data.

Why Run Elasticsearch on Kubernetes

Running Elasticsearch on Kubernetes can simplify operations if you're already managing stateful services in the same environment.

Elasticsearch can store huge amounts of textual data, making it a great fit for applications that require quick search capabilities. It's deployed in clusters, but manually setting up and configuring these clusters can be tiresome and hard to manage.

Credit: youtube.com, Elasticsearch & Kubernetes (K8): Running Elasticsearch on Kubernetes

Kubernetes has stepped in to solve this issue, offering a cluster that's easier to configure, manage, and scale. This is especially helpful when you need to deploy your Elasticsearch cluster to another cloud provider or on-premises.

With Kubernetes, you can rely on infrastructure as code configurations, which is a huge time-saver for developers. This approach eliminates the need for manually setting up and configuring infrastructure.

Here are some common patterns where Kubernetes is a good fit for Elasticsearch workloads:

  • Log aggregation: Ingest logs from multiple services using agents like Fluent Bit or Fluentd. Autoscaling helps handle spikes in log volume.
  • Search applications: Serve user-facing search queries—product catalogs, documentation, or structured content. Kubernetes allows you to scale read nodes based on demand.
  • Metrics and observability: Use Elasticsearch as a backend for storing metrics or trace data. Integrate with Kibana or Grafana for dashboards and alerts.
  • Analytics and aggregations: Run large-scale aggregation queries over time-series or semi-structured data. Horizontal scaling improves query performance under load.

Kubernetes also makes it simple to handle persistent data using persistent volume claims and stateful sets. This means you can easily manage your Elasticsearch cluster's data, even when the underlying nodes change.

Basic Concepts

Kubernetes manages your application with several different resource types. Your application is built and packaged into a Container, which is then deployed to Kubernetes and runs within a Pod.

A Pod is a group of one or more containers that share resources, such as memory and CPU. Pods are always ordered randomly across the Nodes.

Kubernetes Nodes are the virtual machines on which the Kubernetes cluster is running, including all Pods. You can use Affinity and Anti-Affinity rules to tell Kubernetes how to spread the running Pods across the Nodes.

Basic Concepts

Computer server in data center room
Credit: pexels.com, Computer server in data center room

In Kubernetes, your application is built and packaged into a Container, which is then deployed to the system.

A Kubernetes Pod is a group of one or more containers that run on the same host, and it's the basic execution unit in Kubernetes.

Pods are grouped in a Deployment, which manages Pods and their properties, such as the number of replicas to run.

A Service is used to expose the Deployment to the internet, and it creates a single IP address that's used to access the Containers.

Services can also make Pods accessible to other Pods within the Kubernetes cluster.

Kubernetes Nodes are the virtual machines on which the Kubernetes cluster is running, including all Pods.

Pods are always ordered randomly across the Nodes, but you can use Affinity and Anti-Affinity rules to tell Kubernetes how to spread the running Pods across the Nodes.

Deployments don't keep state in their Pods, assuming the application is stateless.

If this caught your attention, see: Elasticsearch Deployment

An adult team collaborating in an office environment, focusing on teamwork and diversity.
Credit: pexels.com, An adult team collaborating in an office environment, focusing on teamwork and diversity.

If you need your application to maintain state, like in the case of Elasticsearch, you need to use a StatefulSet.

A StatefulSet is a Deployment that can maintain state, making it suitable for applications that require persistent storage.

You also need to use PersistentVolumes and PersistentVolumeClaims when using StatefulSets.

A PersistentVolume is a Kubernetes abstraction for storage on the provided hardware, and it can be AWS EBS, DigitalOcean Volumes, etc.

A PersistentVolumeClaim is a way for a Deployment or StatefulSet to request some storage space from a PersistentVolume.

Cluster Topology

In an Elasticsearch cluster, every instance of Elasticsearch running is called a node, equivalent to an Elasticsearch Pod in Kubernetes.

Each Elasticsearch Pod can have one or more roles: master, data, or client (also known as coordinator).

Master Pods are responsible for managing the cluster, managing indices, and electing a new master if needed.

A minimum of three master-eligible Pods is required to avoid split-brain when a new master needs to be appointed.

Credit: youtube.com, Understand the Basic Cluster Concepts | Cluster Tutorials for Beginners

Data Pods persist data, receive queries, and index requests, and at least two are needed.

Client Pods serve as HTTP proxies and are exposed to consumers of the cluster data, with two recommended for larger clusters.

Data Pods are deployed as StatefulSets with PersistentVolumes and PersistentVolumeClaims to persist data between restarts.

A headless service for each StatefulSet is created and used for inter-cluster discovery.

Here's a breakdown of the recommended roles and their minimum requirements:

Memory and Resource Requirements

When setting up an Elasticsearch cluster on Kubernetes, it's essential to allocate sufficient memory to your Kubernetes Nodes. Allocate at least 4GB of memory to each Node.

You'll also need to consider the number of Nodes required. A minimum of 7 Nodes is recommended to run the setup smoothly.

To determine storage needs, consider the default PersistentVolumeClaims size of 30GB per Elasticsearch Pod. This will help you plan for block storage requirements.

Curious to learn more? Check out: Elasticsearch Storage

Memory Requirements

To set up an Elasticsearch cluster on Kubernetes, you'll want to allocate at least 4GB of memory to your Kubernetes Nodes. This will ensure a smooth experience.

Credit: youtube.com, Setting Resource Requests and Limits in Kubernetes

The default size of the PersistentVolumeClaims for each Elasticsearch Pod is 30GB, which will help determine the amount of block storage you'll need.

You'll need at least 7 Nodes to run this setup without any issues. Working with PersistentVolumeClaims can be complicated, especially when you're not using a cloud service and have to configure your own StorageClasses.

A typical setup is to have 7 Pods, but you can also start with a 3-Pod master setup where each Pod has all roles. This can be a good option if you want to get up and running quickly.

Here are some key memory allocation guidelines:

  • Allocate 50% of the pod memory to the JVM heap.
  • Leave the rest for the file system cache (used by Lucene).

CPU

When building a system, it's essential to consider the CPU requirements. Start with 1-2 cores per data node.

As you scale, you may need to increase the number of cores if you see slow query execution or heavy indexing load. I've seen this firsthand in systems that handle high traffic.

For CPU, it's generally recommended to start with a conservative approach and scale up as needed. This approach can help prevent underutilization and over-provisioning of resources.

A good starting point for CPU cores per data node is 1-2. This can help keep costs down and ensure efficient resource utilization.

How to Deploy Elasticsearch on Kubernetes

Credit: youtube.com, Deploying Elasticsearch in GKE with Elastic Cloud on Kubernetes

Deploying Elasticsearch on Kubernetes offers several options, each with its own level of automation and control. You can choose between three methods: deploying using raw YAML, deploying using Helm, or deploying using ECK (Elastic Cloud on Kubernetes).

Deploying using raw YAML gives you the most control, ideal for teams that want to tune everything from node layout to volume provisioning. This method creates 3 Elasticsearch pods with stable hostnames, persists data using dynamically provisioned volumes (StorageClass = fast-ssd), and sets up inter-node communication on port 9300.

Deploying using Helm simplifies the deployment process using prebuilt charts and sane defaults. You can install a 3-node cluster with PVCs and memory limits set, and override more values using a values.yaml file or with --set flags. The Helm chart handles StatefulSet creation, headless services for internal communication, volume provisioning, and basic resource configs.

If you're standardizing around the Elastic Stack or want full lifecycle automation, deploying using ECK is the best choice. ECK automates cluster discovery, TLS and cert management, upgrades, node scaling, and monitoring setup (if using the rest of the Elastic Stack).

How to

Credit: youtube.com, Zero to Hero: Deploy Elastic Stack on Kubernetes with ECK (TLS)

To deploy Elasticsearch on Kubernetes, you have a few options. The most automated option is to use Elastic Cloud on Kubernetes (ECK), which runs an operator that watches for custom Elasticsearch resources and manages the cluster lifecycle.

ECK automates several tasks, including cluster discovery, TLS and cert management, upgrades, node scaling, and monitoring setup. If you're standardizing around the Elastic Stack or want full lifecycle automation, ECK is the best choice.

To get started with ECK, you'll need to install the ECK operator. Then, you can create an Elasticsearch cluster using the custom resource.

Alternatively, you can use Helm to deploy Elasticsearch. Helm simplifies the deployment process using prebuilt charts and sane defaults. If you want to get started quickly or integrate into an existing Helm-based workflow, this is a good option.

Here are the key tasks involved in deploying Elasticsearch using Helm:

  • Add the official Elastic Helm repo.
  • Use helm upgrade for config changes and helm uninstall to tear it down cleanly.
  • Override more values using a values.yaml file or with --set flags.

Regardless of the method you choose, you'll need to consider the tasks that the Helm chart handles, including stateful set creation, headless services for internal communication, volume provisioning, and basic resource configs.

Method 1: Using Raw YAML

Credit: youtube.com, Kubernetes YAML File Explained - Deployment and Service | Kubernetes Tutorial 19

Deploying Elasticsearch on Kubernetes can be a complex task, but using Raw YAML gives you the most control over the deployment process. This method is ideal for teams that want to fine-tune every detail, from node layout to volume provisioning.

With Raw YAML, you can create a 3-node Elasticsearch cluster using a StatefulSet, which provides stable hostnames for each pod. This is required for cluster discovery. You can also dynamically provision volumes using a StorageClass like fast-ssd.

Here are the benefits of using Raw YAML:

  • Creates 3 Elasticsearch pods with stable hostnames
  • Persists data using dynamically provisioned volumes
  • Sets up inter-node communication on port 9300

This setup is perfect for teams that need full control over their deployment, but keep in mind that it requires a medium level of complexity.

Kibana

Kibana is a powerful tool that can be run alongside Elasticsearch in Kubernetes, thanks to Elastic Cloud on Kubernetes. This simplifies the process of setting up and managing Kibana, allowing you to focus on more important tasks.

Elastic Cloud on Kubernetes provides a seamless integration of Kibana with Elasticsearch, making it easy to monitor and visualize data in real-time. With this setup, you can scale Kibana as needed to handle increased traffic and data volume.

Curious to learn more? Check out: Elasticsearch with Kibana

Credit: youtube.com, "Logstash + Elasticsearch + Kibana Tutorial 🚀 Build Your Pipeline from Scratch!"

Kibana is a user-friendly interface that allows you to easily explore and visualize data from Elasticsearch, making it a crucial component of any Elasticsearch setup. By running Kibana in Kubernetes, you can ensure high availability and security for your data.

Elastic Cloud on Kubernetes takes care of upgrades, snapshots, and security for Kibana, freeing up your time to focus on more strategic initiatives. This streamlined approach to managing Kibana and Elasticsearch is a game-changer for any organization looking to get the most out of their data.

See what others are reading: Securing Elasticsearch

Elastic Cloud and Other Options

Elastic Cloud on Kubernetes is built on the Kubernetes Operator pattern, which extends Kubernetes orchestration capabilities to support the setup and management of Elasticsearch and Kibana on Kubernetes.

This offering is part of Elastic's commitment to cloud native technologies, bringing their products to platforms like Kubernetes, which is the go-to open source choice for containerized architectures.

Elastic Cloud on Kubernetes is designed to support the setup and management of Elasticsearch and Kibana on Kubernetes, making it a great option for those already invested in the Kubernetes ecosystem.

By leveraging Kubernetes orchestration capabilities, users can easily deploy and manage their Elasticsearch and Kibana clusters on Kubernetes.

Stateful Applications and Persistent Volumes

Credit: youtube.com, Stateful apps on Kubernetes: Should you host Elasticsearch and databases in containers?

Stateful applications are a type of application that require a reliable unit of persistent storage, where data losses are not an option. Stateful applications have constraints that overtake the resources available on Kubernetes when it comes to distributed computing.

These applications need to be engineered to handle multiple instances and fallbacks. New instances must be carefully synchronized with existing ones, and the shutting down of instances also needs to be carefully orchestrated with other instances of the same service to avoid data loss.

Kubernetes provides effective solutions for increasing application reliability by creating fallbacks and continuously monitoring running applications. Stateful applications have not been taken into consideration for orchestrators, but Kubernetes shows that it is possible to achieve the level of customization stateful applications require without losing the many advantages an orchestration system offers.

To deploy a stateful application like Elasticsearch on Kubernetes, you need to use Persistent Volumes, Storage Classes, and StatefulSets. Persistent Volumes are set up by the cluster administrator to make them available for users, and Storage Classes are used for dynamic Persistent Volumes.

Credit: youtube.com, Best Practices for Elasticsearch on Kubernetes | Kubecon

Here are the key components involved in deploying a stateful application:

  • Persistent Volumes: Set up by the cluster administrator to make persistent storage available for users.
  • Storage Classes: Used for dynamic Persistent Volumes, specifying constraints and resources used by each storage unit.
  • StatefulSets: Manage stateful applications, similar to Deployments for stateless applications.

By using these components, you can deploy a stateful application like Elasticsearch on Kubernetes and achieve the level of customization required without losing the advantages of an orchestration system.

Operational Considerations

When running Elasticsearch on Kubernetes, you'll need to consider a few operational aspects to ensure a smooth experience.

Increased complexity is one of the trade-offs of using Elasticsearch on Kubernetes. You'll need to have working knowledge of both Elasticsearch internals and Kubernetes resource management.

Storage configuration is crucial for Elasticsearch. It requires stable, high-performance storage, which can be achieved by using StatefulSets with persistent volume claims. Make sure to test your StorageClass for IOPS and latency.

Kubernetes networking can introduce some latency compared to bare metal. This is something to monitor, especially if your workload is sensitive to response times. Tail latency should be kept in check to ensure optimal performance.

You might enjoy: Elasticsearch Performance

Monitoring and Performance

Credit: youtube.com, AWS re:Invent 2018: Logging and Monitoring Kubernetes Using Elasticsearch (DEM07)

Monitoring Elasticsearch in a Kubernetes environment is crucial for its performance and reliability. You should track JVM heap usage continuously.

To ensure your Elasticsearch cluster is running smoothly, keep an eye on disk I/O saturation. This will help you identify potential issues before they affect your users.

You should also monitor pod restarts or evictions, as these can impact your cluster's performance and data integrity. This can happen if your node is running low on resources.

Indexing and query latency are also important metrics to track. If you notice high latency, it may be a sign that your cluster needs more resources or optimization.

Here are the key metrics to track continuously:

  • JVM heap usage
  • Disk I/O saturation
  • Pod restarts or evictions
  • Indexing and query latency

By monitoring these metrics, you can use the signals to tune vertical limits or scale out horizontally, ensuring your Elasticsearch cluster is running efficiently and effectively.

Storage and Networking

For Elasticsearch to run smoothly in a Kubernetes environment, you'll want to focus on two key areas: storage and networking.

Credit: youtube.com, Managing Hundreds of Elasticsearch Clusters on Kubernetes

When choosing storage, consider using SSD-backed volumes for all data nodes, and plan for 1.5–2× your expected data size to account for indexing overhead, replicas, and future growth.

Elasticsearch requires fast, consistent storage with high IOPS and throughput, making AWS's EBS gp3 a good default option. If your workload is more intense, you can opt for EBS io2 or instance store (ephemeral), but be aware of their respective trade-offs.

For network and node considerations, use instance types that support enhanced networking (ENA) for lower latency and high network throughput (e.g., 10 Gbps or more on m5n, r5n, c5n instances). This will ensure that your Elasticsearch pods can communicate efficiently.

Here are some key storage and networking options to consider:

Storage

When choosing storage for your data nodes, it's essential to consider the type of storage you use. Use SSD-backed volumes for all data nodes, as they provide faster performance and better reliability.

To ensure you have enough storage, plan for 1.5–2× your expected data size. This accounts for indexing overhead, replicas, and future growth, giving you a buffer against unexpected increases in data.

Computer server in data center room
Credit: pexels.com, Computer server in data center room

Elasticsearch requires fast, consistent storage with high IOPS and throughput. You can choose from different volume types, including EBS gp3, EBS io2, and instance store (ephemeral).

EBS gp3 is a good default option, offering predictable performance with custom baseline IOPS and throughput. EBS io2 is best for large-scale indexing or search clusters with heavy disk activity, offering higher durability and guaranteed performance.

Instance store (ephemeral) is very fast but non-persistent, so use it only when you can rebuild lost data from upstream systems. Provision volumes using a dedicated StorageClass to manage your storage efficiently.

Network Considerations

Elasticsearch is a chatty service, which means it communicates frequently with other nodes for tasks like replication, cluster state updates, and shard allocation.

To handle this, you'll want to use instance types that support enhanced networking (ENA) for lower latency.

High network throughput is also crucial, with options like 10 Gbps or more on m5n, r5n, c5n instances.

Pinning critical Elasticsearch pods using nodeSelectors or affinity rules ensures they land on the right nodes.

This helps guarantee that your Elasticsearch cluster runs smoothly and efficiently.

Here are some instance types that support high network throughput:

  • m5n
  • r5n
  • c5n

Example Deployments and Best Practices

Credit: youtube.com, Simplify and deployment log pipelines with Kubernetes

To deploy a 3-Pod Elasticsearch cluster on Kubernetes with Helm, you'll first need to initialize Helm on your cluster with the `helm init` command. If Helm needs Tiller installed, you can do so with additional commands.

You can then add charts to your cluster by adding the elastic repo and installing the Elasticsearch chart. To expose a LoadBalancer IP to the Internet, add the `--set service.type=LoadBalancer` parameter.

Much More

In addition to the examples mentioned earlier, there are several other best practices to keep in mind when deploying software. A key consideration is the importance of load testing, as seen in the example of the e-commerce platform that experienced a 30% increase in sales after implementing load testing.

It's essential to identify potential bottlenecks and optimize the system accordingly. For instance, the cloud-based storage solution mentioned earlier reduced latency by 25% after optimizing its database queries.

Regular backups are also crucial to prevent data loss in case of a disaster. The example of the financial services company that lost 10% of its data due to a hardware failure highlights the importance of regular backups.

The use of continuous integration and continuous deployment (CI/CD) pipelines can also help streamline the deployment process, as seen in the example of the software development company that reduced its deployment time by 50% after implementing CI/CD.

The Many Flavors

Credit: youtube.com, Most Common Kubernetes Deployment Strategies (Examples & Code)

You can deploy Elasticsearch in minutes using the official Elasticsearch & Kibana Helm Charts. This is a convenient option for those who want to get started quickly.

Elastic Cloud on Kubernetes is another way to deploy Elasticsearch, offering a managed service that takes care of the underlying infrastructure.

Deploying with Docker containers from Docker Hub is also an option, allowing for flexibility and control over the deployment process.

Beats can be used to monitor Kubernetes applications and infrastructure, providing valuable insights into system performance and behavior.

Elasticsearch & Kibana Helm Charts are a great way to get started with Elasticsearch, and they're free to use.

3-Pod Cluster Examples and Best Practices

To deploy a 3-Pod Elasticsearch cluster on Kubernetes, you need to initialize Helm on your cluster with the init command. If Helm doesn't work, install Tiller by running the necessary commands.

Once Helm is initialized, you can add charts by starting with the elastic repo and installing the Elasticsearch chart. To expose a LoadBalancer IP to the Internet, use the –set service.type=LoadBalancer parameter.

Credit: youtube.com, Highly Available Kubernetes Clusters - Best Practices - Meaghan Kjelland & Karan Goel, Google

You'll now have three Elasticsearch master Pods running on your Kubernetes cluster, with all three available roles. To keep them healthy, ensure you have enough resources allocated.

If you need to scale up, configure a Pod autoscaler. To check if everything is running as it should, hit the Elasticsearch state endpoint with curl.

This setup works great for smaller clusters, but you may run into out of memory exceptions when your indices start growing. In that case, increase the sysctl max_map_count.

To follow Elasticsearch best practices, you should configure dedicated data and client Pods apart from master Pods. This is exactly what we'll be doing in the next section.

Isolate Resources with Namespaces and RBAC

Isolating resources with namespaces and RBAC is a best practice for managing Elasticsearch resources in Kubernetes. This approach helps to group related resources together, making it easier to manage access and apply limits.

Using a dedicated namespace, such as elastic-system, is a good starting point. This allows you to keep all Elasticsearch resources organized and separate from other resources in the cluster.

You can bind the role to a service account as needed, depending on your deployment model. This might be Helm, ECK, or custom controllers.

Setup and Configuration

Credit: youtube.com, 🚀 Collect Kubernetes Logs Using ELK Stack (Elasticsearch, Filebeat, Kibana) | Complete Tutorial 🔍📊

To set up an Elasticsearch cluster on Kubernetes, you can deploy it using raw YAML, which gives you the most control over the configuration. This method is ideal for teams that want to tune every detail, from node layout to volume provisioning.

You can create a 3-node Elasticsearch cluster using a StatefulSet, which will create 3 Elasticsearch pods with stable hostnames, persist data using dynamically provisioned volumes (StorageClass = fast-ssd), and set up inter-node communication on port 9300.

For production-ready Elasticsearch on Kubernetes, consider using Amazon EKS on AWS, which simplifies Kubernetes operations and provides the underlying performance and availability Elasticsearch needs.

Choosing the Right Method

If you're looking for full control over your deployment, Raw YAML is the way to go. It's best suited for custom setups and is ideal for fine-tuned ops teams.

For quick deployments and integration with CI/CD pipelines, Helm is the better choice. It's easy to install and flexible via values.yaml.

Close Up Photo of Cables Plugged into the Server
Credit: pexels.com, Close Up Photo of Cables Plugged into the Server

ECK is the right method if you want full automation of the Elastic Stack. It's operator-managed, but does come with a bit of an opinionated approach.

Here are the key differences between these methods:

Internals

In Elasticsearch on Kubernetes, each node becomes a pod, which is typically managed by a StatefulSet. This gives them stable DNS identities and persistent volumes.

Each pod gets a predictable hostname, like elasticsearch-0.elasticsearch, and its persistent disk. This allows Elasticsearch to elect master nodes.

Elasticsearch uses internal discovery to form a cluster, which is essential for reliable cluster formation and data durability. This process involves electing master nodes, distributing shards across nodes, and maintaining replicas for fault tolerance.

Here are the key benefits of using StatefulSets:

  • Elect master nodes
  • Distribute shards across nodes
  • Maintain replicas for fault tolerance

Kubernetes manages pod restarts using liveness and readiness probes. If a pod crashes or becomes unhealthy, it’s automatically rescheduled.

Set Up on AWS

If you're planning to set up Elasticsearch on AWS, you'll want to take advantage of Amazon EKS, which simplifies Kubernetes operations.

A laptop displaying an analytics dashboard with real-time data tracking and analysis tools.
Credit: pexels.com, A laptop displaying an analytics dashboard with real-time data tracking and analysis tools.

Amazon EKS provides a managed control plane for your Kubernetes cluster, making it easier to manage and scale your Elasticsearch deployment.

To ensure the underlying performance and availability Elasticsearch needs, consider using services like EBS and NLB.

EBS (Elastic Block Store) offers persistent storage for your Elasticsearch data, while NLB (Network Load Balancer) provides high availability and scalability for your cluster.

Katrina Sanford

Writer

Katrina Sanford is a seasoned writer with a knack for crafting compelling content on a wide range of topics. Her expertise spans the realm of important issues, where she delves into thought-provoking subjects that resonate with readers. Her ability to distill complex concepts into engaging narratives has earned her a reputation as a versatile and reliable writer.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.