问题导读
问题1:一致性哈希算法,其核心思想是什么?
问题2:如何对swift源码进行配置,并将其运行起来?
我的一点理论理解:
swift使用了一致性哈希算法(一种主流的分布式哈希算法,Distributed Hash Table, DHT)。
普通的哈希算法(一般是对象的哈希对物理结点总数取模,就可以将对象映射到相应的物理结点上)存在这样的问题:
1)可能有的物理结点分配的多,有的物理结点分配的少,数据分布的不平均
2)如果有一台新物理结点加进来,可能需要从其他很多结点往新的移动数据,抖动太大
所以这时候出现了一致性哈希算法,其核心思想是:
1)将哈希空间可看作一个环(Ring).
2)首先求出设备节点(就是一块硬盘)的哈希,这样设备的哈希可以在Ring就是一个点
3)然后就可以再将数据的哈希按顺时针映射到Ring上最近的点,这要的话,每个设备节点只需要处理落在它和它的前驱节点之间的数据
4)添加新设备结点时,新设备结点的哈希又在Ring映射了一个点,这样只需要将离它最近的数据迁移,抖动小
5)可将物理结点再划分更小的虚拟结点以平衡不同性能间物理机器的权重
6)上面第4)步,Ring上直接映射虚拟结点的哈希与对象的哈希,虽然抖动小,但毕竟还得移动数据,所以我们再他们的中间再抽象一个数据分区(Partion)的概念,比如说Ring上有S个虚拟结点,将其划分成Q个等份,每个等份称为一个数据分区(Partion),并且满足Q>>S,则每个虚拟节点对应的分区数Partion=Q/S,这样的话,就不需要移动数据了,直接将Partion的哈希在新结点重新映射就行了。(Zone -> Device Node -> Virtual Node -> Partion -> Object)
但是有的设备节点的硬盘大比如说200G,有的设备节点硬盘小比较100G,所以我们可以在物理节点上面再抽象一个虚拟结点以分担其权重。
注意:可理解成虚拟结点是所有这些之上的抽象( 硬盘(disk drive),一个server,一个机架(cabinet),一个交换机(switch),甚至是一个数据中心(datacenter)
所有的数据需要冗余备份多份(replica),一般replica=3,因为虚拟节点本来就是环上的点,所以备份时,只需要再将该虚拟节点上的数据同步到它顺时针之后的两个虚拟结点即可。
但是进行数据冗余备份的时候,我们也应该考虑地区的分布,所以我们可以在虚拟结点中再引入一个分区(Zone)的字段,这个字段由初始建Ring时指定,即数据只能按时针replica到具有不同Zone值的虚拟结点上(通过linux里的rsync命令同步,这样同时该虚拟结点还能检测与它通信的结点的心跳)。
Swift中有三种Ring, accounts, containers, objects都有各自的环
Objects, 就是对象了,Swift是分布式对象系统,不同于HDFSg一样的分布式文件系统(文件系统需支持标准的文件接口如NFS,Swift只能用它自己的REST cliet)
Containers, Objects的list, 类似于S3里的桶的概念
Accounts, 是上在Containers的list
抽象成三个Ring的目的是不至于让一个环太大(因为环要直接放到内存里的,不能内存溢出哦)
上面3个环分别都有对应的Object Server, Container Server, Account Server三个进程维护着3个数据库,Proxy Server在有数据查询或存储请求时,再请求这三个服务,它们返回应该上哪个物理结点去管理数据,然后Proxy Server就直接和这个物理结点打交道存储数据了,存储完了,还要更新3个服务里的元数据。
1)下载swift代码
yum install xinetd rsync
yum install memcached
yum install python-netifaces python-nose
git clonehttps://github.com/openstack/swift
git clonehttps://github.com/openstack/python-swiftclient
cd/bak/openstack/python-swiftclient
python setup.pydevelop
cd /bak/openstack/swift
pip-python install -r tools/pip-requires
pip-python install -r tools/test-requires
因为我们只对swift在eclipse里调试感兴趣,所以在eclipse里启动swift它就不需要执行pythonsetup.py develop
2)磁盘及目录
mkdir/bak/kvmimages/swift
ddif=/dev/zero of=/bak/kvmimages/swift/swift.imgbs=1024 count=0 seek=1000000
mkfs.xfs -i size=1024 /bak/kvmimages/swift/swift.img
vi /etc/fstab and add
/bak/kvmimages/swift/swift.img/mnt/sdb1 xfs loop,noatime,nodiratime,nobarrier,logbufs=8 0 0
mkdir /mnt/sdb1
mount /mnt/sdb1
mkdir /mnt/sdb1/1 /mnt/sdb1/2 /mnt/sdb1/3 /mnt/sdb1/4
chown hua:hua /mnt/sdb1/*
for x in {1..4}; do ln -s /mnt/sdb1/$x /bak/kvmimages/swift/$x;done
mkdir -p /etc/swift/object-server /etc/swift/container-server /etc/swift/account-server /srv/1/node/sdb1 /srv/2/node/sdb2 /srv/3/node/sdb3 /srv/4/node/sdb4 /var/run/swift
chown -R hua:hua /etc/swift /srv/[1-4]/ /var/run/swift
3)rsync配置
在/etc/rsyncd.conf中追加:
uid = hua
gid = hua
log file =/var/log/rsyncd.log
pid file =/var/run/rsyncd.pid
address =127.0.0.1
[account6012]
maxconnections = 25
path =/bak/kvmimages/swift/1/node/
read only =false
lock file =/var/lock/account6012.lock
[account6022]
maxconnections = 25
path =/bak/kvmimages/swift/2/node/
read only =false
lock file =/var/lock/account6022.lock
[account6032]
maxconnections = 25
path =/bak/kvmimages/swift/3/node/
read only =false
lock file =/var/lock/account6032.lock
[account6042]
maxconnections = 25
path =/bak/kvmimages/swift/4/node/
read only =false
lock file =/var/lock/account6042.lock
[container6011]
maxconnections = 25
path =/bak/kvmimages/swift/1/node/
read only =false
lock file =/var/lock/container6011.lock
[container6021]
maxconnections = 25
path =/bak/kvmimages/swift/2/node/
read only =false
lock file =/var/lock/container6021.lock
[container6031]
maxconnections = 25
path =/bak/kvmimages/swift/3/node/
read only =false
lock file =/var/lock/container6031.lock
[container6041]
maxconnections = 25
path =/bak/kvmimages/swift/4/node/
read only =false
lock file =/var/lock/container6041.lock
[object6010]
maxconnections = 25
path =/bak/kvmimages/swift/1/node/
read only =false
lock file =/var/lock/object6010.lock
[object6020]
maxconnections = 25
path =/bak/kvmimages/swift/2/node/
read only =false
lock file =/var/lock/object6020.lock
[object6030]
maxconnections = 25
path =/bak/kvmimages/swift/3/node/
read only =false
lock file =/var/lock/object6030.lock
[object6040]
maxconnections = 25
path =/bak/kvmimages/swift/4/node/
read only =false
lock file =/var/lock/object6040.lock
On fedora,vi /etc/xinetd.d/rsync
disable = no
service xinetd restart
On Ubuntu vi /etc/default/rsync
RSYNC_ENABLE=true
service rsync restart
4)启动memcached
systemctlenable memcached.service
systemctl startmemcached.service
5)vi/etc/proxy-server.conf
[DEFAULT]
bind_port= 8080
user= hua
log_facility= LOG_LOCAL1
[pipeline:main]
#pipeline= healthcheck cache tempauth proxy-server
pipeline= healthcheck cache swift3 s3token tokenauth keystone proxy-server
[filter:keystone]
paste.filter_factory= keystone.middleware.swift_auth:filter_factory
operator_roles= Member,admin
[app:proxy-server]
use= egg:swift#proxy
allow_account_management= true
account_autocreate= true
[filter:tokenauth]
paste.filter_factory= keystone.middleware.auth_token:filter_factory
service_port= 5000
service_host= 127.0.0.1
auth_port= 35357
auth_host= 127.0.0.1
auth_protocol= http
auth_token= ADMIN
admin_token= ADMIN
cache= swift.cache
[filter:s3token]
paste.filter_factory= keystone.middleware.s3_token:filter_factory
service_port= 5000
service_host= 127.0.0.1
auth_port= 35357
auth_host= 127.0.0.1
auth_protocol= http
auth_token= ADMIN
admin_token= ADMIN
cache= swift.cache
[app:proxy-server]
use= egg:swift#proxy
allow_account_management= true
account_autocreate= true
[filter:tempauth]
use= egg:swift#tempauth
user_admin_admin= admin .admin .reseller_admin
user_test_tester= testing .admin
user_test2_tester2= testing2 .admin
user_test_tester3= testing3
[filter:healthcheck]
use= egg:swift#healthcheck
[filter:cache]
use= egg:swift#memcache
[filter:swift3]
use= egg:swift#swift3
vi/etc/swift/swift.conf
[swift-hash]
swift_hash_path_suffix= ADMIN
vi/etc/swift/account-server/1.conf
[DEFAULT]
devices= /bak/kvmimages/swift/1/node
mount_check= false
bind_port= 6012
user= hua
log_facility= LOG_LOCAL2
[pipeline:main]
pipeline= account-server
[app:account-server]
use= egg:swift#account
[account-replicator]
vm_test_mode= yes
[account-auditor]
[account-reaper]
vi/etc/swift/account-server/2.conf
[DEFAULT]
devices= /bak/kvmimages/swift/2/node
mount_check= false
bind_port= 6022
user= hua
log_facility= LOG_LOCAL3
[pipeline:main]
pipeline= account-server
[app:account-server]
use= egg:swift#account
[account-replicator]
vm_test_mode= yes
[account-auditor]
[account-reaper]
vi/etc/swift/account-server/3.conf
[DEFAULT]
devices= /bak/kvmimages/swift/3/node
mount_check= false
bind_port= 6032
user= hua
log_facility= LOG_LOCAL4
[pipeline:main]
pipeline= account-server
[app:account-server]
use= egg:swift#account
[account-replicator]
vm_test_mode= yes
[account-auditor]
[account-reaper]
vi/etc/swift/account-server/4.conf
[DEFAULT]
devices= /bak/kvmimages/swift/4/node
mount_check= false
bind_port= 6042
user= hua
log_facility= LOG_LOCAL5
[pipeline:main]
pipeline= account-server
[app:account-server]
use= egg:swift#account
[account-replicator]
vm_test_mode= yes
[account-auditor]
[account-reaper]
vi/etc/swift/container-server/1.conf
[DEFAULT]
devices= /bak/kvmimages/swift/1/node
mount_check= false
bind_port= 6011
user= hua
log_facility= LOG_LOCAL2
[pipeline:main]
pipeline= container-server
[app:container-server]
use= egg:swift#container
[container-replicator]
vm_test_mode= yes
[container-updater]
[container-auditor]
[container-sync]
vi/etc/swift/container-server/2.conf
[DEFAULT]
devices= /bak/kvmimages/swift/2/node
mount_check= false
bind_port= 6021
user= hua
log_facility= LOG_LOCAL3
[pipeline:main]
pipeline= container-server
[app:container-server]
use= egg:swift#container
[container-replicator]
vm_test_mode= yes
[container-updater]
[container-auditor]
[container-sync]
vi/etc/swift/container-server/3.conf
[DEFAULT]
devices= /bak/kvmimages/swift/3/node
mount_check= false
bind_port= 6031
user= hua
log_facility= LOG_LOCAL4
[pipeline:main]
pipeline= container-server
[app:container-server]
use= egg:swift#container
[container-replicator]
vm_test_mode= yes
[container-updater]
[container-auditor]
[container-sync]
vi/etc/swift/container-server/4.conf
[DEFAULT]
devices= /bak/kvmimages/swift/4/node
mount_check= false
bind_port= 6041
user= hua
log_facility= LOG_LOCAL5
[pipeline:main]
pipeline= container-server
[app:container-server]
use= egg:swift#container
[container-replicator]
vm_test_mode= yes
[container-updater]
[container-auditor]
[container-sync]
vi/etc/swift/object-server/1.conf
[DEFAULT]
devices= /bak/kvmimages/swift/1/node
mount_check= false
bind_port= 6010
user= hua
log_facility= LOG_LOCAL2
[pipeline:main]
pipeline= object-server
[app:object-server]
use= egg:swift#object
[object-replicator]
vm_test_mode= yes
[object-updater]
[object-auditor]
vi/etc/swift/object-server/2.conf
[DEFAULT]
devices= /bak/kvmimages/swift/2/node
mount_check= false
bind_port= 6020
user= hua
log_facility= LOG_LOCAL3
[pipeline:main]
pipeline= object-server
[app:object-server]
use= egg:swift#object
[object-replicator]
vm_test_mode= yes
[object-updater]
[object-auditor]
vi/etc/swift/object-server/3.conf
[DEFAULT]
devices= /bak/kvmimages/swift/3/node
mount_check= false
bind_port= 6030
user= hua
log_facility= LOG_LOCAL4
[pipeline:main]
pipeline= object-server
[app:object-server]
use= egg:swift#object
[object-replicator]
vm_test_mode= yes
[object-updater]
[object-auditor]
vi/etc/swift/object-server/4.conf
[DEFAULT]
devices= /bak/kvmimages/swift/4/node
mount_check= false
bind_port= 6040
user= hua
log_facility= LOG_LOCAL5
[pipeline:main]
pipeline= object-server
[app:object-server]
use= egg:swift#object
[object-replicator]
vm_test_mode= yes
[object-updater]
[object-auditor]
6)创建脚本方便使用
vi /bak/bin/resetswift
#!/bin/bash
swift -initall stop
#find /var/log/swift -type f -exec rm -f {} \;
sudo umount /mnt/sdb1
sudo mkfs.xfs -f -i size=1024 /bak/kvmimages/swift/swift.img
sudo mount /mnt/sdb1
sudo mkdir /mnt/sdb1/1 /mnt/sdb1/2 /mnt/sdb1/3 /mnt/sdb1/4
sudo chown hua:hua /mnt/sdb1/*
mkdir -p /bak/kvmimages/swift/1/node/sdb1 /bak/kvmimages/swift/2/node/sdb2/bak/kvmimages/swift/3/node/sdb3 /bak/kvmimages/swift/4/node/sdb4
sudo rm -f /var/log/debug /var/log/messages /var/log/rsyncd.log/var/log/syslog
#sudo service rsyslog restart
sudo service memcached restart
vi/bak/bin/remakerings
#!/bin/bash
cd /etc/swift
rm -f *.builder *.ring.gz backups/*.builder backups/*.ring.gz
swift-ring-builder object.builder create 18 3 1
swift-ring-builder object.builder add z1-127.0.0.1:6010/sdb1 1
swift-ring-builder object.builder add z2-127.0.0.1:6020/sdb2 1
swift-ring-builder object.builder add z3-127.0.0.1:6030/sdb3 1
swift-ring-builder object.builder add z4-127.0.0.1:6040/sdb4 1
swift-ring-builder object.builder rebalance
swift-ring-builder container.builder create 18 3 1
swift-ring-builder container.builder add z1-127.0.0.1:6011/sdb1 1
swift-ring-builder container.builder add z2-127.0.0.1:6021/sdb2 1
swift-ring-builder container.builder add z3-127.0.0.1:6031/sdb3 1
swift-ring-builder container.builder add z4-127.0.0.1:6041/sdb4 1
swift-ring-builder container.builder rebalance
swift-ring-builder account.builder create 18 3 1
swift-ring-builder account.builder add z1-127.0.0.1:6012/sdb1 1
swift-ring-builder account.builder add z2-127.0.0.1:6022/sdb2 1
swift-ring-builder account.builder add z3-127.0.0.1:6032/sdb3 1
swift-ring-builder account.builder add z4-127.0.0.1:6042/sdb4 1
swift-ring-builder account.builder rebalance
7)启动
/bak/bin/remakerings
cd/bak/openstack/swift/ && ./.unittests
swift-init main start
swift-init rest start
停止, swift-init all stop
测试:
curl -v -H 'X-Storage-User: test:tester' -H 'X-Storage-Pass: testing' http://127.0.0.1:8080/auth/v1.0
curl -v -H 'X-Auth-Token: <token-from-x-auth-token-above>' <url-from-x-storage-url-above>
eg:
curl -v -H 'X-Auth-Token: AUTH_tk4fefc43ce05d4b1594811f5161ed5e00' http://127.0.0.1:8080/v1/AUTH_test
swift -A http://127.0.0.1:8080/auth/v1.0 -U test:tester -K testing stat
|