Upgrade ELK là phép thử cuối cùng cho mọi setup. Snapshot có hoạt động không? ILM có ép wait không? Beats có compatible không? Đa số setup nhìn ngon trong 6 tháng đầu, tới khi cần upgrade ES 7.17 lên 8.13 thì lộ ra hàng loạt mapping deprecated, security setting không tương thích, plugin chưa build cho version mới.

Đây là bài 20 trong series Kibana từ A đến Z, đóng Part 5 Production Operations. Sau bài này bạn sẽ làm được:

  • Phân biệt 2 chiến lược: in-place rolling upgrade và cluster-swap
  • Đọc compatibility matrix Elastic 7.x/8.x
  • Chạy deprecation API và fix mapping trước khi upgrade
  • Viết runbook upgrade với downgrade plan rõ ràng
  • Tránh pitfall index version cũ không upgrade được

Phần 1: Hai chiến lược

Strategy 1: in-place rolling upgrade

Áp dụng cho:

  • Minor version (7.16 lên 7.17, 8.12 lên 8.13).
  • Cluster nhỏ tới vừa (3-15 node).
  • Cluster cho phép single-node downtime trong vài phút.

Cách làm: stop từng node, upgrade binary, start lại. Cluster vẫn xanh nhờ replica.

Ưu: đơn giản, ít hardware. Nhược: nếu bug version mới thì rollback từng node, mất thời gian.

Strategy 2: cluster-swap (blue-green)

Áp dụng cho:

  • Major version (7.x lên 8.x).
  • Cluster lớn (over 20 node) hoặc business-critical.
  • Cần zero-downtime tuyệt đối.

Cách làm: dựng cluster mới (B) song song với cũ (A), dual-write log vào cả hai, copy data lịch sử, switch traffic, retire A.

Ưu: rollback nhanh (chỉ switch traffic ngược). Nhược: 2x hardware tạm thời, dual-write tốn ingest pipeline.

Phần 2: Pre-upgrade checklist (mọi version)

Trước mọi upgrade, qua từng dòng này. Đừng skip vì “trông ổn”:

  1. Snapshot fresh trong vòng 1 giờ trước upgrade. Lifecycle (bài 17) không tin cậy đủ ở thời điểm critical.
  2. Verify snapshot restore được trong cluster staging. Nếu chưa drill quý này, drill ngay.
  3. Chạy deprecation info API, fix mọi level: critical:
GET _migration/deprecations

Response liệt kê index/cluster setting/index template cần fix. Critical = block upgrade, không upgrade được nếu chưa fix.

  1. Check compatibility client: app dùng client SDK nào, version mới support không.
  2. Check plugin custom: plugin viết riêng phải rebuild cho ES version mới.
  3. Backup elasticsearch.yml, kibana.yml, certs, keystore sang storage độc lập.
  4. Check disk free: upgrade tạm thời cần thêm 20-30% disk cho merge plus replica resync.
  5. Notify stakeholders: maintenance window, expected downtime, contact on-call.
  6. Có downgrade plan: ghi rõ bước rollback nếu fail. Bài 17 snapshot là plan B.

Phần 3: Compatibility matrix

Elastic compatibility rule chính:

  • ES và Kibana cùng version (major.minor patch khớp). Lệch 1 minor được tạm thời (Kibana 8.13 tới ES 8.12 OK trong cửa sổ upgrade).
  • Beats phải cùng major với ES. Beats 7.x đẩy vào ES 8.x được nhưng có cảnh báo và một số feature mới không dùng được.
  • Elastic Agent plus Fleet: lệch tối đa 1 minor.
  • Snapshot restore: ES N có thể restore snapshot từ ES N-2 (2 major trở lên cũ). 6.x snapshot restore vào 8.x cần qua 7.x trung gian.

Bảng quick:

