kubectl run with restart onFailure not creating pod

12/28/2019

I am new to kubernetes and created a new pod using using the kubectl run command as follows:

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

and the output is that it is creating a job and not a pod:

job.batch/new-app created

However, when i create using the "restart Never" option, the pod is getting generated:

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

Here is the version, which i am using:

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"}

What I am doing wrong here?

-- KayV
kubernetes

2 Answers

1/7/2020

Since generators are to be deprecated you can play with --restart flag values to get different objects created.

kubectl help has clear examples and explanation on how to use --restart flag for creating pod/job/cronjob etc ..

grep should list all options as below

$ 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

Using --restart flag with different values for --result flag in different object creation .

      --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`.

Hence Below command will result in creation of 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: {}

Below command will result in a Deployment ( which creates a replication set and corresponding pod with one replica)

$ 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: {}

New recommended way to create deployment kubectl create deployment nginx --image=nginx

Below creates a Job

$ 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: {}

New recommended way to create deployment kubectl create job nginx --image=nginx

Below creates a 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: {}

New recommended way to create deployment kubectl create cronjob nginx --image=nginx

-- DT.
Source: StackOverflow

12/30/2019

Adding the generator explicitly solved the issue as follows:

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

Reason as per documentation:

kubectl run by default adds --generator=job/v1, and as per documentation, this is DEPRECATED and will be removed in future versions.

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.

-- KayV
Source: StackOverflow