• SpringBoot applies ELK log collection
    • An overview of the
      • Architecture diagram
    • Environment set up
      • Base environment dependency
    • Docker installation
      • Install yum-utils (install docker-CE dependencies)
      • Add docker repository location for yum sources
      • Install the docker
      • Start the docker
    • Use Docker to install the corresponding service
      • Elasticsearch installation
        • Run the following command to start the Elasticsearch service
        • An error is reported during startup
        • Install the Chinese word analyzer IKAnalyzer and restart it
        • Access will return version information:http://39.103.203.41:9200/
      • Logstash installation
        • Download the docker image for Logstash7.6.2
        • The following configuration file is logstash
        • Create the/myData /logstash directory and copy the logstash configuration file logstash
        • Run the following command to start the Logstash service
        • Install the jSON_lines plug-in in the Logstash
      • Kibana installation
        • Download the docker image for Kibana7.6.2
        • Run the following command to start the Kibana service
        • Access the address to testhttp://39.103.203.41:5601/
      • SpringBoot application integration Logstash
        • Add the logback-spring. XML configuration file to output logback logs to logstash
        • Run the SpringBoot application
      • View log information in Kibana
        • Create index pattern
        • Invoke the interface and view the logs

An overview of the

After docker container is used, it is necessary to consider how to collect the printed logs of applications located in docker container for operation and maintenance analysis. For example, log collection for SpringBoot applications. The ELK Logging Center is used to collect logs generated by containerized applications and to query and analyze logs in a visual manner, as shown in the following figure

Architecture diagram

Environment set up

Base environment dependency

component The version number
Mysql 5.7
Elasticsearch 7.6.2
Logstash 7.6.2
Kibana 7.6.2

Docker installation

(For different operating systems see Yeasy.gatbook. IO /docker_prac…)

Install yum-utils (install docker-CE dependencies)

 yum install -y yum-utils device-mapper-persistent-data lvm2
Copy the code

Add docker repository location for yum sources

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

yum.repos.d

# Location of warehouse source storage
cd /etc/yum.repos.d 

vim docker-ce.repo 
Copy the code

Install the docker

⚠️ Pay attention to the differences between docker-CE and Docker two warehouse versions (I climb more pit docker does not support file mount older version)

 yum install docker-ce

# If the following information is incorrect
errors during downloading metadata for repository 'base':
  - Curl error (28): Timeout was reached for http://mirrors.aliyuncs.com/centos/3/os/x86_64/repodata/repomd.xml [Connection timed out after 30001 milliseconds]
  - Status code: 404 forhttp://mirrors.cloud.aliyuncs.com/centos/3/os/x86_64/repodata/repomd.xml (IP: 100.100.2.148) - the Status code: 404forhttp://mirrors.aliyun.com/centos/3/os/x86_64/repodata/repomd.xml (IP: 39.96.118.193) Error: Failed to download the metadatafor repo 'base': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried

/etc/yum/repos. D /docker-ce
# Replace the source with version 7 or you will receive an error when downloading the link address does not exist.
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/stable
Copy the code

Start the docker

 systemctl start docker
Copy the code

Use Docker to install the corresponding service

Elasticsearch installation

Run the following command to start the Elasticsearch service

docker run -p 9200:9200 -p 9300:9300 --name elasticsearch \
-e "discovery.type=single-node" \
-e "cluster.name=elasticsearch"\ -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \ -v / mydata/elasticsearch/data: / usr/share/elasticsearch/data \ - d elasticsearch: 7.6.2Copy the code