ChannelLệch tối đa
ES node với ES nodeKhông lệch khi upgrade rolling, lệch 1 minor trong cửa sổ
Kibana với ES1 minor (Kibana không bao giờ mới hơn ES)
Beats 7.x với ES 8.xOK với cảnh báo, không feature mới
Beats 8.x với ES 7.xKhông hỗ trợ
Snapshot ES NRestore vào ES N, N+1, N+2

Khuyến nghị: Kibana không bao giờ mới hơn ES. Upgrade ES trước, sau đó Kibana.

Phần 4: Minor upgrade in-place (workflow)

Ví dụ ES 8.12 lên 8.13 trên 3 node cluster.

Bước 1: disable shard allocation

PUT _cluster/settings
{
  "persistent": {
    "cluster.routing.allocation.enable": "primaries"
  }
}

Chỉ allocate primary, không relocate replica khi node tạm thiếu. Tránh rebalance tốn I/O.

Bước 2: synced flush plus stop machine learning

POST _flush
POST _ml/set_upgrade_mode?enabled=true

ML job pause để không corrupt khi restart.

Bước 3: stop node 1

sudo systemctl stop elasticsearch

Bước 4: upgrade binary

Tuỳ install method:

# RPM
sudo dnf install elasticsearch-8.13.0

# DEB
sudo apt-get install elasticsearch=8.13.0

# Tar
tar -xzf elasticsearch-8.13.0-linux-x86_64.tar.gz
# Migrate config, certs, plugins

Verify config elasticsearch.yml không có setting deprecated. Một số setting bị rename giữa minor.

Bước 5: start node, verify rejoin

sudo systemctl start elasticsearch
GET _cat/nodes?v
# Node 1 phải xuất hiện với version 8.13.0

Bước 6: enable allocation

PUT _cluster/settings
{
  "persistent": {
    "cluster.routing.allocation.enable": null
  }
}
GET _cluster/health
# wait_for_status: green

Bước 7: repeat cho node 2, 3

Mỗi lần chỉ 1 node down. Cluster green xong mới sang node tiếp.

Bước 8: upgrade Kibana

sudo systemctl stop kibana
sudo apt-get install kibana=8.13.0
sudo systemctl start kibana

Kibana có thể có multiple instance. Upgrade từng cái sau load balancer.

Bước 9: re-enable ML

POST _ml/set_upgrade_mode?enabled=false

Bước 10: smoke test

  • Login Kibana: OK
  • Discover query: OK
  • Dashboard load: OK
  • Index doc qua API: OK
  • ILM rotate: verify _ilm/explain
  • Alert rule fire: verify connector

Mọi item OK thì upgrade hoàn tất. Total time cho cluster 3 node: 30-60 phút.

Phần 5: Major upgrade cluster-swap (workflow)

ES 7.17 lên 8.13 là major. In-place rolling vẫn được nhưng có nhiều thay đổi breaking. Cluster-swap an toàn hơn cho production lớn.

Bước 1: dựng cluster B song song

Cluster B chạy ES 8.13 + Kibana 8.13 từ đầu. Hạ tầng riêng, chung mạng.

Bước 2: dual-write log

App ingest đẩy vào cả A và B. Cách thực hiện tuỳ pipeline:

  • Vector có [sinks.es_b] thêm vào, output bản sao.
  • Logstash có thêm output block.
  • Beats không native dual output, dùng 2 instance hoặc proxy ngoài.

Đảm bảo: từ thời điểm dual-write bật, data mới có ở cả hai.

Bước 3: copy data lịch sử

Snapshot từ cluster A, restore vào cluster B. Bài 17 đã cover:

# Trên cluster A
PUT _slm/policy/migration-snapshot
{ ... }

# Trên cluster B
PUT _snapshot/migration-repo
{ "type": "s3", "settings": { ..., "readonly": true } }

POST _snapshot/migration-repo/<snapshot-name>/_restore
{
  "indices": "*",
  "include_global_state": false
}

Chỉ restore index data, không restore .kibana_* (saved object) lúc đầu. Saved object có thể incompatible giữa major version.

Bước 4: import saved object thủ công

