01 引言
注意:需要保证elk
套件版本统一,升级需要从es
开始!
02 部署kafka
备注:假设在172.16.3.5机子上执行
2.1 下载
① 下载kafka
至/opt
目录:
mkdir -p ~/tools/kafka cd ~/tools/kafka wget https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/3.0.0/kafka_2.13-3.0.0.tgz tar xf kafka_2.13-3.0.0.tgz mv kafka_2.13-3.0.0 /opt/
② 创建软链接:
ln -s /opt/kafka_2.13-3.0.0 /opt/kafka
2.2 配置
① 配置用户以及权限:
useradd kafka chown -R kafka:kafka /opt/kafka chown -R kafka:kafka /opt/kafka_2.13-3.0.0/
② 配置kafka data log
:
mkdir -p /var/log/kafka chown -R kafka:kafka /var/log/kafka
③ 编辑kafka配置 :
在/opt/kafka/bin/kafka-server-start.sh
最后一行exec
之前加入以下内容
export KAFKA_HEAP_OPTS=" -Xmx6144m -Xms16m " export KAFKA_JVM_PERFORMANCE_OPTS=" -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=1024m "
④ 执行以下指令配置kafka
的server.properties
,注意修改IP
信息和zookeeper
配置:
cat <<'EOF' > /opt/kafka/config/server.properties broker.id=0 listeners=PLAINTEXT://:9092 advertised.listeners=PLAINTEXT://172.16.3.5:9092 num.network.threads=8 num.io.threads=16 socket.send.buffer.bytes=102400 socket.receive.buffer.bytes=102400 socket.request.max.bytes=104857600 log.dirs=/var/log/kafka/ num.partitions=1 num.recovery.threads.per.data.dir=2 offsets.topic.replication.factor=1 transaction.state.log.replication.factor=1 transaction.state.log.min.isr=1 log.retention.hours=1 log.segment.bytes=536870912 log.retention.check.interval.ms=300000 log.cleanup.policy=delete log.retention.hours=4 log.retention.bytes=1073741824 zookeeper.connect=172.16.3.5:2181 zookeeper.connection.timeout.ms=6000 group.initial.rebalance.delay.ms=0 EOF
⑤ 执行以下指令编辑kafka
的systemd
服务文件:
cat <<'EOF' > /etc/systemd/system/kafka.service [Unit] Description=kafka After=network-online.target [Service] WorkingDirectory=/opt/kafka ExecStart=/opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server.properties Type=forking User=kafka LimitNOFILE=32768 Restart=always [Install] WantedBy=multi-user.target EOF
2.3 启动
systemctl daemon-reload systemctl enable kafka systemctl restart kafka
03 部署ES
备注:假设在172.16.3.4)机子上执行
注意: 修改配置文件
/etc/elasticsearch/elasticsearch.yml
中的hosts
等配置信息
3.1 添加es源
① 添加es
的gpg key
:
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys xxx
② 添加es的源:
echo "deb https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/apt/ stable main" > /etc/apt/sources.list.d/es.list
3.2 安装es
① 安装es
:
apt update apt install elasticsearch=7.15.1 -y
② 设置apt
保持es
版本:
apt-mark hold elasticsearch
3.3 配置es
先停止服务,然后配置es jvm
堆内存:
systemctl stop elasticsearch echo '-Xms7g' > /etc/elasticsearch/jvm.options.d/jvm.options echo '-Xmx7g' >> /etc/elasticsearch/jvm.options.d/jvm.options # 配置es cat <<'EOF' > /etc/elasticsearch/elasticsearch.yml path.data: /var/lib/elasticsearch path.logs: /var/log/elasticsearch network.host: 0.0.0.0 http.port: 9200 discovery.seed_hosts: ["172.16.3.4"] cluster.initial_master_nodes: ["172.16.3.4"] xpack.security.enabled: false EOF
3.4 启动es
① 启动es:
systemctl restart elasticsearch
② 设置es开机自启动:
systemctl enable elasticsearch
4. 部署kibana
备注:假设在172.16.3.5)机子上执行,与kafaka同一台机子
注意:修改配置文件
/etc/kibana/kibana.yml
中的elasticsearch.hosts
和server.publicBaseUrl
等配置信息:
4.1 添加kibana源
① 添加es
的gpg key
:
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys xxx # 添加es的源 echo "deb https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/apt/ stable main" > /etc/apt/sources.list.d/es.list
4.2 安装kibana
① 安装kibana:
apt update apt install -y kibana=7.15.1
② 设置apt
保持kibana
版本:
apt-mark hold kibana
4.3 配置kibana
先停止kibana
,然后配置:
systemctl stop kibana cat <<'EOF' > /etc/kibana/kibana.yml server.port: 5601 server.host: "0.0.0.0" elasticsearch.hosts: ["http://172.16.3.4:9200"] i18n.locale: "zh-CN" logging.quiet: true server.basePath: "/kibana" server.publicBaseUrl: "https://demo.kexinxiaoyuan.cn/kibana" EOF
4.4 启动kibana
启动并设置kibana
自动启动:
systemctl restart kibana systemctl enable kibana
5. 配置域名转发
5.1 haproxy配置
① 配置域名转发,ssh
登录nginx
服务器,先修改haproxy
配置/etc/haproxy/haproxy.cfg
,添加:
listen kibana bind 127.0.0.1:5601 mode tcp option tcp-check balance roundrobin server kibana_b_1 172.16.3.5:5601 check inter 2000 rise 2 fall 3
② 修改保存/etc/haproxy/haproxy.cfg
文件后,执行:
# 测试配置文件是否有问题 haproxy -c -f /etc/haproxy/haproxy.cfg # 若上一步结果显示 Configuration file is valid ,则重启服务 systemctl restart haproxy.service
5.2 nginx配置
① 添加nginx
转发,编辑nginx
配置文件/etc/nginx/sites-enabled/demo.kexinxiaoyuan.cn
,在server(443)
段添加:
# kibana location /kibana { auth_basic "need auth"; auth_basic_user_file /etc/nginx/.kibana_passwd; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Proto https; proxy_pass http://127.0.0.1:5601; rewrite ^/kibana/(.*)$ /$1 break; }
② 创建访问kibana
的http
认证文件,并重载nginx
,执行:
# 创建kibana密码文件 echo 'xxx:$sfdfdfsdfsd/' > /etc/nginx/.kibana_passwd # 测试Nginx配置 nginx -t # 重载Nginx nginx -s reload
最后:kibana(ELK)
访问地址:https://demo.xxx.cn/kibana/