An error is reported during startup

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated inVersion 9.0 and will likely be removedin a future release.
{"type": "server"."timestamp": "The 2021-06-03 T11:24:02, 468 z"."level": "ERROR"."component": "o.e.b.ElasticsearchUncaughtExceptionHandler"."cluster.name": "elasticsearch"."node.name": "4f5aaf7716c0"."message": "uncaught exception in thread [main]"."stacktrace": ["org.elasticsearch.bootstrap.StartupException: ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];"."At org. Elasticsearch. The bootstrap. Elasticsearch. Init (elasticsearch. Java: 174) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. The bootstrap. Elasticsearch. Execute (elasticsearch. Java: 161) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org.elasticsearch.cli.Com mand. MainWithoutErrorHandling (125). The Command Java: ~ [elasticsearch - cli - 7.6.2. Jar: 7.6.2]"."At org.elasticsearch.cli.Com mand. Main (90). The Command Java: ~ [elasticsearch - cli - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. The bootstrap. Elasticsearch. Main (elasticsearch. Java: 126) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. The bootstrap. Elasticsearch. Main (elasticsearch. Java: 92) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."Caused by: org.elasticsearch.ElasticsearchException: failed to bind service"."At org. Elasticsearch. Node. Node. < init > (node. Java: 615) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. Node. Node. < init > (node. Java: 257) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."at org.elasticsearch.bootstrap.BootstrapA $5. < init > (221) the Bootstrap. Java: ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. The bootstrap. The bootstrap. Setup (221) the bootstrap. Java: ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. The bootstrap. The bootstrap. Init (349) the bootstrap. Java: ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. The bootstrap. Elasticsearch. Init (elasticsearch. Java: 170) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."... 6 more"."Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes"."at sun.nio.fs.UnixException.translateToIOException(UnixException.java:90) ~[?:?] "."at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111) ~[?:?] "."at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116) ~[?:?] "."at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:389) ~[?:?] "."at java.nio.file.Files.createDirectory(Files.java:693) ~[?:?] "."at java.nio.file.Files.createAndCheckIsDirectory(Files.java:800) ~[?:?] "."at java.nio.file.Files.createDirectories(Files.java:786) ~[?:?] "."at org.elasticsearch.env.NodeEnvironment.lambda$new$0(NodeEnvironment. Java: 274) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."at org.elasticsearch.env.NodeEnvironment$NodeLock. < init > (NodeEnvironment. Java: 211) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. Env. NodeEnvironment. < init > (NodeEnvironment. Java: 271) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. Node. Node. < init > (node. Java: 277) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. Node. Node. < init > (node. Java: 257) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."at org.elasticsearch.bootstrap.BootstrapA $5. < init > (221) the Bootstrap. Java: ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. The bootstrap. The bootstrap. Setup (221) the bootstrap. Java: ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. The bootstrap. The bootstrap. Init (349) the bootstrap. Java: ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."At org. Elasticsearch. The bootstrap. Elasticsearch. Init (elasticsearch. Java: 170) ~ [elasticsearch - 7.6.2. Jar: 7.6.2]"."... 6 more"] }
uncaught exception in thread [main]
Copy the code

Start will find/usr/share/elasticsearch/data directory access permissions, only need to modify/mydata/elasticsearch/data directory permissions, restart again can

chmod 777 /mydata/elasticsearch/data/
Copy the code

Install the Chinese word analyzer IKAnalyzer and restart it


Enter the docker container
docker exec -it elasticsearch /bin/bash

This command needs to be run in a containerelasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip - > Installing https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip - > Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 100%?? @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @  WARNING: plugin requires additional permissions @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ * java.net.SocketPermission * connect,resolve See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.htmlfor descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik

The command takes effect after you exit
docker restart elasticsearch
Copy the code

Access will return version information:http://39.103.203.41:9200/

{
  "name" : "4f5aaf7716c0"."cluster_name" : "elasticsearch"."cluster_uuid" : "PBB0ZytDStO-9SJjAarNyw"."version" : {
    "number" : "7.6.2"."build_flavor" : "default"."build_type" : "docker"."build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f"."build_date" : "The 2020-03-26 T06:34:37. 794943 z"."build_snapshot" : false."lucene_version" : "8.4.0"."minimum_wire_compatibility_version" : "6.8.0"."minimum_index_compatibility_version" : "6.0.0 - beta1"
  },
  "tagline" : "You Know, for Search"
}
Copy the code

Logstash installation

Download the docker image for Logstash7.6.2

Docker pull logstash: 7.6.2Copy the code

