Kubectl imperative command for deployment

4/9/2020

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?

-- RamPrakash
kubectl
kubernetes

4 Answers

4/11/2020

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.

-- paltaa
Source: StackOverflow

5/9/2020

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
-- user13507210
Source: StackOverflow

5/12/2020

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.

-- esboych
Source: StackOverflow

4/9/2020

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

-- vaishal shah
Source: StackOverflow