Vagrantfile 如何设置磁盘大小
1、安装vagrant-disksize插件
下载地址
链接:https://pan.baidu.com/s/1kYMBJqzQH1-57nRVTz4Sig?pwd=r15z
安装命令
vagrant plugin install --plugin-clean-sources vagrant-disksize-0.1.3.gem
注意一定要加 --plugin-clean-sources, 否则会去官网下载插件。
vagrant 启动日志
==> node2-186: Resized disk: old 40960 MB, req 102400 MB, new 102400 MB ==> node2-186: You may need to resize the filesystem from within the guest.
2、vagrant根分区大小为何是40GB?
这个根分区是怎么规定的呢?为何root才40GB?,如何修改root多一些空间呢?
这个40GB是box镜像生成者设定的. 如果需要可以自己重新自定义.
经过测试这是一个默认值,不管disksize.size=100GB还是200GB根目录总是40GB。
disksize.size=200GB时
disksize.size=100GB时
参考扩分区.
3、vagrant 设置启动磁盘空间大小
3.1 设置 node.disksize.size 参数
node.disksize.size = '500GB' 这个意思并不是将根分区扩展为500GB,而是将磁盘大小设置为500GB,**并且没有做分区**。
Vagrant.configure("2") do |config| config.vm.box_check_update = false config.vm.provider 'virtualbox' do |vb| end $num_instances = 1 (1..$num_instances).each do |i| config.vm.define "node#{i}-host-179" do |node| node.vm.box = "Centos-7-min" node.vm.hostname = "vm128-host-179" node.disksize.size = '500GB' ip = "10.50.10.#{i+27}" node.vm.network "public_network", ip: ip,bridge: "team1" node.vm.provider "virtualbox" do |vb| vb.memory = "65536" vb.cpus = 8 vb.name = "meta-host-179" end node.vm.provision "shell", path: "install.sh" end end end
启动之后就可以看到
lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 500G 0 disk └─sda1 8:1 0 40G 0 part /
5
所以还需要手动分区 ,这一步可以考虑自动化,加一个fdisk 的分区脚本。
3.2 fdisk格式化磁盘
[root@master1-186 ~]# fdisk /dev/sda 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. Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 2): First sector (83886080-419430399, default 83886080): Using default value 83886080 Last sector, +sectors or +size{K,M,G} (83886080-419430399, default 419430399): Using default value 419430399 Partition 2 of type Linux and of size 160 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8)
这里一定要注意,机器需要重启生效。
啥重启? 显然还是lvm香。 自动扩充磁盘。
如果生产使用还是建议采用LVM自动扩容。
因为生产环境的重启代价很大,不过如果程序部署在k8s上,问题就变得简单了。
重启之后可以看到已经格式化好了
[root@vm128-host-179 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 500G 0 disk ├─sda2 8:2 0 460G 0 part └─sda1 8:1 0 40G 0 part /
3.3 绑定新分区
New partition: /dev/sda2
Mount point: /data
需要格式化以及开机自动挂载。 虚拟机的磁盘的设备UUID 通过blkid查看.
注意mkfs 和 xfs_growfs 的区别(前者是格式化裸盘的后者是用于lvm中)
[root@vm128-host-179 ~]# mkfs -t xfs -f /dev/sda2 meta-data=/dev/sda2 isize=512 agcount=4, agsize=30146560 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=120586240, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=58880, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@vm128-host-179 ~]# mkdir /data [root@vm128-host-179 ~]# mount -t xfs -o defaults /dev/sda2 /data [root@vm128-host-179 ~]# mount -t xfs -o defaults /dev/sda2 /data [root@vm128-host-179 ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 32G 0 32G 0% /dev tmpfs 32G 0 32G 0% /dev/shm tmpfs 32G 8.5M 32G 1% /run tmpfs 32G 0 32G 0% /sys/fs/cgroup /dev/sda1 40G 3.4G 37G 9% / tmpfs 6.3G 0 6.3G 0% /run/user/0 /dev/sda2 460G 33M 460G 1% /data [root@master1-186 ~]# blkid /dev/sda1: UUID="1c419d6c-5064-4a2b-953c-05b2c67edb15" TYPE="xfs" /dev/sda2: UUID="999afc8e-f2f9-49e4-ad97-c1608cafe7f9" TYPE="xfs"
3.4 修改/etc/fstab 开机自动挂载
blkid 查看UUID
修改/etc/fstab添加新分区以及挂载目录
vi /etc/fstab UUID=1c419d6c-5064-4a2b-953c-05b2c67edb15 / xfs defaults 0 0 UUID=69703dd6-ae3b-41cc-824b-be975184ba0f /data xfs default 0 0
3.4 重启虚拟机确认磁盘挂载
重启后磁盘已自动挂载
[root@vm128-host-179 ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 32G 0 32G 0% /dev tmpfs 32G 0 32G 0% /dev/shm tmpfs 32G 8.5M 32G 1% /run tmpfs 32G 0 32G 0% /sys/fs/cgroup /dev/sda1 40G 3.4G 37G 9% / /dev/sda2 460G 33M 460G 1% /data tmpfs 6.3G 0 6.3G 0% /run/user/0
4、自动扩充磁盘
这个自动扩磁盘,因为是没有lvm所以没有测试这个。
大家有兴趣可以尝试下,测试时注意box选择bento/centos-7
Vagrant.configure(2) do |config| common = <<-SCRIPT sudo parted /dev/sda resizepart 2 100% sudo pvresize /dev/sda2 sudo lvextend -l +100%FREE /dev/centos/root sudo xfs_growfs /dev/centos/root SCRIPT config.vm.define "node01" do |node1| node1.vm.hostname = "node01" node1.vm.network "private_network", ip: "192.168.56.121" config.vm.provision :shell, :inline => common end end
注意需要使用这个centos box: bento/centos-7
5、vagrant 拉虚拟机性能测试
vagrant up三台服务器耗时
三台大约5mins(含安装一部分软件)
Vagrant.configure("2") do |config| config.vm.box_check_update = false config.vm.provider 'virtualbox' do |vb| end $num_instances = 3 (1..$num_instances).each do |i| config.vm.define "node#{i}-186" do |node| node.vm.box = "centos-7" node.vm.hostname = "master#{i}-186" node.disksize.size = '100GB' ip = "10.50.10.#{i+26}" node.vm.network "public_network", ip: ip,bridge: "bond0" node.vm.provider "virtualbox" do |vb| vb.memory = "65536" vb.cpus = 8 vb.name = "master#{i}-186" end node.vm.provision "shell", path: "install.sh" end end end
install.sh
#!/usr/bin/env bash # yum net-tools & udate route cd /tmp && curl -O 10.50.10.25/pigsty/net-tools-2.0-0.25.20131004git.el7.x86_64.rpm yum -y install net-tools-2.0-0.25.20131004git.el7.x86_64.rpm route add default gw 10.50.10.254 eth1 route -n # modify ssh parpmeter passwd=yes sed -ri '/^PasswordAuthentication/cPasswordAuthentication yes' /etc/ssh/sshd_config systemctl restart sshd # change time zone cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime timedatectl set-timezone Asia/Shanghai rm /etc/yum.repos.d/CentOS-Base.repo curl 10.50.10.25/pigsty/Centos-Base.repo -o /etc/yum.repos.d/CentOS-Base.repo # install kmod and ceph-common for rook yum install -y wget curl conntrack-tools vim net-tools telnet tcpdump bind-utils socat ntp kmo d dos2unix echo 'disable selinux' setenforce 0 sed -i 's/=enforcing/=disabled/g' /etc/selinux/config echo 'enable iptable kernel parameter' cat >> /etc/sysctl.conf <<EOF net.ipv4.ip_forward=1 EOF sysctl -p # 将桥接的IPv4流量传递到iptables的链 cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system echo 'set host name resolution' cat >> /etc/hosts <<EOF 10.50.10.27 master1-186 10.50.10.28 master2-186 10.50.10.29 master3-186 EOF cat /etc/hosts echo 'disable swap' swapoff -a sed -i '/swap/s/^/#/' /etc/fstab # download kernel cd /tmp && curl -O http://10.50.10.25/pigsty/kernel-lt-5.4.200-1.el7.elrepo.x86_64.rpm && rpm -Uvh kernel-lt-5.4.200-1.el7.elrepo.x86_64.rpm
6、问题
重启竟然又啦不起来了
分区完磁盘之后,设置自动挂载竟然无法启动. 2022年11月21日10:30:29
7、参考
1.【扩磁盘】
2. 【扩分区】
3. 【自动扩磁盘】