近期專案需求,需要使用到Log工具,在效能、快速搭建、費用的考量下,決定使用Gray & Opensearch
version: '3'
services:
opensearch:
image: "opensearchproject/opensearch:2.15.0"
environment:
- "OPENSEARCH_JAVA_OPTS=-Xms1g -Xmx1g"
- "bootstrap.memory_lock=true"
- "discovery.type=single-node"
- "path.repo=/home/mmsd/graylog" # 配置快照路徑
- "plugins.security.disabled=true"
ulimits:
memlock:
hard: -1
soft: -1
ports:
- "9203:9200"
- "9303:9300"
volumes:
- "opensearch:/usr/share/opensearch/data" # 數據卷
- "/path/on/host/graylog:/home/mmsd/graylog" # 快照存儲路徑
volumes:
opensearch:
docker compose up -d
curl -X PUT "http://localhost:9203/_snapshot/my_backup" -H 'Content-Type: application/json' -d'
{
"type": "fs",
"settings": {
"location": "/home/mmsd/graylog",
"compress": true
}
}'
curl -X PUT "http://localhost:9203/_snapshot/my_backup/snapshot_1"
curl -X GET "http://localhost:9203/_snapshot/my_backup?pretty"
/path/on/host/graylog
目錄中能看到生成的快照文件ls /path/on/host/graylog
curl -X PUT "http://localhost:9203/_snapshot/my_backup/graylog_backup_$(date +%Y%m%d%H%M%S)?wait_for_completion=true" -H 'Content-Type: application/json' -d'
{
"indices": "graylog_0",
"ignore_unavailable": true,
"include_global_state": false
}'
curl -X DELETE "http://localhost:9203/graylog_0?pretty"
curl -X POST "http://localhost:9203/graylog_0/_close?pretty"
curl -X POST "http://localhost:9203/_snapshot/my_backup/graylog_backup/_restore" -H 'Content-Type: application/json' -d'
{
"indices": "graylog_0"
}'
curl -X POST "http://localhost:9203/graylog_0/_open?pretty"
#!/bin/bash
# 備份索引
curl -X PUT "http://localhost:9203/_snapshot/my_backup/backup_$(date +%Y%m%d)?wait_for_completion=true" -H 'Content-Type: application/json' -d'
{
"indices": "graylog_*",
"ignore_unavailable": true,
"include_global_state": false
}'
# 刪除索引
curl -X DELETE "http://localhost:9203/graylog_0?pretty"
# 日誌
echo "$(date): Backup and deletion completed." >> /var/log/opensearch_backup.log
crontab -e
0 2 * * * /path/to/backup_script.sh
排查與解決方式
由於你使用的是 bind
挂载 /path/on/host/graylog:/home/mmsd/graylog
,宿主機的目錄權限可能限制了 OpenSearch 的操作。
執行以下命令,檢查該目錄的所有者與權限:
ls -ld /path/on/host/graylog
假設輸出類似以下內容:
drwxr-xr-x 2 root root 4096 Nov 21 14:00 /path/on/host/graylog
可以看到,該目錄歸屬於 root
,而 Docker 容器內的 OpenSearch 默認運行用戶不是 root
,因此無法寫入。
給該目錄賦予容器的用戶寫入權限:
docker exec -it <opensearch_container_id> id
輸出類似:
uid=1000 gid=1000(opensearch)
在宿主機執行:
sudo chown -R 1000:1000 /path/on/host/graylog
權限修改後,重新啟動容器:
docker compose restart opensearch
再次執行 API 請求來創建快照存儲庫:
curl -X PUT "http://localhost:9203/_snapshot/my_backup" -H 'Content-Type: application/json' -d'
{
"type": "fs",
"settings": {
"location": "/home/mmsd/graylog",
"compress": true
}
}'