Skip to main content
Version: Preview

Manage Elasticsearch with KubeBlocks

Elasticsearch is a distributed, RESTful search and analytics engine that is capable of solving an ever-growing number of use cases. As the heart of the Elastic Stack, Elasticsearch stores your data centrally, allowing you to search it quickly, tune relevancy, perform sophisticated analytics, and easily scale.

KubeBlocks supports the management of Elasticsearch. This tutorial illustrates how to create and manage an Elasticsearch cluster by kbcli, kubectl or a YAML file. You can find the YAML examples and guides in the GitHub repository.

Before you start

Create a cluster

KubeBlocks implements a Cluster CRD to define a cluster. Here is an example of creating an Elasticsearch cluster with multiple nodes. For more examples, refer to the GitHub repository.

If you only have one node for deploying a cluster with multiple nodes, configure the cluster affinity by setting spec.schedulingPolicy or spec.componentSpecs.schedulingPolicy. For details, you can refer to the API docs. But for a production environment, it is not recommended to deploy all replicas on one node, which may decrease the cluster availability.

cat <<EOF | kubectl apply -f -
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: es-multinode
namespace: default
annotations:
kubeblocks.io/extra-env: '{"master-roles":"master", "data-roles": "data", "ingest-roles": "ingest", "transform-roles": "transform"}'
spec:
terminationPolicy: Delete
componentSpecs:
- name: master
componentDef: elasticsearch-8-1.0.0
replicas: 3
resources:
limits:
cpu: '0.5'
memory: 2Gi
requests:
cpu: '0.5'
memory: 2Gi
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
- name: data
componentDef: elasticsearch-8-1.0.0
replicas: 3
resources:
limits:
cpu: '0.5'
memory: 2Gi
requests:
cpu: '0.5'
memory: 2Gi
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
- name: ingest
componentDef: elasticsearch-8-1.0.0
replicas: 1
resources:
limits:
cpu: '0.5'
memory: 2Gi
requests:
cpu: '0.5'
memory: 2Gi
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
- name: transform
componentDef: elasticsearch-8-1.0.0
replicas: 1
resources:
limits:
cpu: '0.5'
memory: 2Gi
requests:
cpu: '0.5'
memory: 2Gi
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
EOF
FieldDefinition
metadata.annotationsIt specifies the Elasticsearch cluster type.
spec.terminationPolicyIt is the policy of cluster termination. Valid values are DoNotTerminate, Delete, WipeOut. For the detailed definition, you can refer to Termination Policy.
spec.componentSpecsIt is the list of ClusterComponentSpec objects that define the individual Components that make up a Cluster. This field allows customized configuration of each component within a cluster.
spec.componentSpecs.componentDefIt specifies the ComponentDefinition custom resource (CR) that defines the Component's characteristics and behavior.
spec.componentSpecs.serviceVersionIt specifies the version of the Service expected to be provisioned by this Component.
spec.componentSpecs.replicasIt specifies the number of replicas of the component.
spec.componentSpecs.resourcesIt specifies the resources required by the Component.
spec.componentSpecs.volumeClaimTemplatesIt specifies a list of PersistentVolumeClaim templates that define the storage requirements for the Component.
spec.componentSpecs.volumeClaimTemplates.nameIt refers to the name of a volumeMount defined in componentDefinition.spec.runtime.containers[*].volumeMounts.
spec.componentSpecs.volumeClaimTemplates.spec.storageClassNameIt is the name of the StorageClass required by the claim. If not specified, the StorageClass annotated with storageclass.kubernetes.io/is-default-class=true will be used by default.
spec.componentSpecs.volumeClaimTemplates.spec.resources.storageYou can set the storage size as needed.

For more API fields and descriptions, refer to the API Reference.

KubeBlocks operator watches for the Cluster CRD and creates the cluster and all dependent resources. You can get all the resources created by the cluster with kubectl get all,secret,rolebinding,serviceaccount -l app.kubernetes.io/instance=mycluster -n demo.

kubectl get all,secret,rolebinding,serviceaccount -l app.kubernetes.io/instance=mycluster -n demo

Run the following command to see the created Elasticsearch cluster object:

kubectl get cluster mycluster -n demo -o yaml

Connect to the Elasticsearch cluster

Elasticsearch provides the HTTP protocol for client access on port 9200. You can visit the cluster by the local host.

curl http://127.0.0.1:9200/_cat/nodes?v

Scale

KubeBlocks supports horizontally and vertically scaling an Elasticsearch cluster.

Before you start

Check whether the cluster status is Running. Otherwise, the following operations may fail.

kubectl get cluster mycluster -n demo
>
NAME CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS AGE
mycluster Delete Running 4m29s

Scale horizontally

Horizontal scaling changes the amount of pods. For example, you can scale out replicas from three to five.

