天天看點

安裝elasticsearch(centos7)

雲主機上需設定root密碼

注:目前阿裡雲為7.4

sudo passwd root 回車後出入密碼兩次

jdk11頁面

https://www.oracle.com/technetwork/java/javase/downloads/index.html

https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html

安裝jdk

mkdir /opt/downloads

jdk11

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" -P /opt/downloads http://download.oracle.com/otn-pub/java/jdk/11.0.2+9/f51449fcd52f4d52b93a989c5c56ed3c/jdk-11.0.2_linux-x64_bin.rpm

rpm -ivh /opt/downloads/jdk-11.0.2_linux-x64_bin.rpm

yum install java-11-openjdk.x86_64 java-11-openjdk-devel.x86_64 

jdk1.8

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" -P /opt/downloads https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.rpm

rpm -ivh /opt/downloads/jdk-8u201-linux-x64.rpm

yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel

檢視是否安裝成功

java -version

安裝elasticsearch

下載下傳和安裝前切換使用者,root使用者無法執行es

cat /etc/passwd 檢視所有使用者的清單

cat /etc/group 檢視使用者組

groupadd elasticsearch

userdel -r elasticsearch

useradd -s /bin/bash -g elasticsearch elasticsearch

添權重限

vim /etc/sudoers

root下添加 elasticsearch ALL=(ALL) ALL

:x!強制儲存

另:避免輸密碼可以用 elasticsearch ALL=(ALL) NOPASSWD: ALL

passwd elasticsearch *****   設定你的密碼

sudo chown -R elasticsearch /opt/downloads;sudo chgrp -R elasticsearch /opt/downloads;sudo chown -R elasticsearch /var/lib/rpm;sudo chgrp -R elasticsearch /var/lib/rpm;

sudo chown -R elasticsearch /opt/downloads/elasticsearch-7.5.0-x86_64.rpm;sudo chgrp -R elasticsearch /opt/downloads/elasticsearch-7.5.0-x86_64.rpm;

su elasticsearch

https://www.elastic.co/cn/downloads/elasticsearch

cd  /opt/downloads

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" -P /opt/downloads https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.0-x86_64.rpm

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" -P /opt/downloads https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.0-linux-x86_64.tar.gz

sudo rpm -ivh /opt/downloads/elasticsearch-7.5.0-x86_64.rpm

解除安裝sudo rpm -e elasticsearch

配置elasticsearch

vim /etc/elasticsearch/elasticsearch.yml

修改network.host的值

network.host: 192.168.x.x 自身ip,限制為内網通路

network.host: 0.0.0.0 也可以不限制ip

http.port: 9200

添加

action.auto_create_index: .monitoring*,.watches,.triggered_watches,.watcher-history*,.ml*

取消注釋 

node.name: node-1 

取消注釋 bootstrap.memory_lock: true

修改cluster.initial_master_nodes: ["node-1"]

若是單機版加入discovery.type: single-node

 如果要安裝xpack需要添加

xpack.security.enabled: true

xpack.security.transport.ssl.enabled: true

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: elasticsearch-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /var/lib/elasticsearch
#
# Path to log files:
#
path.logs: /var/log/elasticsearch
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.0.101
#
# Set a custom port for HTTP:
#
http.port: 9200

