elasticsearch部署搭建

直接部署

1. 准备工作

​ 准备目录结构并赋权。

1
2
mkdir -p /data/soft/elasticsearch/{conf,data,logs,snapshots}
chown -R 1000:1000 /data/soft/elasticsearch

2. 编写elasticsearch配置文件

1
vi /data/soft/elasticsearch/conf/elasticsearch.yml
1
2
3
4
5
6
7
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
node.name: node-1
network.host: 0.0.0.0
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
path.repo: ["/usr/share/elasticsearch/snapshots"]

3. 系统参数优化

1
2
3
4
5
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
echo "fs.file-max=65536" >> /etc/sysctl.conf
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p

4. run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
docker run -d \
--name es \
-p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e "ELASTIC_PASSWORD=Admin@123" \
-e "ES_JAVA_OPTS=-Xms4g -Xmx4g -Duser.timezone=Asia/Shanghai" \
--ulimit nofile=65535:65535 \
-v /etc/localtime:/etc/localtime \
-v /data/soft/elasticsearch/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /data/soft/elasticsearch/data:/usr/share/elasticsearch/data \
-v /data/soft/elasticsearch/logs:/usr/share/elasticsearch/logs \
-v /data/soft/elasticsearch/snapshots:/usr/share/elasticsearch/snapshots \
--restart always \
elasticsearch:7.17.10

脚本一键部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash

#es版本
es_version=7.17.10
#设置es资源下载目录,根据实际情况修改
es_download_dir=/data/resources
#设置软件安装根目录
install_root_dir=/data/soft
#设置es安装目录,根据实际情况修改
es_install_dir=$install_root_dir/elasticsearch
#指定es容器名称
es_container_name=es
#指定es容器暴露的端口号
es_container_expose_port1=9200
es_container_expose_port2=9300

#设置ElasticSearch的用户以及访问密码
#判断输入的密码是否满足密码复杂度(长度8位以上,同时包含大小写字母,数字和特殊符号)。
while true; do
echo "请设置 ElasticSearch 密码,需要满足密码复杂度(长度 8 位以上,同时包含大小写字母,数字和特殊符号),"
read -p "为了系统安全,请不要设置常见的弱密码口令,例如Admin@123、1qaz@2WSX等,请输入: " es_passwd

# 判断是否传入了一个参数
if [[ "$es_passwd" = "" ]]
then
echo "密码不能为空!"
continue
fi

# 判断密码的复杂度
if (( $(echo $es_passwd | wc -L) <8 ))
then
echo "您的密码长度不足8位!"
continue
fi
if echo $es_passwd | egrep "[0-9]" | egrep "[a-z]" |egrep "[A-Z]" | egrep "[^0-Z]"
then
echo "密码满足复杂度要求。"
break
else
echo "密码复杂度不够!"
echo "密码必须同时包含大小写,数字,特殊字符!请重新输入。"
fi
done

#获取服务器ip地址最后一位,作为节点id
command -v ifconfig >> temp.log
if [ $? -ne 0 ]; then
echo "服务器不存在ifconfig命令,自动安装ifconfig......" >> temp.log
yum -y install net-tools >> temp.log
else
echo "已经存在ifconfig命令,无需再安装ifconfig" >> temp.log
fi
local_ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 172|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
ip_count=`echo $local_ip|wc -w`
if [ "$ip_count" -eq 1 ] ;then
echo "当前脚本已经获取到服务器ip地址: $local_ip"
else
read -p "请输入服务器的IP地址: " local_ip
echo "$local_ip"
fi
last_ip_section=`echo $local_ip |awk -F '.' '{print $4}'`
echo -e "\033[31m 当前服务器ip地址的最后一位为: $last_ip_section \033[0m"

#判断selinux的值,如果为enforce,则需要修改,将下一行注释去掉,并修改/etc/selinux/config
sed -i "7s#enforcing#disabled#g" /etc/selinux/config
selinux_value="`getenforce`"
if [ "$selinux_value" != "Enforcing" ]; then
echo "服务器没有启用Selinux,无需临时设置";
else
setenforce 0
echo "已经临时关闭selinux"
fi