From v0.9.0, besides replicas, KubeBlocks also supports scaling in and out instances, refer to the Horizontal Scale tutorial for more details and examples.

  1. Apply an OpsRequest to a specified cluster. Configure the parameters according to your needs.

    The example below means adding two replicas.

    kubectl apply -f - <<EOF
    >
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-horizontal-scaling
    namespace: demo
    spec:
    clusterName: mycluster
    type: HorizontalScaling
    horizontalScaling:
    - componentName: elasticsearch
    scaleOut:
    replicaChanges: 2
    EOF

    If you want to scale in replicas, replace scaleOut with scaleIn.

    The example below means deleting two replicas.

    kubectl apply -f - <<EOF
    >
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-horizontal-scaling
    namespace: demo
    spec:
    clusterName: mycluster
    type: HorizontalScaling
    horizontalScaling:
    - componentName: elasticsearch
    scaleIn:
    replicaChanges: 2
    EOF
  2. Check the operation status to validate the horizontal scaling.

    kubectl get ops -n demo
    >
    NAMESPACE NAME TYPE CLUSTER STATUS PROGRESS AGE
    demo ops-horizontal-scaling HorizontalScaling mycluster Succeed 3/3 6m

    If an error occurs, you can troubleshoot with kubectl describe ops -n demo command to view the events of this operation.

  3. Check whether the corresponding resources change.

    kubectl describe cluster mycluster -n demo

Scale vertically

  1. Apply an OpsRequest to a specified cluster. Configure the parameters according to your needs.

    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: elasticsearch-verticalscaling
    namespace: demo
    spec:
    clusterName: mycluster
    type: VerticalScaling
    verticalScaling:
    - componentName: mdit
    requests:
    cpu: '1'
    memory: '3Gi'
    limits:
    cpu: '1'
    memory: '3Gi'
  2. Check the operation status to validate the horizontal scaling.

    kubectl get ops -n demo
    >
    NAMESPACE NAME TYPE CLUSTER STATUS PROGRESS AGE
    demo ops-horizontal-scaling HorizontalScaling mycluster Succeed 3/3 6m

    If an error occurs, you can troubleshoot with kubectl describe ops -n demo command to view the events of this operation.

  3. Check whether the corresponding resources change.

    kubectl describe cluster mycluster -n demo

Volume Expansion

Before you start

Check whether the cluster status is Running. Otherwise, the following operations may fail.

kubectl get cluster mycluster -n demo
>
NAME CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS AGE
mycluster Delete Running 4m29s

Steps

  1. Change the value of storage according to your need and run the command below to expand the volume of a cluster.

    kubectl apply -f - <<EOF
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-volume-expansion
    namespace: demo
    spec:
    clusterName: mycluster
    type: VolumeExpansion
    volumeExpansion:
    - componentName: elasticsearch
    volumeClaimTemplates:
    - name: data
    storage: "40Gi"
    EOF
  2. Validate the volume expansion operation.

    kubectl get ops -n demo
    >
    NAMESPACE NAME TYPE CLUSTER STATUS PROGRESS AGE
    demo ops-volume-expansion VolumeExpansion mycluster Succeed 3/3 6m

    If an error occurs, you can troubleshoot with kubectl describe ops -n demo command to view the events of this operation.

  3. Check whether the corresponding cluster resources change.

    kubectl describe cluster mycluster -n demo

Stop/Start a cluster

You can stop/start a cluster to save computing resources. When a cluster is stopped, the computing resources of this cluster are released, which means the pods of Kubernetes are released, but the storage resources are reserved. Start this cluster again if you want to restore the cluster resources from the original storage by snapshots.

Stop a cluster

  1. Configure the name of your cluster and run the command below to stop this cluster.

    Configure replicas as 0 to delete pods.

    kubectl apply -f - <<EOF
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-stop
    namespace: demo
    spec:
    clusterName: mycluster
    type: Stop
    EOF
  2. Check the status of the cluster to see whether it is stopped.

    kubectl get cluster mycluster -n demo

Start a cluster

  1. Configure the name of your cluster and run the command below to start this cluster.

    Run the command below to start a cluster.

    kubectl apply -f - <<EOF
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-start
    namespace: demo
    spec:
    clusterName: mycluster
    type: Start
    EOF
  2. Check the status of the cluster to see whether it is running again.

    kubectl get cluster mycluster -n demo

Restart

  1. Restart a cluster.

    kubectl apply -f - <<EOF
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-restart
    namespace: demo
    spec:
    clusterName: mycluster
    type: Restart
    restart:
    - componentName: elasticsearch
    EOF
  2. Check the pod and operation status to validate the restarting.

    kubectl get pod -n demo

    kubectl get ops ops-restart -n demo

    During the restarting process, there are two status types for pods.

    • STATUS=Terminating: it means the cluster restart is in progress.
    • STATUS=Running: it means the cluster has been restarted.

Delete a cluster

Termination policy

note

The termination policy determines how a cluster is deleted.

terminationPolicyDeleting Operation
DoNotTerminateDoNotTerminate prevents deletion of the Cluster. This policy ensures that all resources remain intact.
DeleteDelete deletes Cluster resources like Pods, Services, and Persistent Volume Claims (PVCs), leading to a thorough cleanup while removing all persistent data.
WipeOutWipeOut is an aggressive policy that deletes all Cluster resources, including volume snapshots and backups in external storage. This results in complete data removal and should be used cautiously, primarily in non-production environments to avoid irreversible data loss.

To check the termination policy, execute the following command.

kubectl get cluster mycluster -n demo
>
NAME CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS AGE
mydemo Delete Creating 27m

Steps

Run the command below to delete a specified cluster.

If you want to delete a cluster and its all related resources, you can modify the termination policy to WipeOut, then delete the cluster.

kubectl patch -n demo cluster mycluster -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"

kubectl delete -n demo cluster mycluster