EC2 EBS-Volumes

Amazon EBS (Elastic Block Store), EC2 Instances ile kullanılabilen depolama alanları sağlamaktadır. Temel düzeyde bir Instance için “Volume nasıl oluşturulur ve kaldırılır?“ bu süreci ele almak istiyorum. İlk olarak bir EC2 Instance’a sahip olduğunuzu ve sisteminizde AWS CLI kullanabildiğinizi varsayıyorum. 

$ aws --version
aws-cli/2.7.21 Python/3.9.11 Linux/5.15.0-46-generic exe/x86_64.ubuntu.20 prompt/off

$ aws sts get-caller-identity
{
    "UserId": "<USERID>",
    "Account": "<ACCOUNT>",
    "Arn": "arn:aws:iam:<ACCOUNT>:user/awscli"
}

Eğer sisteminizde AWS-CLI kurulu değilse veya yapılandırmalarınız eksik ise official rehberi takip edebilirsiniz. İşte bunun için bir link.

Daha öncesinden basit bir EC2 Instance oluşturmuştum. Söz konusu Instance için genel özellikler şu şekilde:

$ aws ec2 describe-instances
{
    "Reservations": [
        {
            "Groups": [],
            "Instances": [
                {
                    "AmiLaunchIndex": 0,
                    "ImageId": "ami-02358d9f5245918a3",
                    "InstanceId": "i-044de26112ecbce1d",
                    "InstanceType": "t2.micro",
                    "KeyName": "waso-dev",
                    "LaunchTime": "2022-08-16T09:10:58+00:00",
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "Placement": {
                        "AvailabilityZone": "us-east-1b",
                        "GroupName": "",
                        "Tenancy": "default"
                    },
...
...

Sonuç uzun olduğundan dolayı tüm çıktıyı dahil etmedim. Burada not etmemiz gereken iki önemli alan bulunmakta bunlardan ilki  "InstanceId" diğeri ise AvailabilityZone. Unique ID'imizi elde ettiğimize göre Instance'da varolan EBS'lerin durumu hakkında bilgi alabiliriz. Buradan sonra saklamış olduğumuz Instance ID'yi bol bol kullanacağız. Not etmekte fayda var.

$ aws ec2 describe-instance-attribute --attribute blockDeviceMapping --instance-id i-044de26112ecbce1d
{
    "BlockDeviceMappings": [
        {
            "DeviceName": "/dev/sda1",
            "Ebs": {
                "AttachTime": "2022-08-16T09:10:59+00:00",
                "DeleteOnTermination": true,
                "Status": "attached",
                "VolumeId": "vol-066d29f3dae2a6200"
            }
        }
    ],
    "InstanceId": "i-044de26112ecbce1d"
}

Çıktıdan da anlaşılacağı üzere Instance'a bağlı bir Volume bulunmakta ve statüsü "attached" yani bir Instance ile ilişkili olduğu anlamına gelmekte. Buraya kadar genel olarak Instance ile ilgilendik. Instance'a bağlamak için bir Volume oluşturalım:

$ aws ec2 create-volume --availability-zone us-east-1b --no-encrypted --size 5 --volume-type gp2 --tag-specifications 'ResourceType=volume, Tags=[{Key=Name, Value=prj-web-images}]'
{
    "AvailabilityZone": "us-east-1b",
    "CreateTime": "2022-08-16T15:42:21+00:00",
    "Encrypted": false,
    "Size": 5,
    "SnapshotId": "",
    "State": "creating",
    "VolumeId": "vol-09d8a4575df6c3334",
    "Iops": 100,
    "Tags": [
        {
            "Key": "Name",
            "Value": "prj-web-images"
        }
    ],
    "VolumeType": "gp2",
    "MultiAttachEnabled": false
}

Oluşturduğumuz Volume için bazı özellikler belirledik. Bunlar dışındaki özellikleri de tercih edebilirsiniz fakat bunu yapmadan önce Amazon'un belgelerini okumanızı tavsiye ederim. Belirlediğiniz özelliklerle aldığınız bir hizmet ücretlendirilebilinir. Komut satırına baktığımız zaman ilk olarak bir Zone belirledik "us-east-1b". Bu bölgenin Instance ile aynı bölge olmasına dikkat edin. İkinci parametre olan --no-encrypted ile bir şifreleme istemediğimizi belirttik. Üçüncü parametre ile anlaşılacağı üzere 5GB'lık bir Size belirledik. Sonraki alanlarda ise Type ve Tag belirledik. Bu çıktıda bizim için önemli olan iki key VolumeId ve State olacak. Tekradan tüm mevcut Volume objelerini sorgulayalım.

$ aws ec2 describe-volumes
{
    "Volumes": [
        {
            "Attachments": [
                {
                    "AttachTime": "2022-08-16T09:10:59+00:00",
                    "Device": "/dev/sda1",
                    "InstanceId": "i-044de26112ecbce1d",
                    "State": "attached",
                    "VolumeId": "vol-066d29f3dae2a6200",
                    "DeleteOnTermination": true
                }
            ],
            "AvailabilityZone": "us-east-1b",
            "CreateTime": "2022-08-16T09:10:59.916000+00:00",
            "Encrypted": false,
            "Size": 10,
            "SnapshotId": "snap-0c4bdb8287dbda45c",
            "State": "in-use",
            "VolumeId": "vol-066d29f3dae2a6200",
            "Iops": 100,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "wasa-ROOT-images"
                }
            ],
            "VolumeType": "gp2",
            "MultiAttachEnabled": false
        },
        {
            "Attachments": [],
            "AvailabilityZone": "us-east-1b",
            "CreateTime": "2022-08-16T15:42:21.863000+00:00",
            "Encrypted": false,
            "Size": 5,
            "SnapshotId": "",
            "State": "available",
            "VolumeId": "vol-09d8a4575df6c3334",
            "Iops": 100,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "prj-web-images"
                }
            ],
            "VolumeType": "gp2",
            "MultiAttachEnabled": false
        }
    ]
}

Toplamda iki farklı Volume'a sahip olduğumuz anlaşılıyor. Farkettiğiniz üzere ilk Volume bir Instance ile ilişkili. Bu Volume, Instance ilk oluşturulduğunda default olarak oluşturulan Root Volume. İkincisi ise bizim oluşturduğumuz. Attachments alanının boş olduğunu rahatlıkla görebilirsiniz çünkü bu Volume henüz bir Instance ile ilişkili değil. İkisinin de State alanlarını incelediğimizde Root Volume için "in-use"; bizim oluşturduğumuz Volume için ise "available" olduğu görülmekte. İlk Volume oluşturulduğunda bu alan "creating" olarak gözüküyordu. Şimdi Volume-Instance ilişkisi (attach) kuralım.

$ aws ec2 attach-volume --device xvdh --instance-id i-044de26112ecbce1d --volume-id vol-09d8a4575df6c3334
{
    "AttachTime": "2022-08-16T16:02:03.424000+00:00",
    "Device": "xvdh",
    "InstanceId": "i-044de26112ecbce1d",
    "State": "attaching",
    "VolumeId": "vol-09d8a4575df6c3334"
}

Komut satırı oldukça anlaşılır. Instance-Volume ilişkisini kurduğumuza göre tekrardan kontrol edelim.

$ aws ec2 describe-volumes
{
    "Volumes": [
        {
            "Attachments": [
                {
                    "AttachTime": "2022-08-16T09:10:59+00:00",
                    "Device": "/dev/sda1",
                    "InstanceId": "i-044de26112ecbce1d",
                    "State": "attached",
                    "VolumeId": "vol-066d29f3dae2a6200",
                    "DeleteOnTermination": true
                }
            ],
            "AvailabilityZone": "us-east-1b",
            "CreateTime": "2022-08-16T09:10:59.916000+00:00",
            "Encrypted": false,
            "Size": 10,
            "SnapshotId": "snap-0c4bdb8287dbda45c",
            "State": "in-use",
            "VolumeId": "vol-066d29f3dae2a6200",
            "Iops": 100,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "wasa-ROOT-images"
                }
            ],
            "VolumeType": "gp2",
            "MultiAttachEnabled": false
        },
        {
            "Attachments": [
                {
                    "AttachTime": "2022-08-16T16:02:03+00:00",
                    "Device": "xvdh",
                    "InstanceId": "i-044de26112ecbce1d",
                    "State": "attached",
                    "VolumeId": "vol-09d8a4575df6c3334",
                    "DeleteOnTermination": false
                }
            ],
            "AvailabilityZone": "us-east-1b",
            "CreateTime": "2022-08-16T15:42:21.863000+00:00",
            "Encrypted": false,
            "Size": 5,
            "SnapshotId": "",
            "State": "in-use",
            "VolumeId": "vol-09d8a4575df6c3334",
            "Iops": 100,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "prj-web-images"
                }
            ],
            "VolumeType": "gp2",
            "MultiAttachEnabled": false
        }
    ]
}

