网站模拟效果,营销和销售的区别,高端大气网站推荐,集团公司网站开发方案容器化安装环境
Docker中安装并启动ElasticSearch
前置配置 第一步#xff1a;在宿主机上执行echo “net.ipv4.ip_forward1” /usr/lib/sysctl.d/00-system.conf 2.第二步#xff1a;重启network和docker服务 [rootlocalhost /]# systemctl restart network 在宿主机上执行echo “net.ipv4.ip_forward1” /usr/lib/sysctl.d/00-system.conf 2.第二步重启network和docker服务 [rootlocalhost /]# systemctl restart network systemctl restart docker 安装ElasticSearch
1.下载镜像
docker pull elasticsearch:7.6.22.创建挂载的目录
mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data
mkdir -p /mydata/elasticsearch/plugins
echo http.host: 0.0.0.0 /mydata/elasticsearch/config/elasticsearch.yml3.创建容器并启动 chmod -R 777 /mydata/elasticsearch/data /mydata/elasticsearch/config /mydata/elasticsearch/plugins docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e discovery.typesingle-node \
-e ES_JAVA_OPTS-Xms84m -Xmx512m \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.6.2其中elasticsearch.yml是挂载的配置文件data是挂载的数据plugins是es的插件如ik而数据挂载需要权限需要设置data文件的权限为可读可写,需要下边的指令。
chmod -R 777 要修改的路径-e discovery.typesingle-node 设置为单节点
特别注意
-e ES_JAVA_OPTS-Xms256m -Xmx256m \ 测试环境下设置ES的初始内存和最大内存否则导致过大启动不了ES访问查看
4.修改配置
进入容器打开文件
docker exec -it elasticsearch bash
cd config
vi elasticsearch.yml进入config目录里创建elasticsearch.yml文件并使用vi命令插入如下内容
http.host: 0.0.0.0
5.重启es并设置密码 配置x-pack 不然会报错 进入 docker exec -it elasticsearch bash目录执行以下命令 ./bin/elasticsearch-setup-passwords interactive会出现以下错误信息 Unexpected response code [500] from calling GET http://127.0.0.1:9200/_security/_authenticate?pretty
It doesnt look like the X-Pack security feature is enabled on this Elasticsearch node.
Please check if you have enabled X-Pack security in your elasticsearch.yml configuration file.我们需要配置文件中开启x-pack验证修改 config/elasticsearch.yml配置文件在尾部添加以下内容然后重启elasticsearch xpack.security.enabled: true./bin/elasticsearch -d重复第1步为elastic、apm_system、kibana、logstash_system、beats_system、remote_monitoring_user设置密码这里我设置了统一密码123456具体操作
cd bin
elasticsearch-setup-passwords interactive
// 输出
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,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]YEnter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana_system]:
Reenter password for [kibana_system]:
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_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]5.先不加用户密码进行访问curl 127.0.0.1:9200
{error: {root_cause: [{type: security_exception,reason: missing authentication credentials for REST request [/],header: {WWW-Authenticate: Basic realm\security\ charset\UTF-8\}}],type: security_exception,reason: missing authentication credentials for REST request [/],header: {WWW-Authenticate: Basic realm\security\ charset\UTF-8\}},status: 401
}6.CURL密码访问Elasticsearch
curl -u elastic:123456 127.0.0.1:9200
# 或者
curl -u elastic 127.0.0.1:9200
Enter host password for user elastic: 123456成功打印 安装 elasticsearch-head 此时如果想查看es的服务是否启动正常还可以基于docker来安装es的插件。过程基本一样先docker去拉取插件然后docker运行容器 docker pull mobz/elasticsearch-head:5
执行docker命令后也可以使用docker ps -a来查看是否成功启动。不过更好的方式是和es服务链接上。因此接下来首先需要修改es的config目录的配置文件elasticsearch.yml在其后面增加两行语句
http.cors.enabled: true
http.cors.allow-origin: *主要目的就是允许跨域请求。
由于修改了配置文件所以需要重启一下es的服务。直接运行如下语句
docker restart elasticsearch到此就可以使用网页浏览器来查看es的运行状况了。打开一个浏览器地址栏上输入安装es插件的服务器ip地址和其端口号 安装Kibana
7.6版本Kibana启动 参数 ELASTICSEARCH_URL docker pull kibana:7.6.2docker run --name kibana -e ELASTICSEARCH_HOSTShttp://自己的IP地址:9200 -p 5601:5601 -d kibana:7.6.2
//docker run --name kibana -e ELASTICSEARCH_URLhttp://自己的IP地址:9200 -p 5601:5601 -d kibana:7.6.2或者 docker run --name kibana -d --link YOUR_ELASTICSEARCH_CONTAINER_NAME_OR_ID:elasticsearch -p 5601:5601 kibana:7.6.2
YOUR_ELASTICSEARCH_CONTAINER_NAME_OR_IDElasticsearch容器的名字或容器ID修改配置 1、进入容器打开文件
docker exec -it kibana bash
cd config
vi kibana.yml2、编辑文件
IpAddressdocker inspect es查看es容器内部的ip地址 link启动容器需要查看server.name: kibana
server.host: 0.0.0.0
elasticsearch.hosts: [ http://192.168.59.139:9200 ]
xpack.monitoring.ui.container.elasticsearch.enabled: true
elasticsearch.username: elastic
elasticsearch.password: 123456
i18n.locale: zh-CN3、退出重启
exit
docker restart kibana然后访问页面 http://自己的IP地址:5601/app/kibana kibana操作ElasticSearch
文档操作
1. _cat
GET /_cat/node 查看所有节点
GET /_cat/health 查看es健康状况
GET /_cat/master 查看主节点
GET /_cat/indices 查看所有索引2. 保存文档
保存一个数据保存在那个索引的那个类型下指定用唯一的标识customer为索引,external为类型,1为标识。其中PUT和POST都可以POST新增。如果不指定ID会自动生成ID指定ID就会修改这个数据并新增版本号。PUT可以新增可以修改PUT必须指定ID一般都用来修改操作不指定ID会报错。
PUT customer/external/1
{name:张三
}返回结果
{_index : customer,_type : external,_id : 1,_version : 3,result : updated,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 1001,_primary_term : 2
}3. 查询文档
GET customer/external/1结果:
{_index : customer, //在那个索引_type : external, //在那个类型_id : 1, //记录ID_version : 1, //版本号_seq_no : 0, //并发控制字段每次更新就1可用于乐观锁_primary_term : 1, //主分片重新分配如重启就会变化found : true, //true就是找到数据了_source : { //数据name : 张三}
}4. 更新文档
POST操作带_update会对比原来的数据如果是一样的那就不会更新了
POST customer/external/1/_update
{doc:{name:你好}
}
POST操作不带_update会直接更新操作
POST customer/external/1
{name:你好
}5. 删除文档
DELETE customer/external/1安装FileBeat 拉取 镜像 docker pull elastic/filebeat:7.6.2拉取完成之后先不着急启动在启动之前需要完成先建立一份映射的配置文filebeat.docker.yml,选择目录创建filebeat.docker.yml
# Filebeat inputs
filebeat.inputs:
- input_type: logenable: truepaths: # 采集日志的路径这里是容器内的path- /usr/share/filebeat/logs/*.logmultiline.pattern: ^[0-9]{4}-[0-9]{2}-[0-9]{2}multiline.negate: truemultiline.match: aftermultiline.timeout: 10s# 为每个项目标识,或者分组可区分不同格式的日志tags: [pre-logs]# 这个文件记录日志读取的位置如果容器重启可以从记录的位置开始取日志registry_file: /usr/share/filebeat/data/fields:logsource: node1logtype: pre#-------------------------- Elasticsearch output ---------
output.elasticsearch:hosts: [http://192.168.59.139:9200]为什么不直接去filbeat容器里面去改配置文件呢因为filebeat容器的配置文件是只读的不可更改所以只能通过映射配置文件的方式修改。
docker run --userroot -d \
-v /home/filebeat/filebeat.docker.yml:/usr/share/filebeat/filebeat.yml \
-v /home/logs:/usr/share/filebeat/logs \
-v /home/data:/usr/share/filebeat/data \
--link elasticsearch \
--link kibana \
--name filebeat elastic/filebeat:7.6.2docker run --name filebeat -d --user root \
-v /Users/lihaodong/Desktop/log:/usr/share/filebeat/logs \
-v /usr/local/src/elk/filebeat/config/filebeat.yml:/usr/share/filebeat/filebeat.yml \
-v /usr/local/src/elk/filebeat/data:/usr/share/filebeat/data \
docker.elastic.co/beats/filebeat:7.6.2建立好配置文件之后启动filebeat容器 docker run --userroot -d \
-v /home/filebeat.docker.yml:/usr/share/filebeat/filebeat.yml \
-v /home/logs:/usr/share/filebeat/logs \
-v /home/data:/usr/share/filebeat/data \
--link elasticsearch \
--link kibana \
--name filebeat elastic/filebeat:7.6.2docker run --name filebeat -d --user root \
-v /Users/lihaodong/Desktop/log:/usr/share/filebeat/logs \
-v /usr/local/src/elk/filebeat/config/filebeat.yml:/usr/share/filebeat/filebeat.yml \
-v /usr/local/src/elk/filebeat/data:/usr/share/filebeat/data \
docker.elastic.co/beats/filebeat:7.6.2这里 -v 就是挂载目录的意思就是将自己本地的目录挂载到容器当中第一个挂载映射的是配置文件第二个是要收集的日志目录如果不挂载日志目录的话filebeat是不会收集日志的因为在容器里面根本找不到要收集的路径。–userroot 指定启动用户因为读取文件可能没有权限link起的别名等信息可以在同一网络中通过别名访问。
docker run --name filebeat -d --user root \
-v /Users/lihaodong/Desktop/log:/usr/share/filebeat/logs \
-v /usr/local/src/elk/filebeat/config/filebeat.yml:/usr/share/filebeat/filebeat.yml \
-v /usr/local/src/elk/filebeat/data:/usr/share/filebeat/data \
docker.elastic.co/beats/filebeat:7.6.2可以看到filebeat已经成功启动了如果启动失败的话可以看filebeat的配置文件es和kibana的host是否正确。
在收集日志的目录下面添加日志文件或者更新日志然后去kibana查看是否有filebeat的索引生成必须是添加或者更新日志原有的数据不会同步。 手动创建模拟下可以看到被filebeat加载收集了 通过elasticsearch-head查看日志索引状态 Kibana中可以看到已经有生成了索引并且有数据了在Discover查看具体数据
注意参考下一节内容预先配置下索引 参考 配置filebeat.yml文件
# Kibana # Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:# Kibana Host# Scheme and port can be left out and will be set to the default (http and 5601)# In case you specify and additional path, the scheme is required: http://localhost:5601/path# IPv6 addresses should always be defined as: https://[2001:db8::1]:5601host: 192.168.110.130:5601 #指定kibanausername: elastic #用户password: ${ES_PWD} #密码这里使用了keystore防止明文密码# Kibana Space ID# ID of the Kibana Space into which the dashboards should be loaded. By default,# the Default Space will be used.#space.id:# Outputs # Configure what output to use when sending the data collected by the beat.#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:# Array of hosts to connect to.hosts: [192.168.110.130:9200,192.168.110.131:9200]# Protocol - either http (default) or https.#protocol: https# Authentication credentials - either API key or username/password.#api_key: id:api_keyusername: elastic #es的用户password: ${ES_PWD} # es的密码#这里不能指定index因为我没有配置模板会自动生成一个名为filebeat-%{[beat.version]}-%{yyyy.MM.dd}的索引简单介绍如何配置索引 在Kibana中配置索引 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接 配置筛选字段 这里按照时间 创建成功了 查看最终索引数据 模拟手动添加日志 查看控制台实时采集了 我们到Kibana里查询下 可以看到最终的日志信息 可以通过搜索或者日期筛选字段筛选等等各种操作查看你需要的日志信息。或者可以在 日志目录下 查看日志 Kibana基本操作
清理配置的索引数据 日志可视化 保存这个视图并加入仪表盘中 实战练习
通过rsyslog收集本机所有日志通过filebeat收集日志给到elasticsearchElasticsearch分析日志Elasticsearch将分析结果给到kibana
通过rsyslog收集本机所有日志
先安装rsyslogyum -y install rsyslog 然后修改配置文件
修改配置文件
[rootVM-20-10-centos ~]# vim /etc/rsyslog.conf
解封2行
module(loadimudp) # needs to be done just once
input(typeimudp port514)
新增一行
# Save boot messages also to boot.log
local7.* /var/log/boot.log
#以下为新增即将所有日志都收集到一个文件
*.* /var/log/baism.log最后启动rsyslog
systemctl restart rsyslog查询下看看 利用之前的搭建好的filebeatrsyslog会不断追加日志信息到 - /var/log/baism.log
docker run --name filebeat2 -d --user root \
-v /var/log:/usr/share/filebeat/logs \
-v /usr/local/src/elk/filebeat2/config/filebeat.yml:/usr/share/filebeat/filebeat.yml \
-v /usr/local/src/elk/filebeat2/data:/usr/share/filebeat/data \
docker.elastic.co/beats/filebeat:7.6.2启动容器化开始收集日志文件中的数据 打开浏览器重新查看此时 elasticsearch-head更新索引 发现新增索引 对应的Kibana也会更新 新增了索引 我配置下新增的索引即可查看系统日志 查看日志数据 目前默认收集路径是容器内的 /usr/share/filebeat/logs 文件夹 如果有不同文件夹 可以分开收集吗大家可以试试 启动后进入容器创建syslog文件夹 用于存放系统日志文件
docker exec -it filebeat3 bash
/usr/share/filebeat/logs
mkdir syslog 那么启动容器的命令改为
docker run --name filebeat3 -d --user root \
-v /home/logs:/usr/share/filebeat/logs/syslog \
-v /usr/local/src/elk/filebeat3/config/filebeat.yml:/usr/share/filebeat/filebeat.yml \
-v /usr/local/src/elk/filebeat3/data:/usr/share/filebeat/data \
docker.elastic.co/beats/filebeat:7.6.2信息采集模板改为
filebeat.inputs:
- input_type: logenable: truepaths: # 采集日志的路径这里是容器内的path - /usr/share/filebeat/logs/syslog/*.logmultiline.pattern: ^[0-9]{4}-[0-9]{2}-[0-9]{2}multiline.negate: truemultiline.match: aftermultiline.timeout: 10s# 为每个项目标识,或者分组可区分不同格式的日志tags: [pre-logs3]# 这个文件记录日志读取的位置如果容器重启可以从记录的位置开始取日志registry_file: /usr/share/filebeat/data/fields:logsource: node1logtype: pre#-------------------------- Elasticsearch output ---------
output.elasticsearch:hosts: [http://192.168.59.139:9200]
最终验证结果 看着似乎可以 其实不行 关键看看这里 rest-high-level-client整合ElasticSearch
1.导入依赖 !-- 修改springboot默认整合的es的版本 --propertiesjava.version1.8/java.versionelasticsearch.version7.6.2/elasticsearch.version/properties!-- elasticsearch-rest-high-level-client --dependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactIdversion7.6.2/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.68/version/dependency2.编写配置类
Configuration
public class ElasticSearchClientConfig {Beanpublic RestHighLevelClient restHighLevelClient(){RestHighLevelClient client new RestHighLevelClient(RestClient.builder(new HttpHost(自己的IP地址, 9200, http)));return client;}
}3.进行es的索引操作
AutowiredQualifier(restHighLevelClient)private RestHighLevelClient client;//index名字静态一般都是放在另一个类中的public static final String ES_INDEXhan_index;//创建索引Testpublic void createIndex() throws IOException {//1. 创建索引CreateIndexRequest index new CreateIndexRequest(ES_INDEX);//2. 客户端执行请求,请求后获得相应CreateIndexResponse response client.indices().create(index, RequestOptions.DEFAULT);//3.打印结果System.out.println(response.toString());}//测试索引是否存在Testpublic void exitIndex() throws IOException{//1.GetIndexRequest request new GetIndexRequest(ES_INDEX);boolean exists client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(是否存在exists);}//删除索引Testpublic void deleteIndex() throws IOException{DeleteIndexRequest request new DeleteIndexRequest(ES_INDEX);AcknowledgedResponse response client.indices().delete(request, RequestOptions.DEFAULT);System.out.println(是否删除response);}4.es的文档操作 AutowiredQualifier(restHighLevelClient)private RestHighLevelClient client;public static final String ES_INDEXhan_index;//创建文档Testpublic void createDocument() throws IOException {//创建对象UserInfo userInfo new UserInfo(张三,12);//创建请求IndexRequest request new IndexRequest(ES_INDEX);//规则request.id(1).timeout(TimeValue.timeValueSeconds(1));//将数据放到请求中request.source(JSON.toJSONString(userInfo), XContentType.JSON);//客户端发送请求获取相应的结果IndexResponse response client.index(request, RequestOptions.DEFAULT);//打印一下System.out.println(response.toString());System.out.println(response.status());}//判断是否存在Testpublic void exitDocument() throws IOException {GetRequest request new GetRequest(ES_INDEX, 1);//不获取返回的_source 的上下文request.fetchSourceContext(new FetchSourceContext(false));request.storedFields(_none);boolean exists client.exists(request, RequestOptions.DEFAULT);System.out.println(exists);}//获取文档信息Testpublic void getDocument() throws IOException {GetRequest request new GetRequest(ES_INDEX, 1);GetResponse response client.get(request, RequestOptions.DEFAULT);System.out.println(获取到的结果response.getSourceAsString());}//更新文档Testpublic void updateDocument() throws IOException {//创建对象UserInfo userInfo new UserInfo(李四,12);UpdateRequest request new UpdateRequest(ES_INDEX, 1);request.timeout(1s);request.doc(JSON.toJSONString(userInfo),XContentType.JSON);UpdateResponse response client.update(request, RequestOptions.DEFAULT);System.out.println(response.status());}//删除文档Testpublic void deleteDocument() throws IOException{DeleteRequest request new DeleteRequest(ES_INDEX, 1);request.timeout(1s);DeleteResponse response client.delete(request, RequestOptions.DEFAULT);System.out.println(response.status());}//批量添加Testpublic void bulkDocument() throws IOException{BulkRequest request new BulkRequest();request.timeout(10s);ArrayListUserInfo userInfos new ArrayList();userInfos.add(new UserInfo(李四,1));userInfos.add(new UserInfo(李四,2));userInfos.add(new UserInfo(李四,3));userInfos.add(new UserInfo(李四,4));userInfos.add(new UserInfo(李四,5));userInfos.add(new UserInfo(李四,6));userInfos.add(new UserInfo(李四,7));//进行批处理请求for (int i 0; i userInfos.size() ; i) {request.add(new IndexRequest(ES_INDEX).id((i1)).source(JSON.toJSONString(userInfos.get(i)),XContentType.JSON));}BulkResponse response client.bulk(request, RequestOptions.DEFAULT);System.out.println(response.hasFailures());}//查询Testpublic void SearchDocument() throws IOException{SearchRequest request new SearchRequest(ES_INDEX);//构建搜索条件SearchSourceBuilder builder new SearchSourceBuilder();//查询条件使用QueryBuilders工具来实现//QueryBuilders.termQuery 精准查询//QueryBuilders.matchAllQuery() 匹配全部MatchQueryBuilder matchQuery QueryBuilders.matchQuery(name, 李四);builder.query(matchQuery);builder.timeout(new TimeValue(60, TimeUnit.SECONDS));request.source(builder);SearchResponse response client.search(request, RequestOptions.DEFAULT);System.out.println(查询出的结果JSON.toJSONString(response.getHits()));}