RousselTMLEARNING

TP · OpenSearch: from discovery to expertise

Final lab: Lifecycle, backup and permissions with correction

The correction is integrated into the TP (See the green boxes)

What you will learn in this TP
  • Write an ISM policy
  • Restore a snapshot
  • Limit permissions

Introduction #

What you will learn in this section
  • Write an ISM policy
  • Restore a snapshot
  • Limit permissions

Context. Before production, you must prove that indexes manage themselves, that an accidental deletion can be repaired and that a reader cannot break anything.

Prerequisites: the first lab  ·  Estimated time: 50 min

A prototype ready to operate.

Setup #

Guided steps, with their solution.

  1. 1. Write the policy

    Why? Without ISM, indexes grow until the disk is full.

    Create a policy: rollover, read-only at 7 days, deletion at 30 days. The 2-document threshold only serves to observe the rollover.
    See the correction
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT https://localhost:9200/_plugins/_ism/policies/cycle_logs -d '{
      "policy": {
        "description": "Rollover, lecture seule puis suppression",
        "default_state": "chaud",
        "states": [
          {"name": "chaud",
           "actions": [{"rollover": {"min_doc_count": 2}}],
           "transitions": [{"state_name": "tiede", "conditions": {"min_index_age": "7d"}}]},
          {"name": "tiede",
           "actions": [{"read_only": {}}, {"force_merge": {"max_num_segments": 1}}],
           "transitions": [{"state_name": "supprime", "conditions": {"min_index_age": "30d"}}]},
          {"name": "supprime", "actions": [{"delete": {}}], "transitions": []}
        ],
        "ism_template": [{"index_patterns": ["cycle-*"], "priority": 100}]
      }
    }'
  2. 2. Trigger the rollover

    Why? Rollover needs a write alias and the rollover_alias setting.

    Create the template and first index, then write two documents. After two to three minutes, a cycle-000002 index should appear.
    See the correction
    # Template: the rollover alias the policy must use
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT https://localhost:9200/_index_template/cycle -d '{
      "index_patterns": ["cycle-*"],
      "template": {"settings": {
        "number_of_shards": 1, "number_of_replicas": 0,
        "plugins.index_state_management.rollover_alias": "cycle"
      }}
    }'
    
    # First index, with the write alias
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT https://localhost:9200/cycle-000001 -d '{"aliases":{"cycle":{"is_write_index":true}}}'
    
    # For the lab only: check policies every minute (5 by default)
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT https://localhost:9200/_cluster/settings \
      -d '{"persistent":{"plugins.index_state_management.job_interval":1}}'
    
    # Write two documents through the alias
    for i in 1 2; do
      curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X POST 'https://localhost:9200/cycle/_doc?refresh=true' -d "{\"n\":$i}"
    done
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' https://localhost:9200/_plugins/_ism/explain/cycle-000001
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' 'https://localhost:9200/_cat/indices/cycle-*?v'
  3. 3. Back up and restore

    Why? A replica does not protect against deletion.

    Create the repository, take a snapshot, delete logs-app-000001 and restore it: the count must return to 3.
    See the correction
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT https://localhost:9200/_snapshot/sauvegardes -d '{
      "type": "fs",
      "settings": {"location": "/usr/share/opensearch/data/snapshots"}
    }'
    
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT 'https://localhost:9200/_snapshot/sauvegardes/snap1?wait_for_completion=true' \
      -d '{"indices":"logs-app-*"}'
    
    # Lose an index, then restore it
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X DELETE https://localhost:9200/logs-app-000001
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X POST 'https://localhost:9200/_snapshot/sauvegardes/snap1/_restore?wait_for_completion=true' \
      -d '{"indices":"logs-app-000001"}'
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' https://localhost:9200/logs-app-000001/_count
  4. 4. Create a read-only role

    Why? Least privilege is proven by a refusal.

    Create the user, the role and their mapping. Check that reading returns 200 and deleting 403.
    See the correction
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT https://localhost:9200/_plugins/_security/api/internalusers/lecteur \
      -d '{"password":"L3cteur!Passw0rd-26","backend_roles":[]}'
    
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT https://localhost:9200/_plugins/_security/api/roles/lecture_logs -d '{
      "cluster_permissions": ["cluster_composite_ops_ro"],
      "index_permissions": [{"index_patterns": ["logs-app-*"], "allowed_actions": ["read"]}]
    }'
    
    curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT https://localhost:9200/_plugins/_security/api/rolesmapping/lecture_logs \
      -d '{"users":["lecteur"]}'
    
    # Read: 200. Delete: 403.
    curl -sk -u 'lecteur:L3cteur!Passw0rd-26' -o /dev/null -w '%{http_code}\n' https://localhost:9200/logs-app-000001/_search
    curl -sk -u 'lecteur:L3cteur!Passw0rd-26' -o /dev/null -w '%{http_code}\n' -X DELETE https://localhost:9200/logs-app-000001

Difficulty level : ●●●○○ (3/5)

My progress

  • ⏱ Estimated time : ≈ 50 min

Sign in to track your progress and take notes. Sign in

💬 Ask a question

Recommended Articles

OpenSearch in production: ISM, backups, security and best practices

Automate the lifecycle with ISM, back up with snapshots, manage roles and size an OpenSearch clus...

Snapshot Lifecycle Management : sauvegardes automatisées, restauration et reprise après sinistre

Protéger les données Elasticsearch : dépôts de snapshots (S3, GCS, Azure, système de fichiers), p...

Index Lifecycle Management (ILM) : data tiers, rollover et rétention

Automatiser le cycle de vie des index avec ILM : data tiers hot, warm, cold et frozen, rollover p...

Data streams et templates Elasticsearch : component templates, index templates et séries temporelles

Gérer les données horodatées avec les data streams : index templates et component templates, conv...

Kibana : rôle, installation et premiers pas

Ce qu'est Kibana dans la suite Elastic, comment l'installer avec Elasticsearch (start-local, Dock...

Indexing and searching in OpenSearch: mapping, DSL, PPL and SQL

Declare an index template, index with bulk, search with the Query DSL, aggregate and query with P...

OpenSearch Dashboards, alerting and observability

Explore data in OpenSearch Dashboards, create alert monitors and discover Trace Analytics, Event ...

Superviser et protéger Azure : Monitor, Log Analytics, Defender, Key Vault et Backup

Exploiter Azure : Azure Monitor (métriques, journaux d'activité, alertes), Log Analytics et KQL, ...

OpenSearch: concepts, architecture and differences with Elasticsearch

Understanding OpenSearch: project origin, license, differences with Elasticsearch, indexes, shard...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!