在 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

相关文章

Java Lambda表达式的使用详解

《JavaLambda表达式的使用详解》:本文主要介绍JavaLambda表达式的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、前言二、Lambda表达式概述1. 什么是Lambda表达式?三、Lambda表达式的语法规则1. 无参数的Lambda表

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Spring @RequestMapping 注解及使用技巧详解

《Spring@RequestMapping注解及使用技巧详解》@RequestMapping是SpringMVC中定义请求映射规则的核心注解,用于将HTTP请求映射到Controller处理方法... 目录一、核心作用二、关键参数说明三、快捷组合注解四、动态路径参数(@PathVariable)五、匹配请

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

Java中的record使用详解

《Java中的record使用详解》record是Java14引入的一种新语法(在Java16中成为正式功能),用于定义不可变的数据类,这篇文章给大家介绍Java中的record相关知识,感兴趣的朋友... 目录1. 什么是 record?2. 基本语法3. record 的核心特性4. 使用场景5. 自定

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

Python验证码识别方式(使用pytesseract库)

《Python验证码识别方式(使用pytesseract库)》:本文主要介绍Python验证码识别方式(使用pytesseract库),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录1、安装Tesseract-OCR2、在python中使用3、本地图片识别4、结合playwrigh