泥泞的沼泽 一个程序员的博客

Create a MariaDB service on CentOS with Docker

Learning Docker for a long time, this is my first time to create a service with it. In Docker’s concept, each service should have a container, and your application may comes with many containers.

This time, I am gonna create a MariaDB service.

  1. First of all, let’s create a file named Dockerfile. Dockerfile is used to build an image:

     FROM centos:latest
     MAINTAINER David Xie "david.scriptfan@gmail.com"
    
     EXPOSE 3306
    
  2. With this file, we can get a clean CentOS image, but we don’t have MariaDB installed. MariaDB in CentOS’s repo is not the latest, we can install it from MariaDB’s official repo. This is easy, just create a repo file and put it /etc/yum.repos.d/. Our Dockerfile will look like this:

     FROM centos:latest
     MAINTAINER David Xie "david.scriptfan@gmail.com"
    
     ADD mariadb.repo /etc/yum.repos.d/mariadb.repo
    
     RUN yum install -y hostname MariaDB-server
     RUN yum clean all
    
     EXPOSE 3306
    

    And mariadb.repo:

     [mariadb]
     name = MariaDB
     baseurl = http://yum.mariadb.org/10.0/centos6-amd64
     gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
     gpgcheck=1
    
  3. Now we have MariaDB installed, but how can I connect to it, and how to set password for root? Let’s add more files for it.

    server.cnf is used to make sure we are not blocked by MariaDB:

     [mysqld]
     bind-address=0.0.0.0
     console=1
     general_log=1
     general_log_file=/dev/stdout
     log_error=/dev/stderr
     collation-server=utf8_unicode_ci
     character-set-server=utf8
    

    mariadb.sql is used to update password for root to root and grant all privilliges to all IPs.

     USE mysql;
     GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
     FLUSH PRIVILEGES;
     UPDATE user SET password=PASSWORD("root") WHERE user='root';
     FLUSH PRIVILEGES;
    

    mariadb.sh is used to apply all updates and start MariaDB service for us:

     #!/bin/sh
     chown -R mysql:mysql /var/lib/mysql
     mysql_install_db --user mysql > /dev/null
    
     mysqld_safe --user mysql &
    
     sleep 5s
    
     mysql -v < /root/mariadb.sql
    
     sleep 5s
    
     ps -wef | grep mysql | grep -v grep | awk '{print $2}' | xargs kill -9
    
     mysqld_safe --user mysql
    
  4. Final thing, we need to update our Dockerfile:

     # application container for MySQL
     # VERSION 2014-07-14
    
     FROM centos:latest
     MAINTAINER David Xie "david.scriptfan@gmail.com"
    
     ADD mariadb.repo /etc/yum.repos.d/mariadb.repo
     ADD mariadb.sql /root/mariadb.sql
     ADD server.cnf /etc/my.cnf.d/server.cnf
     ADD mariadb.sh /root/mariadb.sh
    
     RUN yum install -y hostname MariaDB-server
     RUN yum clean all
     RUN chmod +x /root/mariadb.sh
    
     EXPOSE 3306
    
     CMD ["/root/mariadb.sh"]
    
  5. We already have everything we need, we can create our container now.

     docker build --rm=true --no-cache=true -t mariadb .
     docker run -d -p 3306:3306 mariadb
    

    After 10s, our container is ready to use. If you are using Docker on boot2docker, use boot2docker ip to get correct IP for it. Then you can connect your mariadb with IP:3306.

This project is on github, please visit: mariadb. If anything is wrong here, please let me know.

Rabbitmq and Pika

Install rabbitmq

It’s really easy to install rabbitmq on any linux distro.

  • CentOS You need to enable epel first, and then, run yum install -y rabbitmq-server

  • Fedora You don’t need to install epel to install rabbitmq, just run yum install -y rabbitmq-server

Run rabbitmq

After installing rabbitmq, you need to start rabbitmq service, which is also easy. Just type service rabbitmq-server start

Use pika to send message

When you have a rabbitmq environment, you can use pika to send and receive messages.

Send messages to rabbitmq using exchange