#判断是否存在相关的目录,如无则自动创建
if [ ! -d "$es_download_dir" ]; then
mkdir -p ${es_download_dir}
echo "已经自动创建文件夹${es_download_dir}" >>temp.log
else
echo "文件夹${es_download_dir}已经存在,无需再自动创建" >>temp.log
fi

#设置挂载目录
mount_conf=${es_install_dir}/conf
mount_data=${es_install_dir}/data
mount_logs=${es_install_dir}/logs
mount_snapshots=${es_install_dir}/snapshots

#检查挂载目录是否存在,如果不存在就创建
if [ -d "$mount_conf" ];then
echo "挂载目录 "$mount_conf" 已存在,无需创建" >>temp.log
else
echo "挂载目录 "$mount_conf" 不存在,由脚本创建" >>temp.log
mkdir -p "$mount_conf"
fi

if [ -d "$mount_data" ];then
echo "挂载目录 "$mount_data" 已存在,无需创建" >>temp.log
else
echo "挂载目录 "$mount_data" 不存在,由脚本创建" >>temp.log
mkdir -p "$mount_data"
fi

if [ -d "$mount_logs" ];then
echo "挂载目录 "$mount_logs" 已存在,无需创建" >>temp.log
else
echo "挂载目录 "$mount_logs" 不存在,由脚本创建" >>temp.log
mkdir -p "$mount_logs"
fi

if [ -d "$mount_snapshots" ];then
echo "挂载目录 "$mount_snapshots" 已存在,无需创建" >>temp.log
else
echo "挂载目录 "$mount_snapshots" 不存在,由脚本创建" >>temp.log
mkdir -p "$mount_snapshots"
fi
#指定es容器里面运行用户的uid
es_container_uid=1000
#设置挂载目录属主
chown -R ${es_container_uid} ${mount_conf}
chown -R ${es_container_uid} ${mount_data}
chown -R ${es_container_uid} ${mount_logs}
chown -R ${es_container_uid} ${mount_snapshots}
#chmod -R 777 ${mount_logs}
#chmod -R 777 ${mount_data}
#chmod -R 777 ${mount_conf}
#判断是否有wget命令
#if ! [ -x "$(command -v wget)" ]; then
# echo "服务器不存在wget命令,自动安装wget" >>temp.log
# yum -y install wget
#else
# echo "已经存在wget命令,无需再安装wget" >>temp.log
#fi

#切换到资源下载目录
#cd ${es_download_dir}

#获取镜像
#wget http://

#解压镜像
#docker load -i elasticsearch.tar

#修改系统线程最大文件数据
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65536
* hard nofile 65536
EOF

cat >>/etc/sysctl.conf <<EOF
fs.file-max=65536
vm.max_map_count=262144
EOF

#内核改动立即生效
sysctl -p >> temp.log

#创建配置文件
cat > ${mount_conf}/elasticsearch.yml <<EOF
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
node.name: node-${last_ip_section}
network.host: 0.0.0.0
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
path.repo: ["/usr/share/elasticsearch/snapshots"]
EOF

#部署
docker run --name ${es_container_name} -p ${es_container_expose_port1}:9200 -p ${es_container_expose_port2}:9300 \
--restart=always \
-e "discovery.type=single-node" \
-e ELASTIC_PASSWORD=${es_passwd} \
-e ES_JAVA_OPTS="-Xms4096m -Xmx4096m -Duser.timezone=Asia/Shanghai " \
--ulimit nofile=65535:65535 \
-v /etc/localtime:/etc/localtime \
-v ${mount_conf}/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v ${mount_logs}:/usr/share/elasticsearch/logs \
-v ${mount_data}:/usr/share/elasticsearch/data \
-v ${mount_snapshots}:/usr/share/elasticsearch/snapshots \
-d elasticsearch:${es_version}