💡
原文英文,约300词,阅读约需1分钟。
📝
内容提要
本文介绍了如何在Docker中设置Elasticsearch和Grafana进行日志监控,包括创建共享网络、启动容器、检查连接及配置数据源。如果遇到问题,可检查Elasticsearch的索引和日志。
🎯
关键要点
- 前提条件:安装并配置Docker(使用wsl和ubuntu)。
- 创建共享网络:使用命令'docker network create monitoring'。
- 启动Elasticsearch容器:使用命令'docker run -d --name=elasticsearch --network=monitoring -p 9200:9200 ...'。
- 检查Elasticsearch是否运行:使用命令'curl -X GET "http://localhost:9200"'。
- 启动Grafana容器:使用命令'docker run -d --name=grafana --network=monitoring -p 3003:3000 grafana/grafana'。
- 访问Grafana:使用http://localhost:3003,默认用户名和密码均为'admin'。
- 检查Grafana是否能连接到Elasticsearch:使用命令'docker exec -it grafana sh'和'curl -X GET "http://elasticsearch:9200"'。
- 如果DNS未解析,检查共享网络或使用Elasticsearch的IP。
- 在Grafana中配置Elasticsearch:通过界面添加数据源,填写连接信息。
- 确保先运行API以生成日志并注册索引。
- 在Grafana中查看日志:前往'Explore'。
- 如遇问题,检查Elasticsearch中的可用索引:使用命令'curl -X GET "http://localhost:9200/_cat/indices?v"'。
- 查看特定索引配置:使用命令'curl -X GET "http://localhost:9200/<api-index-name>/_mapping?pretty"'。
- 检查日志是否已注册:访问'http://localhost:9200/_all/_search?pretty'。
❓
延伸问答
如何在Docker中创建共享网络?
使用命令'docker network create monitoring'来创建共享网络。
如何启动Elasticsearch容器?
使用命令'docker run -d --name=elasticsearch --network=monitoring -p 9200:9200 ...'来启动Elasticsearch容器。
如何检查Elasticsearch是否运行?
可以使用命令'curl -X GET "http://localhost:9200"'来检查Elasticsearch是否运行。
如何启动Grafana容器并访问?
使用命令'docker run -d --name=grafana --network=monitoring -p 3003:3000 grafana/grafana'启动Grafana容器,然后访问http://localhost:3003。
如何在Grafana中配置Elasticsearch数据源?
在Grafana中,前往配置 -> 数据源 -> 添加数据源,填写连接信息,URL为'http://elasticsearch:9200'。
如果Grafana无法连接到Elasticsearch,该怎么办?
检查共享网络或使用Elasticsearch的IP地址代替DNS进行连接。
🏷️
标签
➡️