How to use docker volume update command to manage cluster volume availability

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage the availability of Docker cluster volumes using the docker volume update command. You will begin by creating a cluster volume, which is essential for sharing data across nodes in a Docker Swarm.

Following the creation, you will explore how to change the volume's availability state. This includes updating the availability to pause, which prevents new tasks from using the volume, and then to drain, which aims to move tasks away from nodes where the volume is unavailable. Finally, you will learn how to set the availability back to active, allowing the volume to be fully utilized by new and existing tasks. This lab provides practical experience in controlling how your cluster volumes are accessed and used within a Swarm environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") subgraph Lab Skills docker/ls -.-> lab-555263{{"How to use docker volume update command to manage cluster volume availability"}} docker/inspect -.-> lab-555263{{"How to use docker volume update command to manage cluster volume availability"}} docker/volume -.-> lab-555263{{"How to use docker volume update command to manage cluster volume availability"}} end

Create a cluster volume

In this step, we will learn how to create a cluster volume in Docker. A cluster volume is a volume that can be accessed by multiple containers across different nodes in a Docker Swarm. This is useful for applications that need to share data, such as databases or file servers.

Before creating a cluster volume, we need to ensure that Docker Swarm is initialized. If you haven't initialized Swarm yet, you can do so with the following command:

docker swarm init

You should see output indicating that the swarm has been initialized.

Now, let's create a cluster volume. We will use the docker volume create command with the --driver local flag and specify the volume name. The local driver is the default driver and is suitable for creating volumes on a single node. For a cluster volume, Docker Swarm handles the distribution and access across nodes.

docker volume create my-cluster-volume

After running the command, you should see the name of the created volume printed to the console:

my-cluster-volume

To verify that the volume has been created, you can list the existing volumes using the docker volume ls command:

docker volume ls

You should see my-cluster-volume listed in the output.

Update cluster volume availability to pause

In this step, we will learn how to update the availability of a cluster volume to pause. When a volume's availability is set to pause, new tasks that require this volume will not be scheduled on nodes where the volume is unavailable. Existing tasks using the volume will continue to run. This state is useful for temporarily preventing new usage of a volume without disrupting currently running services.

We will use the docker volume update command to change the availability of the my-cluster-volume we created in the previous step. The --availability flag is used to specify the desired availability state.

docker volume update --availability pause my-cluster-volume

After executing the command, you should see output confirming the update:

my-cluster-volume

To verify that the volume's availability has been updated to pause, we can inspect the volume using the docker volume inspect command and look for the Availability field.

docker volume inspect my-cluster-volume

In the output of the inspect command, look for the Availability field within the Spec section. It should show pause.

[
  {
    "CreatedAt": "...",
    "Driver": "local",
    "Labels": {},
    "Mountpoint": "/var/lib/docker/volumes/my-cluster-volume/_data",
    "Name": "my-cluster-volume",
    "Options": null,
    "Scope": "local",
    "Spec": {
      "Availability": "pause"
    },
    "Status": {}
  }
]

Update cluster volume availability to drain

In this step, we will learn how to update the availability of a cluster volume to drain. When a volume's availability is set to drain, new tasks that require this volume will not be scheduled on nodes where the volume is unavailable. Additionally, Docker Swarm will attempt to shut down existing tasks that are using the volume and reschedule them on nodes where the volume is available. This state is useful for gracefully migrating services away from a volume before performing maintenance or removing it.

We will again use the docker volume update command, but this time we will set the --availability flag to drain.

docker volume update --availability drain my-cluster-volume

You should see the volume name printed as confirmation:

my-cluster-volume

To verify that the volume's availability has been updated to drain, we will inspect the volume using docker volume inspect and check the Availability field.

docker volume inspect my-cluster-volume

In the output, confirm that the Availability field within the Spec section is now drain.

[
  {
    "CreatedAt": "...",
    "Driver": "local",
    "Labels": {},
    "Mountpoint": "/var/lib/docker/volumes/my-cluster-volume/_data",
    "Name": "my-cluster-volume",
    "Options": null,
    "Scope": "local",
    "Spec": {
      "Availability": "drain"
    },
    "Status": {}
  }
]

Update cluster volume availability to active

In this final step, we will learn how to update the availability of a cluster volume back to active. When a volume's availability is set to active, new tasks that require this volume can be scheduled on any node where the volume is available. This is the default state for a volume and indicates that it is ready for use by services.

We will use the docker volume update command one last time, setting the --availability flag to active.

docker volume update --availability active my-cluster-volume

You should see the volume name printed as confirmation:

my-cluster-volume

To verify that the volume's availability has been successfully set back to active, we will inspect the volume using docker volume inspect and check the Availability field.

docker volume inspect my-cluster-volume

In the output, confirm that the Availability field within the Spec section is now active.

[
  {
    "CreatedAt": "...",
    "Driver": "local",
    "Labels": {},
    "Mountpoint": "/var/lib/docker/volumes/my-cluster-volume/_data",
    "Name": "my-cluster-volume",
    "Options": null,
    "Scope": "local",
    "Spec": {
      "Availability": "active"
    },
    "Status": {}
  }
]

You have now successfully created a cluster volume and practiced updating its availability between pause, drain, and active states.

Summary

In this lab, we learned how to manage the availability of Docker cluster volumes using the docker volume update command. We began by creating a cluster volume using docker volume create.

Subsequently, we explored how to modify the volume's availability state. We learned to set the availability to pause, which prevents new tasks from using the volume while allowing existing tasks to continue. We then saw how to change the availability to drain, which attempts to stop tasks using the volume and reschedule them elsewhere. Finally, we learned to set the availability back to active, allowing new tasks to utilize the volume. These steps demonstrated the flexibility of managing cluster volume access within a Docker Swarm environment.