天天看点

安装 Kibana(本地及 Docker)- Elastic Stack 实战手册

安装 Kibana(本地及 Docker)- Elastic Stack 实战手册
https://developer.aliyun.com/topic/download?id=1295 · 更多精彩内容,请下载阅读全本《Elastic Stack实战手册》 https://developer.aliyun.com/topic/download?id=1295 https://developer.aliyun.com/topic/es100 · 加入创作人行列,一起交流碰撞,参与技术圈年度盛事吧 https://developer.aliyun.com/topic/es100

创作人:陈晨

审稿人:刘帅

环境准备

Kibana 是一个基于 Nodejs 构建出来的前端项目,它本身不包含数据存储功能,所以需要配合一个 Elasticsearch 节点/集群一起进行使用。本节将从系统环境的选择,必须的基础应用的安装等方面进行阐述。

环境选择策略

  1. 操作系统
    1. 由于 Kibana 不能独立存在,需要绑定一个Elasticsearch 节点/集群,所以本文主要会以一个

      CentOS7

      系统来承载它配套的 Elasticsearch 节点。我们也将介绍其它常用操作系统的安装。
    2. Kibana 可以支持的系统和 Elasticsearch 类似,可以大致认为支持 Elasticsearch 的系统都可以承载 Kibana。
  2. 内存、CPU
    1. Kibana 是一个前端系统,绑定的 Elasticsearch 可以认为是它用来存取数据用的数据库,所以不需要特别高的配置。
    2. 本文将以一个最小能够顺畅运行 Elasticsearch 节点的配置进行描述(

      1C2G

实际系统配置

Kibana 的系统安装、配置和上一节 Elasticsearch 的安装一致,本节将不在赘述,只保留最简初始化方式。

  1. 修改源并安装必要工具
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
        -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
        -i.bak \
        /etc/yum.repos.d/CentOS-Base.repo &&  \
        yum makecache && \
        yum update -y && \
        yum install -y epel-release && \
        yum install -y curl wget htop unzip && \
        yum install -y docker docker-compose
           
  1. 开启

    docker

    服务
    1. systemctl start docker

    2. systemctl enable docker

小结

本节对 Kibana 节点部署所需环境对选择策略、必备软件等方面进行了阐述。

下载、安装及启动

本节将对几个主流的 Kibana 安装部署的方式进行阐述,会对节点的安装、部署、启动、停机等流程进行详细描述。

tar 包安装

下载链接(后面以

KIBANA_DOWNLOAD_URL

指代):

  1. https://artifacts.elastic.co/downloads/kibana/kibana-7.10.0-linux-x86_64.tar.gz

下载并解压:

  1. mkdir -p /usr/local/kibana

  2. cd /usr/local/kibana

  3. wget -c ${KIBANA_DOWNLOAD_URL}

  4. tar vxf kibana-7.10.0-linux-x86_64.tar.gz

解压出来的文件:

[elasticsearch@esteam7002 kibana]$ ls -ltr kibana-7.10.0-linux-x86_64
总用量 1324
-rw-rw-r--   1 elasticsearch elasticsearch    3968 11月 10 06:16 README.txt -> 项目说明文档
drwxrwxr-x   2 elasticsearch elasticsearch    4096 11月 10 06:16 plugins -> 插件文件夹,目前为空,自定义插件会放置在这里
-rw-rw-r--   1 elasticsearch elasticsearch     740 11月 10 06:16 package.json -> 项目打包文件
-rw-rw-r--   1 elasticsearch elasticsearch 1263836 11月 10 06:16 NOTICE.txt -> 一些协议的说明以及违反后果的警告
-rw-rw-r--   1 elasticsearch elasticsearch   13675 11月 10 06:16 LICENSE.txt -> 协议
drwxrwxr-x   2 elasticsearch elasticsearch    4096 11月 10 06:16 data -> Kibana 和它的插件写本地文件的文件夹
drwxrwxr-x   2 elasticsearch elasticsearch    4096 4月  29 15:11 bin -> Kibana 内置命令行工具
drwxrwxr-x   4 elasticsearch elasticsearch    4096 4月  29 15:11 x-pack
drwxrwxr-x  11 elasticsearch elasticsearch    4096 4月  29 15:11 src
drwxrwxr-x   6 elasticsearch elasticsearch    4096 4月  29 15:11 node
drwxrwxr-x 995 elasticsearch elasticsearch   36864 4月  29 15:11 node_modules
drwxrwxr-x   2 elasticsearch elasticsearch    4096 4月  29 15:12 config -> 配置文件目录
           

修改配置文件

${KIBANA_HOME}/config/kibana.yml

  1. 添加 Elasticsearch 访问地址:

    elasticsearch.hosts: ["http://localhost:9200"]

服务启动

  1. 通过命令

    ./bin/kibana

    启动
  2. kibana 不像 ES 有直接的后台运行参数,只能通过

    nohup

    配合

    &

    的方式后台运行
  3. 完整命令:

    nohup ./bin/kibana > kibana.log 2>&1 &

  4. 出现类似以下命令则代表启动完成
{"type":"log","@timestamp":"2021-04-29T07:21:08Z","tags":["info","plugins-service"],"pid":13089,"message":"Plugin \"visTypeXy\" is disabled."}
...中间省略...在配套的ES 中创建了一些Kibana 自己用的索引...
{"type":"log","@timestamp":"2021-04-29T07:21:11Z","tags":["info","plugins","watcher"],"pid":13089,"message":"Your basic license does not support watcher. Please upgrade your license."}
{"type":"log","@timestamp":"2021-04-29T07:21:11Z","tags":["info","plugins","monitoring","monitoring","kibana-monitoring"],"pid":13089,"message":"Starting monitoring stats collection"}
{"type":"log","@timestamp":"2021-04-29T07:21:12Z","tags":["listening","info"],"pid":13089,"message":"Server running at http://localhost:5601"}
{"type":"log","@timestamp":"2021-04-29T07:21:13Z","tags":["info","http","server","Kibana"],"pid":13089,"message":"http server running at http://localhost:5601"} -> 开启服务并可通过以下域名进行访问 http://localhost:5601
{"type":"log","@timestamp":"2021-04-29T07:21:15Z","tags":["warning","plugins","reporting"],"pid":13089,"message":"Enabling the Chromium sandbox provides an additional layer of protection."}
           
  1. 在浏览器里通过地址

    http://${node_ip}:5601

    进行访问

服务停机

  1. Kibana 的进程是一个 nodejs 服务,所以不能像 Elasticsearch 一样通过

    ps -ef | grep kibana

    的方式获取进程 id
  2. 只能通过命令

    netstat

    ,通过查找监听端口的方式来找到

    kibana

    对应的

    pid

    并进行

    kill

    操作
    1. netstat -anp | grep 5601 | awk '{ print $7 }' | cut -d '/' -f 1 | xargs kill -15

rpm 包安装

KIBANA_DOWNLOAD_URL

  1. https://artifacts.elastic.co/downloads/kibana/kibana-7.10.0-x86_64.rpm

下载并安装:

  1. 切换到 root 账户(否则无法进行安装)

    sudo -i

  2. mkdir -p /usr/local/kibana

  3. cd /usr/local/kibana

  4. wget -c ${KIBANA_DOWNLOAD_URL}

  5. rpm -ivh kibana-7.10.0-x86_64.rpm

安装成功日志

  1. 日志会根据当前操作系统的语言而显示不同的语言提示,本示例系统为中文
[root@esteam7002 kibana]# rpm -ivh kibana-7.10.0-x86_64.rpm
警告:kibana-7.10.0-x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:kibana-7.10.0-1                  ################################# [100%]
           

/etc/kibana/kibana.yml

  1. elasticsearch.hosts: ["http://localhost:9200"]

  1. 确认自己处于

    root

    用户,否则命令需要加

    sudo

    前缀
  2. systemctl start kibana

  3. 没有报错表明节点启动完成
  4. http://${node_ip}:5601

  1. systemctl stop kibana

    停止服务
  2. 没有报错表明服务停机完成

Docker/docker-compose 安装

下载对应镜像

  1. docker pull kibana:7.10.1

(可选)如果目标机器无法上网,可以尝试通过其他机器下载并导入镜像

  1. 在宿主机下载镜像

    docker pull kibana:7.10.1

  2. 把镜像导出为文件

    docker save -o kibana-7.10.1-image.tar docker.io/kibana:7.10.1

  3. 把导出的文件拷贝到目标机器

    scp kibana-7.10.1-image.tar [email protected]:/tmp

  4. 登陆目标机器

    ssh [email protected]

  5. 导入目标镜像

    docker load < kibana-7.10.1-image.tar

镜像校验

  1. docker images

[root@esteam7002 kibana]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
kibana                    7.10.1              3e014820ee3f        5 months ago        992 MB
           
  1. 不太建议直接命令行启动,因为需要和 Elasticsearch 节点配置共通网络之类的事情
  2. 这里主要介绍通过 docker-compose 的方式进行管理
  3. vi docker-compose.yml

# 声明docker-compose版本,Mac等环境可以使用3,但是在一些Linux环境中只支持到2
   version: "2.2"

   # 声明节点使用的网络空间
   networks:
      bigdata:
         driver: bridge

   # 声明 Kibana 节点
   services:
      kibana:
         # kibana 版本要和 ES 相匹配,否则会报错甚至无法正常启动
         image: kibana:7.10.1
         container_name: kibana
         environment:
            # 如果 ES 节点和当前 kibana 节点在同一个 docker-compose 环境中
            # 可以直接写对应的 ES container_name,否则需要填完整的 URL
            ELASTICSEARCH_HOSTS: http://es01:9200
         depends_on:
            - es01
         ports:
            - 5601:5601
         networks:
            - bigdata
           

MacOS 环境

MacOS 系统其实是一个类 Unix 系统,所以大部分的命令、环境属性等都和 Unix 系统类似。

tar 包

安装和上文提到的,在 Linux 系统中基于 tar 包安装的过程大致一样。

  1. 需要注意的是,MacOS 作为一个较为特殊的操作系统,也会有一个专门针对 MacOS 系统编译出来的安装包
  2. 路径上的主要区别为 Linux 系统的安装包被标记了

    linux

    ,MacOS 的安装包被标记了

    darwin

  3. https://artifacts.elastic.co/downloads/kibana/kibana-7.10.2-darwin-x86_64.tar.gz

brew

安装

  1. Kibana 的 brew 安装和 Elasticsearch 类似,需要一些非常规的操作顺序
  2. brew tap elastic/tap

    将 Elastic 原厂的仓库地址加入 homebrew 的配置
brew tap elastic/tap
Updating Homebrew...
==> Tapping elastic/tap
Cloning into '/usr/local/Homebrew/Library/Taps/elastic/homebrew-tap'...
remote: Enumerating objects: 890, done.
remote: Counting objects: 100% (131/131), done.
remote: Compressing objects: 100% (104/104), done.
remote: Total 890 (delta 80), reused 57 (delta 26), pack-reused 759
Receiving objects: 100% (890/890), 206.46 KiB | 310.00 KiB/s, done.
Resolving deltas: 100% (666/666), done.
Tapped 17 formulae (51 files, 327.7KB).
           

brew install elastic/tap/kibana-full

进行安装

➜ ~ brew install elastic/tap/kibana-full
==> Installing kibana-full from elastic/tap
==> Downloading https://artifacts.elastic.co/downloads/kibana/kibana-7.12.1-darwin-x86_64.tar.gz?tap=elastic/homebrew-t
######################################################################## 100.0%
==> Caveats
Config: /usr/local/etc/kibana/ -> 配置文件路径
If you wish to preserve your plugins upon upgrade, make a copy of
/usr/local/opt/kibana-full/plugins before upgrading, and copy it into the
new keg location after upgrading.

To have launchd start elastic/tap/kibana-full now and restart at login:
brew services start elastic/tap/kibana-full
Or, if you don't want/need a background service you can just run:
kibana
==> Summary
🍺  /usr/local/Cellar/kibana-full/7.12.1: 53,439 files, 719.9MB, built in 1 minute 26 seconds
           

注意,通过常规方式安装

brew install elasticsearch

会安装

7.10.2

,通过命令

brew info elasticsearch

也可以得到相应的信息

  1. 还是建议通过原厂建议的

    brew info elasticsearch-full

    命令进行安装和测试,目前通过这个命令会安装 7.12 版本,和本文中其它的版本不太一致。
  2. 这些路径可能会因为系统的不同而有些许不同,也可能会有

    /Users/steven/working/sourcecode/homebrew/etc/kibana/

    等格式的情况

主要文件路径,也可以通过命令

brew info kibana-full

查看

➜ ~ brew info kibana-full
elastic/tap/kibana-full: stable 7.12.1
Analytics and search dashboard for Elasticsearch
https://www.elastic.co/products/kibana
Conflicts with:
kibana
/usr/local/Cellar/kibana-full/7.12.1 (53,439 files, 719.9MB) *
Built from source on 2021-05-15 at 15:33:02
From: https://github.com/elastic/homebrew-tap/blob/HEAD/Formula/kibana-full.rb
==> Caveats
Config: /usr/local/etc/kibana/
If you wish to preserve your plugins upon upgrade, make a copy of
/usr/local/opt/kibana-full/plugins before upgrading, and copy it into the
new keg location after upgrading.

To have launchd start elastic/tap/kibana-full now and restart at login:
brew services start elastic/tap/kibana-full
Or, if you don't want/need a background service you can just run:
kibana
           
  1. $KIBANA_HOME/config/kibana.yml

    (本例为:

    /usr/local/etc/kibana/config/kibana.yml

  2. 在里面配置 Elasticsearch 的 url,默认为

    http://localhost:9200

    或者

    http://127.0.0.1:9200

  3. 前台启动命令:

    ➜ ~ kibana

  4. 后台/作为系统服务启动:

    brew services start elastic/tap/kibana-full

  5. http://${node_ip}:5601

Windows OS 环境

Windows 的操作系统相对自成体系,所以在 Windows 平台中的安装主要为

.zip

包安装

  1. 下载地址:

    https://artifacts.elastic.co/downloads/kibana/kibana-7.10.2-windows-x86_64.zip

  2. 主要的安装流程和

    tar包

    安装类似,只是需要运行的是

    $KIBANA_HOME\bin\kibana.bat

    文件而非 Xnix 系统中的

    $KIBANA_HOME/bin/kibana

  3. Kibana 所需要的配置文件为

    $KIBANA_HOME\config\kibana.yml

本节列举了通过多种方式对 Kibana 节点进行最简安装、部署、停机等操作,希望读者可以找到适合自己的方式进行操作。

安全设置开启

Kibana 作为与 Elasticsearch 紧密相关的应用,在 Elasticsearch 开启了安全性认证的时候也需要相应的开启安全性认证。

  1. 在 Elasticsearch 集群中开启安全性设置
  2. 通过

    bin/elasticsearch-setup-passwords

    命令生成所需要账户的密码
  3. $KIBANA_HOME/config/kibana.yml

    kibana

    账号(7.x之后可能会是

    kibana_system

    账号)对应的密码配置进相关参数中
    1. elasticsearch.username: "kibana_system"

    2. elasticsearch.password: "password"

  4. 在配置好安全设置之后初次登陆时需要以管理员(

    elastic

    )账号进行登陆,并进行后续的配置和操作

各环境的配置方式参考

  1. rpm 包安装:

    /etc/kibana/kibana.yml

  2. tar 包安装:

    $KIBANA_HOME/config/kibana.yml

  3. docker 安装:
    1. -v

      参数将本地配置文件映射到 docker 节点里:

      docker run -v $KIBANA_CONFIG_PATH/kibana.yml:/usr/share/kibana/config/kibana.yml docker.io/kibana:7.10.1

    2. 修改

      docker-compose.yml

      文件里的

      environment

      配置
...
   environment:
      # 注意这几行
      ELASTICSEARCH_HOSTS: http://es01:9200
      ELASTICSEARCH_USERNAME: kibana
      ELASTICSEARCH_PASSWORD: kibana-password
...
           

MacOS:

  1. tar 包安装:同上
  2. brew 安装:

    brew info kibana-full

    中的配置地址,本例为:

    /usr/local/etc/kibana/config/kibana.yml

Windows:

  1. .zip 包安装:同上文中 tar包安装

本节中主要针对 Elasticsearch 集群开启了安全认证时,Kibana 需要进行的相关配置进行了阐述。

常见的参数优化

kibana 的功能已经较为完整,不太需要进行参数上的调整和优化,本节只探讨开启中文显示的方式。

  1. 同上节中的方式修改对应的配置文件
  2. 将参数

    i18n.locale

    设置成

    zh-CN

    • docker-compose.yml

      中对应的参数为

      I18N_LOCALE

常见问题及解决方案

本节将针对 Kibana 节点安装部署过程中经常遇到的一些问题进行分析,并提供一些简单的解决方案以供参考。

  1. Kibana should not be run as root. Use --allow-root to continue.

    1. Kibana 和 ES 一样,不能直接通过 root 账号启动
    2. 像提示中一样,可以通过添加参数

      --allow-root

      来启动
    3. ./bin/kibana --allow-root

  2. FATAL Error: Port 5601 is already in use. Another instance of Kibana may be running!

    1. 5601 端口已被占用
    2. 修复方式:
      1. netstat -anp | grep 5601

        命令寻找绑定5601 端口的进程
[root@esteam7002 ~]# netstat -anp | grep 5601
tcp6       0      0 :::5601                 :::*                    LISTEN      3480/docker-proxy-c
           
1. 根据进程信息来决定是否需要关闭已有进程
           
  1. 无法连接到 ES
log   [10:08:28.980] [error][elasticsearch][monitoring] Request error, retrying
GET http://localhost:9200/_xpack => connect ECONNREFUSED 127.0.0.1:9200
log   [10:08:28.993] [warning][elasticsearch][monitoring] Unable to revive connection: http://localhost:9200/
log   [10:08:28.994] [warning][elasticsearch][monitoring] No living connections
log   [10:08:28.995] [warning][licensing][plugins] License information could not be obtained from Elasticsearch due to Error: No Living connections error
log   [10:08:29.016] [warning][monitoring][monitoring][plugins] X-Pack Monitoring Cluster Alerts will not be available: No Living connections
log   [10:08:29.025] [error][data][elasticsearch] [ConnectionError]: connect ECONNREFUSED 127.0.0.1:9200
log   [10:08:29.059] [error][savedobjects-service] Unable to retrieve version information from Elasticsearch nodes.
log   [10:08:31.476] [error][data][elasticsearch] [ConnectionError]: connect ECONNREFUSED 127.0.0.1:9200
           
  1. Kibana 无法直接连接 Elasticsearch url,可能有以下原因
    1. 地址配置错误
    2. 当前节点和目标地址中间的网络不通
    3. Elasticsearch 配置的监听地址/端口有误
    1. 检查该地址/域名是否正确
    2. curl http://localhost:9200/

      命令测试一下当前节点是否能够正常的连接到目标地址
    3. 调整并调试到正确的连接
  2. 认证失败
log   [10:19:42.007] [error][data][elasticsearch] [security_exception]: missing authentication credentials for REST request [/_nodes?filter_path=nodes.*.version%2Cnodes.*.http.publish_address%2Cnodes.*.ip]
log   [10:19:42.042] [error][savedobjects-service] Unable to retrieve version information from Elasticsearch nodes.
log   [10:19:42.047] [warning][licensing][plugins] License information could not be obtained from Elasticsearch due to [security_exception] missing authentication credentials for REST request [/_xpack], with { header={ WWW-Authenticate="Basic realm=\"security\" charset=\"UTF-8\"" } } :: {"path":"/_xpack","statusCode":401,"response":"{\"error\":{\"root_cause\":[{\"type\":\"security_exception\",\"reason\":\"missing authentication credentials for REST request [/_xpack]\",\"header\":{\"WWW-Authenticate\":\"Basic realm=\\\"security\\\" charset=\\\"UTF-8\\\"\"}}],\"type\":\"security_exception\",\"reason\":\"missing authentication credentials for REST request [/_xpack]\",\"header\":{\"WWW-Authenticate\":\"Basic realm=\\\"security\\\" charset=\\\"UTF-8\\\"\"}},\"status\":401}","wwwAuthenticateDirective":"Basic realm=\"security\" charset=\"UTF-8\""} error
log   [10:19:42.050] [warning][monitoring][monitoring][plugins] X-Pack Monitoring Cluster Alerts will not be available: [security_exception] missing authentication credentials for REST request [/_xpack], with { header={ WWW-Authenticate="Basic realm=\"security\" charset=\"UTF-8\"" } }
log   [10:19:44.442] [error][data][elasticsearch] [security_exception]: missing authentication credentials for REST request [/_nodes?filter_path=nodes.*.version%2Cnodes.*.http.publish_address%2Cnodes.*.ip]
log   [10:19:46.941] [error][data][elasticsearch] [security_exception]: missing authentication credentials for REST request [/_nodes?filter_path=nodes.*.version%2Cnodes.*.http.publish_address%2Cnodes.*.ip]
           
  1. Kibana 未配置安全性设置,以至于无法正常连接 Elasticsearch 节点/集群
  2. 当 Elasticsearch 节点/集群开启了安全性设置之后,所有的 restful 访问都需要添加认证设置,包括 kibana 的访问
  3. (如果没有设置)在 Elasticsearch 集群中选一个节点,运行

    bin/elasticsearch-setup-passwords

  4. $KIBANA_HOME/config/kibana.yml

    kibana

    kibana_system

    1. `elasticsearch.username: "kibana_system"`
    
        1. `elasticsearch.password: "password"`
               
  5. elastic

  6. Error: Unable to find a match: docker-compose

    ,找不到

    docker-compose

    对应安装包
    1. 可能 yum 仓库中没有最新安装包信息或者精简版系统中没有对应的软件信息
      1. 把源文件中应用市场的地址替换成中科大
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-Base.repo

           
  1. 先安装

    epel-release

    (拓展应用市场)
  2. 再进行后续安装
  3. Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

    ,docker 进程没启动
    1. docker 安装之后不会自动启动,在未设置之前,服务器重启之后 docker 多半也不会自动重启
      1. 启动docker 进程

        systemctl start docker

      2. 设置docker 随系统启动

        systemctl enable docker

  4. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

    ,访问 docker 仓库失败
  5. 在某些节点中可能无法直接访问外网进行 docker 镜像的下载
  6. 1. 开启外网访问
    
    1. (或者)在其他能够访问外网的节点中下载对应镜像 `docker pull kibana:7.10.1`
    
    1. 把镜像导出为文件 `docker save -o kibana-7.10.1-image.tar docker.io/kibana:7.10.1`
    
    1. 把导出的文件拷贝到目标机器 `scp kibana-7.10.1-image.tar [email protected]:/tmp`
    
    1. 登陆目标机器 `ssh [email protected]`
    
    1. 导入目标镜像 `docker load < kibana-7.10.1-image.tar`
               
  7. Error response from daemon: manifest for kibana:7.9.11 not found: manifest unknown: manifest unknown

    ,找不到目标镜像
    1. 可能在 docker 仓库中找不到指定版本的镜像
    2. 登陆镜像仓库搜索合适版本(

      http://dockerhub.com/

    3. (或者)通过命令搜索合适的镜像

      docker search kibana

本节对 Kibana 部署和启动过程中常见的问题及参考解决方案进行了简要描述。

继续阅读