Daha önceden yapmış olduğumuz sorgudan farklı olarak her iki Volume bir Instance ile ilişkilendirilmiş olarak gözüküyor. İlişki Instance ID'lerin de aynı olduğu anlaşılmakta. Bunu bir de ilk satırlarda çalıştırdığımız komut satırı ile kontrol edelim.

$ aws ec2 describe-instance-attribute --attribute blockDeviceMapping --instance-id i-044de26112ecbce1d
{
    "BlockDeviceMappings": [
        {
            "DeviceName": "/dev/sda1",
            "Ebs": {
                "AttachTime": "2022-08-16T09:10:59+00:00",
                "DeleteOnTermination": true,
                "Status": "attached",
                "VolumeId": "vol-066d29f3dae2a6200"
            }
        },
        {
            "DeviceName": "xvdh",
            "Ebs": {
                "AttachTime": "2022-08-16T16:02:03+00:00",
                "DeleteOnTermination": false,
                "Status": "attached",
                "VolumeId": "vol-09d8a4575df6c3334"
            }
        }
    ],
    "InstanceId": "i-044de26112ecbce1d"
}

Ilk sonuçtan farklı olarak oluşturmuş olduğumuz ikinci Volume'da bir Instance ile ilişkilendirilmiş. Aslında buraya kadar işlemimiz bitti. Buradan sonra bir kaç temel komut ile eklemiş olduğumuz 5GB alanı istediğimiz gibi yönetebiliriz. Buraya kadar gelmişken temel düzeyde o işlemleri de göstereyim. Mevcut Instance ile SSH bağlantızı kuralım. SSH bağlantızı kurduktan sonra komut satırlarını tek seferde vereceğim ve çok kısa açıklayacağım. Büyük ihtimalle çok rahat anlarsınız çünkü artık temel Linux komutları kullanacağız.

