当kubectl将一个生命周期补丁到容器时,请求无效



在我的情况下,我必须先部署一个部署,然后在jenkins中为部署补丁一个preStop钩子。

我尝试使用

kubectl -n mobile patch deployment hero-orders-app --type "json" -p '[
{"op":"add","path":"/spec/template/spec/containers/0/lifecycle/preStop/exec/command","value":[]},
{"op":"add","path":"/spec/template/spec/containers/0/lifecycle/preStop/exec/command/-","value":"/bin/sleep"},
{"op":"add","path":"/spec/template/spec/containers/0/lifecycle/preStop/exec/command/-","value":"10"}]'

但是它返回

the request is invaild

如果patch命令可以添加不存在的路径?或者我需要换一个解决方案?

这里是hero-orders-app部署文件

apiVersion: apps/v1
kind: Deployment
metadata:
name: hero-orders-app$SUFFIX
namespace: $K8S_NAMESPACE
labels:
branch: $LABEL
run: hero-orders-app$SUFFIX
spec:
selector:
matchLabels:
run: hero-orders-app$SUFFIX
revisionHistoryLimit: 5
minReadySeconds: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
template:
metadata:
labels:
run: hero-orders-app$SUFFIX
namespace: $K8S_NAMESPACE
branch: $LABEL
role: hero-orders-app
spec:
dnsConfig:
options:
- name: ndots
value: "1"
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
run: hero-orders-app$SUFFIX
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: run
operator: In
values:
- hero-orders-app$SUFFIX
topologyKey: kubernetes.io/hostname
imagePullSecrets:
- name: $K8S_IMAGE_SECRETS
containers:
- name: hero-orders-$CLUSTER_NAME
image: $K8S_IMAGE
imagePullPolicy: IfNotPresent
securityContext:
runAsUser: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- CHOWN
- NET_RAW
- SETPCAP
ports:
- containerPort: 3000
protocol: TCP
resources:
limits:
cpu: $K8S_CPU_LIMITS
memory: $K8S_RAM_LIMITS
requests:
cpu: $K8S_CPU_REQUESTS
memory: $K8S_RAM_REQUESTS
readinessProbe:
httpGet:
path: /gw-api/v2/_manage/health
port: 3000
initialDelaySeconds: 15
timeoutSeconds: 10
livenessProbe:
httpGet:
path: /gw-api/v2/_manage/health
port: 3000
initialDelaySeconds: 20
timeoutSeconds: 10
periodSeconds: 45

它运行在AWS上,有service, pdb和hpa。

这是我的kubectl版本

Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:59:11Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.9", GitCommit:"7a576bc3935a6b555e33346fd73ad77c925e9e4a", GitTreeState:"clean", BuildDate:"2021-07-15T20:56:38Z", GoVersion:"go1.15.14", Compiler:"gc", Platform:"linux/amd64"}

我将使用一个更简单的部署来演示修补生命周期钩子,您可以在自己的部署中使用相同的技术。

apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
spec:
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox
command: ["ash","-c","while :; do echo $(date); sleep 1; done"]

路径为/spec/template/spec/containers/0/lifecycle,否则您将得到响应"请求无效。">

kubectl patch deployment busybox --type json -p '[{"op":"add","path":"/spec/template/spec/containers/0/lifecycle","value":{"preStop": {"exec": {"command": ["/bin/sleep","10"]}}}}]'
deployment.apps/busybox patched

修补后,部署将重新启动。您可以执行kubectl get deployment busybox -o yaml来检查补丁。如果您再次使用相同的值进行补丁,则不会发生变化。

最新更新