温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OpenStack Object Storage swift操作和实践

发布时间:2020-04-20 09:31:06 来源:网络 阅读:2032 作者:三国奶糕 栏目:移动开发

OpenStack Object Storage swift操作和实践

    之前已经介绍了OpenStack Object Storage (swift)的相关概念,接下来介绍一些swift的操作。

【环境搭建】

为了方便在初期学习swift功能的时候我们可以不考虑身份验证的部分,所以我们要做一点处理:

在安装之前我们可以修改源码中一个配置文件proxy-server.conf,如图:

将tempauth去掉

OpenStack Object Storage swift操作和实践

同时屏蔽掉下面两行

OpenStack Object Storage swift操作和实践

因为在swift中功能都是模块化的,你可以根据需求来开启或者关闭。

修改之后我们可以安照上篇博客中介绍的快速部署Swift的SAIO环境  一文中的方法执行脚本setup_saio.sh安装了。


这里我们简单注释一下setup_saio.sh脚本(是根据swift官网文档编写的)


#!/bin/sh
#install indepency
sudo apt-get install -y python-software-properties
# If you want install swift use deb package,enable it
#sudo add-apt-repository ppa:swift-core/release
sudo apt-get update
sudo apt-get install -y curl
sudo apt-get install -y gcc
sudo apt-get install -y git-core
sudo apt-get install -y memcached
sudo apt-get install -y python-configobj
sudo apt-get install -y python-coverage
sudo apt-get install -y python-dev
sudo apt-get install -y python-nose
sudo apt-get install -y python-setuptools
sudo apt-get install -y python-simplejson
sudo apt-get install -y python-xattr
sudo apt-get install -y sqlite3
sudo apt-get install -y xfsprogs
sudo apt-get install -y python-webob
sudo apt-get install -y python-eventlet
sudo apt-get install -y python-greenlet
sudo apt-get install -y python-pastedeploy
sudo apt-get install -y python-netifaces
sudo apt-get install -y memcached
#Add swift user
sudo useradd -d /home/swift -s /bin/bash -U swift
sudo password swift
#setup xfs for disk
sudo apt-get -y install vsftpd
#创建一个回环设备来做存储点
sudo dd if=/dev/zero of=:w
sudo dd if=/dev/zero of=/srv/swift-disk bs=1024 count=0 seek=1000000
sudo mkfs.xfs -i size=1024 /srv/swift-disk
sudo echo "/srv/swift-disk /mnt/sdb1 xfs loop,noatime,nodiratime,nobarrier,logbufs=8 0 0" >> /etc/fstab
sudo mkdir /mnt/sdb1
sudo mount /mnt/sdb1
sudo mkdir /mnt/sdb1/1 /mnt/sdb1/2 /mnt/sdb1/3 /mnt/sdb1/4 /mnt/sdb1/test
sudo chown -R swift:swift /mnt/sdb1/*
if ! [ -e /srv ]
then
    sudo mkdir /srv
fi
for x in 1 2 3 4
do
  sudo ln -s /mnt/sdb1/$x /srv/$X
done
sudo mkdir -p /etc/swift/object-server /etc/swift/container-server /etc/swift/account-server
for x in 1 2 3 4
do
  sudo mkdir -p /srv/$x/node/sdb$x
done
sudo mkdir -p /var/run/swift
sudo chown -R swift:swift /etc/swift /srv/1/ /srv/2/ /srv/3/ /srv/4/ /var/run/swift
sudo echo -e "mkdir /var/run/swift \\n chown swift:swift /var/run/swift" >> /etc/rc.local
if [ -e /home/swift/.bashrc ]
then
   sudo echo -e "export SWIFT_TEST_CONFIG_FILE=/etc/swift/test.conf \\n export PATH=${PATH}:~/bin" >> /home/swift/.bashrc
   sudo source ~/.bashrc
fi
#configure swift and related conf
if ! [ -e /etc/swift ]
then
    sudo mkdir /etc/swift
fi
chown swift:swift /etc/swift
# copy conf files
cp --recursive conf/* /etc/swift/
cp rsyncd.conf /etc
sudo service rsync restart
#some system need easy_install and pip
apt-get -y install python-setuptools
easy_install pip
#Install swift
git clone git://github.com/openstack/swift.git
cd swift ; pip install -r tools/pip-requires
sudo python setup.py install; cd ..
#Install swiftclient
git clone https://github.com/openstack/python-swiftclient.git
cd python-swiftclient; pip install -r tools/pip-requires
 sudo python setup.py install;cd ..
#make ring and start server
cd bin;
#创建ring
sh remakerings
#启动服务
swift-init main start

此时我们搭建的环境是一个默认的三副本配置。


【基本操作】

1.生成builder文件和builder相关配置

   你可以在终端中直接输入swift-ring-builder,查看swift-ring-builder命令相关的解释:

swift-ring-builder <builder_file>
    Shows information about the ring and the devices within.
   显示ring以及ring中设备的信息,swift-1.8.0中对device新增了一个region属性swift-ring-builder <builder_file> add    z<zone>-<ip>:<port>/<device_name>_<meta> <weight>    [z<zone>-<ip>:<port>/<device_name>_<meta> <weight>] ...    Adds devices to the ring with the given information. No partitions will be    assigned to the new device until after running 'rebalance'. This is so you    can make multiple device changes and rebalance them all just once.
   使用给出的信息添加新的设备到ring上。add操作不会分配partitions到新的设备上,只有运行了'rebalance'命令后才会进行分区的分配。
   因此,这种机制可以允许你一次添加多个设备,并只执行一次rebalance实现对这些设备的分区分配。

swift-ring-builder <builder_file> create <part_power> <replicas> <min_part_hours>
    Creates <builder_file> with 2^<part_power> partitions and <replicas>.
    <min_part_hours> is number of hours to restrict moving a partition more
    than once.
   使用2的<part_power>次方个分区和<replicas>副本数创建<builder_file>.<min_part_hour>是一个分区被连续移动两次之间的最小时间间隔swift-ring-builder <builder_file> list_parts <search-value> [<search-value>] ..    Returns a 2 column list of all the partitions that are assigned to any of    the devices matching the search values given. The first column is the    assigned partition number and the second column is the number of device    matches for that partition. The list is ordered from most number of matches    to least. If there are a lot of devices to match against, this command    could take a while to run.
   返回一个两列的列表,包含与搜索值相匹配的所有设备的所有分区。
   第一列是关联的分区编号
   第二列是与分区匹配的设备编号

   列表按匹配的编号大小从大到小排序,如果有很多设备与搜索符合,则这个命令需要多运行一会儿

swift-ring-builder <builder_file> rebalance
    Attempts to rebalance the ring by reassigning partitions that haven't been
    recently reassigned.
   rebalance命令尝试重新平衡环,通过重新分配分区最近没有被重新分配的分区。
swift-ring-builder <builder_file> remove <search-value> [search-value ...]
    Removes the device(s) from the ring. This should normally just be used for
    a device that has failed. For a device you wish to decommission, it's best
    to set its weight to 0, wait for it to drain all its data, then use this
    remove command. This will not take effect until after running 'rebalance'.
    This is so you can make multiple device changes and rebalance them all just
    once.
   remove命令将设备从ring中移除。一般情况下,这个命令应该仅用在那些失败的设备上。
   如果你想将一个设备退役掉,那么最好的方式是将它的weight设置为0,待它将其上所有的数据都移走之后,再使用这个命令移除设备。
   remove操作不会重新分配partitions,只有运行了'rebalance'命令后才会进行分区的分配。因此,这种机制可以允许你一次添加删除个设备,并只执行一次rebalance实现对这些设备的分区分配。
swift-ring-builder <builder_file> search <search-value>
    Shows information about matching devices.
   显示匹配的设备的信息swift-ring-builder <builder_file> set_info    <search-value> <ip>:<port>/<device_name>_<meta>    [<search-value> <ip>:<port>/<device_name>_<meta>] ...    For each search-value, resets the matched device's information.    This information isn't used to assign partitions, so you can use    'write_ring' afterward to rewrite the current ring with the newer    device information. Any of the parts are optional in the final    <ip>:<port>/<device_name>_<meta> parameter; just give what you    want to change. For instance set_info d74 _"snet: 5.6.7.8" would    just update the meta data for device id 74.
   set_info命令会重新设置每一个与<search-value>相匹配的设备信息。这个信息不会用来重新分配分区,因此你可以使用'write_ring'来直接重写当前的ring。
   <ip>:<port>/<device_name>_<meta>参数的任意一个部分都是可选的,你只需要给出你需要更改的部分。
   比如,set_info d74 _"snet: 5.6.7.8"就仅仅会把id为74的设备的元数据更新为"snet: 5.6.7.8"
swift-ring-builder <builder_file> set_min_part_hours <hours>
    Changes the <min_part_hours> to the given <hours>. This should be set to
    however long a full replication/update cycle takes. We're working on a way
    to determine this more easily than scanning logs.
   set_min_part_hours命令将<min_part_hours>设置为参数给定的<hours>.
   这个时间应该被设置的至少满足一个完整的replication/update周期。我们正在努力找到一个方法可以比看日志更简单的决定这个时间
swift-ring-builder <builder_file> set_weight <search-value> <weight>
    [<search-value> <weight] ...
    Resets the devices' weights. No partitions will be reassigned to or from
    the device until after running 'rebalance'. This is so you can make
    multiple device changes and rebalance them all just once.
   重新设置设备的weight。set_weight操作后,设备上的partition不会重新分配,只有运行了'rebalance'命令后才会进行分区的分配。
   因此,这种机制可以允许你一次添加多个设备,并只执行一次rebalance实现对这些设备的分区分配。

 swift-ring-builder <builder_file> set_replicas <replicas>
    Changes the replica count to the given <replicas>. <replicas> may
    be a floating-point value, in which case some partitions will have
    floor(<replicas>) replicas and some will have ceiling(<replicas>)
    in the correct proportions.A rebalance is needed to make the change take effect.

    set_replicas命令用于使用参数中的<replicas>来设置副本数。
    <replicas>可以是一个浮点数,因此在一些场景中一些分区的副本数可能是floor(<replicas>),也可能是(<replicas>),这取决于正确的比例。

    需要执行一个rebalance命令来使副本设置生效。该命令是swift-1.8.0新增的。

swift-ring-builder <builder_file> validate
    Just runs the validation routines on the ring.    仅运行builder的validate方法,使ring生效

swift-ring-builder <builder_file> write_ring    Just rewrites the distributable ring file. This is done automatically after    a successful rebalance, so really this is only useful after one or more    'set_info' calls when no rebalance is needed but you want to send out the    new device information.
   write_ring命令仅是用来重写分部环境下的ring文件。这个命令会在成功执行一个rebalance操作后呗自动执行。
   因此,它仅在你执行了一次或多次'set_info'命令,不想rebalance却想保留新信息时使用。

2.使用curl工具访问swift

创建账户:

#curl -X PUT -I http://localhost:8080/v1.0/myaccount01

返回201 Created,表示正常。


创建容器

#curl -X PUT -I http://localhost:8080/v1.0/myaccount01/mycontainer01/

返回 201 Created,表示正常。


上传文件

#curl -X PUT -I -T yourfile http://localhost:8080/v1.0/myaccount01/mycontainer01/

返回 201 Created,表示正常。

 

查看容器中的文件:

#curl -D- http://localhost:8080/v1.0/myaccount01/mycontainer01


#curl -s http://localhost:8080/v1.0/myaccount01/mycontainer01/ -X GET

 

删除容器中的对象:

curl -s http://localhost:8080/v1.0/myaccount01/mycontainer01 -X DELETE


查看自己的文件存放的物理位置:

#swift-get-nodes /etc/swift/object.ring.gz myaccount01 mycontainer01 yourfile

根据返回结果,可以自己去验证文件的最终物理存储位置。


获取帐户的所有容器

curl -D- http://localhost:8080/v1.0/myaccount01  


------------------------------------------全文完------------------------------------------

PS:

关注个人 微博:爱数三国奶糕 

         微信公众账号:比特科技

二维码:

OpenStack Object Storage swift操作和实践

可以及时获得趣味文章、热门技术、行业热点等相关文章的推送。

    关注比特科技,构建IT生态圈。   


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI