ELK日志系统
Elasticsearch部署
(1)配置JDK
# ES运行依赖JDK,7.x版本需要JDK11版本 tar -zxf jdk-11.0.9_linux-x64_bin.tar.gz -C /usr/local/ echo ' export JAVA_HOME=/usr/local/jdk-11.0.9 export PATH=$JAVA_HOME/bin:$PATH ' >> /etc/profile source /etc/profile java -version #java version "11.0.9" 2020-10-20 LTS #Java(TM) SE Runtime Environment 18.9 (build 11.0.9+7-LTS) #Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.9+7-LTS, mixed mode)
(2)创建用户
因为Elasticsearch无法使用root用户启动,所有创建一个用户来启动Elasticsearch。 useradd elk echo 'elk' | passwd --stdin "elk"
(3)安装配置
tar -zxf elasticsearch-7.10.0.tar.gz -C /usr/local/ echo ' cluster.name: bjbpe01-elk node.name: elk01 node.master: true node.data: true path.data: /data/elasticsearch/data path.logs: /data/elasticsearch/logs bootstrap.memory_lock: false bootstrap.system_call_filter: false network.host: 0.0.0.0 http.port: 9200 cluster.initial_master_nodes: ["elk01"] #discovery.zen.ping.unicast.hosts: ["172.16.244.26", "172.16.244.27"] #discovery.zen.minimum_master_nodes: 2 #discovery.zen.ping_timeout: 150s #discovery.zen.fd.ping_retries: 10 #client.tansport.ping_timeout: 60s http.cors.enabled: true http.cors.allow-origin: "*" ' >> /usr/local/elasticsearch-7.10.0/conf/elasticsearch.yml
配置详解
cluster.name 集群名称,各节点配成相同的集群名称。 node.name 节点名称,各节点配置不同。 node.master 指示某个节点是否符合成为主节点的条件。 node.data 指示节点是否为数据节点。数据节点包含并管理索引的一部分。 path.data 数据存储目录。 path.logs 日志存储目录。 bootstrap.memory_lock 内存锁定,是否禁用交换。 bootstrap.system_call_filter 系统调用过滤器。 network.host 绑定节点IP。 http.port rest api端口。 cluster.initial_master_nodes 集群协调子系统 discovery.zen.ping.unicast.hosts 提供其他 Elasticsearch 服务节点的单点广播发现功能。 discovery.zen.minimum_master_nodes 集群中可工作的具有Master节点资格的最小数量,官方的推荐值是(N/2)+1,其中N是具有master资格的节点的数量。 discovery.zen.ping_timeout 节点在发现过程中的等待时间。 discovery.zen.fd.ping_retries 节点发现重试次数。 http.cors.enabled 是否允许跨源 REST 请求,用于允许head插件访问ES。 http.cors.allow-origin 允许的源地址。
(4)设置JVM堆大小
sed -i 's/-Xms1g/-Xms4g/' /usr/local/elasticsearch-7.10.0/config/jvm.options sed -i 's/-Xmx1g/-Xmx4g/' /usr/local/elasticsearch-7.10.0/config/jvm.options
(5)创建数据及日志存储目录
mkdir -p /data/elasticsearch/data mkdir -p /data/elasticsearch/logs
(6)修改目录和存储用户权限
chown -R elk:elk /data/elasticsearch/ chown -R elk:elk /usr/local/elasticsearch-7.10.0/
(7)系统优化
elasticsearch占用资源很大,如果不设置无法启动 增加最大文件打开数 echo "* - nofile 65536" >> /etc/security/limits.conf 增加最大进程数 echo "* soft nproc 31717" >> /etc/security/limits.conf 也可以直接使用下面的参数调整 echo ' * soft nofile 65536 * hard nofile * soft nproc 2048 * hard nproc 4096 ' >> /etc/security/limits.conf 增加最大内存映射数 echo "vm.max_map_count=" >> /etc/sysctl.conf sysctl -p
(8)启动ES
切换用户 su - elk cd /usr/local/elasticsearch-7.10.0 nohup bin/elasticsearch & 或者执行: su - elk -c "cd /usr/local/elasticsearch-7.10.0 && nohup bin/elasticsearch &"
报错问题解决
memory locking requested for elasticsearch process but memory is not locked 修改elasticsearch.yml bootstrap.memory_lock: ture 修改为: bootstrap.memory_lock: false max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] 意思是elasticsearch用户拥有的客串建文件描述的权限太低,知道需要65536个 切换到root用户 vim /etc/security/limits.conf 在最后添加 * hard nofile 65536 * hard nofile 65536 重新启动elasticsearch,还是无效? 必须重新登录启动elasticsearch的账户才可以,例如我的账户名是elasticsearch,退出重新登录。 另外*也可以换为启动elasticsearch的账户也可以,* 代表所有,其实比较不合适 max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [] 意思是:elasticsearch用户拥有的内存权限太小了,至少需要。这个比较简单,也不需要重启,直接执行 sysctl -w vm.max_map_count= 就可以了 the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured 缺少配置 cluster.initial_master_nodes 修改elasticsearch.yml 添加上 cluster.initial_master_nodes:["节点名称"]
安装配置head监控插件
(1)安装node
node官网:https://nodejs.org/zh-cn/
wget https://nodejs.org/dist/v14.15.1/node-v14.15.1-linux-x64.tar.xz tar -xf node-v14.15.1-linux-x64.tar.xz -C /usr/local echo ' export NODE_HOME=/usr/local/node-v14.15.1-linux-x64 export PATH=$NODE_HOME/bin:$PATH ' >> /etc/profile source /etc/profile node -v 查看node版本 npm -v查看npm版本 更换淘宝源: npm config set registry https://registry.npm.taobao.org npm config get registry 查看验证是否成功
(2)下载head插件
cd /usr/local git clone https://github.com/mobz/elasticsearch-head.git 或者 wget https://github.com/mobz/elasticsearch-head/archive/master.zip unzip –d /usr/local elasticsearch-head-master.zip
(3)安装grunt
cd /usr/local/elasticsearch-head-master npm install -g grunt-cli 检查版本号: grunt –-version
(4)修改head源码
vim /usr/local/elasticsearch-head-master/Gruntfile.js +95 (第100行左右)