$ sudo ssh -i waso-dev.pem <USER>@<PublicIPv4>

[centos@ip-172-31-95-163 ~]$ :

SSH bağlantımızı kurduğumuza göre sonradan eklediğimiz Volume için gerekenleri yapalım.

$ sudo -i

$ fdisk -l
Disk /dev/xvda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b050e
    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *        2048    20971486    10484719+  83  Linux
Disk /dev/xvdh: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

$ fdisk /dev/xvdh
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x50c83f5c.
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): p
Disk /dev/xvdh: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x50c83f5c
    Device Boot      Start         End      Blocks   Id  System
/dev/xvdh1            2048    10485759     5241856   83  Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

$ fdisk -l
Disk /dev/xvda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b050e
    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *        2048    20971486    10484719+  83  Linux
Disk /dev/xvdh: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x50c83f5c
    Device Boot      Start         End      Blocks   Id  System
/dev/xvdh1            2048    10485759     5241856   83  Linux

$ cd /usr/sbin/

$ mkfs.ext4 /dev/xvdh1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310464 blocks
65523 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

$ mkdir /tmp/example-path

$ mount /dev/xvdh1 /tmp/example-path/

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        471M     0  471M   0% /dev
tmpfs           495M     0  495M   0% /dev/shm
tmpfs           495M   13M  482M   3% /run
tmpfs           495M     0  495M   0% /sys/fs/cgroup
/dev/xvda1       10G  1,9G  8,2G  19% /
tmpfs            99M     0   99M   0% /run/user/1000
/dev/xvdh1      4,8G   20M  4,6G   1% /tmp/example-path

$ vi /etc/fstab
/dev/xvdh1 /tmp/example-path ext4 defaults 0 0

$ mount -a
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        471M     0  471M   0% /dev
tmpfs           495M     0  495M   0% /dev/shm
tmpfs           495M   13M  482M   3% /run
tmpfs           495M     0  495M   0% /sys/fs/cgroup
/dev/xvda1       10G  1,9G  8,2G  19% /
tmpfs            99M     0   99M   0% /run/user/1000
/dev/xvdh1      4,8G   20M  4,6G   1% /tmp/example-path

