Why kubectl run create deplyment sometimes

7/6/2019

Can I know why kubectl run sometimes create deployment and sometimes pod.

You can see 1st one creates pod and 2nd one creates deployment. only diff is --restart=Never

// 1
chams@master:~/yml$ kubectl run ng --image=ngnix --command --restart=Never --dry-run -o yaml
apiVersion: v1
kind: Pod
..
status: {}

//2
chams@master:~/yml$ kubectl run ng --image=ngnix --command  --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: ng
  name: ng
..
status: {}
-- Chaminda
kubectl
kubernetes
kubernetes-deployment

2 Answers

7/6/2019

The flags are intentionally meant to create different kind of objects. I am copying from the help of kubectl run:

  --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`.
  • Never acts like a cronjob which is scheduled immediately.
  • Always creates a deployment and the deployment monitors the pod and restarts in case of failure.
-- Vishal Biyani
Source: StackOverflow

7/9/2019

By default kubectl run command creates a Deployment.

Using kubectl run command you can create and run a particular image, possibly replicated. Creates a deployment or job to manage the created containers.

The difference in your case is seen in command (1st one) including restart policy argument.

If value of restart policy is set to 'Never', a regular pod is created. For the latter two --replicas must be 1. Default 'Always', for CronJobs Never.

Try to use command:

$ kubectl run --generator=run-pod/v1 ng --image=ngnix --command  --dry-run -o yaml

instead of

$ kubectl run ng --image=ngnix --command  --dry-run -o yaml

to avoid statement "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."

More information you can find here:docker-kubectl, kubectl-run.

-- MaggieO
Source: StackOverflow