我正试图将侧车容器添加到现有的pod (webapp-1)以保存日志。然而,我得到错误后创建pod。pod正在崩溃,状态变为error.
对于下面的问题,我添加了yaml文件。如果可以的话,请告诉我。
按吹风规格在跑步舱日志舱中增加一个侧车集装箱
sidecar容器的映像为busybox,容器写入的日志如下
tail -n+1/var/log/k8log/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: {}