Export saved object từ cluster A qua API _export, import vào B qua _import. Bài 1 đã cover workflow này. Trong quá trình import:

  • Kibana 8 sẽ auto migrate format saved object cũ.
  • Báo cáo conflict nếu có ID trùng.
  • Resolve manual nếu cần.

Bước 5: validate cluster B

Trên cluster B:

  • Doc count khớp với A (tolerance vài phần nghìn cho gap dual-write).
  • Dashboard render OK.
  • Alert rule fire đúng.
  • Performance baseline tương đương hoặc tốt hơn.

Bước 6: switch traffic

Đổi DNS hoặc reverse proxy upstream từ cluster A sang B. Có nhiều cách:

  • DNS CNAME (TTL low). Đổi nhanh nhưng có cache.
  • Nginx upstream block, reload config. Đổi tức thời.
  • Load balancer (AWS ALB target group). Đổi tức thời.

Đề xuất: dùng reverse proxy/LB, không dùng DNS cho switch.

Bước 7: monitor 24-72h

Giữ cluster A còn sống (chưa shutdown) trong 1-3 ngày. Nếu phát hiện regression ở B, switch ngược lại A (DNS/upstream về A) là rollback hoàn tất.

Bước 8: retire cluster A

Sau confirm B ổn:

  • Tắt dual-write.
  • Snapshot cuối cùng từ A vào S3 archive.
  • Shutdown node A.
  • Xoá hardware.

Total time: 1-2 tuần (chạy song song để build confidence).

Phần 6: Deprecation thường gặp khi 7.x lên 8.x

_doc type không còn

ES 6.x dùng _type. ES 7.x deprecated, vẫn dùng được. ES 8.x bỏ hoàn toàn.

Fix: chỉnh ingest pipeline dùng endpoint /<index>/_doc/<id> thay vì /<index>/<type>/<id>. Beats default 7.x trở lên không gửi _type nữa, OK.

Cluster setting xpack.monitoring.collection.enabled

Đổi tên thành xpack.monitoring.enabled ở cluster setting (qua API), không còn ở elasticsearch.yml.

Mapping setting _all

Đã deprecated từ 6.x. ES 8 không nhận. Reindex tay cần thiết.

Type setting dynamic_templates match pattern

Một số pattern regex deprecated. Check qua _migration/deprecations cho cảnh báo cụ thể.

Security default-on

ES 8.x bật security default. Setup cũ disable security qua xpack.security.enabled: false sẽ vẫn chạy nhưng không khuyến nghị. Lên 8.x thì bật security đầy đủ luôn (bài 12 plus 19).

Kibana setting server.basePath hoạt động khác

Một số plugin path hard-code base path 7.x không tự re-write trong 8.x. Test reverse proxy plus sub-path kỹ.

Phần 7: Downgrade plan

Nguyên tắc: snapshot là plan rollback duy nhất an toàn.

ES 8.13 không thể downgrade thành 8.12 in-place. Index format có thể đã upgrade, không tương thích ngược.

Quy trình rollback major:

  1. Stop cluster B.
  2. Restore snapshot tiền upgrade lên cluster A (hoặc cluster A chưa retire nếu cluster-swap).
  3. Switch traffic về A.
  4. Báo cáo incident.

Rollback minor (in-place):

  1. Stop cluster.
  2. Restore snapshot (vì có thể có metadata change).
  3. Cài lại binary version cũ.
  4. Start cluster.

Cluster-swap ưu việt ở chỗ: rollback chỉ là switch traffic. Không cần restore snapshot trừ khi B đã ghi đè data shared.

Phần 8: Pitfall hay gặp

Pitfall 1: index quá cũ không upgrade được

Index tạo từ ES 6.x không tương thích trực tiếp ES 8.x. Phải reindex qua 7.x trung gian:

# Trên cluster 7.x
POST _reindex
{
  "source": { "index": "old-index" },
  "dest": { "index": "old-index-reindexed" }
}

