mruby-ipvs is an interface to the IP Virtual Server(IPVS) for mruby.
- Install dependencies.
apt-get -y install libnl-dev # or yum -y install libnl-devel, and so on...
- Clone mruby
git clone https://github.com/mruby/mruby/ /usr/local/src/mruby
- Set up
build_config.rb
Use mgem
(recommended)
mgem add mruby-ipvs
# and add some mgems if you want
# mgem add ...
mgem config default > /usr/local/src/mruby/build_config.rb # generate build_config.rb
or add conf.gem
line to build_config.rb
(/usr/local/src/mruby/build_config.rb
).
MRuby::Build.new do |conf|
# ... (snip) ...
conf.gem :git => 'https://github.com/rrreeeyyy/mruby-ipvs', :options => '--recursive'
end
- Build mruby
cd /usr/local/src/mruby && ruby ./minirake
- Check the instllation
/usr/local/src/mruby/build/host/bin/mruby -e 'p IPVS'
IPVS
- Add service.
# Create IPVS::Service instance.
s = IPVS::Service.new({
'addr' => '10.0.0.1',
'port' => 80,
'sched_name' => 'wrr'
})
# apply to IPVS.
s.add_service
- Add destination.
# Create IPVS::Dest instance.
d1 = IPVS::Dest.new({
'addr' => '192.168.0.1',
'port' => 80,
'weight' => 1
})
d2 = IPVS::Dest.new({
'addr' => '192.168.0.2',
'port' => 80,
'weight' => 1,
'conn' => 'DR'
})
# Add destination to IPVS::Service instance.
s.add_dest(d1)
s.add_dest(d2)
# Change the destination weight, connection flag.
d1.weight = 3
d2.conn = 'TUN'
- Sync daemon start/stop
# Create IPVS::Daemon instance.
daemon = IPVS::Daemon.new({
'state' => 'master', # "master" or "backup"
'ifname' => 'eth0',
'syncid' => 1
})
daemon.start() # you can daemon.stop() after start.
- Check the results
$ cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 0A000001:0050 wrr
-> C0A80002:0050 Tunnel 1 0 0
-> C0A80001:0050 Masq 3 0 0
- Check the sync daemon multicast group
# netstat -anu | grep 224.0.0.81:8848
udp 0 0 10.0.2.15:50758 224.0.0.81:8848 ESTABLISHED
- more examples in
examples/
.- examples/keepalived.rb: DSL like keepalived.
You can run mruby-ipvs with docker.
- Build docker image:
docker build -t mruby-ipvs .
- In docker host machine, load
ip_vs
kernel module.
modprobe ip_vs
or if you use docker-machine
, just type:
docker-machine ssh ${DOCKER_MACHINE_NAME:-default} sudo modprobe ip_vs
- Then just docker run:
docker run -d mruby-ipvs ${MRUBY_FILE}
Local Source Testing (Requirements: Vagrant)
$ git clone https://github.com/rrreeeyyy/mruby-ipvs/
$ cd mruby-ipvs
$ git submodule update --init
$ vagrant up
$ vagrant ssh -c 'cd /usr/local/src/mruby && rake test'
- [in Japanese] http://rrreeeyyy.com/blog/2014/03/17/mruby-ipvs/ (blog)
- [in Japanese] https://speakerdeck.com/rrreeeyyy/mruby-ipvs (presentation)
- [in Japanese] http://rrreeeyyy.com/thesis/thesis.pdf (graduation thesis)
- Fork it ( https://github.com/rrreeeyyy/mruby-ipvs/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
GNU General Public License Version 2