30
votes

I want to debug the pod in a simple way, therefore I want to start the pod without deployment.

But it will automatically create a deployment

$ kubectl run nginx --image=nginx --port=80
deployment "nginx" created

So I have to create the nginx.yaml file

---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx
      image: nginx
      ports:
        - containerPort: 80

And create the pod like below, then it creates pod only

kubectl create -f nginx.yaml
pod "nginx" created

How can I specify in the command line the kind:Pod to avoid deployment?

// I run under minikue 0.20.0 and kubernetes 1.7.0 under Windows 7

6

6 Answers

49
votes
kubectl run nginx --image=nginx --port=80 --restart=Never

--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 [...]

see official document https://kubernetes.io/docs/user-guide/kubectl-conventions/#generators

6
votes

Now there are two ways one can create pod through command line.

kubectl run nginx --image=nginx --restart=Never

OR

kubectl run --generator=run-pod/v1 nginx1 --image=nginx

See official documentation. https://kubernetes.io/docs/reference/kubectl/conventions/#generators

3
votes

Use generators for this, default kubectl run will create a deployment object. If you want to override this behavior use "run-pod/v1" generator.

kubectl run --generator=run-pod/v1 nginx1 --image=nginx

You may refer the link below for better understanding.

https://kubernetes.io/docs/reference/kubectl/conventions/#generators

3
votes

I'm relatively new to kubernetes, but it seems it has evolved quite a bit since this question was asked. As of its latest versions(I'm running v1.16) generators are deprecated and they are completely removed in v1.18. See the corresponding ticket and the release notes. Release notes explicitly say:

Remove all the generators from kubectl run. It will now only create pods.

I've tested kubectl run with various --restart flags and never got any deployments created. What we do have now is called "naked" Pod. And while you might be tempted to use it, it goes against k8s best practices:

Don’t use naked Pods (that is, Pods not bound to a ReplicaSet or Deployment) if you can avoid it. Naked Pods will not be rescheduled in the event of a node failure.

-1
votes

When you are using "kubectl run nginx --image=nginx --port=80" it creates a deployment by default. To create a pod you have two options.

  1. kubectl run --generator=run-pod/v1 nginx --image=nginx --port=80
  2. kubectl create pod nginx --image=nginx --port=80
-3
votes

Do you mean "expose a service"? I think this command line will help you to do it.

 kubectl expose pod nginx  --type=LoadBalancer --port=80