在 k8s(kubernetes)中使用 Loki 进行日志监控

2023-11-09 10:59

本文主要是介绍在 k8s(kubernetes)中使用 Loki 进行日志监控,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

2a81a52021c1d2b79674fabac21eb5c4.png

安装helm环境

[root@hello ~/yaml]#
[root@hello ~/yaml]# curl https://baltocdn.com/helm/signing.asc | sudo apt-key add -
[root@hello ~/yaml]# sudo apt-get install apt-transport-https --yes
[root@hello ~/yaml]# echo "deb https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
deb https://baltocdn.com/helm/stable/debian/ all main
[root@hello ~/yaml]# sudo apt-get update
[root@hello ~/yaml]# sudo apt-get install helm
[root@hello ~/yaml]#

添加安装下载源

[root@hello ~/yaml]# helm repo add loki https://grafana.github.io/loki/charts && helm repo update
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /root/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /root/.kube/config
"loki" has been added to your repositories
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /root/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /root/.kube/config
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "loki" chart repository
Update Complete. ⎈Happy Helming!⎈
[root@hello ~/yaml]#
[root@hello ~/yaml]#[root@hello ~/yaml]# helm pull loki/loki-stack
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /root/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /root/.kube/config
[root@hello ~/yaml]# ls
loki-stack-2.1.2.tgz  nfs-storage.yaml  nginx-ingress.yaml
[root@hello ~/yaml]# tar xf loki-stack-2.1.2.tgz
[root@hello ~/yaml]# ls
loki-stack  loki-stack-2.1.2.tgz  nfs-storage.yaml  nginx-ingress.yaml

安装loki日志系统

[root@hello ~/yaml]# helm install loki loki-stack/
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /root/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /root/.kube/config
WARNING: This chart is deprecated
W1203 07:31:04.751065  212245 warnings.go:70] policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
W1203 07:31:04.754254  212245 warnings.go:70] policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
W1203 07:31:04.833003  212245 warnings.go:70] policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
W1203 07:31:04.833003  212245 warnings.go:70] policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
NAME: loki
LAST DEPLOYED: Fri Dec  3 07:31:04 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
The Loki stack has been deployed to your cluster. Loki can now be added as a datasource in Grafana.See http://docs.grafana.org/features/datasources/loki/ for more detail.
[root@hello ~/yaml]#

查看安装后是否完成

[root@hello ~/yaml]# helm list -A
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /root/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /root/.kube/config
NAME    NAMESPACE   REVISION    UPDATED                                 STATUS      CHART               APP VERSION
loki    default     1           2021-12-03 07:31:04.3324429 +0000 UTC   deployed    loki-stack-2.1.2    v2.0.0    
[root@hello ~/yaml]#[root@hello ~/yaml]# kubectl  get pod
NAME                                     READY   STATUS    RESTARTS   AGE
loki-0                                   0/1     Running   0          68s
loki-promtail-79tn8                      1/1     Running   0          68s
loki-promtail-qzjjs                      1/1     Running   0          68s
loki-promtail-zlt7p                      1/1     Running   0          68s
nfs-client-provisioner-dc5789f74-jsrh7   1/1     Running   0          44m
[root@hello ~/yaml]#

查看svc并修改类型

[root@hello ~/yaml]# kubectl  get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes      ClusterIP   10.68.0.1       <none>        443/TCP    4h44m
loki            ClusterIP   10.68.140.107   <none>        3100/TCP   2m58s
loki-headless   ClusterIP   None            <none>        3100/TCP   2m58s
[root@hello ~/yaml]#

将svc设置为 type: NodePort

[root@hello ~/yaml]# kubectl  edit  svc loki
service/loki edited
[root@hello ~/yaml]#
[root@hello ~/yaml]# kubectl  get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.68.0.1       <none>        443/TCP          4h46m
loki            NodePort    10.68.140.107   <none>        3100:31089/TCP   4m34s
loki-headless   ClusterIP   None            <none>        3100/TCP         4m34s
[root@hello ~/yaml]#

添加nginx应用

[root@hello ~/yaml]# vim nginx-app.yaml
[root@hello ~/yaml]# cat nginx-app.yaml
apiVersion: apps/v1 
kind: Deployment
metadata:name: nginx
spec:selector:matchLabels:app: nginxreplicas: 1 template:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.7.9ports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:name: nginxlabels:app: nginxjobLabel: nginx
spec:ports:- name: nginxport: 80protocol: TCPselector:app: nginxtype: NodePort
[root@hello ~/yaml]#

查看nginx的pod

[root@hello ~/yaml]# kubectl apply -f nginx-app.yaml 
deployment.apps/nginx created
service/nginx created[root@hello ~/yaml]# kubectl get pod  | grep nginx
nginx-5d59d67564-7fj4b                   1/1     Running   0          29s
[root@hello ~/yaml]#

测试访问

[root@hello ~/yaml]# kubectl  get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.68.0.1       <none>        443/TCP          4h57m
loki            NodePort    10.68.140.107   <none>        3100:31089/TCP   15m
loki-headless   ClusterIP   None            <none>        3100/TCP         15m
nginx           NodePort    10.68.150.95    <none>        80:31317/TCP     105s
[root@hello ~/yaml]# 
[root@hello ~/yaml]# while true; do curl --silent --output /dev/null --write-out '%{http_code}' http://192.168.1.12:31317; sleep 1; echo; done

在grafana中添加源

d0ba95eded764002faaf7edc3f223a1b.png

查看日志

15d94f7fc492dd9d8d2f59f95f3e189d.png

添加面板

5c253ba6a3055b20d9d56a00e3eb9906.png

a6954d631d6b9d05841fa283aeac0b52.png

2ea335c15859d3f5b054224fcfbe0f90.png

https://blog.csdn.net/qq_33921750

https://my.oschina.net/u/3981543

https://www.zhihu.com/people/chen-bu-yun-2

https://segmentfault.com/u/hppyvyv6/articles

https://juejin.cn/user/3315782802482007

https://space.bilibili.com/352476552/article

https://cloud.tencent.com/developer/column/93230

知乎、CSDN、开源中国、思否、掘金、哔哩哔哩、腾讯云

这篇关于在 k8s(kubernetes)中使用 Loki 进行日志监控的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/375699

相关文章

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

MySQL进行数据库审计的详细步骤和示例代码

《MySQL进行数据库审计的详细步骤和示例代码》数据库审计通过触发器、内置功能及第三方工具记录和监控数据库活动,确保安全、完整与合规,Java代码实现自动化日志记录,整合分析系统提升监控效率,本文给大... 目录一、数据库审计的基本概念二、使用触发器进行数据库审计1. 创建审计表2. 创建触发器三、Java

Zabbix在MySQL性能监控方面的运用及最佳实践记录

《Zabbix在MySQL性能监控方面的运用及最佳实践记录》Zabbix通过自定义脚本和内置模板监控MySQL核心指标(连接、查询、资源、复制),支持自动发现多实例及告警通知,结合可视化仪表盘,可有效... 目录一、核心监控指标及配置1. 关键监控指标示例2. 配置方法二、自动发现与多实例管理1. 实践步骤

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)