Skip to content

Setting Up and Managing GCP Instance Groups with Docker Configuration

Simardeep Singh edited this page Jun 12, 2024 · 1 revision

Overview

This guide provides detailed instructions on how to resize a managed instance group in Google Cloud Platform (GCP), attach a persistent disk to an instance within the group, and configure Docker to utilize this disk for its data storage.

Prerequisites

  • A Google Cloud Platform account.
  • A configured GCP project with the necessary API access.
  • Google Cloud SDK installed and configured on your local machine.

1. Resizing the Instance Group

To adjust the number of VM instances in your managed instance group, use the following command:

gcloud compute instance-groups managed resize sdp-instance-group-us-east-5 --size=1 --zone=us-east5-b

This command increases the size of the instance group sdp-instance-group-us-east-5 to 1 instance.

2. Attaching a Persistent Disk

After resizing your instance group and ensuring that your instance is running, attach a persistent disk to the new instance using the following command:

gcloud compute instances attach-disk <enter-your-instance-name> --disk sdp-instance-group-jknz-1 --zone us-east5-b

This command attaches the disk sdp-instance-group-jknz-1 to the instance sdp-instance-group-us-east-5-s6m9.

3. Configuring the Instance to Use Docker with an External Disk

The following steps involve setting up the instance to use Docker, configuring Docker to use an externally attached disk, and ensuring the disk is properly mounted and configured to persist on reboot.

Script: Setup Docker and Mount Disk

Save the following script as setup_docker.sh on your instance:

#!/bin/bash

# Variables
DISK="/dev/sdb"
MOUNT_POINT="/mnt/data"
DOCKER_DATA_DIR="${MOUNT_POINT}/docker"

# Create the mount directory
sudo mkdir -p ${MOUNT_POINT}

# Check if the disk already has a file system
FS=$(sudo blkid -s TYPE -o value ${DISK})
if [ -z "${FS}" ]; then
    # No filesystem, so format the disk with EXT4
    echo "No filesystem detected on ${DISK}, formatting..."
    sudo mkfs -t ext4 ${DISK}
fi

# Mount the disk
echo "Mounting ${DISK} to ${MOUNT_POINT}..."
sudo mount ${DISK} ${MOUNT_POINT}

# Add to /etc/fstab to ensure it mounts on reboot
echo "${DISK} ${MOUNT_POINT} ext4 defaults 0 2" | sudo tee -a /etc/fstab

# Set permissions (optional, change as per your requirement)
sudo chown -R $USER:$USER ${MOUNT_POINT}
sudo chmod -R 755 ${MOUNT_POINT}

echo "${DISK} has been mounted to ${MOUNT_POINT} and configured to mount on boot."

# Check if Docker is installed
if ! [ -x "$(command -v docker)" ]; then
    echo "Docker is not installed. Installing Docker..."
    sudo apt-get update
    sudo apt-get install -y docker-ce
fi

# Configure Docker to use new data directory
echo "Configuring Docker to use ${DOCKER_DATA_DIR}..."
sudo systemctl stop docker
sudo pkill -SIGHUP dockerd

# Configure Docker daemon options
echo "{
  \"data-root\": \"${DOCKER_DATA_DIR}\"
}" | sudo tee /etc/docker/daemon.json

# Restart Docker to apply changes
sudo systemctl daemon-reload
sudo systemctl start docker

echo "Docker has been installed and configured to use ${DOCKER_DATA_DIR} as its data directory."

Running the Script

Ensure the script is executable and run it:

chmod +x setup_docker.sh
./setup_docker.sh