Joey Yi Zhao
Joey Yi Zhao

Reputation: 42556

When do I use `apply` when to use `rollout`?

I am new to kubernete and have a bit confused about apply and rollout command. If I update the kubernete configuration file, should I use kubectl apply -f or kubectl rollout?

if I update kubernete configuration and I run kubectl apply -f, it will terminate the running pod and create a new one.

but rollout also has restart command which is used to restart the pod. so when should I use rollout restart?

Upvotes: 8

Views: 3384

Answers (3)

Mukus
Mukus

Reputation: 5033

Expanding on Krishna's answer.

Using deployment as an example.

You're using apply -f <filename> if you're creating pods for the first time, adding new pods, altering config like replicas or if you have versioned images in the pods and you're bumping up or down the version.

With the last point where you are bumping up or down the version of image (used by the pod), if you were to use the latest and you run apply -f <filename> kubernetes is not going to see any change in file. But if you did in fact update the image of the pod and you want to rollout the new change, you would have to use rollout.

Additionally with rollout you can use revisions of your rollout (history) to go back (undo), pause deployment and resume paused deployments.

For more information on rollout refer to Deployments

Upvotes: 1

Dashrath Mundkar
Dashrath Mundkar

Reputation: 9202

The kubectl apply -f used to apply the configuration file kubernetes(where your deploy your desired application).

And kubectl rollout is used to check the above deployed application

Example

Suppose your deployment configuration file looks like this and you saved that in nginx.yaml file. Now you want deploy the nginx app from the below yaml file. so you should use kubectl apply -f nginx.yaml, and now you want to check if your application deployed successfully or not using kubectl rollout status nginx

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

and if you updated the yaml locally and you want replace that with existing then use kubectl replace -f nginx.yaml

Upvotes: 6

Krishna Chaurasia
Krishna Chaurasia

Reputation: 9580

One major difference I can think of is kubectl apply can be used for all Kubernetes objects (Pod, Deployment, ConfigMaps, Secrets, etc.) where as the kubectl rollout applies specifically to objects that deals with some computation like Deployments, Statefulsets, etc.

Further, kubectl rollout restart is useful to restart the pods without requiring any changes in the spec fields which is not possible with kubectl apply. If we run kubectl apply with no changes in the spec fields, the pods will not get updated as there is no change to update.

Consider the scenario where some configuration (say, external certificate) is mounted to the pods as ConfigMap and any change in the ConfigMap do not cause the pods to get updated automatically. kubectl rollout restart can be useful in such scenarios to create new pods which can then read updated configurations from the ConfigMap.

Also, a important note from the docs:

Note: A Deployment's rollout is triggered if and only if the Deployment's Pod template (that is, .spec.template) is changed, for example if the labels or container images of the template are updated. Other updates, such as scaling the Deployment, do not trigger a rollout.

Upvotes: 11

Related Questions