hostname,注意在上一行末尾添加逗号,hostname 不需要添加逗号
vim /usr/local/elasticsearch-head-master/_site/app.js (4388左右)

原本是http://localhost:9200 ,如果head和ES不在同一个节点,注意修改成ES的IP地址
(5)下载head必要文件
网址:https://github.com/Medium/phantomjs/releases
wget https://github.com/Medium/phantomjs/archive/2.1.14.tar.gz tar -zxf 2.1.14.tar.gz /tmp/ 或者: cd /tmp/ git clone https://github.com/Medium/phantomjs.git 设置环境变量 echo ' export PHANTOMJS_HOME=/tmp/phantomjs-2.1.14 export PATH=$PHANTOMJS_HOME/bin:$PATH ' >> /etc/profile source /etc/profile
(6)启动
cd /usr/local/elasticsearch-head-master/ npm install nohup grunt server & 访问 http://192.168.200.55:9100
Kibana部署
(1)解压并配置
tar zxf kibana-7.10.0-linux-x86_64.tar.gz -C /usr/local/ echo ' server.port: 5601 server.host: "192.168.200.55" elasticsearch.hosts: "http://192.168.200.55:9200" kibana.index: ".kibana" i18n.locale: "zh-CN" '>>/usr/local/kibana-7.10.0-linux-x86_64/config/kibana.yml
server.port kibana服务端口,默认5601 server.host kibana主机IP地址,默认localhost elasticsearch.hosts 用来做查询的ES节点的URL,默认http://localhost:9200 更高版本需要使用 elasticsearch.hosts kibana.index kibana在Elasticsearch中使用索引来存储保存的searches, visualizations和dashboards,默认.kibana i18n.locale: "zh-CN" 修改为中文
(2)启动
kibana也是不支持root启动的,所有我们可以授权给elk用户启动 chown -R elk:elk /usr/local/kibana-7.10.0-linux-x86_64/ su - elk cd /usr/local/kibana-7.10.0-linux-x86_64/ nohup bin/kibana & 启动完成后可访问 http://192.168.200.55:5601/
安装配置Nginx反向代理
(1)安装nginx
配置YUM源 rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 安装 yum install -y nginx httpd-tools 注意:httpd-tools用于生成nginx认证访问的用户密码文件
(2)配置反向代理
vim /etc/nginx/nginx.conf
ser nginx; worker_processes 4; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; worker_rlimit_nofile 65535; events {
worker_connections 65535; use epoll; } http {
include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; server_names_hash_bucket_size 128; autoindex on; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 120; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; #gzip模块设置 gzip on; #开启gzip压缩输出 gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0) gzip_comp_level 2; #压缩等级 gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。 gzip_vary on; #开启限制IP连接数的时候需要使用 #limit_zone crawler $binary_remote_addr 10m; #tips: #upstream bakend{#定义负载均衡设备的Ip及设备状态}{
# ip_hash; # server 127.0.0.1:9090 down; # server 127.0.0.1:8080 weight=2; # server 127.0.0.1:6060; # server 127.0.0.1:7070 backup; #} #在需要使用负载均衡的server中增加 proxy_pass http://bakend/; server {
listen 80; server_name 172.16.244.28; #charset koi8-r; # access_log /var/log/nginx/host.access.log main; access_log off; location / {
auth_basic "Kibana"; #可以是string或off,任意string表示开启认证,off表示关闭认证。 auth_basic_user_file /etc/nginx/passwd.db; #指定存储用户名和密码的认证文件。 proxy_pass http://172.16.244.28:5601; proxy_set_header Host $host:5601; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Via "nginx"; } location /status {
stub_status on; #开启网站监控状态 access_log /var/log/nginx/kibana_status.log; #监控日志 auth_basic "NginxStatus"; } location /head/{
auth_basic "head"; auth_basic_user_file /etc/nginx/passwd.db; proxy_pass http://172.16.244.25:9100; proxy_set_header Host $host:9100; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Via "nginx"; } # redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html; location = /50x.html {
root html; } } }
(3)配置授权用户和密码并启动nginx
htpasswd -cm /etc/nginx/passwd.db username systemctl start nginx 然后就可以访问
Logstach部署
(1)环境与安装
Logstach依赖Java环境,这里不再说明如何部署Java环境 tar -zxf logstash-7.10.0.tar.gz -C /usr/local/
(2)新增配置文件
创建目录,我们将所有input、filter、output配置文件全部放到该目录中。 mkdir –p /usr/local/logstash-7.10.0/etc/conf.d
(3)配置
vim /usr/local/logstash-7.10.0/etc/conf.d/nginx.conf
这里以nginx作为例子,但是这个例子没有分 input {
file {
path => "/usr/local/nginx/logs/caaess.log" type => "ngixn" start_position => "beginning" stat_interval => "2" } } output {
elasticsearch {
hosts => ["192.168.200.55:9200"] index => "logstash-%{type}-%{+YYYY.MM.dd}" } }
(4)启动Logstach
nohup /usr/local/logstash-7.10.0/bin/logstash -f /usr/local/logstash-7.10.0/etc/conf.d/ &
(4)启动Logstach ~~~shell nohup /usr/local/logstash-7.10.0/bin/logstash -f /usr/local/logstash-7.10.0/etc/conf.d/ &
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231567.html原文链接:https://javaforall.net
