构建Logstash+tomcat镜像(让logstash收集tomcat日志)
1、首先pull logstash镜像作为父镜像(logstash的Dockerfile在最下面):
2、构建my-logstash镜像,使其在docker镜像实例化时,可以使用自定义的logstash配置文件。
Dockerfile:
1
2
3
|
FROM logstash
COPY logstash.conf /some/config-dir/
CMD ["-f", "/some/config-dir/logstash.conf"]
|
logstash.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
input{
file {
path => "/usr/local/tomcat/logs/*.txt"
start_position => "beginning"
}
}
filter {
grok {
match => [ "message", "%{COMMONAPACHELOG}" ]
}
kv {
source => "request"
field_split => "&?"
value_split => "="
}
urldecode {
all_fields => true
}
}
output{
stdout { codec => rubydebug }
elasticsearch {
hosts => ["172.16.163.67:9200"]
index => "logstash-apacheaccesslog-%{+YYYY.MM.dd}"
}
}
|
构建my-logstash镜像:build -t my-logstash .
3、运行my-logstash镜像:docker run -it -d my-logstash
4、进入my-logstash镜像:docker exec -it a7529cad79df /bin/bash (假设容器id为a7529cad79df )
1、首先查看系统版本:cat /etc/issue Debian GNU/Linux 8 \n \l
2、apt-get update,然后我又安装了vim
3、然后安装配置tomcat使其可以使用服务的方式运行.
首先 wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.0.39/bin/apache-tomcat-8.0.39.tar.gz
我将刚下载的tomcat移动到/usr/local目录下,解压,并重命名为tomcat。
4、创建一个tomcat用户
查看提取 tomcat文件夹,这是属于 “root” 用户。一个好的做法是:我们应该创建一个新的用户来运行Tomcat。在这 个例子中,我们将创建一个非登录用户“tomcat”,并设置Home目录是 /usr/local/tomcat/temp。
useradd -s /sbin/nologin -d /usr/local/tomcat/temp tomcat
5、更改 /usr/local/tomcat文件夹的权限,以使新的 “tomcat” 用户可以运行Tomcat
chmod -R 777 /usr/local/tomcat
chown -R tomcat:tomcat /usr/local/tomcat
6、/etc/init.d/tomcat
要运行Tomcat作为初始化服务,创建一个自定义脚本,并把它放在 /etc/init.d 文件夹。
6.1 创建一个脚本,并将其保存为/etc/init.d/tomcat
cat /etc/init.d/tomcat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
export CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
export PATH=$JAVA_HOME/bin:$PATH
start() {
echo "Starting Tomcat 8..."
/bin/su -s /bin/bash tomcat -c $CATALINA_HOME/bin/startup.sh
}
stop() {
echo "Stopping Tomcat 8..."
/bin/su -s /bin/bash tomcat -c $CATALINA_HOME/bin/shutdown.sh
}
case $1 in
start|stop) $1;;
restart) stop; start;;
*) echo "Usage : $0
<start|stop|restart>
"; exit 1;;
esac
exit 0
|
6.2 分配“执行”权限。
sudo chmod 777 /etc/init.d/tomcat
6.3 安装脚本
update-rc.d tomcat8 defaults
6.4 测试它
service tomcat start
Tomcat started.
Starting tomcat…-
5、docker run时,同时启动logstash与tomcat服务。
通过inspect my-logstash镜像可以看到:
“Entrypoint”: [
“/docker-entrypoint.sh”
],
docker run时会运行/docker-entrypoint.sh脚本,所以我将service tomcat start放进了脚本里面,这样dokcer实例化时,就可以同时启动logstash与tomcat服务,这样logstash就可以获取到tomcat的日志信息了。
6、构建logstash-tomcat镜像
docker commit a7529cad79df logstash-tomcat
logstash的Dockerfile(Debian8 系统)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
FROM openjdk:8-jre
# install plugin dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
apt-transport-https \
libzmq3 \
&& rm -rf /var/lib/apt/lists/*
# the "ffi-rzmq-core" gem is very picky about where it looks for libzmq.so
RUN mkdir -p /usr/local/lib \
&& ln -s /usr/lib/*/libzmq.so.3 /usr/local/lib/libzmq.so
# grab gosu for easy step-down from root
ENV GOSU_VERSION 1.7
RUN set -x \
&& wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \
&& wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \
&& export GNUPGHOME="$(mktemp -d)" \
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \
&& gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \
&& rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \
&& chmod +x /usr/local/bin/gosu \
&& gosu nobody true
# https://www.elastic.co/guide/en/logstash/5.0/installing-logstash.html#_apt
# https://artifacts.elastic.co/GPG-KEY-elasticsearch
RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys 46095ACC8548582C1A2699A9D27D666CD88E42B4
RUN echo 'deb http://packages.elastic.co/logstash/2.4/debian stable main' > /etc/apt/sources.list.d/logstash.list
ENV LOGSTASH_VERSION 2.4.1
ENV LOGSTASH_DEB_VERSION 1:2.4.1-1
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends "logstash=$LOGSTASH_DEB_VERSION" \
&& rm -rf /var/lib/apt/lists/*
ENV PATH /opt/logstash/bin:$PATH
# necessary for 5.0+ (overriden via "--path.settings", ignored by < 5.0)
ENV LS_SETTINGS_DIR /etc/logstash
# comment out some troublesome configuration parameters
# path.config: No config files found: /etc/logstash/conf.d/*
RUN set -ex; \
if [ -f "$LS_SETTINGS_DIR/logstash.yml" ]; then \
sed -ri 's!^path\.config:!#&!g' "$LS_SETTINGS_DIR/logstash.yml"; \
fi; \
# if the "log4j2.properties" file exists (logstash 5.x), let's empty it out so we get the default: "logging only errors to the console"
if [ -f "$LS_SETTINGS_DIR/log4j2.properties" ]; then \
cp "$LS_SETTINGS_DIR/log4j2.properties" "$LS_SETTINGS_DIR/log4j2.properties.dist"; \
truncate --size=0 "$LS_SETTINGS_DIR/log4j2.properties"; \
fi
COPY docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["-e", ""]
|