k8s日志收集 - 利用HOSTNAME环境变量对多个pod挂载不同目录方案

k8s的日志收集有多种方案,其中一种是将pod的日志挂载到node节点,然后在node节点运行单独的日志收集客户端,将日志收集并分发到其他日志分析服务如elasticsearch上。

1、阿里云k8s部署服务日志挂载参数

注释:启动后处理:是pod创建成功后执行的命令命令:["/bin/sh","-c","mkdir -p /home/mount/${HOSTNAME} && ln -s /home/mount/${HOSTNAME} /home/wx/services/es-provider-auth/logs"]

 

2、挂载后node节点查看挂载情况

3、yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: ‘10‘
  creationTimestamp: ‘2020-04-30T04:58:12Z‘
  generation: 10
  labels:
    app: es-api-gateway
    appname: es-api-gateway
    env: pro
  name: es-api-gateway
  namespace: default
  resourceVersion: ‘1322821‘
  selfLink: /apis/apps/v1/namespaces/default/deployments/es-api-gateway
  uid: c9164f6e-e8f9-4221-95bf-ce974154c280
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: es-api-gateway
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: es-api-gateway
        appname: es-api-gateway
        env: pro
    spec:
      containers:
        - env:
            - name: LANG
              value: en_US.UTF-8
            - name: LANGUAGE
              value: ‘en_US:en‘
            - name: LC_ALL
              value: en_US.UTF-8
            - name: JAVA_HOME
              value: /opt/java/openjdk
          image: >-
            registry-vpc.cn-beijing.aliyuncs.com/wx-k8s-pro/es-api-gateway:v20200505145433
          imagePullPolicy: IfNotPresent
          lifecycle:
            postStart:
              exec:
                command:
                  - /bin/sh
                  - ‘-c‘
                  - >-
                    mkdir -p /home/mount/${HOSTNAME} && ln -s 
                    /home/mount/${HOSTNAME} 
                    /home/wx/services/es-api-gateway/logs
          name: es-api-gateway
          ports:
            - containerPort: 11000
              name: http
              protocol: TCP
          resources:
            limits:
              cpu: ‘2‘
              memory: 2500Mi
            requests:
              cpu: 300m
              memory: 2500Mi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /etc/localtime
              name: volume-localtime
            - mountPath: /home/mount
              name: volume-1588659668375
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
        - hostPath:
            path: /etc/localtime
            type: ‘‘
          name: volume-localtime
        - hostPath:
            path: /home/wx/services/es-api-gateway
            type: ‘‘
          name: volume-1588659668375
status:
  availableReplicas: 2
  conditions:
    - lastTransitionTime: ‘2020-04-30T05:11:14Z‘
      lastUpdateTime: ‘2020-04-30T05:11:14Z‘
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: ‘True‘
      type: Available
    - lastTransitionTime: ‘2020-04-30T05:11:11Z‘
      lastUpdateTime: ‘2020-05-05T06:56:08Z‘
      message: ReplicaSet "es-api-gateway-7d467dc799" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: ‘True‘
      type: Progressing
  observedGeneration: 10
  readyReplicas: 2
  replicas: 2
  updatedReplicas: 2