Ubuntu enforce memory freedom

Sometimes it occurs that a device runs full with memory. Simply deleting the files on the device doesn’t help. Ubuntu stills shows that the device is 100% full, even if there are no files on it anymore. This command usually helps to give free the memory.

sudo tune2fs -m 0 /dev/sda5

If it doesn’t help you need to execute this command as well. It quits all processes which are using the device.

sudo fuser -km /mnt/share

Docker Introduction

Docker is one of the most promising technologies these days. It is a container technology based on Linux. A very lightweight form of virtualization. Docker containers can be as small as 50 MB. Much smaller than traditional VMs.

The Docker project was started in March 2013 by dotCloud. In the mean while the makers of dotCloud sold dotCloud to CloudControl and raised $55 Million to focus only on the Docker development.

Check out my slides to “Docker Introduction”. I did this talk at the Webmontag in Mannheim. Feedback was very good.

I’m using Docker since beginning of 2014 in production and I love it. It’s a great technology!

daemon script

This shell script runs forever and checks if the rails worker is running and if not it starts it again:

while :
do
  if ps ax | grep -v grep | grep 'rails worker' > /dev/null
  then
      echo "service running, everything is fine"
      sleep 5
  else
      echo "service is not running. Lets start again"
      cd $APP_ROOT
      $BUNDLE exec unicorn_rails -D -c $CONF
      echo "restarted on $(cat /rails/pids/unicorn.pid)"
      sleep 15
  fi
done

Linux Tage 2013 in Berlin

Linux Tage 2013 in Berlin is the biggest Linux Conference in Germany. It goes from Wednesday to Saturday. There are all day long sessions & tech talks. And of course a big exhibition hall, where you can meet some of the vendors. It is a place there .com meets .org.

I have been there today and will be there tomorrow and on Saturday. You can find me at the Yunicon stand in the exhibition hall. Yunicon is an awesome private cloud provider from Berlin. They have the coolest stand in the exhibition hall because they offer free cocktails! 🙂

LinuxTage_VersionEye

I am doing every day at 15:30 a talk about Continuous Updating with VersionEye.

LinuxTage_VersionEye_Talk

If you want to learn something about Continuous Updating or VersionEye, join the talk. Grab a free cocktail and ask me your questions. I am happy to answer them.

LinuxTage_VersionEye_TechTalk

See you tomorrow.

Capistrano + Rails + Unicorn + NGinx + RVM

Capistrano is a ruby based deployment tool which executes commands in parallel on multiple remote machines, via the SSH protocol. With Capistrano you can deploy your application on N server with one single command from your dev machine. You don’t need to login via SSH to your server.

Screen Shot 2013-01-15 at 8.15.33 PM

If you don’t deploy your app on Heroku or CloudControl, if you have dedicated servers or you are using AWS, than Capistrano is the way to go.

Before you start with Capistrano, you have to implement SSH with authentification keys instead of password. In that way you can just login to your server with a simple “ssh user@server” without password. That is possible if your public ssh certificates are on the server. In that way the server “knows” you.

First you need to add the Gem to your Gemfile.

gem 'capistrano'
gem 'rvm-capistrano'

I am adding here “rvm-capistraon”, too. Because I am working with RVM on my dev machine and on the server. Now you have to run bundler.

bundle install

As next step you have to capify your rails project. Just run:

capify .

That will create some files in your project.

[add] writing './Capfile'
[add] writing './config/deploy.rb'
[done] capified!

Your Capfile is loading the deploy.rb script. The Capfile should look like this:

load 'deploy'
# Uncomment if you are using Rails' asset pipeline
# load 'deploy/assets'
load 'config/deploy'

The magic happens in the deploy.rb file. The full documentation to this script you will find here: https://github.com/capistrano/capistrano. It basically executes a couple of commands on your server. It fetches the current code from your GIT server, runs bundler, rake db:migrate, precompiles your assets and runs the ruby app server.

Here is my deploy.rb with some additional comments.


