ES7.3.2版本集群搭建
es7.x版本后自带jdk和xpack插件
1.集群规划
master-1 10.0.0.90:9200 kibana安装节点
master-2 10.0.0.91:9200
data-1 10.0.0.90:9250
data-2 10.0.0.91:9250
2.安装与配置
1.解压es包
[bankdplyop@es01 ~]$ tar xvf elasticsearch-7.3.2-linux-x86_64.tar.gz -C /usr/local/
[bankdplyop@es01 ~]$ mv elasticsearch-7.3.2 elasticsearch-7-1
[bankdplyop@es01 ~]$ cp -r elasticsearch-7-1 elasticsearch-7-2
2.配置es配置文件
[bankdplyop@es01 ~]$ cat /usr/local/elasticsearch-7-1/config/elasticsearch.yml
cluster.name: elastic
node.name: master-1
node.master: true
node.data: true
node.ingest: true
#数据目录
path.data: /srv/data01/esdata,/srv/data02/esdata,/srv/data03/esdata,/srv/data04/esdata,/srv/data05/esdata,/srv/data06/esdata
#日志目录
path.logs: /var/log/elasticsearch_1
#监听的ip
network.host: 10.0.0.90
#es服务端口
http.port: 9200
transport.port: 9300 #集群各个节点通讯端口
#自动发现节点
discovery.seed_hosts: ["10.0.0.90", "10.0.0.91"] file和hosts这两种方式都可以 都是配置集群节点发现
#discovery.seed_providers: file #这种配置方式需要在config目录下配置具体要发现的主机 unicast_hosts.txt
cluster.initial_master_nodes: ["master-1", "master-2"] #集群可当选master节点
bootstrap.memory_lock: false #内存锁
bootstrap.system_call_filter: false
gateway.expected_data_nodes: 0
gateway.expected_master_nodes: 0
gateway.expected_nodes: 0
gateway.recover_after_master_nodes: 1
gateway.recover_after_nodes: 2
gateway.recover_after_time: 5m
#开启跨域访问
http.cors.enabled: true
http.cors.allow-origin: "*" #开启跨域访问后的地址限制,*表示无限制
#unicast_hosts.txt 配置
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ cat config/unicast_hosts.txt
master-1 10.0.0.90
master-2 10.0.0.91
#创建日志和数据目录,并授权
[bankdplyop@es01 /usr/local/elasticsearch-7-1] mkdir /srv
[bankdplyop@es01 /usr/local/elasticsearch-7-1] mkdir /var/log/elasticsearch_{1,2}
[root@es01 /usr/local/elasticsearch-7-1]chown -R bankdplyop.bankdplyop /var/log/elasticsearch_1
[root@es01 /usr/local/elasticsearch-7-1]chown -R bankdplyop.bankdplyop /var/log/elasticsearch_2
3.配置jvm
[bankdplyop@es01 ~]$cat /usr/local/elasticsearch-7-1/config/jvm.options
-Xms2g 默认1g
-Xmx2g 默认1g
一般配置成内存大小的一半,最多设置为内存大小的一半
4.优化系统
[bankdplyop@es01 ~]$ cat /etc/sysctl.conf
vm.max_map_count=262144
[bankdplyop@es01 ~]$sysctl -p
[bankdplyop@es01 ~]$ cat /etc/security/limits.conf 配置好之后需要重启
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
5.创建运行的用户 (es,kibana不支持root用户运行)
[bankdplyop@es01 ~]$useradd bankdplyop
[bankdplyop@es01 ~]$echo "1"|passwd --stdin bankdplyop
6.生成TLS证书拷贝证书(elasticsearch x-pack安全认证登录/tcp启用TLS)
[bankdplyop@es01 ~]cd /usr/local/elasticsearch-7-1/bin/
#生成ca证书
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ bin/elasticsearch-certutil ca
#上面命令执行成功后,会在`/etc/elasticsearch/`文件夹下生成elastic-certificates.p12证书
将产生新文件 elastic-certificates.p12。系统还会提示你输入密码,你可以输入证书和密钥的密码,也可以按Enter键将密码留空。默认情况下 elasticsearch-certutil 生成没有主机名信息的证书,这意味着你可以将证书用于集群中的每个节点,另外要关闭主机名验证。(elastic-certificates.p12生成后移动到config目录下)
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ ll
total 544
drwxr-xr-x 2 bankdplyop bankdplyop 4096 Sep 6 2019 bin
drwxr-xr-x 2 bankdplyop bankdplyop 235 Aug 21 09:54 config
-rw------- 1 bankdplyop bankdplyop 3443 Aug 15 16:07 elastic-certificates.p12 #ca证书
drwxr-xr-x 8 bankdplyop bankdplyop 96 Sep 6 2019 jdk
drwxr-xr-x 3 bankdplyop bankdplyop 4096 Sep 6 2019 lib
-rw-r--r-- 1 bankdplyop bankdplyop 13675 Sep 6 2019 LICENSE.txt
drwxr-xr-x 2 bankdplyop bankdplyop 4096 Aug 21 09:40 logs
drwxr-xr-x 33 bankdplyop bankdplyop 4096 Sep 6 2019 modules
-rw-r--r-- 1 bankdplyop bankdplyop 502598 Sep 6 2019 NOTICE.txt
drwxr-xr-x 2 bankdplyop bankdplyop 6 Sep 6 2019 plugins
-rw-r--r-- 1 bankdplyop bankdplyop 8500 Sep 6 2019 README.textile
注意:在本机生成的证书需要更改证书权限:`chmod 644 elastic-certificates.p12` (若是使用运行es程序的用户创建证书,则不用更改证书权限)
默认证书权限是600,运行elasticsearch程序的用户没有权限读取,会造成elasticsearch启动失败
#生成节点证书,节点证书是通用的,只需要在单一节点生成,并拷贝到其他节点即可
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ ll
total 544
drwxr-xr-x 2 bankdplyop bankdplyop 4096 Sep 6 2019 bin
drwxr-xr-x 2 bankdplyop bankdplyop 235 Aug 21 09:54 config
-rw------- 1 bankdplyop bankdplyop 3443 Aug 15 16:07 elastic-certificates.p12 #节点证书
-rw------- 1 bankdplyop bankdplyop 2527 Aug 15 16:05 elastic-stack-ca.p12
drwxr-xr-x 8 bankdplyop bankdplyop 96 Sep 6 2019 jdk
drwxr-xr-x 3 bankdplyop bankdplyop 4096 Sep 6 2019 lib
-rw-r--r-- 1 bankdplyop bankdplyop 13675 Sep 6 2019 LICENSE.txt
drwxr-xr-x 2 bankdplyop bankdplyop 4096 Aug 21 09:40 logs
drwxr-xr-x 33 bankdplyop bankdplyop 4096 Sep 6 2019 modules
-rw-r--r-- 1 bankdplyop bankdplyop 502598 Sep 6 2019 NOTICE.txt
drwxr-xr-x 2 bankdplyop bankdplyop 6 Sep 6 2019 plugins
-rw-r--r-- 1 bankdplyop bankdplyop 8500 Sep 6 2019 README.textile
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$cp elastic-certificates.p12 config/
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$cp elastic-certificates.p12 /usr/local/elasticsearch-7-2/config/
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ scp elastic-certificates.p12 10.0.0.91:/usr/local/elasticsearch-7-1/config/
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ scp elastic-certificates.p12 10.0.0.91:/usr/local/elasticsearch-7-2/config/
#如果生成生成证书和私钥的时候设置了密码,则还需要进行下面操作(每个节点都要)添加密码:
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ bin/elasticsearch-keystore add xpack.security.transport.ssl.trustore.secure_password
#编辑所有节点es配置文件,开启xpack认证
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$vim /usr/local/elasticsearch-7-1/config/elasticsearch.yml
...
#tcp启用TLS
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
#http启用TLS
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: elastic-certificates.p12
xpack.security.http.ssl.truststore.path: elastic-certificates.p12
#其它组件中启用加密(非强制)
根据自己的需要配置,不配置并不影响本次启用安全认证功能。
配置X-Pack监视以使用加密连接 https://www.elastic.co/guide/en/elasticsearch/reference/6.8/secure-monitoring.html
配置Kibana以加密浏览器和Kibana服务器之间的通信,https://www.elastic.co/guide/en/kibana/6.8/using-kibana-with-security.html 并通过HTTPS连接到Elasticsearch。
配置Logstash https://www.elastic.co/guide/en/logstash/6.8/ls-security.html 以使用TLS加密。
配置Beats https://www.elastic.co/guide/en/elasticsearch/reference/6.8/beats.html 以使用加密连接。
配置Java https://www.elastic.co/guide/en/elasticsearch/reference/6.8/java-clients.html 传输客户端以使用加密通信。
配置Elasticsearch for Apache Hadoop https://www.elastic.co/guide/en/elasticsearch/hadoop/6.8/security.html 以使用安全传输。
7.启动es各个节点
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ bin/elasticsearch &
8.设置密码(账号默认为elastic)
#elastic为集群超级用户
#在elasticsearch-7-1/bin/目录下运行:
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ bin/elasticsearch-setup-passwords interactive
它会不止是设置elasticsearch,其他的kibana、logstash也会一起设置了,密码最好全设置同一个,也可以用下面命令#
#方式1:自动生成密码:
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ bin/elasticsearch-setup-passwords auto
#方式2:指定用户密码
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ bin/elasticsearch-setup-passwords interactive
# 输出结果
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y # 输入y
# 直接输入密码,然后再重复一遍密码,中括号里是账号
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
9.安装kibana
[root@es01 ~]# tar xf kibana-7.3.2-linux-x86_64.tar.gz -C /usr/local/
[root@es01 /usr/local]# grep "^[a-z]" kibana/config/kibana.yml
server.port: 5601 #kibana端口
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://10.0.0.90:9200"] #这里可以配置es集群的任意一个节点,也可以配置多个节点
kibana.index: ".kibana"
kibana.defaultAppId: "home"
elasticsearch.username: "kibana" #kibana访问es集群的账户与密码
elasticsearch.password: "123456"
i18n.locale: "zh-CN" #配置kibana为中文页面
#如果你不想将用户ID和密码放在kibana.yml文件中明文配置,可以将它们存储在密钥库中。运行以下命令以创建Kibana密钥库并添加配置
bin/kibana-keystore create
bin/kibana-keystore add elasticsearch.name
bin/kibana-keystore add elasticsearch.password
#删除
bin/kibana-keystore remove xxxx
#带密码查询es
curl -XGET --user user:passwd 'http://XXXX:9200/XX/XXX'
#比如想要清空某个索引下的数据:
curl -XPOST --user admin:admin 'http://XXXX:9200/XXXX/XXX/_delete_by_query' -H "Content-Type: application/json" -d '{ "query":{"match_all":{}}}'
#查看X-pack过期时间
[root@k8s-node02 kibana-6.5.4-linux-x86_64]# curl -u elastic:123456 -XGET 'http://10.0.0.90:9200/_xpack/license' # 如果用的是默认的话过期时间是一个月,一个月过后就无法使用
{
"license" : {
"status" : "active",
"uid" : "6015cd0f-afa8-4869-9e95-264bb69fd10e",
"type" : "platinum",
"issue_date" : "2021-03-20T00:00:00.000Z",
"issue_date_in_millis" : 1616198400000,
"expiry_date" : "2021-04-19T06:50:00.000Z",
"expiry_date_in_millis" : 1561420799999 ,
"max_nodes" : 100,
"issued_to" : "chinaedu chinaedu (chinaedu)",
"issuer" : "Web Form",
"start_date_in_millis" : 1616198400000
}
}
#启动kibana
[bankdplyop@es01 ~]$ /usr/local/kibana/bin/kibana &
#给kibana生成证书
[bankdplyop@es01 ~]$ /usr/local/elasticsearch/bin/elasticsearch-certutil cert --ca \
elastic-stack-ca.p12 \
-name "CN=kibana,OU=elk,DC=mydomain,DC=com"
ENTER
kibana.p12 ENTER
ENTER
#转换成其他格式
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ openssl pkcs12 -in kibana.p12 -nocerts -nodes > kibana.key
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ openssl pkcs12 -in kibana.p12 -clcerts -nokeys > kibana.cer
[bankdplyop@es01 /usr/local/elasticsearch-7-1]$ openssl pkcs12 -in kibana.p12 -cacerts -nokeys -chain > kibana-ca.cer
#拷贝证书到配置目录
mkdir /etc/kibana/certs
cp kibana* /etc/kibana/certs/
chown kibana:kibana -R /etc/kibana/certs/
#修改kibana配置文件/etc/kibana/kibana.yml
elasticsearch.hosts: "https://10.0.0.90:9200"
xpack.security.enabled: true
elasticsearch.username: "kibana"
elasticsearch.password: "上边修改的密码"
elasticsearch.ssl.certificateAuthorities: /etc/kibana/certs/kibana-ca.cer
elasticsearch.ssl.verificationMode: certificate
server.ssl.enabled: true
server.ssl.key: /etc/kibana/certs/kibana.key
server.ssl.certificate: /etc/kibana/certs/kibana.cer
server.ssl.certificateAuthorities: /etc/kibana/certs/kibana-ca.cer
server.ssl.clientAuthentication: none
#重启elasticsearch,kibana
##补充 logstash增加访问es集群的用户及密码##
logstash过滤数据之后往es中推送的时候,需要添加权限认证,增加访问es集群的账号及密码
output {
if [fields][log_source] == 'messages' {
elasticsearch {
hosts => ["http://192.168.1.189:9200", "http://192.168.1.189:9200","http://192.168.1.189:9200"]
index => "messages-%{+YYYY.MM.dd}"
user => "elastic" # 注意:这里演示使用超级账号,安全起见最好是使用自定义的账号,并授予该用户创建索引的权限,具体看下方地址
password => "liuyingyue" # 密码是上面步骤设置的
}
}
if [fields][log_source] == "secure" {
elasticsearch {
hosts => ["http://192.168.1.189:9200", "http://192.168.1.189:9200","http://192.168.1.189:9200"]
index => "secure-%{+YYYY.MM.dd}"
user => "elastic" # 注意:这里演示使用超级账号,安全起见最好是使用自定义的账号,并授予该用户创建索引的权限,具体看下方地址
password => "liuyingyue"
}
}
}
4、elasticsearch-head访问es集群的用户及密码
elasticsearch-head插件此时再去访问有安全认证的es集群时,会发现无法进行查看,打开控制台可以看到报错:401 unauthorized 解决办法是修改elasticsearch.yml文件,增加以下配置。
http.cors.allow-headers: Authorization,content-type
修改三台es节点,然后重新启动,再次url+认证信息方式可以正常访问es集群。
5、kibana组件访问带有安全认证的elasticsearch集群
配置文件kiban.yml中需要加入以下配置
elasticsearch.username: "kibana" # 注意:此处不用超级账号elastic,而是使用kibana跟es连接的账号kibana
elasticsearch.password: "changeme"
然后重启kibana,再次访问的话就就需要输入上述账号密码才能登陆访问了
不一样的地方: 在Management下面的Kibana最后出现一个Security,有User和Role 方便kibana多用户创建及角色权限控制
6、kibana所用户创建及角色权限控制
介绍
ELK日志管理属于基础设施平台,接入多个应用系统是正常现象,如果接入多个系统的索引文件没有进行权限划分,那么很大程度会出现索引文件误处理现象,为了避免这种情况发生,多用户及权限设置必不可少。
通过Filebeat采集应用日志,发送到redis(或者kafka),通过在filebeat.yml中设置fields.log_type属性来确定是哪个应用生成的日志文件,然后在logstash中针对不同的fields.log_type发往elasticsearch时创建不同的索引文件。
针对不同用户只能查看各自系统的索引文件.
前提条件:已对ElasticSearch集群配置TLS加密通信及身份验证功能,否则kibana界面在Management下面的Kibana最后不会出现Security 只有出现这个Security才能继续下面的步骤
步骤
创建第一个应用系统app1index-log角色,选择对应的索引文件,分配对应的权限read
在Management下面的Kibana最后出现一个Security,有User和Role点击Role,
注意:若是在这一步中的run as privileges中选中角色kibana_user,则在创建用户的时候就不应该拿再选择kibana_user角色了
输入角色名
选择权限
选择查看的索引
然后点击最后的creat role
role name
indices privileges
-
创建两个用户app1index/app1index(用户名/密码),app2index/app2index, 然后分配对应系统角色和kibana_user角色
在Management下面的Kibana最后出现一个Security,有User和Role,点击User 右上角有Creat User 输入用户账号,密码,重复密码,用户全名,邮箱,选择用户角色 app1index用户角色为app1index-log和kibana_user app2index用户角色为app2index-log和kibana_user
分别添加后可以在用户列表中查看到效果
-
用户创建完后,可以进行登录验证 登录app1index用户,只能在logs中查询app1的索引日志
虽然app1index用户可以看到其他的索引index-pattern, 但是无法查询到数据。所以保证了其他系统索引文件的安全。
登录app2index用户,只能在logs中查询app2index 索引日志,其他索引无法查询到数据。 app2index 用户选择其他未分配权限的索引,无法查询到数据。

浙公网安备 33010602011771号