Yukarıdaki komut satırlarındaki mantık şu olaydan öte değil aslında: yeni bir partition oluşturduk, ext4 formatına çevirdik ve belirlediğimiz bir pathe mount işlemini gerçekleştirdik. vi satırı ile başlayan komut satırında ise fstab dosyasının içersine gerekli alanları ekledik. Böylece reboot sonrasında bile mount işlemimiz kaybolmayacak. Yani mount işlemini kalıcı bir hale getiriyoruz.

Son olarak mevcut bir Volume için detach ve delete işlemlerini gerçekleştirerek yazıyı bitirelim.

$ aws ec2 detach-volume --force --volume-id vol-09d8a4575df6c3334
{
    "AttachTime": "2022-08-16T16:02:03+00:00",
    "Device": "xvdh",
    "InstanceId": "i-044de26112ecbce1d",
    "State": "detaching",
    "VolumeId": "vol-09d8a4575df6c3334"
}

İlk olarak --force parametresi ile detach işlemini gerçekleştirdik. Diğer bir ifade ile Instance ile ilişkisini kestik.

$ aws ec2 describe-volumes
{
    "Volumes": [
        {
            "Attachments": [
                {
                    "AttachTime": "2022-08-16T09:10:59+00:00",
                    "Device": "/dev/sda1",
                    "InstanceId": "i-044de26112ecbce1d",
                    "State": "attached",
                    "VolumeId": "vol-066d29f3dae2a6200",
                    "DeleteOnTermination": true
                }
            ],
            "AvailabilityZone": "us-east-1b",
            "CreateTime": "2022-08-16T09:10:59.916000+00:00",
            "Encrypted": false,
            "Size": 10,
            "SnapshotId": "snap-0c4bdb8287dbda45c",
            "State": "in-use",
            "VolumeId": "vol-066d29f3dae2a6200",
            "Iops": 100,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "wasa-ROOT-images"
                }
            ],
            "VolumeType": "gp2",
            "MultiAttachEnabled": false
        },
        {
            "Attachments": [],
            "AvailabilityZone": "us-east-1b",
            "CreateTime": "2022-08-16T15:42:21.863000+00:00",
            "Encrypted": false,
            "Size": 5,
            "SnapshotId": "",
            "State": "available",
            "VolumeId": "vol-09d8a4575df6c3334",
            "Iops": 100,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "prj-web-images"
                }
            ],
            "VolumeType": "gp2",
            "MultiAttachEnabled": false
        }
    ]
}

Kontrol ettiğimiz de Attachments alanının boş olduğunu ve State alanının ise in-use'dan available'a döndüğünü görüyoruz.

$ aws ec2 delete-volume --volume-id vol-09d8a4575df6c3334

$ aws ec2 describe-volumes
{
    "Volumes": [
        {
            "Attachments": [
                {
                    "AttachTime": "2022-08-16T09:10:59+00:00",
                    "Device": "/dev/sda1",
                    "InstanceId": "i-044de26112ecbce1d",
                    "State": "attached",
                    "VolumeId": "vol-066d29f3dae2a6200",
                    "DeleteOnTermination": true
                }
            ],
            "AvailabilityZone": "us-east-1b",
            "CreateTime": "2022-08-16T09:10:59.916000+00:00",
            "Encrypted": false,
            "Size": 10,
            "SnapshotId": "snap-0c4bdb8287dbda45c",
            "State": "in-use",
            "VolumeId": "vol-066d29f3dae2a6200",
            "Iops": 100,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "wasa-ROOT-images"
                }
            ],
            "VolumeType": "gp2",
            "MultiAttachEnabled": false
        }
    ]
}

Ilk satır ile oluşturmuş olduğumuz Volume tamamen silindi. Kontrol ettiğimizde de sadece tek bir Volume olduğunu görüyoruz. O ise Instance ilk oluşturulduğunda sistem dosyaları için oluşturulan Root Volume.

Temel düzeyde konuyu bu şekilde ele aldım. Ben de henüz bu alanda çok yeni olduğumdan dolayı yazılanlar en doğru yol olmayabilir. Hatalarım varsa düzeltmekten çekinme!


İletişime geçmek, yorum bırakmak veya hatalarımı düzetlmek istersen mail atabilirsin.

iletişim için tıklama yeri