一.准备环境:
1.centos7 环境
2.安装ansible环境
3.elasticsearch安装文件下载路径:
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.6.2-linux-x86_64.tar.gz
4.elasticsearch安装主机,作者用192.168.126.129作为elasticsearch安装环境
二.规划:
1.变量规划:
安装路径:elasticsearch_dir: "/kingdee/elasticsearch"
运行用户:es_start_user: elasticsearch
用户名称:esusername: logadmin
用户密码:espassword: Mytest@@@@@1203
监听端口:elasticsearch_port: 9200
最大内存(GB): memory_max: 1
最小内存(GB): memory_small: 1
2.mongodb目录规划:
安装目录:/kingdee/elasticsearch
三.编写ansible-playbook:
1.创建ansible-playbook剧本目录
mkdir -p roles/elasticsearch_single/{files,tasks,templates,vars}
2.模板文件配置
cd roles/elasticsearch_single/templates
vim elasticsearch.yml.j2
#cluster.name: my_test_elasticsearch
node.name: {{ansible_ssh_host}}
path.data: {{elasticsearch_dir}}/data
path.logs: {{elasticsearch_dir}}/logs
action.auto_create_index: false
bootstrap.memory_lock: false
network.host: {{ansible_ssh_host}}
http.port: {{elasticsearch_port}}
#discovery.seed_hosts: ["{{ ansible_ssh_host }}"]
cluster.initial_master_nodes: ["{{ansible_ssh_host}}"]
readiness.port: 19200
action.destructive_requires_name: false
xpack.security.enabled: true
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:
enabled: false
keystore.path: certs/http.p12
xpack.security.transport.ssl:
enabled: yes
verification_mode: certificate
keystore.path: certs/transport.p12
truststore.path: certs/transport.p12
http.host: {{ ansible_ssh_host }}
transport.host: {{ ansible_ssh_host }}
vim jvm.options.j2
-Xms{{memory_small}}g
-Xmx{{memory_max}}g
-XX:+UseG1GC
-Djava.io.tmpdir=${ES_TMPDIR}
-XX:+HeapDumpOnOutOfMemoryError
-XX:+ExitOnOutOfMemoryError
-XX:HeapDumpPath=data
-XX:ErrorFile=logs/hs_err_pid%p.log
-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m
2.tasks任务文件
cd roles/elasticsearch_single/tasks
#安装单机版elasticsearch文件
vim install_elasticsearch.yml
---
- name: "user is elasticsearch exist"
shell: |
getent passwd {{es_start_user}}|wc -l
register: user_exist
become: yes
- name: "user is elasticsearch exist"
shell: |
cat /etc/shadow|grep {{es_start_user}}|wc -l
register: group_exist
become: yes
- name: "add elasticsearch group"
group:
name: "{{es_start_user}}"
gid: 1112
become: yes
when: group_exist.stdout != "1"
- name: "add elasticsearch user"
user:
name: "{{es_start_user}}"
uid: 1112
group: "{{es_start_user}}"
become: yes
when: user_exist.stdout != "1"
- name: "mkdir elasticsearch directory"
file:
path: "/{{elasticsearch_dir.split('/')[1]}}"
state: directory
mode: 0755
owner: "{{es_start_user}}"
group: "{{es_start_user}}"
become: yes
- name: "copy elasticsearch packge"
copy:
src: elasticsearch-8.6.2-linux-x86_64.tar.gz
dest: "/tmp/elasticsearch-8.6.2-linux-x86_64.tar.gz"
- name: "tar -zxvf elasticsearch-8.6.2-linux-x86_64.tar.gz"
shell: |
tar -xf /tmp/elasticsearch-8.6.2-linux-x86_64.tar.gz -C /{{elasticsearch_dir.split('/')[1]}}
mv /{{elasticsearch_dir.split('/')[1]}}/elasticsearch-8.6.2 {{elasticsearch_dir}}
become: yes
- name: "chown -R {{es_start_user}}:{{es_start_user}} {{elasticsearch_dir}}"
file:
path: "{{elasticsearch_dir}}"
owner: "{{es_start_user}}"
group: "{{es_start_user}}"
mode: 0755
recurse: yes
become: yes
- name: "start elasticsearch"
shell: |
su - elasticsearch -c "cd {{elasticsearch_dir}}/bin && sh elasticsearch -d"
sleep 50
become: yes
- name: "copy elasticsearch conf"
template:
src: "{{item}}"
dest: "{{elasticsearch_dir}}/config/{{item.split('.j2')[0]}}"
mode: 0755
become: yes
with_items:
- elasticsearch.yml.j2
- jvm.options.j2
- name: "remove elasticsearch-8.6.2-linux-x86_64.tar.gz"
file:
path: "/tmp/elasticsearch-8.6.2-linux-x86_64.tar.gz"
state: absent
- name: "stop elasticsearch"
shell: |
ps -ef|grep elasticsearch|grep -v grep|awk '{print "kill -s 9 "$2}'|sh
become: yes
- name: "start elasticsearch"
shell: su - elasticsearch -c "cd {{elasticsearch_dir}}/bin && sh elasticsearch -d"
become: yes
- name: "add elasticsearch user {{ esusername }} and superuser to {{ esusername }}"
shell: |
{{elasticsearch_dir}}/bin/elasticsearch-users useradd {{esusername}} -r superuser,kibana_system<<EOF
{{espassword}}
{{espassword}}
>>EOF
become: yes
ignore_errors: yes
- name: "create test index in elasticsearch"
shell: |
curl -XPUT http://{{ansible_ssh_host}}:9200/{{item}} -u {{esusername}}:'{{espassword}}'
with_items:
- applog
- name: "chage elastic password"
shell: |
echo 'y'|{{elasticsearch_dir}}/bin/elasticsearch-reset-password -u elastic|grep "New value"|cut -d ':' -f2
register: elastic_passwd
- name: "check chech_server"
debug:
msg: "url:http://{{ ansible_ssh_host }}:9200/_cat/nodes?v username:{{ esusername }},password:{{espassword}}"
#环境配置
vim init.yml
---
- name: "set security config"
shell: |
echo "* soft nproc 65535" >> /etc/security/limits.conf
echo "* hard nproc 65535" >> /etc/security/limits.conf
echo "root soft nproc unlimited" >> /etc/security/limits.conf
echo "root hard nproc unlimited" >> /etc/security/limits.conf
become: yes
- name: "add sysctl config"
shell: |
echo "vm.max_map_count=655360" >> /etc/sysctl.conf
sysctl -p
become: yes
#主任务文件
vim mian.yml
---
- include_tasks: init.yml
- include_tasks: install_elasticsearch.yml
3.拿取elsticsearch安装包
cd roles/elasticsearch_single/files
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.6.2-linux-x86_64.tar.gz -O elasticsearch-8.6.2-linux-x86_64.tar.gz
整个roles目录如下:
四.编写hosts文件,与roles文件在同级目录:
vim hosts
[elasticsearch]
ansible-master ansible_ssh_host=192.168.126.128
五.编写运行yml文件:
vim startinstall_single_elasticsearch.yml
---
- hosts: elasticsearch_single
remote_user: yzj
gather_facts: no
vars:
- es_start_user: elasticsearch
- esusername: logadmin
- espassword: Kingdee@1203
- elasticsearch_dir: "/kingdee/elasticsearch"
- memory_small: 1
- memory_max: 1
- elasticsearch_port: 9200
roles:
- role: elasticsearch_single
六.运行elasticsearch安装脚本:
ansible-playbook -i hosts startinstall_single_elasticsearch.yml
七:访问elasticsearch查看状态:
curl http://192.168.126.129:9200/_cat/nodes?v --user logadmin:'Mytest@@@@@1203'
curl http://192.168.126.129:9200/_cat/health?v --user logadmin:'Mytest@@@@@1203'
至此,ansible-playbook部署elasticsearch单机部署已完成,下面会讲到elasticsearch[集群]使用ansible-playbook进行部署,如果对您有用,帮忙点个赞呗!