kubernetes - K8s init 容器自行重启
问题描述
我有一个包含一个 init 容器和一个应用程序容器的 pod,它们之间有一个带有共享文件夹的卷。
我的问题是,init 容器每天运行一次甚至更多,因此它会从卷中删除节点模块,然后由于缺少模块而导致主应用程序崩溃。应用程序容器没有重新启动,只有 init 容器。
有人熟悉k8s中的这个问题吗?为什么这些重启只发生在 init 容器中?
谢谢 :)
编辑:部署 yaml 文件 -
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "25"
creationTimestamp: "2020-05-19T06:48:18Z"
generation: 25
labels:
apps: ******
commit: ******
name: *******
namespace: fleet
resourceVersion: "24059934"
selfLink: *******
uid: *******
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: *******
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: *******
commit: *******
revision: *******
spec:
containers:
image: XXXXXXXXXXXX
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
path: /ping
port: http
scheme: HTTP
initialDelaySeconds: 120
periodSeconds: 30
successThreshold: 1
timeoutSeconds: 30
name: *******
ports:
- containerPort: 1880
name: http
protocol: TCP
readinessProbe:
failureThreshold: 20
httpGet:
path: /ping
port: http
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 30
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /opt/breeze
name: workdir
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: flowregistrykey
initContainers:
image: XXXXXXXXXXXX
imagePullPolicy: IfNotPresent
name: get-flow-json
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /work-dir
name: workdir
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- emptyDir: {}
name: workdir
status:
availableReplicas: 1
conditions:
- lastTransitionTime: "2020-06-01T12:30:10Z"
lastUpdateTime: "2020-06-01T12:30:10Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2020-05-19T06:48:18Z"
lastUpdateTime: "2020-06-01T12:45:05Z"
message: ReplicaSet "collection-associator.sandbox.services.collection-8784dcb9d"
has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
observedGeneration: 25
readyReplicas: 1
replicas: 1
updatedReplicas: 1
这张图片解释了问题 - pod 是 7 天前创建的,但里面的文件是今天创建的,并且没有 node_modules 文件夹 - 因为只有 init 容器再次运行,而不是 app 容器,所以没有 mpm install
解决方案
init 容器自己运行,因此它从卷中删除节点模块
initContainer 仅在 pod 重新启动时运行。不要将其视为服务或应用程序。它应该是一个脚本,一个仅用于在您的应用程序之前进行设置的工作。
然后由于缺少模块,主应用程序崩溃。
node_modules
不是动态加载。当你加载它npm start
您可能想尝试livenessProbe。
许多长时间运行的应用程序最终会转变为损坏状态,除非重新启动,否则无法恢复。Kubernetes 提供了活性探针来检测和补救这种情况。
initContainers:
- name: set-delete-time
command: ["/bin/sh","-c"]
args:
- |
# Let's set 3600
echo 3600 > deleteTime
...
containers:
- name: node-app
livenessProbe:
exec:
command:
# If deleteTime > 0 exit 0
# Else /tmp/deleteTime - $periodSeconds > /tmp/deleteTime
- cat
- /tmp/deleteTime
- ...
initialDelaySeconds: 5
periodSeconds: 5
推荐阅读
- javascript - 谷歌脚本循环性能
- javascript - 通过jquery复制样式而不删除另一个元素
- git - github 上的 SSH 密钥验证在 crontab 中不起作用
- android - 使用 WiFi 直接 Android 以编程方式创建热点
- symfony - 如何通过 Swagger 装饰器在 API 平台中记录自定义 POST 操作?
- macos - 通过 tapGesture 处理程序保留的幽灵视图
- gcc - 已解决:GCC 比较 String IF else(一种验证您是否写过某个单词的方法)
- docker - 节点重启后 Docker 本地存储库不工作
- quire-api - 您可以通过 Quire API 创建一个新项目吗?
- python - Python,如何检查文件是否存在于 Windows 10 上?