Kubernetes-安装容器标准输出标准错误日志

时间:2019-02-27 23:10:05

标签: docker kubernetes containers stdout stderr

我使用“ chentex/random-logger”图像,该图像将stdout / stderr写入容器。我想制作部署yaml,它运行chentex的映像,并将其记录在共享卷内的文件中。我可以不修改图像就能做到吗?

这是映像的简单部署:

apiVersion: v1
kind: Deployment
metadata:
  name: random-logger
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: random-logger
    spec:
      containers:
      - name: random-logger
        image: chentex/random-logger:latest

1 个答案:

答案 0 :(得分:1)

对于在容器中运行的应用程序,最好将日志消息发送到stdoutchentex/random-logger只是follows this approach,没有任何配置选项,但是我们可以提出这样的黑客建议:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: random-logger
spec:
  selector:
    matchLabels:
      app: random-logger
  template:
    metadata:
      labels:
        app: random-logger
    spec:
      containers:
      - name: random-logger
        image: chentex/random-logger:latest
        command: ["sh", "-c",  "./entrypoint.sh &> /logfile"]

从运行中的pod请求日志时,看不到任何内容

$ kubectl logs random-logger-76c6fd98d5-8d5fm

应用程序日志将写入容器中的logfile

$ kubectl exec random-logger-76c6fd98d5-8d5fm cat /logfile
2019-02-28T00:23:23+0000 DEBUG first loop completed.
2019-02-28T00:23:25+0000 ERROR something happened in this execution.
2019-02-28T00:23:29+0000 INFO takes the value and converts it to string.
2019-02-28T00:23:31+0000 WARN variable not in use.
2019-02-28T00:23:37+0000 INFO takes the value and converts it to string.

尽管这是可能的,但通常不建议这样做。有关更多背景信息,请参见有关Logging Architecture的Kubernetes文档。