fluent-kafka-elasticsearch

fluent-kafka-elasticsearch

一月 17, 2019

前段时间因为线上elasticsearch压力太大,kibana查询总是崩溃,做了升级后使用了kafka,现有时间了整理记录下

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# cd /opt/
# wget https://tencent-init-1255595812.cos.ap-beijing.myqcloud.com/kafka/kafka_2.12-2.0.0.tgz
# wget https://tencent-init-1255595812.cos.ap-beijing.myqcloud.com/zookeeper/zookeeper-3.4.13.tar.gz
# tar xf zookeeper-3.4.13.tar.gz
# tar xf kafka_2.12-2.0.0.tgz
# mkdir kafka
# mkdir zookeeper
# cd kafka
# mkdir data kafka-logs log
# cd ..
# cd zookeeper
# mkdir data zkdatalog
# cd data/
# vim myid
1
---
# cd ../..
# cd kafka_2.12-2.0.0
# cd config/
#vim server.properties
[root@ip-00-00-00-235 config]# cat server.properties | grep -Ev '^$|#'
broker.id=1
port=9092
host.name=00.00.00.235
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/opt/kafka/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
log.cleaner.enable=False
zookeeper.connect=00.00.00.235:2181,00.00.00.218:2181,00.00.00.25:2181
zookeeper.connection.timeout.ms=6000
---
# vim zookeeper.properties
[root@ip-00-00-00-235 config]# cat zookeeper.properties |grep -Ev '^$|#'
dataDir=/opt/zookeeper/snapshot
clientPort=2181
maxClientCnxns=0
# cd ../..
# cd zookeeper-3.4.13
# cd conf/
# vim zoo.cfg
[root@ip-00-00-00-235 conf]# cat zoo.cfg | grep -Ev '^$|#'
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/opt/zookeeper/data
dataLogDir=/opt/zookeeper/zkdatalog
clientPort=2181
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
server.1=00.00.00.235:2888:3888
server.2=00.00.00.218:2888:3888
server.3=00.00.00.25:2888:3888
# cd /etc/elasticsearch/
# vim template_run.sh
[root@ip-00-00-00-235 elasticsearch]# cat template_run.sh
#!/bin/sh
curl -H "Content-Type: application/json" -XPUT '00.00.00.235:9200/_template/all ' -d '
{
"template": "*",
"settings": {
"refresh_interval": "30s",
"number_of_replicas": "0"
}
}'
curl -H "Content-Type: application/json" -XPUT '00.00.00.235:9200/_template/all ' -d '
{
"template": "*",
"settings": {
"index.mapping.total_fields.limit": 20000,
"refresh_interval": "30s"
}
}'

curl -H "Content-Type: application/json" -XPUT '00.00.00.235:9200/_settings' -d '
{
"index" : {
"number_of_replicas" : 0
}
}
'
curl -H "Content-Type: application/json" -XPUT '00.00.00.235:9200/_cluster/settings' -d '
{
"transient" : {
"indices.recovery.max_bytes_per_sec" : "200mb",
"indices.breaker.fielddata.limit" : "50%",
"indices.breaker.request.limit" : "30%",
"indices.breaker.total.limit" : "60%"
}
}'

curl -H "Content-Type: application/json" -XPUT '00.00.00.235:9200/_template/kbilogs ' -d '
{
"index_patterns": ["kbilogs*"],
"settings": {
"auto_expand_replicas": "0-2"
}
}'

curl -H "Content-Type: application/json" -XPUT '00.00.00.235:9200/_template/fluentd ' -d '
{
"index_patterns": ["fluentd*"],
"settings": {
"auto_expand_replicas": "0-2"
}
}'


# vim healthy.sh
curl 'http://00.00.00.235:9200/_cat/health?v'

# /etc/init.d/elasticsearch status
# vim elasticsearch.yml
[root@ip-00-00-00-235 elasticsearch]# cat elasticsearch.yml | grep -Ev '^$|#'
cluster.name: taiyouxi-jws
node.name: vpc1-ip-00-00-00-235
node.attr.type: strong
node.master: true
node.data: true
xpack.monitoring.exporters.my_local:
type: local
use_ingest: false
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 00.00.00.235
http.port: 9200
discovery.zen.ping.unicast.hosts:
- 00.00.00.235
- 00.00.00.218
- 00.00.00.25
discovery.zen.minimum_master_nodes: 2
gateway.expected_master_nodes: 2
gateway.expected_data_nodes: 2
gateway.recover_after_time: 1m
gateway.recover_after_nodes: 1
gateway.recover_after_master_nodes: 1
gateway.recover_after_data_nodes: 1

