Create deployments with kubectl version 1.18 +

Viewed 636

At page 67 of Kubernetes: Up and Running, 2nd Edition, the author uses the command below in order to create a Deployment:

kubectl run alpaca-prod \
--image=gcr.io/kuar-demo/kuard-amd64:blue \
--replicas=2 \
--labels="ver=1,app=alpaca,env=prod"

However this command is deprecated with kubectl 1.19+, and it now creates a pod:

$ kubectl run alpaca-prod \     
--image=gcr.io/kuar-demo/kuard-amd64:blue \
--replicas=2 \
--labels="ver=1,app=alpaca,env=prod"
Flag --replicas has been deprecated, has no effect and will be removed in the future.
pod/alpaca-prod created

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", GitCommit:"e19964183377d0ec2052d1f1fa930c4d7575bd50", GitTreeState:"clean", BuildDate:"2020-08-26T14:30:33Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-21T01:11:42Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

Is there a way to use kubectl run to create a deployment with replicas and custom label with kubectl 1.19+?

3 Answers

It is now preferred to use kubectl create to create a new Deployment, instead of kubectl run.

This is the corresponsing command to your kubectl run

kubectl create deployment alpaca-prod --image=gcr.io/kuar-demo/kuard-amd64:blue --replicas=2

Labels

By default from kubectl create deployment alpaca-proc you will get the label app=alpaca.

The get the other labels, you need to add them later. Use kubectl label to add labels to the Deployment, e.g.

kubectl label deployment alpaca-prod ver=1

Note: this only adds the label to the Deployment and not to the Pod-template, e.g. the Pods will not get the label. To also add the label to the pods, you need to edit the template: part of the Deployment-yaml.

Note: With kubectl version 1.18 things have changed. Like its no longer possible to use kubectl run to create Jobs, CronJobs or Deployments, only Pods still work.

So yes you cannot create a deployment from kubectl run from 1.18.

Step 1: Create deployment from kubectl create command

kubectl create deploy alpaca-prod --image=gcr.io/kuar-demo/kuard-amd64:blue --replicas=2

Step 2 Update labels with kubectl label command

kubectl label deploy -l app=alpaca-prod ver=1

kubectl label deploy -l app=alpaca-prod app=alpaca

kubectl label deploy -l app=alpaca-prod env=prod

Here is the yaml file which produces the expected result for p67 of 'Kubernetes: Up and Running, 2nd Edition':

apiVersion: apps/v1
kind: Deployment
metadata:
  name: alpaca-prod
spec:
  selector:
    matchLabels:
      ver: "1"
      app: "alpaca"
      env: "prod"
  replicas: 2
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        ver: "1"
        app: "alpaca"
        env: "prod"
    spec:
      containers:
      - name: kuard
        image: gcr.io/kuar-demo/kuard-amd64:blue
Related