'Kubectl imperative command for deployment

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?



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 esboych
Solution 2 Machavity
Solution 3 paltaa
Solution 4 Parthasarathy B