# vim /etc/init.d/elasticsearch

[root@ip-00-00-00-235 elasticsearch]# cat /etc/init.d/elasticsearch
#!/bin/bash
#
# elasticsearch <summary>
#
# chkconfig: 2345 80 20
# description: Starts and stops a single elasticsearch instance on this system
#

### BEGIN INIT INFO
# Provides: Elasticsearch
# Required-Start: $network $named
# Required-Stop: $network $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: This service manages the elasticsearch daemon
# Description: Elasticsearch is a very scalable, schema-free and high-performance search solution supporting multi-tenancy and near realtime search.
### END INIT INFO

#
# init.d / servicectl compatibility (openSUSE)
#
if [ -f /etc/rc.status ]; then
. /etc/rc.status
rc_reset
fi

#
# Source function library.
#
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi

# Sets the default values for elasticsearch variables used in this script
ES_HOME="/usr/share/elasticsearch"
MAX_OPEN_FILES=65536
MAX_MAP_COUNT=262144
ES_PATH_CONF="/etc/elasticsearch"

PID_DIR="/var/run/elasticsearch"

# Source the default env file
ES_ENV_FILE="/etc/sysconfig/elasticsearch"
if [ -f "$ES_ENV_FILE" ]; then
. "$ES_ENV_FILE"
fi

# ES_USER and ES_GROUP settings were removed
if [ ! -z "$ES_USER" ] || [ ! -z "$ES_GROUP" ]; then
echo "ES_USER and ES_GROUP settings are no longer supported. To run as a custom user/group use the archive distribution of Elasticsearch."
exit 1
fi

exec="$ES_HOME/bin/elasticsearch"
prog="elasticsearch"
pidfile="$PID_DIR/${prog}.pid"

export ES_JAVA_OPTS
export JAVA_HOME
export ES_PATH_CONF
export ES_STARTUP_SLEEP_TIME

lockfile=/var/lock/subsys/$prog

if [ ! -x "$exec" ]; then
echo "The elasticsearch startup script does not exists or it is not executable, tried: $exec"
exit 1
fi

checkJava() {
if [ -x "$JAVA_HOME/bin/java" ]; then
JAVA="$JAVA_HOME/bin/java"
else
JAVA=`which java`
fi

if [ ! -x "$JAVA" ]; then
echo "Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME"
exit 1
fi
}

start() {
checkJava
[ -x $exec ] || exit 5

if [ -n "$MAX_OPEN_FILES" ]; then
ulimit -n $MAX_OPEN_FILES
fi
if [ -n "$MAX_LOCKED_MEMORY" ]; then
ulimit -l $MAX_LOCKED_MEMORY
fi
if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count -a "$MAX_MAP_COUNT" -gt $(cat /proc/sys/vm/max_map_count) ]; then
sysctl -q -w vm.max_map_count=$MAX_MAP_COUNT
fi

# Ensure that the PID_DIR exists (it is cleaned at OS startup time)
if [ -n "$PID_DIR" ] && [ ! -e "$PID_DIR" ]; then
mkdir -p "$PID_DIR" && chown elasticsearch:elasticsearch "$PID_DIR"
fi
if [ -n "$pidfile" ] && [ ! -e "$pidfile" ]; then
touch "$pidfile" && chown elasticsearch:elasticsearch "$pidfile"
fi

cd $ES_HOME
echo -n $"Starting $prog: "
# if not running, start it up here, usually something like "daemon $exec"
daemon --user elasticsearch --pidfile $pidfile $exec -p $pidfile -d
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc -p $pidfile -d 86400 $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
stop
start
}

reload() {
restart
}

force_reload() {
restart
}

rh_status() {
# run checks to determine if the service is running or use generic status
status -p $pidfile $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}


