Updating Multiple Objects

Learn how to update multiple deployments at a time.

The use of selector labels#

Even though most of the time we send requests to specific objects, almost everything is happening using selector labels. When we updated the Deployments, they looked for matching selectors to choose which ReplicaSets to create and scale. They, in turn, created or terminated Pods also using the matching selectors.

Almost everything in Kubernetes is operated using label selectors. It’s just that sometimes that is obscured from us.

We do not have to update an object only by specifying its name or the YAML file where its definition resides. We can also use labels to decide which object should be updated. That opens some interesting possibilities since the selectors might match multiple objects.

Defining the deployment#

Imagine that we are running several Deployments with Mongo databases and that the time has come to update them all to a newer release. Before we explore how we could do that, we’ll create another Deployment so that we have at least two with the database Pods.

Let us first take a look at the definition of different-app-db.

Definition of `different-app-db`

When compared with the go-demo-2-db Deployment, the only difference is in the service label. Both have the type set to db.

Creating the deployment#

Let’s create the deployment.

Deploy `different-app-db`

Now that we have two deployments with the mongo:3.3 Pods, we can try to update them both at the same time.

The trick is to find a label (or a set of labels) that uniquely identifies all the Deployments we want to update.

Let’s take a look at the list of Deployments with their labels.

Deployment label

The output is as follows.

Output of 'get deployments'

We want to update mongo Pods created using different-app-db and go-demo-2-db Deployments. Both are uniquely identified with the labels type=db and vendor=MongoLabs. Let’s test that.

Get specific deployments

The output is as follows.

Output of updated deployments

Updating the deployments#

We can see that filtering with those two labels worked. We retrieved only the Deployments we want to update, so let’s proceed and roll out the new release.

Update deployments

The output is as follows.

Output of above command

Finally, before we move into the next subject, we should validate that the image indeed changed to mongo:3.4.

Describe 'go-demo-2.yml'

The output, limited to the relevant parts, is as follows.

Output of above command

As we can see, the update was indeed successful, at least with that Deployment. Feel free to describe the Deployment defined in different-app-db.yml. You should see that its image was also updated to the newer version.

Try it yourself#

A list of all the commands used in the lesson is given below.

Commands used in this lesson

You can practice the commands in the following code playground by pressing the Run button and waiting for the cluster to set up:

/
different-app-db.yml
go-demo-2.yml
Code Playground
Merging Everything into the Same YAML Definition
Scaling Deployments
Mark as Completed
Report an Issue