Backup Elasticsearch on Kubernetes

You can use the instructions on this page to create pre and post backup rules with PX-Backup, which take application-consistent backups for Elasticsearch on Kubernetes in production.

The Elasticsearch data directory is used prevent permanent data loss and is typically located at /user/share/elasticsearch/data inside the Kubernetes pod. This will also enable PX-Backup to backup and restore the data stored in this location.

Before using this guide, make sure and configure PVCs for elasticsearch-data. Use the below file as an example.

kind: Elasticsearch
  name: elasticsearch
  version: 7.8.0
  - name: default
    count: 3
          appname: "elastisearch-app"
          - name: elasticsearch
            - name: elasticsearch-backups
              mountPath: /usr/share/elasticsearch/backups
        - name: elasticsearch-backups
            claimName: elasticsearch-backups
    - metadata:
        name: elasticsearch-data
        - ReadWriteOnce
            storage: 5Gi
        storageClassName: elastic-pwx-storage-class
      node.master: true true
      node.ingest: true false
      path.repo: ["/usr/share/elasticsearch/backups"]

Elasticsearch can also create a snapshot repository to store index snapshots produced by the internal snapshot and restore API. For this, the above template uses a Portworx shared volume and named elasticsearch-backups to create a shared file-system across all Elasticsearch nodes to be used to store the index snapshots.



If you have not used Portworx, PX-Backup, or run Data services on Kubernetes it is a good idea to review the following information:


  • The templat above can not be used alone. Please follow pre-requisites from the following elastic on kubernetes operations guide.
  • The rules below will use a user name elastic and password specific to the environment. You will need to modify the rule to use your user name and password for your environment.
  • You should mount a PVC to the elasticsearch data directory. This is typically located at /user/share/elasticsearch/data.

Create rules for Elasticsearch

Create rules for Elasticsearch that will run both before and after the backup operation runs:

Create a pre-exec backup rule for Elasticsearch

For the pre-backup rule you will create a rule that performs multiple actions.

  • Freeze the index
  • Flush all indexes in Elasticsearch
  • Create an Elasticsearch index snapshot of all indexes

Create the rule.

  1. Navigate to SettingsRulesAdd New.
  2. Add a name for your Rule.
  3. Add the following app label:

  4. Add the following action:

    curl -X POST -u "elastic:<password>" -k "https://elasticsearch-es-http:9200/customer/_freeze&pretty"
  5. Add the following additional action:

    curl -X PUT -u "elastic:<password>" -k "https://elasticsearch-es-http:9200/all/_flush&pretty"
  6. Add the following additional action:

    curl -X PUT -u "elastic:<password>" -k "https://elasticsearch-es-http:9200/_snapshot/es_backups/%3Csnapshot-$(uuidgen)-%7Bnow%2Fd%7D%3E?wait_for_completion=true&pretty"

Create a post-exec backup rule for Elasticsearch

Performing the _freeze and _flush operations on your index before calling the snapshot API ensures flexible and accurate restores by making sure the database isn’t currently being written into.

Since you performed a _freezeoperation when you created a backup, you must create a post exec rule to perform an _unfreeze operation. The steps below create a post-exec rule which runs _unfreeze on the customer index:

  1. Navigate to SettingsRulesAdd New.
  2. Add a name for your Rule.
  3. Add the following app label:

  4. Add the following action:

    curl -X POST -u "elastic:<password>" -k "https://elasticsearch-es-http:9200/customer/_unfreeze&pretty"

Associate the pre and post-exec rules with the Elasticsearch backup

Create a backup for your Elasticsearch application. Select the rules you created in the steps above from the pre-exec and post-exec drop downs:

Once you’ve filled out the backup form, click Create


Watch this short demo of the above information.

Last edited: Thursday, Oct 1, 2020