Is there a way to get UID in pod spec

9/4/2018

What I want to do is providing pod with unified log store, currently persisted to hostPath, but I also want this path including UID so I can easily get its path after pod destroyed.

For example:

apiVersion: v1
kind: Pod
metadata:
  name: pod-with-logging-support
spec:
  containers:
  - image: python:2.7
    name: web-server
    command:
    - "sh"
    - "-c"
    - "python -m SimpleHTTPServer > /logs/http.log 2>&1"
    volumeMounts:
    - mountPath: /logs
      name: log-dir
  volumes:
  - name: log-dir
    hostPath:
      path: /var/log/apps/{metadata.uid}
      type: DirectoryOrCreate

metadata.uid is what I want to fill in, but I do not how to do it.

-- xudifsd
kubernetes

2 Answers

9/4/2018

For logging it's better to use another strategy.

I suggest you to look at this link.

Your logs are best managed if streamed to stdout and grabbed by an agent, like shown in this picture:

enter image description here

Don't persist your log on filesystem, but gather them using an agent and put them together for further analysis.

Fluentd is very popular and deserves to be known.

-- Nicola Ben
Source: StackOverflow

9/6/2018

After searching the doc from kubernetes, I finally see a solution for my specific problem. This feature is exactly what I wanted.

So I can create the pod with

apiVersion: v1
kind: Pod
metadata:
  name: pod-with-logging-support
spec:
  containers:
  - image: python:2.7
    name: web-server
    command:
    - "sh"
    - "-c"
    - "python -m SimpleHTTPServer > /logs/http.log 2>&1"
    env:
    - name: POD_UID
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.uid
    volumeMounts:
    - mountPath: /logs
      name: log-dir
      subPath: $(POD_UID)
  volumes:
  - name: log-dir
    hostPath:
      path: /var/log/apps/
      type: DirectoryOrCreate
-- xudifsd
Source: StackOverflow