1.安装yum-utils
yum install -y yum-utils device-mapper-persistent-data lvm2
2.为yum源添加docker仓库位置:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
3.安装docker
yum install docker-ce
4.启动docker
systemctl start docker
5.下载镜像(进入docker hub的官网,地址:https://hub.docker.com)
docker pull elasticsearch:7.10.1
docker pull logstash:7.10.1
docker pull kibana:7.10.1
6.Docker Compose工具下载
下载Docker Compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
修改该文件的权限为可执行
chmod 777 /usr/local/bin/docker-compose (777,可读可写可执行)
查看是否已经安装成功
docker-compose --version
查看容器启用(全部)状态
docker ps (-a)
7.修改elasticsearch内存
sysctl -w vm.max_map_count=262144
使之立即生效
sysctl -p
docker-compose up或docker-compose -f docker-compose.yml up -d命令即可启动所依赖的所有服务
docker-compose.yml
version: '3'
services:
mysql:
image: mysql:5.7
container_name: mysql
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
restart: always
environment:
MYSQL_ROOT_PASSWORD: root #设置root帐号密码
ports:
- 3306:3306
volumes:
- /mydata/mysql/data/db:/var/lib/mysql #数据文件挂载
- /mydata/mysql/data/conf:/etc/mysql/conf.d #配置文件挂载
- /mydata/mysql/log:/var/log/mysql #日志文件挂载
redis:
image: redis:5
container_name: redis
command: redis-server --appendonly yes
volumes:
- /mydata/redis/data:/data #数据文件挂载
ports:
- 6379:6379
nginx:
image: nginx:1.10
container_name: nginx
volumes:
- /mydata/nginx/nginx.conf:/etc/nginx/nginx.conf #配置文件挂载
- /mydata/nginx/html:/usr/share/nginx/html #静态资源根目录挂载
- /mydata/nginx/log:/var/log/nginx #日志文件挂载
ports:
- 80:80
rabbitmq:
image: rabbitmq:3.7.15-management
container_name: rabbitmq
volumes:
- /mydata/rabbitmq/data:/var/lib/rabbitmq #数据文件挂载
- /mydata/rabbitmq/log:/var/log/rabbitmq #日志文件挂载
ports:
- 5672:5672
- 15672:15672
elasticsearch:
image: elasticsearch:7.6.2
container_name: elasticsearch
environment:
- "cluster.name=elasticsearch" #设置集群名称为elasticsearch
- "discovery.type=single-node" #以单一节点模式启动
- "ES_JAVA_OPTS=-Xms512m -Xmx512m" #设置使用jvm内存大小
volumes:
- /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载
- /mydata/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载
ports:
- 9200:9200
- 9300:9300
logstash:
image: logstash:7.6.2
container_name: logstash
environment:
- TZ=Asia/Shanghai
volumes:
- /mydata/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf #挂载logstash的配置文件
depends_on:
- elasticsearch #kibana在elasticsearch启动之后再启动
links:
- elasticsearch:es #可以用es这个域名访问elasticsearch服务
ports:
- 4560:4560
- 4561:4561
- 4562:4562
- 4563:4563
kibana:
image: kibana:7.6.2
container_name: kibana
links:
- elasticsearch:es #可以用es这个域名访问elasticsearch服务
depends_on:
- elasticsearch #kibana在elasticsearch启动之后再启动
environment:
- "elasticsearch.hosts=http://39.99.231.6:9200" #设置访问elasticsearch的地址 (设置为自己服务器的ip)
ports:
- 5601:5601
mongo:
image: mongo:4.2.5
container_name: mongo
volumes:
- /mydata/mongo/db:/data/db #数据文件挂载
ports:
- 27017:27017
8.关闭防火墙
防火墙状态查看
systemctl status firewalld(active running 表示运行)
关闭防火墙
systemctl stop firewalld
9.查看容器的日志
docker logs $ContainerName(或者$ContainerId)
10.问题:
1.elasticsearch 起来之后自动关闭
docker exec -it elasticsearch /bin/bash
cd config/
vi elasticsearch.yml
设置network.host为如下图所示
修改保存退出exit之后重启elasticsearch
docker restart elasticsearch
2.curl 127.0.0.1:9200 可以访问但是外网访问不了的问题
1⃣️查看防火墙
2⃣️如果使用的是阿里云的ecs服务器,查看安全组是否打开对应的端口 9200
3⃣️vim /etc/sysctl.conf
4⃣️vim /etc/security/limits.conf
11.SpringBoot应用集成Logstash
<!--集成logstash-->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.3</version>
</dependency>
添加配置文件logback-spring.xml让logback的日志输出到logstash
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<!--应用名称-->
<property name="APP_NAME" value="cinema"/>
<!--日志文件保存路径-->
<property name="LOG_FILE_PATH" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/logs}"/>
<contextName>${APP_NAME}</contextName>
<!--每天记录日志到文件appender-->
<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>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!--输出到logstash的appender-->
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!--可以访问的logstash日志收集端口-->
<destination>33.99.231.6: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>
注意appender节点下的destination需要改成你自己的logstash服务地址,比如我的是:33.99.231.6:4560
运行Springboot应用
在kibana中查看日志信息