摘要:
下载安装Logstash组件的运行需要java,因此必须先安装JDK组件。# 安装JDKwget https://mirrors.huaweicloud.c...
下载安装
Logstash组件的运行需要java,因此必须先安装JDK组件。
# 安装JDK wget https://mirrors.huaweicloud.com/java/jdk/11.0.2+9/jdk-11.0.2_linux-x64_bin.tar.gz tar xf jdk-11.0.2_linux-x64_bin.tar.gz -C /usr/local/src/ ln -s /usr/local/src/jdk-11.0.2 /usr/local/jdk ln -s /usr/local/jdk/bin/java /usr/bin/ # 必须执行这一条,否则systemd启动服务找不到JAVA_HOME cat >> /etc/profile << \EOF export JAVA_HOME=/usr/local/jdk export PATH=$JAVA_HOME/bin:$PATH EOF # 安装logstash wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.4.2/logstash-7.4.2.rpm rpm -ivh logstash-7.4.2.rpm
配置
mkdir -p /data/logstash/{data,logs} && chown logstash.logstash -R /data/logstash # 软件配置 mv /etc/logstash/logstash.yml /etc/logstash/logstash.yml.orig cat > /etc/logstash/logstash.yml << EOF path.data: /data/logstash path.logs: /data/logstash/logs xpack.monitoring.enabled: true xpack.monitoring.elasticsearch.hosts: ["http://localhost:9200"] #xpack.monitoring.elasticsearch.username: logstash_system #xpack.monitoring.elasticsearch.password: kb2019 EOF # 应用配置:所有.conf配置文件都放在目录/etc/logstash/conf.d # conf配置文件结构大致上是input{}、filter{}、output{} # 主配置: logstash-main.conf, 应用配置app1.conf/app2.conf/...来配置不同应用的输出, 非常方便扩展, 后续添加应用只要拷贝一个模板修改一下应用名关键字即可. # 为了让logstash服务正常能够启动,我们需要添加Input{} 部分监听端口: cat >/etc/logstash/conf.d/logstash-main.conf <<EOF input { beats { port => 5000 } } EOF
服务启停
systemctl restart logstash # 服务检测: 服务启动后有个一二十秒后会监听端口[5000是默认,可以自定义;9600则是服务自身用不对外开放] [root@logstash01 bin]# ss -ltn |egrep "5000|9600" LISTEN 0 128 :::5000 :::* LISTEN 0 50 ::ffff:127.0.0.1:9600 :::*
常见安装错误.
常见错误1:缺少配置文件,服务不停地重启
Apr 5 20:33:08 logstash01 logstash: [2020-04-05T20:33:08,761][INFO ][logstash.config.source.local.configpathloader] No config files found in path {:path=>"/etc/logstash/conf.d/*.conf"} Apr 5 20:33:08 logstash01 logstash: [2020-04-05T20:33:08,807][ERROR][logstash.config.sourceloader] No configuration found in the configured sources. Apr 5 20:33:09 logstash01 logstash: [2020-04-05T20:33:09,152][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600} Apr 5 20:33:14 logstash01 logstash: [2020-04-05T20:33:14,020][INFO ][logstash.runner ] Logstash shut down.
常见错误2:通过systemctl start logstash发现启动失败,通过查看/var/log/message日志发现居然找不到JAVA_HOME环境变量[已在/etc/profile添加过]
Apr 8 22:24:40 logstash01 logstash: could not find java; set JAVA_HOME or ensure java is in PATH Apr 8 22:24:40 logstash01 systemd: logstash.service: main process exited, code=exited, status=1/FAILURE Apr 8 22:24:40 logstash01 systemd: Unit logstash.service entered failed state. Apr 8 22:24:40 logstash01 systemd: logstash.service failed. Apr 8 22:24:40 logstash01 systemd: logstash.service holdoff time over, scheduling restart. Apr 8 22:24:40 logstash01 systemd: Stopped logstash.
后查找原因是因为systemd service不会去查找/etc/profile文件,由systemd自定义的环境文件定义. 解决办法有许多,介绍几种:
1) 给systemd service里的Environment=/etc/default/logstash里添加JAVA_HOME变量: echo "JAVA_HOME=/usr/local/jdk" >> /etc/default/logstash 2) 脚本/usr/share/logstash/bin/logstash.lib.sh首行加入JAVA环境 JAVA_HOME=/usr/local/jdk 3) 添加java软连接到系统默认PATH变量路径里: ln -s /usr/local/jdk/bin/java /usr/bin/ 推荐第三种方案一劳永逸
还没有评论,来说两句吧...