通过上篇博客,我们已经从概念上了解elasticsearch了,相信在安装配置的时候,会有熟悉感的,尤其是在配置elasticsearch.yml文件的时候。
一、前提准备
ElasticSearch分为Linux和Window版本,本篇博客用的Linux版本
ElasticSearch的官方地址: https://www.elastic.co/products/elasticsearch
我用的elasticsearch的包是:elasticsearch-6.3.0.tar.gz,所在路径是/usr/local/elk
安装elasticsearch前提是已经安装好了jdk
安装jdk可参考这篇博客:
【运维】Centos上JDK的下载与安装
查看是否安装jdk,java -version
如果安装jdk过程中出现错误,可参考这篇博客
Linux安装jdk1.8以及出现的问题
二、安装配置
1、解压
我将elasticsearch-6.3.0.tar.gz上传到了/usr/local/elk的路径下,所以需要在/usr/local/elk路径下操作
cd /usr/local/elk
tar -zxvf elasticsearch-6.3.0.tar.gz
2、添加用户
由于elasticsearch不能由root用户启动,所以需要添加一个用户
(1)添加用户组
groupadd es_admin
(2)添加用户
useradd es_admin -g es_admin
(3)修改用户密码
echo 'es_admin' | passwd --stdin es_admin
(4)给用户提升权限
chown -Rf es_admin:es_admin /usr/local/elk/elasticsearch-6.3.0
3、创建数据和日志目录
mkdir -pv /usr/local/elk/es630/data/{data1,data2,data3,logs}
这是一个批量创建命令
4、修改sysctl.conf文件
vim /etc/sysctl.conf
在文件末尾添加
vm.max_map_count=655360
可用sysctl -p查看是否设置成功
5、修改limits.conf文件
vim /etc/security/limits.conf
在文件末尾添加
* soft memlock unlimited
* hard memlock unlimited
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
重新登录终端,上述配置即可生效。客用 ulimit -n查看
6、修改配置文件elasticsearch.yml
切换到config路径下
cd /usr/local/elk/elasticsearch-6.3.0/config/
修改elasticsearch.yml文件
vim elasticsearch.yml
配置如下
# ---------------------------------- Cluster -----------------------------------
cluster.name: elasticsearch
cluster.routing.allocation.cluster_concurrent_rebalance: 16
cluster.routing.allocation.node_concurrent_recoveries: 16
cluster.routing.allocation.node_initial_primaries_recoveries: 16
# ------------------------------------ Node ------------------------------------
node.name: es_node1
node.master: true
node.data: true
# ----------------------------------- Paths ------------------------------------
path.data: /usr/local/elk/elasticsearch-6.3.0/data/data1,/usr/local/elk/elasticsearch-6.3.0/data/data2,/usr/local/elk/elasticsearch-6.3.0/data/data3
path.logs: /usr/local/elk/elasticsearch-6.3.0/data/logs
# ----------------------------------- Memory -----------------------------------
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
# ---------------------------------- Network -----------------------------------
network.host: 192.168.226.133 # 本机ip
network.tcp.no_delay: true
network.tcp.keep_alive: true
network.tcp.reuse_address: true
network.tcp.send_buffer_size: 64mb
network.tcp.receive_buffer_size: 64mb
transport.tcp.port: 9301 # tcp通讯端口号 默认是9300
transport.tcp.compress: true
http.max_content_length: 100mb
http.enabled: true
#解决跨域问题
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User"
http.port: 9201 # http协议端口号 默认是9200
# --------------------------------- Discovery ----------------------------------
discovery.zen.ping.unicast.hosts: ["192.168.226.133", "192.168.226.132","192.168.226.134"] # 所搭建集群的每台机器的ip
discovery.zen.minimum_master_nodes: 2
discovery.zen.fd.ping_timeout: 150s
discovery.zen.fd.ping_retries: 6
discovery.zen.fd.ping_interval: 15s
7、分别另外俩台机器的配置文件elasticsearch.yml
同上,只需要修改节点的名字即可
node.name: es_node1
如果觉得每台都配一遍一样的太费劲的话,我们可以直接将第一个配置好的elasticsearch.yml文件复制到另外俩台服务器的相同路径下即可。
可用服务器间传输文件的形式
scp -r /usr/local/elk/elasticsearch-6.3.0/config/elasticsearch.yml root@192.168.226.132:/usr/local/elk/elasticsearch-6.3.0/config/
scp -r /usr/local/elk/elasticsearch-6.3.0/config/elasticsearch.yml root@192.168.226.134:/usr/local/elk/elasticsearch-6.3.0/config/
8、启动
(1)切换用户
su - es_admin
(2)切换到bin目录下
cd /usr/local/elk/elasticsearch-6.3.0/bin/
(3)启动
./elasticsearch
后台启动
./elasticsearch -d
9、查看是否搭建成功
curl -XGET 'http://192.168.226.133:9201/_cat/nodes?v'
结果如下,就代表已搭建成功了
以上过程,elasticsearch集群就搭建完了。
三、扩展
设置elasticsearch开机自启
做了开机自启,之后服务器重启,我们就不用手动一个一个启动elasticsearch了。
(1)修改elasticsearch文件
vim /etc/init.d/elasticsearch
#!/bin/sh
#chkconfig: 2345 80 05
#description: elasticsearch
export JAVA_HOME=/usr/local/java/jdk1.8.0_65/
export JAVA_BIN=/usr/local/java/jdk1.8.0_65/bin
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
case "$1" in
start)
su es_admin<<!
cd /usr/local/elk/elasticsearch-6.3.0
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
stop)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
;;
restart)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
su es_admin<<!
cd /usr/local/elk/elasticsearch-6.3.0
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
*)
echo "start|stop|restart"
;;
esac
exit $?
(2)修改文件权限
chmod +x /etc/init.d/elasticsearch
(3)添加开机自启
chkconfig --add /etc/init.d/elasticsearch