The following configuration file is logstash

Input {TCP {mode => "server" host => "0.0.0.0" port => 4560 COdec => json_lines type => "debug"} TCP {mode => "Server" host => "0.0.0.0" port => 4561 COdec => json_lines type => "error"} TCP {mode => "server" host => "0.0.0.0" Port => 4562 codec => json_lines type => "business"} TCP {mode => "server" host => "0.0.0.0" port => 4563 codec => json_lines type => "record" } } filter{ if [type] == "record" { mutate { remove_field => "port" remove_field => "host" remove_field => "@version" } json { source => "message" remove_field => ["message"] } } } output { elasticsearch { hosts = > "39.103.203.41:9200 index" = > "mall - % % {type} - {+ YYYY. MM. Dd}"}}Copy the code

Create the/myData /logstash directory and copy the logstash configuration file logstash

mkdir /mydata/logstash
Copy the code

Run the following command to start the Logstash service

docker run --name logstash -p 4560:4560 -p 4561:4561 -p 4562:4562 -p 4563:4563 \ --link elasticsearch:es \ -v / mydata/logstash/logstash. Conf: / usr/share/logstash/pipeline/logstash. Conf \ - d logstash: 7.6.2Copy the code

Install the jSON_lines plug-in in the Logstash

Enter the logstash container
docker exec -it logstash /bin/bash
Go to the bin directory
cd /bin/
Install plugins
logstash-plugin install logstash-codec-json_lines

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated inVersion 9.0 and will likely be removedin a future release.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.headius.backport9.modules.Modules to method sun.nio.ch.NativeThread.signal(long)
WARNING: Please consider reporting this to the maintainers of com.headius.backport9.modules.Modules
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Validating logstash-codec-json_lines
Installing logstash-codec-json_lines
Installation successful
# Exit container
exit
Restart the logstash service
docker restart logstash

Copy the code

Kibana installation

Download the docker image for Kibana7.6.2

7.6.2: Pulling from library/kibana
ab5ef0e58194: Already exists 
c64d415fc4c4: Pull complete 
40a228497f87: Pull complete 
047cebeb3d2b: Pull complete 
a1e90407e522: Pull complete 
b665bda75e65: Pull complete 
12bc27d9cfdc: Pull complete 
2611a8427d9d: Pull complete 
12efd486dee3: Pull complete 
d2dfc5062b56: Pull complete 
Digest: sha256:097e2b7f33f353a8fc19bbf2a6558431c63637113fdc625e6d34fc46f96c0130
Status: Downloaded newer image forKibana: 7.6.2 docker. IO/library/kibana: 7.6.2Copy the code

Run the following command to start the Kibana service

docker run --name kibana -p 5601:5601 \
--link elasticsearch:es \
-e "elasticsearch.hosts=http://es:9200"Kibana \ - d: 7.6.2Copy the code

Access the address to testhttp://39.103.203.41:5601/

SpringBoot application integration Logstash

<! - integration logstash -- -- >
<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>5.3</version>
</dependency>
Copy the code

Add the logback-spring. XML configuration file to output logback logs to logstash

Note that destination under the appender node needs to be changed to your own Logstash service address. For example, mine is 39.103.203.41:4560


      
<! DOCTYPEconfiguration>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <! -- Application name -->
    <property name="APP_NAME" value="mall-admin"/>
    <! -- Log file save path -->
    <property name="LOG_FILE_PATH" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/logs}"/>
    <contextName>${APP_NAME}</contextName>

    <! Log to file appender every day
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE_PATH}/${APP_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <! Output to logstash appender-->
    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <! Accessible Logstash log collection port -->
        <destination>39.103.203.41:4560</destination>
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
        <appender-ref ref="LOGSTASH"/>
    </root>
</configuration>
Copy the code

Run the SpringBoot application

package com.macro.mall.tiny;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MallApplication {

 public static void main(String[] args) { SpringApplication.run(MallApplication.class, args); }}Copy the code

View log information in Kibana

Create index pattern

Invoke the interface and view the logs