Kubectl imperative command for deployment

Viewed 3068

I used to create deployments quickly using imperative commands

kubectl run nginx --image=nginx --restart=Always --port=80 --replicas=3 .

Now run command with deployment seems to have been deprecated. Is there any other way to do the same with kubectl create... with replicas and ports?

6 Answers

Since the Kubernetes v1.18 the kubectl run will no longer create deployments but pods.

What might be used instead is the imperative option of kubectl create deployment. So the following command:

k create deploy nginx --image nginx

will do the trick for you. It will create Deployment object in imperative way. (No need for intermediate yaml files)

# Run:
kubectl create deploy nginx --image nginx && kubectl scale deploy nginx --replicas 3

# Check:
kubectl get deploy
NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
nginx                        3/3     3            3           14s

Note there is no --replicas flag of kubectl create deployemnt so the scaling is controlled separately.

Try this-

kubectl create deploy nginx --image=nginx --dry-run -o yaml > webapp.yaml

change the replicas to 5 in the yaml and create it

kubectl create -f webapp.yaml

Okay, the generators were deprecated because of the pain it was to maintain that code. For easy deployment generator via CLI the best recommendation its helm3, it now doesn't need tillier and its very straightforward to use:

https://helm.sh/docs/intro/install/

Then, after installing running an Nginx deployment via CLI:

Add the repo

helm repo add bitnami https://charts.bitnami.com/bitnami

Also, you can first check what is going to get installed by adding --dry-run

helm install Nginx bitnami/nginx --dry-run

Then run without --dry-run if you are satisfied with what is going to get deployed.

I am using kubernetes : v1.22.5 Usage of imeperative command:

kubectl create deploy mydep --image=nginx --replicas=3 --dry-run=client -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mydep
  name: mydep
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mydep
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mydep
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

The imperative way to do this, including creating the replicas on the commandline without first saving the yaml and then editing the yaml, would be by running the following command:

kubectl create deploy nginx --image nginx --replicas 3 --port 80

you can add the --restart=Always switch if you need it to the above command.

And, if you still want to save the yaml, for any reason, like pushing it to git, you should be able to redirect the above command as usual. No change in the way shell redirection works.

kubectl create deploy nginx --image nginx --replicas 3 --port 80 --output yaml --dry-run=client > file.yaml

Create nginx-deployment.yaml file with below content.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80

and run kubectl create -f nginx-deployment.yaml

Related