Sau đó index old-index-reindexed format 7.x, restore vào 8.x OK.

Hoặc đơn giản: nếu index cũ không còn cần, archive snapshot ra S3 plus xoá.

Pitfall 2: ILM stuck sau upgrade

Một số ILM step trong 7.x có behaviour khác 8.x. Step freeze deprecated, replaced bằng searchable snapshot. Policy có freeze action sau upgrade sẽ stuck.

Fix: update policy trước upgrade, gỡ action deprecated.

Pitfall 3: API key không tương thích

API key format giữa version có thể đổi. Sau upgrade, một số API key vẫn dùng được, một số fail. Rotate hết API key sau upgrade là an toàn.

Pitfall 4: Beats dùng API endpoint cũ

Beats 7.x gửi tới ES 8.x đôi khi gọi endpoint deprecated. Output có warning trong Beats log. Upgrade Beats sau khi ES upgrade ổn.

Pitfall 5: Kibana saved object migration fail giữa chừng

Migration saved object là 1-way. Nếu fail giữa chừng, một phần object format mới, một phần cũ. Khó rollback. Backup .kibana_* index trước plus restore nếu fail.

Setting an toàn:

migrations.batchSize: 1000
migrations.maxBatchSizeBytes: 100mb
migrations.retryAttempts: 15

Pitfall 6: certificate hết hạn giữa upgrade

Đang upgrade thì cert expire = node không rejoin. Tránh upgrade vào thời điểm cert sắp hết hạn. Rotate cert (bài 19) ít nhất 30 ngày trước upgrade window.

Phần 9: Mẫu runbook upgrade

ELK Upgrade Runbook 8.12 -> 8.13

[ ] T-7d: announce maintenance window
[ ] T-3d: run _migration/deprecations, fix critical
[ ] T-1d: snapshot test restore in staging
[ ] T-1h: fresh snapshot, verify on s3
[ ] T-30m: disable downstream pagers (will fire false during)
[ ] T0: disable shard allocation
[ ] T0: stop ml upgrade mode on
[ ] T+5m: stop es-node-1
[ ] T+10m: upgrade es-node-1 binary, start
[ ] T+15m: cluster green, repeat es-node-2
[ ] T+30m: cluster green, repeat es-node-3
[ ] T+45m: upgrade kibana
[ ] T+60m: smoke test full flow
[ ] T+90m: re-enable allocation, ml, alerts
[ ] T+120m: announce success or rollback trigger
[ ] T+24h: post-upgrade review meeting

Mỗi mục tick xong khi xác nhận thực tế, không phải plan.

Cheatsheet

ViệcCách
Check deprecationGET /_migration/deprecations
Disable allocationcluster.routing.allocation.enable: primaries
Pause MLPOST /_ml/set_upgrade_mode?enabled=true
Synced flushPOST /_flush
Health checkGET /_cluster/health plus wait_for_status
Rolling restartStop, upgrade binary, start, verify rejoin
Cluster-swapDual-write plus snapshot restore plus switch traffic
Snapshot restore versionN có thể đọc N-2
Reindex 6.x cho 8.xQua trung gian 7.x
Kibana migration settingmigrations.batchSize, retryAttempts

Lời kết

Upgrade ELK không khó nếu chuẩn bị kỹ. Đa số sự cố không phải bug, mà là quên check một deprecation, hoặc tự tin skip snapshot. Hai nguyên tắc bám sát: snapshot là plan rollback duy nhất; major upgrade thì cluster-swap an toàn hơn in-place. Lập runbook trước, chạy theo runbook, không ad-hoc.

Đây là bài cuối của Part 5 Production Operations. Series Kibana từ A đến Z tiếp theo sẽ qua Part 6 Integration plus Automation: log shipper comparison, dashboard-as-code workflow, Terraform plus Kibana, API automation. Phần này biến mọi thiết kế đã làm ở Part 1-5 thành infrastructure-as-code đi qua CI/CD, không phụ thuộc click-tay nữa.