在 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

相关文章

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

mybatis中resultMap的association及collectio的使用详解

《mybatis中resultMap的association及collectio的使用详解》MyBatis的resultMap定义数据库结果到Java对象的映射规则,包含id、type等属性,子元素需... 目录1.reusltmap的说明2.association的使用3.collection的使用4.总

Spring Boot配置和使用两个数据源的实现步骤

《SpringBoot配置和使用两个数据源的实现步骤》本文详解SpringBoot配置双数据源方法,包含配置文件设置、Bean创建、事务管理器配置及@Qualifier注解使用,强调主数据源标记、代... 目录Spring Boot配置和使用两个数据源技术背景实现步骤1. 配置数据源信息2. 创建数据源Be

Java中使用 @Builder 注解的简单示例

《Java中使用@Builder注解的简单示例》@Builder简化构建但存在复杂性,需配合其他注解,导致可变性、抽象类型处理难题,链式编程非最佳实践,适合长期对象,避免与@Data混用,改用@G... 目录一、案例二、不足之处大多数同学使用 @Builder 无非就是为了链式编程,然而 @Builder

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分

mybatis-plus QueryWrapper中or,and的使用及说明

《mybatis-plusQueryWrapper中or,and的使用及说明》使用MyBatisPlusQueryWrapper时,因同时添加角色权限固定条件和多字段模糊查询导致数据异常展示,排查发... 目录QueryWrapper中or,and使用列表中还要同时模糊查询多个字段经过排查这就导致只要whe

Python使用openpyxl读取Excel的操作详解

《Python使用openpyxl读取Excel的操作详解》本文介绍了使用Python的openpyxl库进行Excel文件的创建、读写、数据操作、工作簿与工作表管理,包括创建工作簿、加载工作簿、操作... 目录1 概述1.1 图示1.2 安装第三方库2 工作簿 workbook2.1 创建:Workboo

使用Go实现文件复制的完整流程

《使用Go实现文件复制的完整流程》本案例将实现一个实用的文件操作工具:将一个文件的内容完整复制到另一个文件中,这是文件处理中的常见任务,比如配置文件备份、日志迁移、用户上传文件转存等,文中通过代码示例... 目录案例说明涉及China编程知识点示例代码代码解析示例运行练习扩展小结案例说明我们将通过标准库 os

一文解密Python进行监控进程的黑科技

《一文解密Python进行监控进程的黑科技》在计算机系统管理和应用性能优化中,监控进程的CPU、内存和IO使用率是非常重要的任务,下面我们就来讲讲如何Python写一个简单使用的监控进程的工具吧... 目录准备工作监控CPU使用率监控内存使用率监控IO使用率小工具代码整合在计算机系统管理和应用性能优化中,监

postgresql使用UUID函数的方法

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