Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mount a persistent disk to a container volume?

I'm playing around with Google's managed VM feature and finding you can fairly easily create some interesting setups. However, I have yet to figure out whether it's possible to use persistent disks to mount a volume on the container, and it seems not having this feature limits the usefulness of managed VMs for stateful containers such as databases.

So the question is: how can I mount the persistent disk that Google creates for my Compute engine instance, to a container volume?

like image 498
Marcus Stade Avatar asked Nov 01 '22 13:11

Marcus Stade


1 Answers

Attaching a persistent disk to a Google Compute Engine instance

Follow the official persistent-disk guide:

  • Create a disk
  • Attach to an instance during instance creation, or to a running instance
  • Use the tool /usr/share/google/safe_format_and_mount to mount the device file /dev/disk/by-id/google-...
  • As noted by Faizan, use docker run -v /mnt/persistent_disk:/container/target/path to include the volume in the docker container

Referencing a persistent disk in Google Container Engine

In this method, you specify the volume declaratively (after initializing it as mentioned above...) in the Replication Controller or Pod declaration. The following is a minimal excerpt of a replication controller JSON declaration. Note that the volume has to be declared read-only because no more than two instances may write to a persistent disk at one time.

{
    "id": "<id>",
    "kind": "ReplicationController",
    "apiVersion": "v1beta1",
    "desiredState": {
        "replicas": 3,
        "replicaSelector": {
            "name": "<id>"
        },
        "podTemplate": {
            "desiredState": {
                "manifest": {
                    "version": "v1beta1",
                    "id": "<id>",
                    "containers": [
                        {
                            "name": "<id>",
                            "image": "<docker_image>",
                            "volumeMounts": [
                                {
                                    "name": "persistent_disk",
                                    "mountPath": "/pd",
                                    "readOnly": true
                                }
                            ],
                            ...
                        }
                    ],
                    "volumes": [
                        {
                            "name": "persistent_disk",
                            "source": {
                                "persistentDisk": {
                                    "pdName": "<persistend_disk>",
                                    "fsType": "ext4",
                                    "readOnly": true
                                }
                            }
                        }
                    ]
                }
            },
            "labels": {
                "name": "<id>"
            }
        }
    },
    "labels": {
        "name": "<id>"
    }
}
like image 58
Sandro Giessl Avatar answered Nov 11 '22 11:11

Sandro Giessl