Create send.py with below code:

 1 import pika
 2 
 3 # create a connection for rabbitmq using localhost
 4 connection = pika.BlockingConnection(pika.ConnectionParameters(
 5                'localhost'))
 6 channel = connection.channel() # create a channel
 7 # declare an exchange
 8 channel.exchange_declare(exchange='messages', type='direct')
 9 # a routing is a label for a message
10 # receiver will decide which message to get according to routing
11 routings = ['info', 'warning', 'error']
12 
13 for routing in routings:
14     message = '%s message.' % routing
15     channel.basic_publish(exchange='messages',
16                           routing_key=routing,
17                           body=message)
18     print message
19 
20 connection.close()

Receive messages from rabbitmq

Create receive.py with below code:

 1 import pika
 2 
 3 connection = pika.BlockingConnection(pika.ConnectionParameters(
 4                'localhost'))
 5 channel = connection.channel()
 6 channel.exchange_declare(exchange='messages', type='direct')
 7 
 8 routings = ['info', 'warning', 'error']
 9 
10 # declare a queue to receive messages from exchange
11 # and bind queue to a routing_key
12 # this means, this queue can only receive message with this label
13 result = channel.queue_declare(exclusive=True)
14 queue_name = result.method.queue
15 for routing in routings:
16     channel.queue_bind(exchange='messages',
17                        queue=queue_name,
18                        routing_key=routing)
19 
20 def callback(ch, method, properties, body):
21     print " [x] Received %r" % (body,)
22 
23 channel.basic_consume(callback, queue=queue_name, no_ack=True)
24 
25 channel.start_consuming()

Run scripts

First, run send.py and then run receive.py. You can see results from console, to stop it, press ctrl-c

使用docker

什么是docker?

docker是一个Linux Container, 可以把OS运行在里面, 为用户提供一个隔离的, 虚拟的运行环境.

docker和virtualbox的区别就是, docker的container是构建在OS之上, 以进程的方式为用户提供服务, 而virtualbox提供的环境, 是一个完整的机器环境, 包括各种物理设备的虚拟, 像磁盘, 网络, USB等设备. 可以这么说, docker提供的container是一个比virtualbox更简单, 更便捷的容器, 在使用上, docker的速度也快于virtualbox.

安装docker

Docker刚刚release了它的1.0版本, 非常值得去体验啊.

安装docker其实非常简单, 根据自己的OS, 参照文档即可.

比如我是在Mac上装的docker, 使用的文档是: Installing Docker on Mac OS X

几个概念

container

可以理解为VM, 一个container就是一个VM.

image

创建container的镜像, 一般是一个OS的镜像, 里面除了放OS的东西, 还可以放各种application需要的东西, 比如程序代码, 数据等.

使用docker

在docker里创建contrainer, 需要一个可用的image, docker hub上有很多可用的image供大家使用, 比如说我是fedora的使用者, 我就可以用 docker pull fedora:heisenbug 来下载F20到本地. 下载成功后, 使用 docker images 来查看可用的images:

lxie@SanFrancisco ~ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
fedora-packager     heisenbug           8da72b37d6a4        9 hours ago         941.4 MB
fedora              heisenbug           3f2fed40e4b0        5 days ago          372.7 MB

有了image后, 你可以创建自己的container来运行你的application或者使用你的virtual os了: docker -t -i fedora:heisenbug /bin/bash, 怎么样? 已经在你的OS里了吧?

结论

本文只是介绍了如何安装docker, 并且使用已经创建好的image来建立自己的container, 这一步其实只是进入docker世界里最基本的一步, 后面我会继续介绍docker的命令和使用技巧.

如何在OS X里装postgresql和Ruby下的Gem

在OS X里装postgresql有很多方法:

  • 使用EnterpriseDB. 这个是安装程序, 会把postgresql装在OS X里.
  • 使用PostgresApp. 这个是绿色程序, 不需要安装, 打开后postgresql就能用了, 关掉程序, postgresql就停了. 非常适合偶尔使用的. (洁癖必选!)

本文主要讲如何使用PostgresApp来提供postgresql服务, 同时安装Ruby下的gem.

  1. 下载PostgresApp: 查看各版本下载地址
  2. 设置环境变量, export PATH=/Applications/Postgres.app/Contents/Versions/9.3/bin:$PATH
  3. 使用bundle install或者其它命令来安装pg的gem