# Automatically precompile assets
load "deploy/assets"

# Execute "bundle install" after deploy, but only when really needed
require "bundler/capistrano"

# RVM integration
require "rvm/capistrano"

# Name of the application in scm (GIT)
set :application, "your_app"
set :repository, "git@github.com:your_company/your_project/project.git"

# Source Control Management
set :scm, :git

set :deploy_to, "/var/www/#{application}"

# server there the web server is running (nginx)
role :web, "192.168.0.12"

# server there the app server is running (unicorn)
role :app, "192.168.0.12"

# server there the db is running
# This is where Rails migrations will run
role :db, "192.168.0.12", :primary => true

set :rails_env, :production

# user on the server
set :user, "project_user"
set :use_sudo, false

# Target ruby version
set :rvm_ruby_string, '1.9.3'

# System-wide RVM installation
set :rvm_type, :system

# Apply default RVM version for the current account
after "deploy:setup", "deploy:set_rvm_version"

namespace :deploy do

  task :set_rvm_version, :roles => :app, :except => { :no_release => true } do
    run "source /etc/profile.d/rvm.sh && rvm use #{rvm_ruby_string} --default"
  end

  task :start, :roles => :app, :except => { :no_release => true } do
    run "/etc/init.d/unicorn start"
  end

  task :stop, :roles => :app, :except => { :no_release => true } do
    run "/etc/init.d/unicorn stop"
  end

  task :restart, :roles => :app, :except => { :no_release => true } do
    run "/etc/init.d/unicorn restart"
  end

  # Precompile assets
  namespace :assets do
    task :precompile, :roles => :web, :except => { :no_release => true } do
      run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
    end
  end

end

The script for starting and stoping unicorn you can find here: https://robert-reiz.com/2012/02/29/running-unicorn-as-a-service-on-debian-linux/.

On the Linux server you should install a couple things before you deploy:

apt-get install make
apt-get install g++
gem update --system
gem install bundler

Now you can deploy with:

cap deploy

And this command shows you all possible Capistrano tasks:

cap -T

Let me know if you have questions.

Rewriting URLs with Nginx

Rewriting URLs with Apache HTTPD can be pretty ugly. With Nginx it is a breeze. Take a look to this example:

server {
 listen 80;
 server_name my_old_domain.com;
 rewrite ^/(.*) https://www.my_new_domain.com/$1 permanent;
}

This will not just redirect your old URL to your new URL. It passes all the parameter to your new URL, too. If you are migrating a Web Application to new URL, this what you want. All old bookmarked URL are still working with this.

Load balancing with Nginx And Unicorn

Nginx is a pretty awesome lightweight Web Server for Linux. You can use it to deliver static web content. But you can use at as a load balancer, too. If you have a Ruby on Rails Application running on multiple unicorn servers, than Nginx can do the load balancing for the unicorn servers.

Just add the following lines to your “/etc/nginx/conf.d/default.conf”:

upstream unicorns { 
  ip_hash; 
  server XXX.XXX.XXX.XXX:8080; 
  server <IP_ADDRESS>:<PORT>; 
}

XXX stands for an IP Address. You can add as many server lines as you want. The “ip_hash” is not mandatory. This just if you want to force nginx to send the requests from a particular user every time to the same app server. If your Web Application is REST ful and Stateless you don’t need it. Otherwise you should add the line.

Now you just have to add one block for your domain:

server {
  listen 80;
  server_name my-awesome-domain.com;
  location / { 
    proxy_pass http://unicorns; 
  } 
}

And that’s it.

One more thing. If you want limit the file upload size or you have problems with big HTML5 AJAX requests, you should add a little bit more. Here is a more complete example:

server {
  listen 80;
  server_name my-awesome-domain.com;
  client_max_body_size 1M; 
  proxy_buffer_size 128k; 
  proxy_buffers 4 256k; 
  proxy_busy_buffers_size 256k; 
  location / { 
    proxy_pass http://unicorns; 
  } 
}