|
| 1 | +# ELK(Elasticsearch、Logstash、Kibana)安装和配置 |
| 2 | + |
| 3 | + |
| 4 | +## 本机环境 |
| 5 | + |
| 6 | +- 两台机子CPU 1 核,内存 4G |
| 7 | + - 192.168.1.126 |
| 8 | + - 192.168.1.127 |
| 9 | +- 系统:CentOS 7.3 64 位 |
| 10 | +- 依赖环境:JDK 1.8,所在目录:`/usr/program/jdk1.8.0_121` |
| 11 | + |
| 12 | + |
| 13 | +## 说明 |
| 14 | + |
| 15 | + |
| 16 | +- 官网:<https://www.elastic.co/> |
| 17 | +- 官网总文档:<https://www.elastic.co/guide/index.html> |
| 18 | +- 官网最终指南:<https://www.elastic.co/guide/en/elasticsearch/guide/current/administration.html#administration> |
| 19 | +- 此时(201703)最新版本:**5.2**,但是我在使用过程中有很多坑,暂时又退回到 **2.X** |
| 20 | +- 官网对各个系统的支持列表:<https://www.elastic.co/support/matrix> |
| 21 | +- 5.2 版本有一个设置的新特性必须了解,测试建议我们用 CentOS 7:<https://www.elastic.co/guide/en/elasticsearch/reference/5.x/breaking-changes-5.2.html#_system_call_bootstrap_check> |
| 22 | +- Elasticsearch 开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful 风格接口,多数据源,自动搜索负载等。 |
| 23 | +- Logstash 日志进行收集、分析,并将其存储供以后使用(如,搜索) |
| 24 | +- kibana 为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。 |
| 25 | + |
| 26 | + |
| 27 | +## 2.4.X |
| 28 | + |
| 29 | +### 安装 elasticsearch 集群 |
| 30 | + |
| 31 | +### 下载 |
| 32 | + |
| 33 | +- 下载在我个人习惯的子自己创建的目录下:/usr/program/elk |
| 34 | +- elasticsearch 2.4.1(26 M):`wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-2.4.1.tar.gz` |
| 35 | +- logstash 2.4.0(80 M):`wget https://download.elastic.co/logstash/logstash/logstash-2.4.1.tar.gz` |
| 36 | +- kibana 4.6.1(32 M):`wget https://download.elastic.co/kibana/kibana/kibana-4.6.1-linux-x86_64.tar.gz` |
| 37 | + |
| 38 | +### tar 解压安装 |
| 39 | + |
| 40 | +- **确保系统安装有 JDK** |
| 41 | +- 官网文档:<https://www.elastic.co/guide/en/elasticsearch/reference/5.2/zip-targz.html> |
| 42 | +- 添加日志存放目录、数据存放目录:`mkdir -p /opt/elasticsearch/data /opt/elasticsearch/log` |
| 43 | +- 添加组和用户 |
| 44 | + - 该版本不能使用 root 用户进行使用 |
| 45 | + - `useradd elasticsearch -p 123456`,添加一个名为 elasticsearch 的用户,还有一个同名的组 |
| 46 | +- 解压下载的文件 |
| 47 | + - `cd /usr/program/elk` |
| 48 | + - `tar zxvf elasticsearch-2.4.1.tar.gz` |
| 49 | +- 赋权限: |
| 50 | + - `chown -R elasticsearch:elasticsearch /usr/program/elk /opt/elasticsearch` |
| 51 | +- 我 tar 安装后一些路径说明: |
| 52 | + - home:`/usr/program/elk/elasticsearch-2.4.1` |
| 53 | + - bin:`/usr/program/elk/elasticsearch-2.4.1/bin` |
| 54 | + - 配置文件:`/usr/program/elk/elasticsearch-2.4.1/config/elasticsearch.yml` |
| 55 | + - plugins:`/usr/program/elk/elasticsearch-2.4.1/plugins` |
| 56 | + - script:`/usr/program/elk/elasticsearch-2.4.1/scripts` |
| 57 | + - data:`/opt/elasticsearch/data` |
| 58 | + - log:`/opt/elasticsearch/log/集群名称.log` |
| 59 | +- 编辑配置文件:`vim /usr/program/elk/elasticsearch-2.4.1/config/elasticsearch.yml`,打开下面注释,并修改 |
| 60 | + |
| 61 | +``` nginx |
| 62 | +cluster.name: gitnavi-cluster |
| 63 | +node.name: gitnavi-node-1 |
| 64 | +path.data: /opt/elasticsearch/data |
| 65 | +path.logs: /opt/elasticsearch/log |
| 66 | +bootstrap.memory_lock: true |
| 67 | +network.host: 0.0.0.0 # 也可以是本机 IP |
| 68 | +http.port: 9200 |
| 69 | +discovery.zen.ping.multicast.enabled: false |
| 70 | +discovery.zen.ping.unicast.hosts: ["192.168.1.127", "192.168.1.126"] #这个为两台机子的 IP 地址 |
| 71 | +``` |
| 72 | + |
| 73 | +- 修改这个配置文件,不然无法锁内存:`vim /etc/security/limits.conf` |
| 74 | +- 在文件最尾部增加下面内容: |
| 75 | + |
| 76 | +``` nginx |
| 77 | +# allow user 'elasticsearch' mlockall |
| 78 | +elasticsearch soft memlock unlimited |
| 79 | +elasticsearch hard memlock unlimited |
| 80 | +* soft nofile 65536 |
| 81 | +* hard nofile 65536 |
| 82 | +``` |
| 83 | + |
| 84 | +- 关闭 firewall |
| 85 | + - `systemctl stop firewalld.service` #停止firewall |
| 86 | + - `systemctl disable firewalld.service` #禁止firewall开机启动 |
| 87 | + |
| 88 | +- 切换到 elasticsearch 用户下:`su elasticsearch` |
| 89 | +- 带控制台的启动(比较慢):`/usr/program/elk/elasticsearch-2.4.1/bin/elasticsearch` |
| 90 | + - 控制台会输出类似这样的信息: |
| 91 | + |
| 92 | +``` |
| 93 | +[2017-03-13 18:42:51,170][INFO ][node ] [gitnavi-node-1] version[2.4.1], pid[21156], build[c67dc32/2016-09-27T18:57:55Z] |
| 94 | +[2017-03-13 18:42:51,177][INFO ][node ] [gitnavi-node-1] initializing ... |
| 95 | +[2017-03-13 18:42:51,821][INFO ][plugins ] [gitnavi-node-1] modules [reindex, lang-expression, lang-groovy], plugins [head, kopf], sites [head, kopf] |
| 96 | +[2017-03-13 18:42:51,852][INFO ][env ] [gitnavi-node-1] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [12.4gb], net total_space [17.4gb], spins? [unknown], types [rootfs] |
| 97 | +[2017-03-13 18:42:51,852][INFO ][env ] [gitnavi-node-1] heap size [1015.6mb], compressed ordinary object pointers [true] |
| 98 | +[2017-03-13 18:42:54,094][INFO ][node ] [gitnavi-node-1] initialized |
| 99 | +[2017-03-13 18:42:54,094][INFO ][node ] [gitnavi-node-1] starting ... |
| 100 | +[2017-03-13 18:42:54,175][INFO ][transport ] [gitnavi-node-1] publish_address {192.168.1.127:9300}, bound_addresses {[::]:9300} |
| 101 | +[2017-03-13 18:42:54,178][INFO ][discovery ] [gitnavi-node-1] gitnavi-cluster/-XywT60EScO-9lgzjfnsgg |
| 102 | +[2017-03-13 18:42:57,344][INFO ][cluster.service ] [gitnavi-node-1] new_master {gitnavi-node-1}{-XywT60EScO-9lgzjfnsgg}{192.168.1.127}{192.168.1.127:9300}, reason: zen-disco-join(elected_as_master, [0] joins received) |
| 103 | +[2017-03-13 18:42:57,410][INFO ][gateway ] [gitnavi-node-1] recovered [0] indices into cluster_state |
| 104 | +[2017-03-13 18:42:57,414][INFO ][http ] [gitnavi-node-1] publish_address {192.168.1.127:9200}, bound_addresses {[::]:9200} |
| 105 | +[2017-03-13 18:42:57,414][INFO ][node ] [gitnavi-node-1] started |
| 106 | +``` |
| 107 | + |
| 108 | +- 守护进程方式启动:`/usr/program/elk/elasticsearch-2.4.1/bin/elasticsearch -d` |
| 109 | +- 守护进程方式停止:`ps -ef|grep elasticsearc`,只能通过 kill pid 来结束 |
| 110 | +- 访问:`http://192.168.1.127:9200/`,可以看到如下内容: |
| 111 | + |
| 112 | +``` json |
| 113 | +{ |
| 114 | + "name" : "gitnavi-node-1", |
| 115 | + "cluster_name" : "gitnavi-cluster", |
| 116 | + "cluster_uuid" : "0b66dYpnTd-hh7x4Phfm1A", |
| 117 | + "version" : { |
| 118 | + "number" : "2.4.1", |
| 119 | + "build_hash" : "c67dc32e24162035d18d6fe1e952c4cbcbe79d16", |
| 120 | + "build_timestamp" : "2016-09-27T18:57:55Z", |
| 121 | + "build_snapshot" : false, |
| 122 | + "lucene_version" : "5.5.2" |
| 123 | + }, |
| 124 | + "tagline" : "You Know, for Search" |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +- 插件(插件的迭代很容易跟不上官网的版本,所以请牢记关注插件官网的说明) |
| 129 | + - head,节点数据查看管理:<https://github.com/mobz/elasticsearch-head> |
| 130 | + - kopf,集群管理:<https://github.com/lmenezes/elasticsearch-kopf> |
| 131 | + - Bigdesk,监控查看CPU内存索引数据搜索情况http连接数:<https://github.com/hlstudio/bigdesk> |
| 132 | +- 安装(过程比较慢) |
| 133 | + - head:`/usr/program/elk/elasticsearch-2.4.1/bin/plugin install mobz/elasticsearch-head` |
| 134 | + - 安装完的访问地址:`http://192.168.1.127:9200/_plugin/head` |
| 135 | + - kopf:`/usr/program/elk/elasticsearch-2.4.1/bin/plugin install lmenezes/elasticsearch-kopf` |
| 136 | + - 安装完的访问地址:`http://192.168.1.127:9200/_plugin/kopf` |
| 137 | + - Bigdesk:`/usr/program/elk/elasticsearch-2.4.1/bin/plugin install hlstudio/bigdesk` |
| 138 | + - 安装完的访问地址:`http://192.168.1.127:9200/_plugin/bigdesk` |
| 139 | + - 卸载:`/usr/share/elasticsearch/bin/elasticsearch-plugin remove 插件名称` |
| 140 | + |
| 141 | +### 构建 elasticsearch 集群 |
| 142 | + |
| 143 | +- 另外一台机子也同样这样安装,但是有几个地方有差别: |
| 144 | + - 特别注意:集群的关键点是配置文件中的:cluster.name,这个一样就表示在一个集群中 |
| 145 | + - 配置文件:`/usr/program/elk/elasticsearch-2.4.1/config/elasticsearch.yml` |
| 146 | + - node 名称改为不一样的,比如我这边改为 2:node.name: gitnavi-node-2 |
| 147 | + - 插件不用安装,有一台机子安装即可 |
| 148 | + - 先启动装有 head 的机子,然后再启动另外一台,这样好辨别 |
| 149 | + |
| 150 | +### logstash |
| 151 | + |
| 152 | +- logstash 基于 ruby,也需要 JDK 环境 |
| 153 | +- 如果是通过网络来收集,并不需要所有机子都装,但是如果是要通过读取文件来收集,那文件所在的那个机子就的安装 logstash |
| 154 | +- 安装: |
| 155 | + - 切换到存放目录:`cd /usr/program/elk` |
| 156 | + - 解压:`tar zxvf logstash-2.4.1.tar.gz` |
| 157 | +- 切换到 root 用户下,启动 logstash |
| 158 | +- 带控制台的启动(比较慢)进行最简单的 hello world 测试:`/usr/program/elk/logstash-2.4.1/bin/logstash -e 'input { stdin { } } output { stdout { codec => rubydebug} }'` |
| 159 | + - 启动后显示如下内容: |
| 160 | + |
| 161 | + ``` nginx |
| 162 | + Settings: Default pipeline workers: 1 |
| 163 | + Pipeline main started |
| 164 | + ``` |
| 165 | + |
| 166 | + - 然后此时的光标是为可输入状态,我们输入:hello world 回车,然后应该会得到这样的结果: |
| 167 | + |
| 168 | + ``` json |
| 169 | + { |
| 170 | + "message" => "hello world", |
| 171 | + "@version" => "1", |
| 172 | + "@timestamp" => "2017-03-14T06:56:44.690Z", |
| 173 | + "host" => "youmeeklocalhost" |
| 174 | + } |
| 175 | + ``` |
| 176 | + |
| 177 | +- 现在进一步加深,把控制台输入的内容放在 elasticsearch 索引中 |
| 178 | +- 记得先切换到 elasticsearch 用户下,然后先启动 elasticsearch。先确保 elasticsearch 集群是启动的。 |
| 179 | +- 带控制台的启动(比较慢):`/usr/program/elk/logstash-2.4.1/bin/logstash -e 'input { stdin { } } output { elasticsearch { hosts => ["192.168.1.127:9200"] } }'` |
| 180 | + - 启动后显示如下内容: |
| 181 | + |
| 182 | + ``` nginx |
| 183 | + Settings: Default pipeline workers: 1 |
| 184 | + Pipeline main started |
| 185 | + ``` |
| 186 | + |
| 187 | + - 然后此时的光标是为可输入状态,我们输入任意内容回车,然后访问 elasticsearch 的 head 插件控制台:`http://192.168.1.127:9200/_plugin/head/` |
| 188 | + - 然后你可以看到有一个类似这样的名称格式的索引:`logstash-2017.03.14`,这一步必须有,等下 kibana 会用到这个索引 |
| 189 | +- logstash 的高级用法请看我单独的一篇文章:[logstash 相关知识](Logstash-Base.md) |
| 190 | + |
| 191 | +### 安装 Kibana |
| 192 | + |
| 193 | +- 选择一台机子安装即可,我选择:192.168.1.127 这台 |
| 194 | +- 切换到存放目录:`cd /usr/program/elk` |
| 195 | +- 解压:`tar zxvf kibana-4.6.1-linux-x86_64.tar.gz` |
| 196 | +- 修改配置文件:`vim /usr/program/elk/kibana-4.6.1-linux-x86_64/config/kibana.yml`,打开下面注释并配置: |
| 197 | + |
| 198 | +``` nginx |
| 199 | +server.port: 5601 #端口 |
| 200 | +server.host: "192.168.1.127" #访问ip地址 |
| 201 | +elasticsearch.url: "http://192.168.1.127:9200" #连接elastic |
| 202 | +kibana.index: ".kibana" #在elastic中添加.kibana索引 |
| 203 | +``` |
| 204 | + |
| 205 | +- 记得先切换到 elasticsearch 用户下,然后先启动 elasticsearch。先确保 elasticsearch 集群是启动的。 |
| 206 | +- 再切换到 root 用户下,启动 kibana |
| 207 | +- 带控制台的启动(比较慢):`/usr/program/elk/kibana-4.6.1-linux-x86_64/bin/kibana` |
| 208 | +- 守护进程方式启动:`/usr/program/elk/kibana-4.6.1-linux-x86_64/bin/kibana -d` |
| 209 | +- 守护进程方式停止:`ps -ef|grep kibana`,只能通过 kill pid 来结束 |
| 210 | +- 然后你可以访问:`http://192.168.1.127:5601`,可以看到 kibana 的相关界面。 |
| 211 | +- 在 logstash 安装这一步,如果你刚刚有按着我说的去做一个 elasticsearch 索引,那你此时不会看到这样的提示:`Unable to fetch mapping. Do you have indices matching the pattern?` |
| 212 | + - 此时你可以直接点击 `create` 统计 `logstash-*` 格式的索引结果,看到相关内容 |
| 213 | + - 如果你知道你的索引名称的规则,比如我现在要统计 Tomcat 的相关索引,我的索引名称是:`tomcat-log-*`,则我输入这个,点击:create 即可。 |
| 214 | +- kibana 的高级用法请看我单独的一篇文章:[kibana 相关知识](Kibana-Base.md) |
| 215 | + |
| 216 | + |
| 217 | +//================================================================================================================================================================== |
| 218 | + |
| 219 | +## 5.2 安装(未完成) |
| 220 | + |
| 221 | +### RPM 安装 |
| 222 | + |
| 223 | +- 官网总的安装文档:<https://www.elastic.co/guide/en/elastic-stack/current/installing-elastic-stack.html> |
| 224 | + |
| 225 | +### 安装 Elasticsearch |
| 226 | + |
| 227 | +- 确保安装有 JDK |
| 228 | +- 官网文档:<https://www.elastic.co/guide/en/elasticsearch/reference/5.2/install-elasticsearch.html> |
| 229 | +- 创建 repo 文件:`vim /etc/yum.repos.d/elasticsearch.repo`,文件内容如下: |
| 230 | + |
| 231 | +``` ini |
| 232 | +[elasticsearch-5.x] |
| 233 | +name=Elasticsearch repository for 5.x packages |
| 234 | +baseurl=https://artifacts.elastic.co/packages/5.x/yum |
| 235 | +gpgcheck=1 |
| 236 | +gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch |
| 237 | +enabled=1 |
| 238 | +autorefresh=1 |
| 239 | +type=rpm-md |
| 240 | +``` |
| 241 | + |
| 242 | +- 引入 key:`rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch` |
| 243 | +- 开始安装:`yum install -y elasticsearch` |
| 244 | +- 如果网络慢下载不了,那可以手动安装: |
| 245 | + - 下载:`wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.2.rpm` |
| 246 | + - 安装:`rpm --install elasticsearch-5.2.2.rpm` |
| 247 | +- 添加自启动:`systemctl enable elasticsearch.service` |
| 248 | +- 因为我的 JDK 是解压版本,不是 yum 安装的,所以这里要配置 JDK 路径:`vim /etc/sysconfig/elasticsearch` |
| 249 | + - 找到 JAVA_HOME,打开注释,写上你的 JDK 路径即可 |
| 250 | +- 修改配置: |
| 251 | + - 创建数据目录:`mkdir -p /opt/elasticsearch/data` |
| 252 | + - 给 ELK 系统用户授权:`chown -R elasticsearch:elasticsearch /opt/elasticsearch/data` |
| 253 | + - 修改配置:`vim /etc/elasticsearch/elasticsearch.yml`,打开下面这些内容的注释,并修改: |
| 254 | + |
| 255 | + ``` nginx |
| 256 | + cluster.name: gitnavi-cluster |
| 257 | + node.name: gitnavi-node-1 |
| 258 | + path.data: /opt/elasticsearch/data |
| 259 | + path.logs: /var/log/elasticsearch |
| 260 | + bootstrap.memory_lock: true |
| 261 | + network.host: 本机 IP 地址 |
| 262 | + http.port: 9200 |
| 263 | + discovery.zen.ping.multicast.enabled: false |
| 264 | + discovery.zen.ping.unicast.hosts: ["192.168.1.127", "192.168.1.126"] #这个为两台机子的 IP 地址,ES 从2.0版本开始,默认的自动发现方式改为了单播(unicast)方式 |
| 265 | + ``` |
| 266 | + |
| 267 | + - 修改这个配置文件,不然无法锁内存:`vim /etc/security/limits.conf` |
| 268 | + - 增加下面内容: |
| 269 | + |
| 270 | + ``` nginx |
| 271 | + # allow user 'elasticsearch' mlockall |
| 272 | + elasticsearch soft memlock unlimited |
| 273 | + elasticsearch hard memlock unlimited |
| 274 | + * soft nofile 65536 |
| 275 | + * hard nofile 65536 |
| 276 | + ``` |
| 277 | + |
| 278 | +- 修改:`vim /etc/sysctl.conf`,添加下面配置 |
| 279 | + |
| 280 | +``` ini |
| 281 | +vm.max_map_count=655360 |
| 282 | +``` |
| 283 | + |
| 284 | +- 启动(比较慢,耐心点):`systemctl start elasticsearch.service` |
| 285 | +- 查看启动日志:`tail -500f /var/log/elasticsearch/节点名.log` |
| 286 | +- 停止:`systemctl stop elasticsearch.service` |
| 287 | +- rpm 安装后一些路径说明: |
| 288 | + - home:`/usr/share/elasticsearch` |
| 289 | + - bin:`/usr/share/elasticsearch/bin` |
| 290 | + - 配置文件:`/etc/elasticsearch/elasticsearch.yml` |
| 291 | + - 变量配置文件:`/etc/sysconfig/elasticsearch` |
| 292 | + - log:`/var/log/elasticsearch/集群名称.log` |
| 293 | + - plugins:`/usr/share/elasticsearch/plugins` |
| 294 | + - data:`/var/lib/elasticsearch`,只是我在上面改到 /opt 目录下了 |
| 295 | + - script:`/etc/elasticsearch/scripts` |
| 296 | + |
| 297 | +### 安装 X-Pack 或是其他插件 |
| 298 | + |
| 299 | +- X-Pack 是官网提供的管理增强工具,但是全部功能收费,有一个月使用,有部分功能免费。其他免费的插件。 |
| 300 | + - licence 的用法可以看这篇文章: |
| 301 | + - <http://blog.csdn.net/abcd_d_/article/details/53178798> |
| 302 | + - <http://blog.csdn.net/AbnerSunYH/article/details/53436212> |
| 303 | + - 破解:<http://www.lofter.com/lpost/33be15_d4fd028> |
| 304 | + - 免费插件: |
| 305 | + - head - 节点数据查看管理:<https://github.com/mobz/elasticsearch-head> |
| 306 | + - kopf - 集群管理:<https://github.com/lmenezes/elasticsearch-kopf> |
| 307 | +- 官网说明:<https://www.elastic.co/guide/en/x-pack/5.2/installing-xpack.html> |
| 308 | +- 安装(过程比较慢):`/usr/share/elasticsearch/bin/elasticsearch-plugin install x-pack` |
| 309 | +- 如果线上安装速度太慢,那就离线安装: |
| 310 | + - 下载,我放在 /opt 目录下(119M):`wget https://artifacts.elastic.co/downloads/packs/x-pack/x-pack-5.2.2.zip` |
| 311 | + - 安装:`/usr/share/elasticsearch/bin/elasticsearch-plugin install file:///opt/x-pack-5.2.2.zip` |
| 312 | +- 卸载:`/usr/share/elasticsearch/bin/elasticsearch-plugin remove x-pack` |
| 313 | +- 安装后重启服务,重启后访问你会发现需要用户和密码,我们可以关掉这个,在 elasticsearch.yml 中添加:`xpack.security.enabled: false` |
| 314 | +- 其他 5.2 资料: |
| 315 | + - <https://blog.yourtion.com/install-x-pack-for-elasticsearch-and-kibana.html> |
| 316 | + - <https://www.ko178.cn/?p=353> |
| 317 | + - <https://my.oschina.net/HeAlvin/blog/828639> |
| 318 | + - <http://www.jianshu.com/p/004765d2238b> |
| 319 | + - <http://www.cnblogs.com/delgyd/p/elk.html> |
| 320 | + - <http://www.itdadao.com/articles/c15a1135185p0.html> |
| 321 | + - <http://www.busyboy.cn/?p=920> |
| 322 | + - <http://nosmoking.blog.51cto.com/3263888/1897989> |
| 323 | + |
| 324 | + |
| 325 | +## 资料 |
| 326 | + |
| 327 | +- <http://www.centoscn.com/CentosServer/test/2017/0304/8575.html> |
| 328 | +- <https://blog.yourtion.com/install-x-pack-for-elasticsearch-and-kibana.html> |
| 329 | +- <http://www.voidcn.com/blog/987146971/article/p-6290041.html> |
| 330 | +- <http://www.web520.cn/archives/31077> |
| 331 | +- <> |
| 332 | +- <> |
0 commit comments