使用 Loki 和 NFS 在 Kubernetes 上获取日志

的是为了考虑到学生们的需求。

日志监视是保持系统稳定运行的重要事项。通过收集日志,可以帮助检测系统故障、可疑操作和异常行为,并在解决问题时提供帮助。本次使用 Kubernetes 上的 helm 来安装 Loki,并与 Grafana 进行协作,构建一个可以可视化和搜索日志的环境。

与其他日志系统不同,Loki是基于仅创建与日志相关的元数据索引的概念构建的。
然后,日志数据本身被压缩,并以块的形式保存在诸如S3或GCS之类的对象存储或本地文件系统中。
通过小型索引和高度压缩的块,操作得到简化,从而使Loki的成本显著降低。

使用NFS服务器来保存日志。
尽管Loki不推荐使用NFS服务器作为保存位置,但为了验证Loki在包括Kubernetes在内所有本地环境中的运行情况,可以使用NFS服务器。

    第 1 章: この章

使用 NFS 作为卷,并安装 Loki。

    第 2 章: Kubernetes + Grafana Loki + Promtail で任意の VM のログ収集する

安装Promtail,并将其集中到新安装的Loki中。

构成

使用三台Linux机器(Ubuntu 20.04)按照以下配置构建了Kubernetes集群。
在Kubernetes上已经运行了Prometheus + Grafana。
此外,还准备了一台Linux机器(Ubuntu 20.04)作为NFS服务器。

    • Kubernetes (Master Node)

10.100.2.220

Kubernetes (Worker Node)

10.100.2.221
10.100.2.222

NFS サーバ

10.100.2.170

步骤

安装 Helm

    • 実行場所

Kubernetes (Master Node)

安装Kubernetes的包管理器Helm。

curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm

确认是否已安装。

$ helm version
version.BuildInfo{Version:"v3.12.3", GitCommit:"3a31588ad33fe3b89af5a2a54ee1d25bfe6eaa5e", GitTreeState:"clean", GoVersion:"go1.20.7"}

搭建NFS服务器

    • 実行場所

NFS サーバ

搭建 NFS 服务器。
本次我们将共享 /export/nfs 目录以下的文件。

sudo apt-get install nfs-kernel-server
sudo mkdir -p /export/nfs
sudo chmod 1777 /export/nfs/.
sudo sh -c 'echo "/export/nfs 10.100.2.0/22(rw,async,no_root_squash) 127.0.0.1/32(rw,async,no_root_squash)" | tee -a /etc/exports'
sudo systemctl restart nfs-server rpcbind
sudo systemctl enable nfs-server rpcbind

使用 NFS 服务器

    • 実行場所

Kubernetes (Master Node)
Kubernetes (Worker Node)

在所有的服务器上运行与NFS客户端对应的任务。

sudo apt-get install nfs-common

动态配置程序

    • 実行場所

Kubernetes (Master Node)

没有为NFS上的区域提供默认的Dynamic Provisioner功能。
所以,需要安装External Provisioner。
对于NFS,可以选择使用NFS Ganesha或使用NFS子目录作为External Provisioner。

    • NFS Ganesha server and external provisioner

 

    Kubernetes NFS Subdir External Provisioner

本次使用NFS子目录的情况。

添加 Helm 存储库

添加Helm仓库。

helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
helm repo update

安装

安装Kubernetes NFS Subdir External Provisioner。

helm install -n monitoring nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner \
--set nfs.server=10.100.2.170 \
--set nfs.path=/export/nfs/loki

确认。

$ kubectl get all -n monitoring | grep nfs
pod/nfs-subdir-external-provisioner-7697b6f886-gl5pw         1/1     Running   0          176m
deployment.apps/nfs-subdir-external-provisioner       1/1     1            1           176m
replicaset.apps/nfs-subdir-external-provisioner-7697b6f886       1         1         1       176m

洛基

    • 実行場所

Kubernetes (Master Node)

Loki是由许多组件微服务构建而成的,旨在以水平方向可扩展的分布式系统方式运行的设计。

可扩展安装需要AWS S3、Google Cloud Storage等托管对象存储,或者需要Minio等自助托管存储。

