使用 kubectl 版本 1.18 + 创建部署

fja*_*mes 3 kubernetes kubectl

Kubernetes: Up and Running, 2nd Edition的第 67 页,作者使用以下命令来创建Deployment

kubectl run alpaca-prod \
--image=gcr.io/kuar-demo/kuard-amd64:blue \
--replicas=2 \
--labels="ver=1,app=alpaca,env=prod"
Run Code Online (Sandbox Code Playgroud)

然而,此命令在 kubectl 1.19+ 中已弃用,它现在创建一个 pod:

$ kubectl run alpaca-prod \     
--image=gcr.io/kuar-demo/kuard-amd64:blue \
--replicas=2 \
--labels="ver=1,app=alpaca,env=prod"
Flag --replicas has been deprecated, has no effect and will be removed in the future.
pod/alpaca-prod created

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", GitCommit:"e19964183377d0ec2052d1f1fa930c4d7575bd50", GitTreeState:"clean", BuildDate:"2020-08-26T14:30:33Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-21T01:11:42Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

Run Code Online (Sandbox Code Playgroud)

有没有办法使用1.19+kubectl run创建具有副本和自定义标签的部署kubectl

Jon*_*nas 5

现在首选使用kubectl create创建新的Deployment,而不是kubectl run.

这是与您的命令相对应的kubectl run

kubectl create deployment alpaca-prod --image=gcr.io/kuar-demo/kuard-amd64:blue --replicas=2
Run Code Online (Sandbox Code Playgroud)

标签

默认情况下,kubectl create deployment alpaca-proc您将获得标签app=alpaca

获得其他标签后,您需要稍后添加它们。用于kubectl label向 中添加标签Deployment,例如

kubectl label deployment alpaca-prod ver=1
Run Code Online (Sandbox Code Playgroud)

注意:这只会将标签添加到 Pod 模板中Deployment,而不是添加到 Pod 模板中,例如 Pod 将不会获得标签。要将标签添加到 pod,您需要编辑template:Deployment-yaml 的部分。