case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?
---
# vim jvm.options
[root@ip-10-33-3-235 elasticsearch]# cat jvm.options |grep -Ev '^$|#'
-Xms16024m
-Xmx16024m
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:+AlwaysPreTouch
-Xss1m
-Djava.awt.headless=true
-Dfile.encoding=UTF-8
-Djna.nosys=true
-XX:-OmitStackTraceInFastThrow
-Dio.netty.noUnsafe=true
-Dio.netty.noKeySetOptimization=true
-Dio.netty.recycler.maxCapacityPerThread=0
-Dlog4j.shutdownHookEnabled=false
-Dlog4j2.disable.jmx=true
-Djava.io.tmpdir=${ES_TMPDIR}
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/var/lib/elasticsearch
-XX:ErrorFile=/var/log/elasticsearch/hs_err_pid%p.log
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:/var/log/elasticsearch/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m
9-:-Xlog:gc*,gc+age=trace,safepoint:file=/var/log/elasticsearch/gc.log:utctime,pid,tags:filecount=32,filesize=64m
9-:-Djava.locale.providers=COMPAT
10-:-XX:UseAVX=2
# chmod g+w /etc/elasticsearch
# /etc/init.d/elasticsearch start
# tail -f /var/log/elasticsearch/elasticseatch-zd.log
# ps -ef |grep elasticsearch
# sh healthy.sh
# sh template_run.sh
# vim /etc/security/limits.d/max.conf
root soft nofile 500000
root hard nofile 500000
* soft nofile 500000
* hard nofile 500000
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
---
# cd /etc/supervisord/conf.d/
# vim kafka.conf
[root@ip-00-00-00-235 conf.d]# cat kafka.conf
[program:kafka]
directory=/opt/kafka_2.12-2.0.0
command=/opt/kafka_2.12-2.0.0/bin/kafka-server-start.sh /opt/kafka_2.12-2.0.0/config/server.properties
stdout_logfile=/opt/kafka/kafka-logs/kafka.out
stderr_logfile=/opt/kafka/kafka-logs/kafka.err
autorestart=true
stopsignal=KILL
---
# vim zookeeper.conf
[root@ip-00-00-00-235 conf.d]# cat zookeeper.conf
[program:zookeeper]
directory=/opt/zookeeper-3.4.13
command=/opt/zookeeper-3.4.13/bin/zkServer.sh start-foreground
autorestart=true
stopsignal=KILL
stdout_logfile=/opt/zookeeper/zkdatalog/zookeeper.out
stderr_logfile=/opt/zookeeper/zkdatalog/zookeeper.err
---
# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
# vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
---
# yum install elasticsearch
# chkconfig --add elasticsearch
# yum remove java
# yum install java-1.8.0-openjdk.x86_64
# java -version
# yum install kibana
# vim /etc/kibana/kibana.yml
[root@ip-00-00-00-235 conf.d]# cat /etc/kibana/kibana.yml |grep -Ev '^$|#'
server.port: 5601
server.host: "00.00.00.235"
elasticsearch.url: "http://00.00.00.235:9200"
kibana.index: ".kibana"
elasticsearch.pingTimeout: 1500
elasticsearch.requestTimeout: 30000
# /etc/init.d/kibana start
# ps aux |grep kibana
# ./kafka-topics.sh --list --zookeeper 00.00.00.235



---------------
tdagent中转

<source>
@type kafka
brokers 00.00.00.235:9092,00.00.00.218:9092,00.00.00.25:9092
topics logics-auth
offset_zookeeper 00.00.00.235:2181,00.00.00.218:2181,00.00.00.25:2181
offset_zk_root_node /fluent-plugin-kafka
#offset 2458884411
add_prefix logics-auth
</source>

##################################################################################
<match logics-auth**>
type elasticsearch
host 00.00.00.235
port 9200
logstash_format true
logstash_prefix kbilogs
logstash_dateformat logics-auth.%Y.%m.%d
time_key time
flush_interval 10s
request_timeout 15s
reload_on_failure true
reconnect_on_error true
</match>

-----------
auth服务

# cat auth.conf
<source>
type tail
format json
time_key time
time_format %N
refresh_interval 1s
pos_file /var/log/td-agent/logics_auth.log.pos
path /opt/supervisor/log/logics_auth.%Y.%m.%d.%H.log
tag kafka.auth
</source>

<source>
type forward
port 24224
</source>

<match debug.**>
type stdout
</match>

<source>
type debug_agent
bind 127.0.0.1
port 24230
</source>

<match kafka.auth>
@type kafka_buffered
#list of seed brokers
brokers 00.00.00.235:9092,00.00.00.218:9092,00.00.00.25:9092
#buffer settings
buffer_type file
buffer_path /var/log/td-agent/buffer/td-kafka-auth
buffer_chunk_limit 2048m
buffer_queue_limit 32
flush_interval 10s
flush_at_shutdown false
num_threads 1
log_level info
#topic settings
default_topic logics-auth
#data type settings
output_data_type json
compression_codec gzip
#producer settings
max_send_retries 3 #The number of times to retry sending of messages to a leader (default: 1)
required_acks 1 #
ack_timeout 60 #How long the producer waits for acks. The unit is seconds.
</match>