将 sidecar 容器日志拖尾到现有 pod
Tailing sidecar container logs to an existing pod
我正在尝试将边车容器添加到现有 pod (webapp-1) 以保存日志。但是,创建 Pod 后出现错误。 pod 崩溃,状态变为错误..
对于下面的问题我添加了yaml文件。如果可以,请告诉我。
给运行 pod logging-pod添加side car容器,blow规范
sidecar容器镜像为busybox,容器写入日志如下
tail -n+1 /var/log/k8slog/application.log
容器与挂载到
的应用程序容器共享卷日志
目录/var/log/k8slog
不要更改应用程序容器并验证日志是否已正确写入文件
这是yaml文件..我不明白我在哪里犯了错误
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2021-10-25T07:54:07Z"
labels:
name: webapp-1
name: webapp-1
namespace: default
resourceVersion: "3241"
uid: 8cc29748-7879-4726-ac60-497ee41f7bd6
spec:
containers:
- image: kodekloud/event-simulator
imagePullPolicy: Always
name: simple-webapp
- /bin/sh
- -c
- >
i=0;
while true;
do
echo "$i: $(date)" >> /var/log/k8slog/application.log
echo "$(date) INFO $i" >>;
i=$((i+1));
sleep 1;
done
volumeMounts:
- name: varlog
mountPath: /var/log
- name: count-log-1
image: busybox
args: [/bin/sh, -c, 'tail -n+1 /var/log/k8slog/application.log']
volumeMounts:
- name: varlog
mountPath: /var/log
ports:
- containerPort: 8080
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-fgstk
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: controlplane
preemptionPolicy: PreemptLowerPriority
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: varlog
mountPath: /var/log
- name: default-token-fgstk
secret:
defaultMode: 420
secretName: default-token-fgstk
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2021-10-25T07:54:07Z"
status: "True"
type: Initialized
- lastProbeTime: null
首先,您可以创建目录和日志文件本身。如果 count-log-1
容器首先启动,它将没有任何内容可读取并退出并出错。为此,一个好的做法是使用 Init Container。 https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
其次,容器需要有一个共享卷,日志文件将存在于该共享卷上。如果不需要持久化数据,一个 emptyDir 卷就足够了。 https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
最后,您在 shell 命令中出现了一些错误。完整 .yaml
个文件:
apiVersion: v1
kind: Pod
metadata:
labels:
name: webapp-1
name: webapp-1
namespace: default
spec:
# Init container fo creating the log directory and file
# on the emptyDir volume, which will be passed to the containers
initContainers:
- name: create-log-file
image: busybox
command:
- sh
- -c
- |
#!/bin/sh
mkdir -p /var/log/k8slog
touch /var/log/k8slog/application.log
# Mount varlog volume to the Init container
volumeMounts:
- name: varlog
mountPath: /var/log
containers:
- image: kodekloud/event-simulator
imagePullPolicy: Always
name: simple-webapp
command:
- sh
- -c
- |
i=0
while true; do
echo "$i: $(date)" >> /var/log/k8slog/application.log
echo "$(date) INFO $i"
i=$((i+1))
sleep 1
done
# Mount varlog volume to simple-webapp container
volumeMounts:
- name: varlog
mountPath: /var/log
- name: count-log-1
image: busybox
command:
- sh
- -c
- |
tail -f -n 1 /var/log/k8slog/application.log
# Mount varlog volume to count-log-1 container
volumeMounts:
- name: varlog
mountPath: /var/log
# Define na emptyDir shared volume
volumes:
- name: varlog
emptyDir: {}
我正在尝试将边车容器添加到现有 pod (webapp-1) 以保存日志。但是,创建 Pod 后出现错误。 pod 崩溃,状态变为错误..
对于下面的问题我添加了yaml文件。如果可以,请告诉我。
给运行 pod logging-pod添加side car容器,blow规范
sidecar容器镜像为busybox,容器写入日志如下
tail -n+1 /var/log/k8slog/application.log
容器与挂载到
的应用程序容器共享卷日志目录/var/log/k8slog
不要更改应用程序容器并验证日志是否已正确写入文件
这是yaml文件..我不明白我在哪里犯了错误
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2021-10-25T07:54:07Z"
labels:
name: webapp-1
name: webapp-1
namespace: default
resourceVersion: "3241"
uid: 8cc29748-7879-4726-ac60-497ee41f7bd6
spec:
containers:
- image: kodekloud/event-simulator
imagePullPolicy: Always
name: simple-webapp
- /bin/sh
- -c
- >
i=0;
while true;
do
echo "$i: $(date)" >> /var/log/k8slog/application.log
echo "$(date) INFO $i" >>;
i=$((i+1));
sleep 1;
done
volumeMounts:
- name: varlog
mountPath: /var/log
- name: count-log-1
image: busybox
args: [/bin/sh, -c, 'tail -n+1 /var/log/k8slog/application.log']
volumeMounts:
- name: varlog
mountPath: /var/log
ports:
- containerPort: 8080
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-fgstk
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: controlplane
preemptionPolicy: PreemptLowerPriority
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: varlog
mountPath: /var/log
- name: default-token-fgstk
secret:
defaultMode: 420
secretName: default-token-fgstk
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2021-10-25T07:54:07Z"
status: "True"
type: Initialized
- lastProbeTime: null
首先,您可以创建目录和日志文件本身。如果 count-log-1
容器首先启动,它将没有任何内容可读取并退出并出错。为此,一个好的做法是使用 Init Container。 https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
其次,容器需要有一个共享卷,日志文件将存在于该共享卷上。如果不需要持久化数据,一个 emptyDir 卷就足够了。 https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
最后,您在 shell 命令中出现了一些错误。完整 .yaml
个文件:
apiVersion: v1
kind: Pod
metadata:
labels:
name: webapp-1
name: webapp-1
namespace: default
spec:
# Init container fo creating the log directory and file
# on the emptyDir volume, which will be passed to the containers
initContainers:
- name: create-log-file
image: busybox
command:
- sh
- -c
- |
#!/bin/sh
mkdir -p /var/log/k8slog
touch /var/log/k8slog/application.log
# Mount varlog volume to the Init container
volumeMounts:
- name: varlog
mountPath: /var/log
containers:
- image: kodekloud/event-simulator
imagePullPolicy: Always
name: simple-webapp
command:
- sh
- -c
- |
i=0
while true; do
echo "$i: $(date)" >> /var/log/k8slog/application.log
echo "$(date) INFO $i"
i=$((i+1))
sleep 1
done
# Mount varlog volume to simple-webapp container
volumeMounts:
- name: varlog
mountPath: /var/log
- name: count-log-1
image: busybox
command:
- sh
- -c
- |
tail -f -n 1 /var/log/k8slog/application.log
# Mount varlog volume to count-log-1 container
volumeMounts:
- name: varlog
mountPath: /var/log
# Define na emptyDir shared volume
volumes:
- name: varlog
emptyDir: {}