VPS Tutorials
Elasticsearch 9 and Kibana: private TLS, snapshots and recovery
Deploy Elasticsearch and Kibana 9.5.6 with Compose and Caddy. Protect private API ports and CA keys, scope application access, control heap and shards, and verify off-host snapshots and isolated restoration.

When website search, log investigation or product filtering outgrows simple LIKE queries, Elasticsearch provides inverted indexes, analyzers, relevance scoring, aggregations, geographic queries and vector search. Kibana supplies the interface for exploration, visualization and administration.
This single-node guide uses Ubuntu 24.04, Docker Compose and Caddy, with authenticated Elasticsearch HTTP TLS and loopback-only 9200/5601. The September 16, 2026 example used Elasticsearch and Kibana 9.5.3. The official release and Docker production documentation checked on November 16 use 9.5.6. For a new deployment, change the stack version to that reviewed patch before pulling, keeping Elasticsearch and Kibana aligned.
Keep authoritative business records in PostgreSQL/MySQL or another primary store, then index searchable data through applications, Logstash, Beats or CDC.
- Full-text search for websites, documents, products and knowledge bases.
- Centralized log queries, field aggregation and investigations.
- Autocomplete, fuzzy matching, synonyms and relevance ranking.
- Time-based analysis of metrics, events and security data.
- Hybrid keyword/vector search with filters.
For limited-resource container logging, Grafana Loki may be lighter. For agent management and SIEM rules, see Wazuh. Elasticsearch offers broad search/analytics capabilities with greater memory, storage and maintenance requirements.
| Architecture | Benefit | Limitation | Suitable use |
|---|---|---|---|
| Single node | Lower cost and simpler diagnosis | Host/disk failure interrupts service; no node redundancy | Development, internal tools and rebuildable indexes |
| Three nodes on one VPS | Tests cluster configuration | Shared failure domain and competing memory | Laboratory only |
| Three independent data nodes | Replicas can survive one node failure | More capacity, certificate and upgrade planning | Critical search, logs and security workloads |
This deployment uses discovery.type=single-node. Set single-node index replicas to 0 to avoid unassignable replicas; this creates no second data copy. Critical deployments need independent failure domains, a correctly designed master-eligible voting configuration, replicas and tested off-host snapshots.
Plan field counts, analyzers, aggregations, peak indexing, query concurrency, retention and replicas.
| Workload | Suggested VPS | Initial heap | Assumption |
|---|---|---|---|
| Development | 4 vCPU, 8GB RAM, 80GB SSD | 2GB | Low concurrency and rebuildable indexes |
| Small production search | 8 vCPU, 16GB RAM, at least 200GB NVMe | 4–6GB | Leave room for Kibana and filesystem cache |
| Critical/logging cluster | At least 3 hosts, 8 vCPU/16–32GB each, independent NVMe | Load-tested | Replicas, ILM and separate snapshot storage |
Keep heap at or below 50% of the container's available memory; smaller heap can leave more useful filesystem cache. A 16GB host does not justify -Xmx of 12GB when off-heap allocations, Kibana, Docker and Linux also need memory.
Estimate daily raw data × indexing expansion × retention days × (1 + replicas) × safety margin:
每日原始数据 × 索引膨胀系数 × 保留天数 × (1 + 副本数) × 安全余量
Expansion depends on mapping, _source, analyzers and field types. Measure a realistic sample. Keep sustained single-node disk use below roughly 70%–75% rather than waiting for flood-stage read-only blocks.
| Port | Function | Exposure |
|---|---|---|
443/TCP | Kibana HTTPS | Caddy |
5601/TCP | Kibana HTTP | 127.0.0.1 only |
9200/TCP | Authenticated Elasticsearch HTTPS | 127.0.0.1 only |
9300/TCP | Node transport | Unpublished |
The Elasticsearch API is not an administrative webpage to publish universally. Keep remote application access on restricted private networks or tunnels, with TLS and authentication. Check Docker port behavior, security groups and the middleware exposure checklist. Caddy may also need public 80 for HTTP ACME validation/redirects; retain restricted SSH.
Install Docker Engine and Compose v2 using the official Ubuntu instructions first:
sudo apt update
sudo apt full-upgrade -y
sudo apt install -y ca-certificates curl openssl caddy jq
docker --version
docker compose version
sudo systemctl enable --now caddy
Review package upgrades/reboots and configure log rotation, health checks and limits with the Compose production checklist.
The current Docker production requirement is vm.max_map_count=1048576:
printf 'vm.max_map_count=1048576\n' | \
sudo tee /etc/sysctl.d/99-elasticsearch.conf >/dev/null
sudo sysctl --system
sysctl vm.max_map_count
Set this on the host. Investigate swapping instead of masking insufficient RAM. bootstrap.memory_lock=true and memlock reduce swapping risk but do not supply missing memory.
sudo install -d -m 0750 -o "$USER" -g "$USER" /opt/elastic-stack
cd /opt/elastic-stack
sudo install -d -m 0770 -o 1000 -g 0 snapshots
umask 077
Generate administrator/system passwords and three stable Kibana encryption keys:
ELASTIC_PASSWORD="$(openssl rand -hex 32)"
KIBANA_PASSWORD="$(openssl rand -hex 32)"
SECURITY_KEY="$(openssl rand -hex 32)"
SAVED_OBJECTS_KEY="$(openssl rand -hex 32)"
REPORTING_KEY="$(openssl rand -hex 32)"
{
printf 'STACK_VERSION=9.5.3\n'
printf 'CLUSTER_NAME=vps-search\n'
printf 'ELASTIC_PASSWORD=%s\n' "$ELASTIC_PASSWORD"
printf 'KIBANA_PASSWORD=%s\n' "$KIBANA_PASSWORD"
printf 'KIBANA_HOST=search.example.com\n'
printf 'XPACK_SECURITY_KEY=%s\n' "$SECURITY_KEY"
printf 'XPACK_SAVED_OBJECTS_KEY=%s\n' "$SAVED_OBJECTS_KEY"
printf 'XPACK_REPORTING_KEY=%s\n' "$REPORTING_KEY"
} > .env
chmod 600 .env
Replace search.example.com with your domain and configure working DNS; publish AAAA only when IPv6 works. Keep .env private. The generation block overwrites this file and is for a fresh instance only; do not regenerate established passwords or encryption keys.
For the checked patch, change the generated version before first startup:
cd /opt/elastic-stack
sed -i 's/^STACK_VERSION=.*/STACK_VERSION=9.5.6/' .env
chmod 600 .env
The setup service creates the CA/node certificates and sets the kibana_system password after Elasticsearch becomes reachable. Create /opt/elastic-stack/compose.yml:
services:
setup:
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
user: "0"
volumes:
- certs:/usr/share/elasticsearch/config/certs
environment:
ELASTIC_PASSWORD: ${ELASTIC_PASSWORD}
KIBANA_PASSWORD: ${KIBANA_PASSWORD}
command: >
bash -c '
test -n "$${ELASTIC_PASSWORD}" || { echo "ELASTIC_PASSWORD is empty"; exit 1; };
test -n "$${KIBANA_PASSWORD}" || { echo "KIBANA_PASSWORD is empty"; exit 1; };
if [ ! -f config/certs/ca.zip ]; then
bin/elasticsearch-certutil ca --silent --pem -out config/certs/ca.zip;
unzip config/certs/ca.zip -d config/certs;
fi;
if [ ! -f config/certs/certs.zip ]; then
printf "instances:\n - name: es01\n dns:\n - es01\n - localhost\n ip:\n - 127.0.0.1\n" > config/certs/instances.yml;
bin/elasticsearch-certutil cert --silent --pem
-out config/certs/certs.zip
--in config/certs/instances.yml
--ca-cert config/certs/ca/ca.crt
--ca-key config/certs/ca/ca.key;
unzip config/certs/certs.zip -d config/certs;
fi;
chown -R root:root config/certs;
find config/certs -type d -exec chmod 750 {} \;;
find config/certs -type f -exec chmod 640 {} \;;
until curl -s --cacert config/certs/ca/ca.crt https://es01:9200 |
grep -q "missing authentication credentials"; do sleep 10; done;
until curl -s -X POST --cacert config/certs/ca/ca.crt
-u "elastic:$${ELASTIC_PASSWORD}"
-H "Content-Type: application/json"
https://es01:9200/_security/user/kibana_system/_password
-d "{\"password\":\"$${KIBANA_PASSWORD}\"}" |
grep -q "^{}"; do sleep 10; done;
touch config/certs/.setup-complete;
'
healthcheck:
test: ["CMD-SHELL", "test -f config/certs/ca/ca.crt"]
interval: 2s
timeout: 5s
retries: 60
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
container_name: es01
restart: unless-stopped
depends_on:
setup:
condition: service_healthy
ports:
- "127.0.0.1:9200:9200"
volumes:
- certs:/usr/share/elasticsearch/config/certs:ro
- esdata:/usr/share/elasticsearch/data
- ./snapshots:/mnt/snapshots
environment:
node.name: es01
cluster.name: ${CLUSTER_NAME}
discovery.type: single-node
ELASTIC_PASSWORD: ${ELASTIC_PASSWORD}
bootstrap.memory_lock: "true"
xpack.security.enabled: "true"
xpack.security.http.ssl.enabled: "true"
xpack.security.http.ssl.key: certs/es01/es01.key
xpack.security.http.ssl.certificate: certs/es01/es01.crt
xpack.security.http.ssl.certificate_authorities: certs/ca/ca.crt
path.repo: /mnt/snapshots
ES_JAVA_OPTS: -Xms2g -Xmx2g
mem_limit: 4g
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65535
hard: 65535
healthcheck:
test:
- CMD-SHELL
- curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q "missing authentication credentials"
interval: 10s
timeout: 10s
retries: 60
start_period: 60s
logging:
driver: json-file
options:
max-size: 10m
max-file: "5"
kibana:
image: docker.elastic.co/kibana/kibana:${STACK_VERSION}
container_name: kibana
restart: unless-stopped
depends_on:
setup:
condition: service_completed_successfully
es01:
condition: service_healthy
ports:
- "127.0.0.1:5601:5601"
volumes:
- certs:/usr/share/kibana/config/certs:ro
- kibanadata:/usr/share/kibana/data
environment:
SERVER_NAME: kibana
SERVER_PUBLICBASEURL: https://${KIBANA_HOST}
ELASTICSEARCH_HOSTS: '["https://es01:9200"]'
ELASTICSEARCH_USERNAME: kibana_system
ELASTICSEARCH_PASSWORD: ${KIBANA_PASSWORD}
ELASTICSEARCH_SSL_CERTIFICATEAUTHORITIES: config/certs/ca/ca.crt
XPACK_SECURITY_ENCRYPTIONKEY: ${XPACK_SECURITY_KEY}
XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY: ${XPACK_SAVED_OBJECTS_KEY}
XPACK_REPORTING_ENCRYPTIONKEY: ${XPACK_REPORTING_KEY}
mem_limit: 2g
healthcheck:
test:
- CMD-SHELL
- curl -s -I http://localhost:5601 | grep -q "HTTP/1.1 302"
interval: 10s
timeout: 10s
retries: 60
start_period: 60s
logging:
driver: json-file
options:
max-size: 10m
max-file: "5"
volumes:
certs:
esdata:
kibanadata:
Before starting, insert this line in the setup command immediately after its directory/file permission commands. The application containers share certificate storage and must not gain read access to the CA signing key or its archives:
chmod 600 config/certs/ca/ca.key config/certs/ca.zip config/certs/certs.zip
Keep the CA signing material in a separately encrypted recovery backup. The setup root process can still use it, while the non-root applications retain access to their 640-mode certificates. Review certificate handling again before adding nodes; this single-node HTTP-TLS configuration is not a complete multi-node transport-TLS setup.
Elasticsearch is limited to 4GB with 2GB heap, and Kibana to another 2GB. Start with at least 8GB host RAM and leave extra capacity for other services. Tune from measurements.
cd /opt/elastic-stack
docker compose config --quiet
docker compose pull
docker compose up -d
docker compose ps
docker compose logs --tail=100 setup es01 kibana
Load only a trusted .env and inspect the version and cluster:
set -a
. /opt/elastic-stack/.env
set +a
docker compose exec es01 curl -s \
--cacert config/certs/ca/ca.crt \
-u "elastic:$ELASTIC_PASSWORD" \
https://localhost:9200 | jq
docker compose exec es01 curl -s \
--cacert config/certs/ca/ca.crt \
-u "elastic:$ELASTIC_PASSWORD" \
'https://localhost:9200/_cluster/health?pretty'
Single-node yellow commonly means an unassignable replica. Use number_of_replicas: 0 for the intended single-node indexes rather than disabling health alarms; a red primary requires a separate investigation.
Create /etc/caddy/Caddyfile:
search.example.com {
encode zstd gzip
reverse_proxy 127.0.0.1:5601
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains"
X-Content-Type-Options "nosniff"
Referrer-Policy "strict-origin-when-cross-origin"
}
}
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
sudo journalctl -u caddy -n 50 --no-pager
Open https://search.example.com and use elastic with the password preserved in .env for initial administration. Keep 5601 private. Caddy handles HTTP and long-lived proxied connections; apply HSTS subdomain coverage only where all affected names support HTTPS.
elastic is a superuser, not an application credential. The following role reads catalog-*:
PUT /_security/role/catalog_reader
{
"cluster": ["monitor"],
"indices": [
{
"names": ["catalog-*"],
"privileges": ["read", "view_index_metadata"]
}
]
}
Run it in Kibana Dev Tools, then create a user with that role under Stack Management. The example also grants cluster monitoring, which reveals cluster-level information; omit that privilege if the application does not need it.
Prefer scoped, expiring application API keys. Their complete secret is returned at creation and belongs in restricted configuration or a secret manager. Create a separate catalog_writer with only required create_doc, index and mapping privileges. Never share elastic across services. Test allowed requests and denial of unrelated indexes.
Start with one primary shard and zero replicas for the single node:
PUT /_index_template/catalog_template
{
"index_patterns": ["catalog-*"],
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"refresh_interval": "5s"
},
"mappings": {
"dynamic": "strict",
"properties": {
"title": { "type": "text" },
"sku": { "type": "keyword" },
"price": { "type": "scaled_float", "scaling_factor": 100 },
"updated_at": { "type": "date" }
}
}
}
}
Strict mapping prevents unbounded fields and incorrect date/number/ID inference, but also rejects new fields until the mapping is deliberately updated. Templates apply to new matching indexes; existing indexes need separate settings/mapping changes or reindexing.
The general guidance of roughly 10–50GB and fewer than 200 million documents per shard is a planning range, not a guarantee. Measure query latency and restore speed. On independent nodes, add replicas and verify actual placement; raising the replica count on one host does not create HA.
Do not treat a tar copy of /usr/share/elasticsearch/data or the esdata volume as a reliable cluster backup. Use supported Snapshot and Restore:
PUT /_snapshot/local_fs
{
"type": "fs",
"settings": {
"location": "/mnt/snapshots",
"compress": true
}
}
Verify and create the snapshot:
POST /_snapshot/local_fs/_verify
PUT /_snapshot/local_fs/manual-2026-09-16?wait_for_completion=true
{
"include_global_state": true
}
The dated snapshot name is an example identifier, not the current execution date. Choose a unique name for each manual backup and check SUCCESS with no failed shards. Snapshots work while the cluster runs and deduplicate immutable segments.
/opt/elastic-stack/snapshots shares the VPS failure domain. For an off-host repository copy, prevent all repository writes or take an atomic filesystem snapshot before copying; ordinary copying during updates can corrupt the copy. Prefer supported S3/GCS/Azure repositories and SLM for production scheduling and retention. Do not edit repository contents manually.
Back up Compose, the environment, Kibana encryption keys, certificate/configuration material and any plugins separately with encryption. A cluster snapshot does not preserve those host files.
Use an isolated compatible cluster, mount a complete repository copy and register it read-only. Validate repository access before restoring application indexes. This request avoids overwriting existing names and excludes global/security state:
POST /_snapshot/local_fs/YOUR_SNAPSHOT/_restore
{
"indices": "catalog-*",
"include_global_state": false,
"feature_states": ["none"],
"rename_pattern": "(.+)",
"rename_replacement": "restore-$1"
}
Replace the snapshot identifier. Verify document counts, representative queries, mappings, aliases and index health. A complete disaster-recovery exercise must additionally plan Kibana saved objects and security feature-state recovery; restoring security state can replace users and credentials, so retain emergency access. Never test a destructive restore on production.
- Cluster
status, unassigned shards and node count. - Heap use and GC frequency/pauses.
- Search/index latency and thread-pool rejections.
- Documents, shard sizes and field growth.
- Disk/I/O and low/high/flood-stage thresholds.
- Kibana availability, login failures and external Caddy checks.
Use Prometheus and Grafana for host/container signals. Single-node green means allocation is healthy now, not that redundancy or disaster recovery exists.
GET /_cluster/health
GET /_cat/nodes?v&h=name,heap.percent,ram.percent,cpu,load_1m,disk.used_percent
GET /_cat/indices?v&s=store.size:desc
GET /_cat/shards?v&s=store:desc
GET /_cluster/allocation/explain
A single node needs downtime. Read release notes, check plugins and create a tested compatible snapshot. Elasticsearch does not support node downgrades; recovery generally requires an empty cluster on the pre-upgrade version and its pre-upgrade snapshot.
cd /opt/elastic-stack
set -a
. ./.env
set +a
docker compose exec es01 curl -s \
--cacert config/certs/ca/ca.crt \
-u "elastic:$ELASTIC_PASSWORD" \
'https://localhost:9200/_cluster/health?pretty'
docker compose pull
docker compose up -d
docker compose ps
docker compose logs --tail=100 es01 kibana
Change STACK_VERSION in .env first and keep Kibana aligned. The same-tag pull alone does not upgrade. For older 8.x deployments, first reach the supported latest 8.19 release, use Upgrade Assistant and address old indexes/breaking changes before following the target 9.x path. Cross-major changes require the complete official procedure.
Check the host vm.max_map_count:
sysctl vm.max_map_count
cat /etc/sysctl.d/99-elasticsearch.conf
Target 1048576, run sudo sysctl --system and restart Elasticsearch.
cd /opt/elastic-stack
docker compose ps
docker compose logs --tail=200 setup es01 kibana
Check setup finished setting kibana_system, Kibana trusts ca.crt, versions match and Elasticsearch is healthy.
Trust the CA in certs, not a permanent -k bypass:
docker compose exec es01 curl -s \
--cacert config/certs/ca/ca.crt \
https://localhost:9200
Without credentials, missing authentication credentials indicates the TLS/HTTP layer is reachable, not authenticated cluster health.
Inspect _cat/shards. If only single-node replicas are unassigned, adjust number_of_replicas to 0 in templates and existing intended indexes. Never remove production-cluster replicas merely to make the indicator green.
Resolve flood-stage disk pressure first. Add capacity or delete only unneeded data; once below safe thresholds, verify automatic block removal or clear index.blocks.read_only_allow_delete according to the supported procedure. Clearing the block alone will not solve a full disk.
curl -I http://127.0.0.1:5601
sudo caddy validate --config /etc/caddy/Caddyfile
sudo journalctl -u caddy -n 100 --no-pager
If loopback works, inspect DNS, 80/443 security-group/UFW rules and the external-access troubleshooting guide.
For testing, plan 4 vCPU, 8GB RAM and 80GB SSD. Running Kibana as well makes 4GB tight. Small production commonly needs 16GB/NVMe and realistic load tests.
Choose based on licensing, features, plugins, client compatibility and maintenance experience. OpenSearch has its own distribution/Dashboards; do not mix its plugins or upgrade packages with Elasticsearch.
One configured replica has nowhere to go. Zero replicas fixes that allocation condition and means no node redundancy.
Keep it on 127.0.0.1. Remote clients should use restricted VPN/private access or SSH tunnels, with TLS, authentication and least privilege.
No supported cluster backup is obtained that way. Use snapshots, keep repositories off-host and rehearse recovery.
Do not rely on it. Recover a compatible pre-upgrade snapshot into an empty cluster on the appropriate earlier version.