Centos 7.6 安装 ElasticSearch 7.8

32 阅读5分钟

Centos 7.6 安装 ElasticSearch

创建用户

  1. 创建用户

     useradd elastic
    

安装ElasticSearch

手动安装

  1. 创建目录并进入

     # 创建目录
     mkdir /opt/elastic
     # 进入目录
     cd /opt/elastic
    
  2. 查看Linux架构

     arch
    
  3. 下载(用的华为镜像)

     wget https://mirrors.huaweicloud.com/elasticsearch/7.8.0/elasticsearch-7.8.0-linux-x86_64.tar.gz
    
  4. 解压

     tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz
    
  5. 更改权限

     chown -R elastic:elastic /usr/local/elastic/
    
  6. 更换用户

     # 更换用户
     su elastic
     # 进入目录
     cd /usr/local/elastic/elasticsearch-7.8.0
    
  7. 后台启动

     # 修改配置
     vim config/elasticsearch.yml
     # 放出这几个地方
     cluster.name: my-application
     node.name: node-1
     path.data: ./to/data
     path.logs: ./to/logs
     network.host: 0.0.0.0
     http.port: 9200
     cluster.initial_master_nodes: ["node-1"]
     # network.host: 0.0.0.0 这一行在以后可以去掉的
     
     # 后台启动
     bin/elasticsearch -d
    
  8. 报错修改

    [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
    
    # 在root用户下修改配置文件
    vim /etc/sysctl.conf
    # 追加内容
    vm.max_map_count=655360
    # 是配置生效
    sysctl -p 
    
  9. 开启防火墙

    # 回到root权限下
    exit
    # 修改iptables配置
    vim /etc/sysconfig/iptables
    # 把22那一行复制下来改成80贴在22那一行的下面就好啦,然后Esc,然后输入:wq
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    # 启动iptables
    systemctl restart iptables.service
    # 查看iptables
    iptables -L -n
    

docker镜像安装

  1. 如果没有docker的话,下载安装docker

    # 官方一键下载安装
    curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
    # 国内一键下载安装
    curl -sSL https://get.daocloud.io/docker | sh
    
    # 启动
    systemctl start docker
    # 查看状态
    systemctl status docker
    
  2. 下载es镜像,如果没有办法连接的话,需要有台电脑可以下载镜像再保存成文件

    # 下载镜像
    docker pull docker.elastic.co/elasticsearch/elasticsearch:8.6.2
    docker pull docker.elastic.co/kibana/kibana:8.6.2
    # 查看镜像
    docker images
    # 保存为文件
    docker save [镜像id] > [文件名.tar]
    docker save 04485c81cc2d > elasticsearch-8.6.2.tar
    docker save 65e53ffb7df5 > kibana-8.6.2.tar
    # 导入镜像
    docker load elasticsearch-8.6.2.tar
    docker load kibana-8.6.2.tar
    
  3. 创建网桥

    # 默认创建的话,网桥ip是172.17.0.0/16,可以自己设置
    docker network create --subnet=172.14.0.0/16 elastic
    
  4. 运行

    # 查看镜像
    docker images
    # 运行
    docker run --name elasticsearch --net elastic -p 9200:9200 -it 镜像ID
    # 切记要记录下来密码
    # 将安全证书黏贴到本地
    docker cp elasticsearch:/usr/share/elasticsearch/config/certs/http_ca.crt .
    # 尝试是否可以访问
    curl --cacert http_ca.crt -u elastic https://localhost:9200
    
  5. 修改配置

    # 进入镜像
    docker exec -it elasticsearch /bin/bash
    # 修改配置
    vim /usr/share/elasticsearch/config/elasticsearch.yml
    
  6. 重置密码

    docker exec -it elasticsearch /usr/share/elasticsearch/bin/elasticsearch-reset-password
    

安装kibana

  1. 下载

    # 切换用户
    su elastic
    # 进入目录
    cd /usr/local/elastic/
    # 下载
    wget https://mirrors.huaweicloud.com/kibana/7.8.0/kibana-7.8.0-linux-x86_64.tar.gz
    
  2. 解压

    tar -zxvf kibana-7.8.0-linux-x86_64.tar.gz 
    
  3. 重命名一下(强迫症)

    mv kibana-7.8.0-linux-x86_64 kibana-7.8.0
    
  4. 配置

    # 进入文件夹
    cd kibana-7.8.0
    # 修改配置文件
    vim config/kibana.yml
    # 有3个地方要放出来
    server.port: 5601
    server.host: "0.0.0.0"
    elasticsearch.hosts: ["http://localhost:9200"]
    # 保存 先按一下Esc
    :wq
    # server.host: "0.0.0.0" 这一行在以后可以去掉的
    
    # 启动试试
    bin/kibana
    # 打印的日志里面应该有下面两行
    log   [07:18:36.429] [info][listening] Server running at http://0.0.0.0:5601
    log   [07:18:37.084] [info][server][Kibana][http] http server running at http://0.0.0.0:5601
    
  5. 后台启动

    # 什么信息也不要的后台启动 不输出nohup.out
    nohup bin/kibana >/dev/null 2>&1 &
    

停止kibana

我和你们说哦!!!!

要用fuser -n tcp 5601这个命令找到pid

用ps -ef | grep kibana是找不到的!!!

# 获取pid
fuser -n tcp 5601
# 关闭
kill -9 pid

安装ik分词器

# 安装解压工具(有装过unzip就忽略)
yum install -y unzip zip
# 切换用户
su elastic
# 进入插件目录
cd /usr/local/elastic/elasticsearch-7.8.0/plugins/
# 创建目录
mkdir ik
# 进入目录
cd ik
# 下载
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.8.0/elasticsearch-analysis-ik-7.8.0.zip
# 解压
unzip elasticsearch-analysis-ik-7.8.0.zip 
# 删除包
rm elasticsearch-analysis-ik-7.8.0.zip
# 重启elasticsearch
# 找到pid
ps -ef | grep elastic
# 这里pid是上面一个命令找到的pid
kill -9 pid
# 后台启动
cd /usr/local/elastic/elasticsearch-7.8.0
bin/elasticsearch -d

使用二级域名转发

  1. 在nginx.conf中加入转发的内容:

    # es转发到端口
    server {
        listen       80;
        server_name  es.mie.cool;
        proxy_set_header Host $host:$server_port;    
        proxy_set_header X-Real-IP $remote_addr;    
        proxy_set_header REMOTE-HOST $remote_addr;    
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    
        location / {
        	proxy_pass http://localhost:9200/;  
        }
    }
    # kibana转发到端口
    server {
        listen       80;
        server_name  kibana.mie.cool;
        proxy_set_header Host $host:$server_port;    
        proxy_set_header X-Real-IP $remote_addr;    
        proxy_set_header REMOTE-HOST $remote_addr;    
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    
        location / {
        	proxy_pass http://localhost:5601/;  
        }
    }
    
  2. 重新载入配置文件

    /usr/local/nginx/sbin/nginx -s reload
    
  3. 在阿里云上配置DNS解析

  4. 在阿里云的服务器安全组中关闭2333端口

重置密码

只想重置elastic的密码

  1. 存在bin/elasticsearch-reset-password的情况下

    1. ./elasticsearch-reset-password -u elastic重置elastic 的密码,密码会自动生成并打印出来
    2. ./elasticsearch-reset-password -u elastic -i 新密码加上-i以后可以自己设置密码
  2. 停止服务

    # 切换用户
    su elastic 
    # 查询进程号
    ps -ef|  grep elasticsearch
    # 杀进程
    kill -9  进程号
    
  3. 创建新用户

    # 用户名称为my_user,密码为my_user,用户组为superuser
    bin/elasticsearch-users useradd my_user -p my_user -r superuser
    
  4. 开启服务

    bin/elasticsearch
    
  5. 通过API修改超级管理员密码

    curl -XPUT -u"my_user:my_user" 'http://localhost:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "daidai@666" }' -H "Content-Type: application/json"
    
  6. 之后记得删除用户

    bin/elasticsearch-users userdel my_user
    

重置其他密码

  1. 停止服务

  2. 修改配置文件(也可能改过了)后重启

    # 修改文件
    vim config/elasticsearch.yml
    # 添加上这几行
    xpack.security.enabled: true
    xpack.license.self_generated.type: basic
    xpack.security.transport.ssl.enabled: true
    
    # 后台启动服务
    bin/elasticsearch -d
    
  3. 获取所有index

    # 获取所有index
    curl -XGET -u "elastic:刚刚重置好的密码" 'http://localhost:9200/_cat/indices?pretty=true'
    # 删除密码相关index
    curl -XDELETE -u "elastic:刚刚重置好的密码" 'http://localhost:9200/.security-7?pretty=true'
    
  4. 设置密码(交互式设计)

    ./elasticsearch-setup-passwords interactive
    #会出现以下内容
    Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
    You will be prompted to enter passwords as the process progresses.
    Please confirm that you would like to continue [y/N]y
    
    Enter password for [elastic]: 
    Reenter password for [elastic]: 
    Enter password for [apm_system]: 
    Reenter password for [apm_system]: 
    Enter password for [kibana_system]: 
    Reenter password for [kibana_system]: 
    Enter password for [logstash_system]: 
    Reenter password for [logstash_system]: 
    Enter password for [beats_system]: 
    Reenter password for [beats_system]: 
    Enter password for [remote_monitoring_user]: 
    Reenter password for [remote_monitoring_user]: 
    Changed password for user [apm_system]
    Changed password for user [kibana_system]
    Changed password for user [kibana]
    Changed password for user [logstash_system]
    Changed password for user [beats_system]
    Changed password for user [remote_monitoring_user]
    Changed password for user [elastic]
    
  5. 记得给kibana中的conffig/kibana.yml设置密码

    # 进入目录
    cd /usr/local/elastic/kibana-7.8.0/config
    # 修改
    vim kibana.yml
    
    # 改这两行:
    elasticsearch.username: "kibana_system"
    elasticsearch.password: "刚刚修改的密码"