elasticsearch集群配置

配置master主库

修改配置文件

vi config/elasticsearch.yml

添加下面配置

#是否支持跨域,默认为false
http.cors.enabled: true
#当设置允许跨域,默认为*,表示支持所有域名,如果我们只是允许某些网站能访问,那么可以使用正则表达式。比如只允许本地地址。 /https?:\/\/localhost(:[0-9]+)?/
http.cors.allow-origin: "*"
#集群的名称,同一个集群该值必须设置成相同的
cluster.name: zyr
#该节点的名字
node.name: master
#该节点有机会成为master节点
node.master: true
#该参数用于同时设置bind_host和publish_host
network.host: 0.0.0.0

设置slave从库

vi config/elasticsearch.yml

添加下面配置

#集群的名称,同一个集群该值必须设置成相同的
cluster.name: zyr
#该节点的名字
node.name: slave1
#该节点有机会成为master节点
node.master: false
#该参数用于同时设置bind_host和publish_host
network.host: 0.0.0.0
#设置对外服务的http端口,默认为9200。
http.port: 9310
#设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点
discovery.zen.ping.unicast.hosts: ["0.0.0.0"]

更多配置

官网配置
部分配置翻译如下

#配置es的集群名称,默认是elasticsearch,es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集群。
cluster.name: elasticsearch

#节点名,默认随机指定一个name列表中名字,该列表在es的jar包中config文件夹里name.txt文件中,其中有很多作者添加的有趣名字。
node.name: "master"

#指定该节点是否有资格被选举成为node,默认是true,es是默认集群中的第一台机器为master,如果这台机挂了就会重新选举master。
node.master: true

#指定该节点是否存储索引数据,默认为true。
node.data: true

#设置默认索引分片个数,默认为5片。
index.number_of_shards: 5

#设置默认索引副本个数,默认为1个副本。
index.number_of_replicas: 1

#设置配置文件的存储路径,默认是es根目录下的config文件夹。
path.conf: /path/to/conf

path.data: /path/to/data
#path.data: /path/to/data1,/path/to/data2
设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开,例:

#设置临时文件的存储路径,默认是es根目录下的work文件夹。
path.work: /path/to/work

#设置日志文件的存储路径,默认是es根目录下的logs文件夹
path.logs: /path/to/logs

#设置插件的存放路径,默认是es根目录下的plugins文件夹
path.plugins: /path/to/plugins

#设置为true来锁住内存。因为当jvm开始swapping时es的效率会降低,所以要保证它不swap,可以把ES_MIN_MEM和ES_MAX_MEM两个环境变量设置成同一个值,并且保证机器有足够的内存分配给es。同时也要允许elasticsearch的进程可以锁住内存,Linux下可以通过`ulimit-l unlimited`命令。
bootstrap.mlockall: true

#设置绑定的ip地址,可以是ipv4或ipv6的,默认为0.0.0.0。
network.bind_host: 192.168.0.1

#设置其它节点和该节点交互的ip地址,如果不设置它会自动判断,值必须是个真实的ip地址。
network.publish_host: 192.168.0.1

#这个参数是用来同时设置bind_host和publish_host上面两个参数。
network.host: 192.168.0.1

#设置节点间交互的tcp端口,默认是9300。
transport.tcp.port: 9300

#设置是否压缩tcp传输时的数据,默认为false,不压缩。
transport.tcp.compress: true

#设置对外服务的http端口,默认为9200。
http.port: 9200

#设置内容的最大容量,默认100mb
http.max_content_length: 100mb

#是否使用http协议对外提供服务,默认为true,开启。
http.enabled: false

#gateway的类型,默认为local即为本地文件系统,可以设置为本地文件系统,分布式文件系统,Hadoop的HDFS,和amazon的s3服务器,其它文件系统的设置方法下次再详细说。
gateway.type: local

#设置集群中N个节点启动时进行数据恢复,默认为1。
gateway.recover_after_nodes: 1

#设置初始化数据恢复进程的超时时间,默认是5分钟。
gateway.recover_after_time: 5m

#设置这个集群中节点的数量,默认为2,一旦这N个节点启动,就会立即进行数据恢复。
gateway.expected_nodes: 2

#初始化数据恢复时,并发恢复线程的个数,默认为4。
cluster.routing.allocation.node_initial_primaries_recoveries: 4

#添加删除节点或负载均衡时并发恢复线程的个数,默认为4。
cluster.routing.allocation.node_concurrent_recoveries: 2

#设置数据恢复时限制的带宽,如入100mb,默认为0,即无限制。
indices.recovery.max_size_per_sec: 0

#设置这个参数来限制从其它分片恢复数据时最大同时打开并发流的个数,默认为5。
indices.recovery.concurrent_streams: 5

#设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)
discovery.zen.minimum_master_nodes: 1

#设置集群中自动发现其它节点时ping连接超时时间,默认为3秒,对于比较差的网络环境可以高点的值来防止自动发现时出错。
discovery.zen.ping.timeout: 3s

#设置是否打开多播发现节点,默认是true。
discovery.zen.ping.multicast.enabled: false

#设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点
discovery.zen.ping.unicast.hosts: ["host1", "host2: port","host3[portX-portY]"]

设置linux运行环境

由于Elasticsearch可以接收用户输入的脚本并且执行,为了系统安全考虑,不允许root账号启动,所以建议给Elasticsearch单独创建一个用户来运行Elasticsearch。

groupadd elasticsearch_user_group
useradd elasticsearch -g elasticsearch_user_group -p 'new_password'
chown -R elasticsearch:elasticsearch_user_group /usr/local/data/elasticsearch
chmod -R 777 /usr/local/data/elasticsearch

否则会出现如下错误

[2019-11-24T13:10:36,487][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [master] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:140) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:127) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.4.3.jar:6.4.3]
	at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.4.3.jar:6.4.3]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:86) ~[elasticsearch-6.4.3.jar:6.4.3]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
	at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:104) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:171) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:326) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-6.4.3.jar:6.4.3]
	... 6 more

使用root用户修改文件权限,用户线程数

vi /etc/security/limits.conf

修改内容如下

root soft nofile 65536
root hard nofile 131072
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
vi /etc/sysctl.conf

修改内容如下

#添加这行记录
vm.max_map_count = 262144
#使配置生效
sysctl -p

否则出现下面错误

max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
max number of threads [3818] for elasticsearch is too low, increase to at least [4096]
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

启动elasticsearch

#直接启动
./bin/elasticsearch
#后台启动
./bin/elasticsearch -d

你可能感兴趣的:(服务器配置)