kubernetes:kubectl在重新启动时运行onFailure未创建Pod

时间:2019-12-28 16:21:06

标签: kubernetes

我是kubernetes的新手,并使用kubectl run命令创建了一个新的pod,如下所示:

kubectl run new-app --image nginx --restart OnFailure  

并且输出是它正在创建作业而不是pod:

job.batch/new-app created

但是,当我使用“从不重启”选项创建时,会生成pod:

kubectl run new-app --image nginx --restart Never  
pod/new-app created

这是我正在使用的版本:

Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.8", GitCommit:"211047e9a1922595eaa3a1127ed365e9299a6c23", GitTreeState:"clean", BuildDate:"2019-10-15T12:11:03Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.8", GitCommit:"211047e9a1922595eaa3a1127ed365e9299a6c23", GitTreeState:"clean", BuildDate:"2019-10-15T12:02:12Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}

我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

添加生成器可以明确解决以下问题:

kubectl run new-app --image nginx --restart OnFailure --generator=run-pod/v1

根据文档的原因:

默认运行的kubectl添加--generator = job / v1,根据文档,此版本已弃用,并将在以后的版本中删除。

  

kubectl run --generator = job / v1已被弃用,并将在以后的版本中删除。请使用kubectl run --generator = run-pod / v1或kubectl create。

答案 1 :(得分:0)

由于不赞成使用生成器,因此可以使用--restart标志值来创建不同的对象。

kubectl帮助中有关于如何使用--restart标志创建pod / job / cronjob等的清晰示例和说明。

grep应该列出如下所有选项

$ kubectl run --help | grep restart

# Start a pod of busybox and keep it in the foreground, don't restart it if it exits.

$ kubectl run -i -t busybox --image=busybox --restart=Never

在不同的对象创建中,为--result标志使用具有不同值的--restart标志。

      --restart='Always': The restart policy for this Pod.  Legal values [Always, OnFailure, Never].  If set to 'Always' a deployment is created, if set to 'OnFailure' a job is created, if set to 'Never', a regular pod is created. For the latter two --replicas must be 1.  Default 'Always', for CronJobs `Never`.

因此下面的命令将导致创建POD

$ kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

以下命令将导致部署(该部署将创建一个复制集和具有一个副本的相应容器)

$ kubectl run nginx --image=nginx --dry-run -o yaml

kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

推荐的新方法来创建部署kubectl create deployment nginx --image=nginx

以下创建工作

$ kubectl run nginx --image=nginx --restart=OnFailure --dry-run -o yaml
kubectl run --generator=job/v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.

apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
      restartPolicy: OnFailure
status: {}

推荐的新方法来创建部署kubectl create job nginx --image=nginx

下面创建了一个CronJob

$ kubectl run nginx --image=nginx --restart=OnFailure --schedule=*,*,*,*,* --dry-run -o yaml
kubectl run --generator=cronjob/v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  concurrencyPolicy: Allow
  jobTemplate:
    metadata:
      creationTimestamp: null
    spec:
      template:
        metadata:
          creationTimestamp: null
          labels:
            run: nginx
        spec:
          containers:
          - image: nginx
            name: nginx
            resources: {}
          restartPolicy: OnFailure
  schedule: '*,*,*,*,*'
status: {}

推荐的新方法来创建部署kubectl create cronjob nginx --image=nginx