transport.tcp.port: 9300
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
discovery.seed_hosts: ["192.168.0.101"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
cluster.initial_master_nodes: ["node-1"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#

http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
http.cors.allow-credentials: true

node.master: true
node.data: true

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
#xpack.security.transport.ssl.verification_mode: certificate
#xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
#xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
#xpack.security.http.ssl.enabled: true
#xpack.security.http.ssl.keystore.path: elastic-certificates.p12
#xpack.security.http.ssl.truststore.path: elastic-certificates.p12
#xpack.security.http.ssl.client_authentication: optional      

關閉防火牆或在防火牆設定端口穿透

執行/usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive

不需要執行 bin/elasticsearch-keystore create

如果es是root使用者安裝的,檔案歸屬于,root組和root使用者,需要改變歸屬。

sudo chown -R elasticsearch /usr/share/elasticsearch;sudo chgrp -R elasticsearch /usr/share/elasticsearch;sudo chown -R elasticsearch /etc/elasticsearch;sudo chgrp -R elasticsearch /etc/elasticsearch;sudo chown -R elasticsearch /etc/sysconfig/elasticsearch;sudo chgrp -R elasticsearch /etc/sysconfig/elasticsearch;sudo chown -R elasticsearch /var/log/elasticsearch;sudo chgrp -R elasticsearch /var/log/elasticsearch

vim /usr/lib/systemd/system/elasticsearch.service 

加入LimitMEMLOCK=infinity

sudo systemctl daemon-reload

sudo systemctl enable elasticsearch.service

sudo systemctl start elasticsearch.service

生成安全證書必須切換回root

su

/usr/share/elasticsearch/bin/elasticsearch-certutil ca -out elastic-stack-ca.p12 -pass ""

/usr/share/elasticsearch/bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12

回車 回車 回車

mv /usr/share/elasticsearch/elastic-certificates.p12 /etc/elasticsearch/

mv /usr/share/elasticsearch/elastic-stack-ca.p12 /etc/elasticsearch/

chown -R elasticsearch:elasticsearch /etc/elasticsearch/elastic-certificates.p12

chown -R elasticsearch:elasticsearch /etc/elasticsearch/elastic-stack-ca.p12

将配置中的注釋去掉

#xpack.security.transport.ssl.verification_mode: certificate
#xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
#xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
      

重新開機

日志

sudo journalctl -u elasticsearch.service --since "30 min ago"

測試是否正常運作

curl http://localhost:9200/

關閉指令

netstat -anltp|grep 9200

顯示LISTEN xxxx/java

kill -9 xxxx

安裝Elasticsearch-head

先檢視是否安裝git

git --version

安裝git

yum install -y git

解除安裝為

yum remove git

cd /opt

git clone git://github.com/mobz/elasticsearch-head.git

修改配置

vim /opt/elasticsearch-head/Gruntfile.js

在原有的connect:server:options節點上加上hostname: '*',

修改es配置

network.host: 0.0.0.0

http.cors.enabled: true

http.cors.allow-origin: "*"

安裝node和npm

檢視安裝最新版本指令:https://github.com/nodesource/distributions

curl -sL https://rpm.nodesource.com/setup_12.x | bash -

yum install -y nodejs

檢視安裝的版本

node -v

npm -v

cd /opt/elasticsearch

安裝grunt和grunt-cli

sudo npm install -g grunt

sudo npm install -g grunt-cli

安裝子產品依賴

npm install -g

npm install grunt --save #安裝grunt到子產品中并儲存

背景啟動head

cd /opt/elasticsearch-head

nohup npm run start &

回車傳回到shell

守護程序啟動

vim /lib/systemd/system/elasticsearch-head.service

[Unit]

Description=Elasticsearch-head

After=network.target syslog.target

[Service]

Type=simple

WorkingDirectory=/opt/elasticsearch-head

ExecStart=/usr/bin/npm run start

Restart=always

StandardOutput=syslog

StandardError=syslog

[Install]

WantedBy=multi-user.target

ExecStart=/opt/elasticsearch-head/node_modules/grunt/bin/grunt server

chmod 777 /lib/systemd/system/elasticsearch-head.service

systemctl enable elasticsearch-head.service

systemctl daemon-reload

運作

systemctl start elasticsearch-head.service

用戶端浏覽器打開x.x.x.x:9100

若顯示未連接配接修改http://y.y.y.y:9200為es伺服器ip:port

關閉

netstat -anltp|grep 9100

顯示LISTEN xxxx/grunt

安裝IK Analysis插件

/usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.1.0/elasticsearch-analysis-ik-7.1.0.zip

重新開機es服務

sudo systemctl restart elasticsearch.service

守護程序啟動檔案浏覽

vim /usr/lib/systemd/system/elasticsearch.service

Description=Elasticsearch

Documentation=http://www.elastic.co

Wants=network-online.target

After=network-online.target

Type=notify

RuntimeDirectory=elasticsearch

PrivateTmp=true

Environment=ES_HOME=/usr/share/elasticsearch

Environment=ES_PATH_CONF=/etc/elasticsearch

Environment=PID_DIR=/var/run/elasticsearch

Environment=ES_SD_NOTIFY=true

EnvironmentFile=-/etc/sysconfig/elasticsearch

WorkingDirectory=/usr/share/elasticsearch

User=elasticsearch

Group=elasticsearch

ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet

# StandardOutput is configured to redirect to journalctl since

# some error messages may be logged in standard output before

# elasticsearch logging system is initialized. Elasticsearch

# stores its logs in /var/log/elasticsearch and does not use

# journalctl by default. If you also want to enable journalctl

# logging, you can simply remove the "quiet" option from ExecStart.

StandardOutput=journal

StandardError=inherit

# Specifies the maximum file descriptor number that can be opened by this process

LimitNOFILE=65535

# Specifies the maximum number of processes

LimitNPROC=65535

# Specifies the maximum size of virtual memory

LimitAS=infinity

# Specifies the maximum file size

LimitFSIZE=infinity

LimitMEMLOCK=infinity

# Disable timeout logic and wait until process is stopped

TimeoutStopSec=0

# SIGTERM signal is used to stop the Java process

KillSignal=SIGTERM

# Send the signal only to the JVM rather than its control group

KillMode=process

# Java process is never killed

SendSIGKILL=no

# When a JVM receives a SIGTERM signal it exits with code 143

SuccessExitStatus=143

# Built for packages-7.5.0 (packages)