在CentOS 7上配置Elasticsearch开启自启动需要通过systemd服务管理器来实现。
首先,确保你已经安装了Elasticsearch。如果还没有安装,可以按照以下步骤进行安装:
# Import the Elasticsearch PGP key
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
# Create a new repository file
echo '[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md' | sudo tee /etc/yum.repos.d/elasticsearch.repo
# Install Elasticsearch
sudo yum install elasticsearch
在编辑配置文件之前,建议备份原配置文件:
sudo cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
然后根据需要编辑 /etc/elasticsearch/elasticsearch.yml
文件:
sudo nano /etc/elasticsearch/elasticsearch.yml
一些基本配置示例:
cluster.name: my-cluster
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
Elasticsearch安装包自带了systemd服务配置文件,但为了确保服务可以正常运行,检查并启用服务:
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
Elasticsearch默认安装路径为 /usr/share/elasticsearch
。配置文件通常位于 /etc/elasticsearch
。
你需要在 /etc/systemd/system/
目录下创建一个名为 elasticsearch.service
的文件:
sudo nano /etc/systemd/system/elasticsearch.service
将以下内容粘贴到文件中:
[Unit]
Description=Elasticsearch
Documentation=https://www.elastic.co
Wants=network-online.target
After=network-online.target
[Service]
Environment=JAVA_HOME=/usr/share/elasticsearch/jdk
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=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=4096
# Specifies the maximum size of virtual memory
LimitAS=infinity
# Specifies the maximum file size
LimitFSIZE=infinity
# Disable timeout logic and wait until process is stopped
TimeoutStopSec=0
# SIGTERM signal is used to stop the Java process
KillSignal=SIGTERM
# Java process is never killed
SendSIGKILL=no
# When a JVM receives a SIGTERM signal it exits with code 143
SuccessExitStatus=143
# Allow a slow startup before the systemd notifier module kicks in to extend the timeout
TimeoutStartSec=75
[Install]
WantedBy=multi-user.target
重新加载systemd守护进程,启用并启动Elasticsearch服务
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
确保Elasticsearch服务正常启动:
sudo systemctl status elasticsearch
你应该看到类似下面的输出:
● elasticsearch.service - Elasticsearch
Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
Active: active (running) since ...
...
前面的 enable
命令已经配置了Elasticsearch为开机自启动服务。可以通过以下命令验证:
sudo systemctl is-enabled elasticsearch
如果输出 enabled
,说明配置成功。
sudo firewall-cmd --permanent --add-port=9200/tcp
sudo firewall-cmd --reload
/etc/elasticsearch/jvm.options
文件:sudo nano /etc/elasticsearch/jvm.options
修改 -Xms
和 -Xmx
参数,例如:
-Xms2g
-Xmx2g
确保这两个参数的值相同,以避免内存碎片化。
通过上述步骤,咱们在CentOS 7上成功安装并配置了Elasticsearch,并设置其为系统服务以实现开机自启动。此配置确保Elasticsearch在系统启动时自动启动,提高了系统的可用性和管理的便利性。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。