栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于hostpath的k8s pod日志持久化

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于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的文件夹中

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572349.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号