hello云胜

技术与生活

0%

ansible安装es

工程目录

创建playbook工程结构

1
2
3
4
5
mkdir -p ansible/roles/elastic/{files,templates,tasks,handlers,vars,defaults,meta} 

#创建yml空文件
touch ansible/roles/elastic/{defaults,vars,tasks,meta,handlers}/main.yml

本项目实际用到的结构目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# tree -L 4
.
├── es-host
├── es.yml
└── roles
└── elastic
├── files
│   ├── elasticsearch-7.14.1-linux-x86_64.tar.gz
├── tasks
│   └── main.yml
└── templates
├── elasticsearch.yml
├── es.ini
└── jvm.options

服务器清单es-host

1
2
3
[es]
10.x.x.1 cluster_name=es-test host_name=es-k8s-0 network_host=10.x.x.1 http_port=9200 network_host_list=[\"10.x.x.1\",\"10.x.x.2\",\"10.x.x.3\"] node_name_list=[\"10.x.x.1\",\"10.x.x.2\",\"10.x.x.3\"]
另外两台的信息,略

这里的参数在后面的template文件中会用到

主清单文件es.yml

1
2
3
- hosts: es
roles:
- elastic

playbook

task文件

重点来了 main.yml

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
---
- name: 防火墙开放端口
firewalld:
port: "{{ item }}/tcp"
state: enabled
permanent: yes
immediate: yes
loop:
- 9200
- 9300

- name: 下发es安装包,并解压
ansible.builtin.unarchive:
src: elasticsearch-7.14.1-linux-x86_64.tar.gz
dest: /usr/local


- name: 下发修改后的es配置文件
ansible.builtin.template:
src: elasticsearch.yml
dest: /usr/local/elasticsearch/config

- name: 下发jvm参数配置
ansible.builtin.template:
src: jvm.options
dest: /usr/local/elasticsearch/config

- name: 安装前置依赖
yum:
name:
- vim
- epel-release
- supervisor
- unzip

- name: 下发supervisord托管配置文件
ansible.builtin.template:
src: es.ini
dest: /etc/supervisord.d

- name: 启动supervisord
ansible.builtin.shell:
cmd: "{{ item }}"
chdir: /tmp
loop:
- useradd -p /home/es -m es
- chown -R es:es /usr/local/elasticsearch
- chown -R es:es /data
- echo "vm.max_map_count=263000" >> /etc/sysctl.conf
- echo "es soft memlock unlimited" >> /etc/security/limits.conf
- echo "es hard memlock unlimited" >> /etc/security/limits.conf
- sysctl -p
- systemctl enable supervisord
- systemctl start supervisord

用到的配置文件

es的配置文件elasticsearch.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# ======================== Elasticsearch Configuration =========================
cluster.name: {{cluster_name}}
node.name: {{host_name}}
node.data: true
node.master: true
node.ingest: true
#bootstrap.memory_lock: true
path.data: /data
path.logs: /usr/local/elasticsearch/logs
network.host: {{network_host}}
http.port: {{http_port}}
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: {{network_host_list}}
cluster.initial_master_nodes: {{node_name_list}}
action.destructive_requires_name: true
# 是否支持跨域,是:true,在使用head插件时需要此配置
http.cors.enabled: true
# “*” 表示支持所有域名
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Type,Content-Length

jvm的配置信息

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
## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms8g
-Xmx8g

################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################

## GC configuration
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly

## G1GC Configuration
# NOTE: G1 GC is only supported on JDK version 10 or later
# to use G1GC, uncomment the next two lines and update the version on the
# following three lines to your version of the JDK
# 10-13:-XX:-UseConcMarkSweepGC
# 10-13:-XX:-UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30

## JVM temporary directory
-Djava.io.tmpdir=${ES_TMPDIR}

## heap dumps

# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError

# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data

# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log

## JDK 8 GC logging
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m

# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m

Supervisord托管信息es.ini

1
2
3
4
5
6
7
8
9
10
[program:es]
command=/usr/local/elasticsearch/bin/elasticsearch
autostart=true
autorestart=true
startretries=3
user=es
stdout_logfile_maxbytes=500MB
stdout_logfile_backups = 20
stdout_logfile=/usr/local/elasticsearch/es.log
environment=JAVA_HOME=/usr/local/elasticsearch/jdk

执行安装

1
ansible-playbook -i es-host es.yml

验证

安装完成后,在浏览器里可以验证es集群的状态

1
http://你的ip:9200/_cat/health?v

image-20230908162430909

ok。