基于hostpath的k8s pod日志持久化

前置条件:

  1. 考虑到pod的多副本,但同时需要将日志集中收集起来,所以采用hostpath的方式将同一个服务的多pod的日志持久化到一起,日志穿插性的写在一个文件中。
  2. 由于pod重启会打散分配到不同节点上,所以基于nfs的网络文件系统通过共享目录的方式挂载到客户端节点(nfs-server:/mnt/hostpath; nfs-client: /mnt/hostpath,将nfs-server的/mnt/hostpath挂载到nfs-client的/mnt/hostpath下,从而达到同服务多pod可以写到一个文件中 )。

step1:修改服务的yaml文件

在原有的yaml文件中添加数据持久化配置(如截图中红框所示)
cat enterprise-api.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: enterprise-api-v1
  name: enterprise-api-v1
  namespace: hd-cool
spec:
  replicas: 2
  selector:
    matchLabels:
      app: enterprise-api-v1
  template:
    metadata:
      labels:
        app: enterprise-api-v1
    spec:
      imagePullSecrets:
        - name: hd-admin-token
      containers:
      - image: harbor.my.cn/hd-k8s/enterprise-api:latest
        name: enterprise-api-v1
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: data-log
          mountPath: /data/log

      volumes: 
      - name: data-log
        hostPath:
          path: /mnt/hostpath

在这里插入图片描述
可以看到初次启动pod的时候定义了两个副本,但是日志都只持久化到了/mnt/hostpath/下
在这里插入图片描述
将pod的副本数调整到三个,依然在/mnt/hostpath/只有enterprise-api这个目录,同时打开access的日志,会发现有三个pod服务启动加载时要读取的配置参数,说明pod的日志是穿插写入到文件中的。
在这里插入图片描述
在这里插入图片描述

step2:推送日志到minio版保存

由于已经将nfs-server的共享目录已经挂载到所有的nfs-client上,所以在任一节点配置minio客户端工具,将/mnt/hostpath的数据定期推到minio服务中存储起来(结合crontab和清理策略),方便后期需要和查询。
在这里插入图片描述

step3:优化

也可以无需挂载,直接将nfs挂载到pod中,(如截图中红框所示)。
cat enterprise-api.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: enterprise-api-v1
  name: enterprise-api-v1
  namespace: hd-cool
spec:
  replicas: 2
  selector:
    matchLabels:
      app: enterprise-api-v1
  template:
    metadata:
      labels:
        app: enterprise-api-v1
    spec:
      imagePullSecrets:
        - name: hd-admin-token
      containers:
      - image: harbor.my.cn/hd-k8s/enterprise-api:latest
        name: enterprise-api-v1
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: data-log
          mountPath: /data/log

      volumes: 
      - name: data-log
         nfs: 
            server: 172.16.99.140
            path: /mnt/hostpath

在这里插入图片描述

附加:简单了解

有时间需要将同一个服务的多副本pod的日志持久化到本地,方便更加pod的名称更加快速的查看完整的日志,则可以通过subpathexpr可以实现,详见官网文档介绍。
修改yaml文件为,(如截图中红框所示):
cat enterprise-api.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: enterprise-api-v1
  name: enterprise-api-v1
  namespace: hd-cool
spec:
  replicas: 2
  selector:
    matchLabels:
      app: enterprise-api-v1
  template:
    metadata:
      labels:
        app: enterprise-api-v1
    spec:
      imagePullSecrets:
        - name: hd-admin-token
      containers:
      - image: harbor.my.cn/hd-k8s/enterprise-api:latest
        name: enterprise-api-v1
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.name
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: data-log
          mountPath: /data/log
          subPathExpr: $(POD_NAME)

      volumes: 
      - name: data-log
        hostPath:
          path: /mnt/hostpath

在这里插入图片描述

可以看到不管是同一个服务的几个副本,日志都是独立持久化到对应的podname的文件夹中
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44729138/article/details/121431971