Loki 可以以 Single Binary Mode 模式进行安装。
Single Binary Mode 是一种以文件系统存储为基础的单体模式,它被设计为在此模式下运行并执行 Loki 的所有目标。
本次选择以 Single Binary Mode 进行安装。

添加 Helm 仓库

添加 Helm 仓库。

helm repo add grafana https://grafana.github.io/helm-charts
helm repo update

创建配置文件

创建一个用于Loki启动时使用的配置文件。
通过在gateway.serivce中设置NodePort,可以打开外部连接端口。
在下一篇文章中,将连接到此端口,从任意虚拟机获取日志。

cat << _EOF_ > loki-values-single.yaml
loki:
  auth_enabled: false
  containerSecurityContext:
    readOnlyRootFilesystem: false
  podSecurityContext:
    fsGroup: 0
    runAsNonRoot: false
    runAsUser: 0
  commonConfig:
    replication_factor: 1
  storage:
    type: filesystem
  commonStorageConfig:
    type: filesystem
  persistence:
    enabled: true
  rulerConfig:
    storage:
      type: local
singleBinary:
  replicas: 1
  persistence:
    enabled: true
    storageClass: nfs-client
    size: 10Gi
gateway:
  service:
    nodePort: 31001
    type: NodePort
table_manager:
  retention_deletes_enabled: true
  retention_period: 24h
_EOF_

安装

安装Loki时,请使用–namespace参数指定与Prometheus + Grafana在同一命名空间中运行的名称空间。

helm install  --namespace monitoring --values loki-values-single.yaml loki grafana/loki

确认。

$ kubectl get all -n monitoring | grep loki
pod/loki-0                                                   1/1     Running   0          3h58m
pod/loki-canary-xcq8z                                        1/1     Running   0          3h58m
pod/loki-gateway-67569c4d49-tgrtg                            1/1     Running   0          3h58m
pod/loki-grafana-agent-operator-85c78d5968-mvszg             1/1     Running   0          3h58m
pod/loki-logs-8crbw                                          2/2     Running   0          3h58m
service/loki                                      ClusterIP   10.104.90.215   <none>        3100/TCP,9095/TCP               3h58m
service/loki-canary                               ClusterIP   10.101.17.20    <none>        3500/TCP                        3h58m
service/loki-gateway                              NodePort    10.109.55.79    <none>        80:31001/TCP                    3h58m
service/loki-headless                             ClusterIP   None            <none>        3100/TCP                        3h58m
service/loki-memberlist                           ClusterIP   None            <none>        7946/TCP                        3h58m
daemonset.apps/loki-canary                               1         1         1       1            1           <none>                   3h58m
daemonset.apps/loki-logs                                 1         1         1       1            1           <none>                   3h58m
deployment.apps/loki-gateway                          1/1     1            1           3h58m
deployment.apps/loki-grafana-agent-operator           1/1     1            1           3h58m
replicaset.apps/loki-gateway-67569c4d49                          1         1         1       3h58m
replicaset.apps/loki-grafana-agent-operator-85c78d5968           1         1         1       3h58m
statefulset.apps/loki                                                   1/1     3h58m

Grafana 整合

使用浏览器连接到Grafana,并与安装的Loki进行协作。

    • [Home] > [Addministration] > [Data sources]

 

    • [+ Add new data source]

 

    • [Logging & document databases] の [Loki]

 

    • [HTTP]

URL: http://loki-gateway

[Save & test]

如果执行以上操作没有出现错误,就没有问题。

    [Home] > [Explore]

通过”Explore”功能,可以查看日志。
以下是名称为loki-0的Pod的日志。

qiita-loki-log1.PNG

总结

本次我在本地环境搭建了NFS服务器和Kubernetes集群,并尝试了Loki的运行。
此外,通过使用Dynamic Provisoner,我能够动态地分配NFS卷而无需事先准备PV,从而可以查看日志。
Loki本身可以进行日志的聚合和检索,但需要引入其他工具来收集日志。
由于我只安装了Loki,因此无法收集在Kubernetes上运行的其他Pod等的日志。
在下一篇文章中,我将引入Promtail作为工具,以便从任意的虚拟机或其他Pod等收集日志。

同时,目前的状态是以单个二进制文件运行,无法水平扩展Loki。
下一目标是在本地验证Grafana Loki微服务的运行。

bannerAds