xudifsd
xudifsd

Reputation: 850

Is there a way to get UID in pod spec

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.

Upvotes: 0

Views: 2364

Answers (2)

Nicola Ben
Nicola Ben

Reputation: 11357

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.

Upvotes: 2

xudifsd
xudifsd

Reputation: 850

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

Upvotes: 1

Related Questions