Docker部署MySql等常用软件
一、MySql
1.1 获取镜像运行
选定mysql版本,直接用以下命令获取mysql镜像并启动
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql:5.6.35
测试链接:mysql -h ip -uroot -p1234
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
1.2 mysql数据持久化
查看docker mysql 默认配置:
$ docker exec mysql cat /etc/mysql/my.cnf
配置信息如下:
# Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
#log-error = /var/log/mysql/error.log
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
从配置可以看出mysql的data默认在docker容器的/var/lib/mysql
目录下。如果不希望数据和容器生命周期一样,可以把宿主机某某目录挂载到/var/lib/mysql
。于是,启动命令变成了:
docker run --name mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=1234 \
-v /data/docker/mysql:/var/lib/mysql \
-d mysql:5.6.35
可以在宿主机编写自己的mysql配置文件如:/my/custom/config-file.cnf
,启动命令就变成了:
docker run --name mysql \
-p 3306:3306 \
-v /data/docker/mysql:/var/lib/mysql \
-v /my/custom:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=1234 \
-d mysql:5.5
RabbitMQ
rabbit的启动很简单,如果用docker-compose
编排,mysql和rabiit放在一起如下
version: '2'
services:
mysql:
image: mysql:5.6.35
ports:
- "3306:3306"
volumes:
- /data/docker/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: IWEkzXvG5940
rabbitmq:
image: 10.69.42.85:5000/mxsev/rabbitmq:management
expose:
- 5672
- 15672
ports:
- "5672:5672"
- "15672:15672"
environment:
RABBITMQ_DEFAULT_VHOST: bs-vhost
三、Dockerfile构建redis伪集群
文件结构:
3,1 编写Dockerfie
FROM centos
MAINTAINER erdaoya
ENV HOME /
# # Ensure UTF-8 lang and locale
#RUN locale-gen en_US.UTF-8
#ENV LANG en_US.UTF-8
#ENV LC_ALL en_US.UTF-8
# Install system dependencies
RUN yum install -y gcc make g++ build-essential libc6-dev wget iproute
# Install ruby dependencies so we can bootstrap the cluster via redis-trib.rb
RUN yum -y install ruby ruby-rdoc && \
yum -y install rubygems && \
gem sources --add https://ruby.taobao.org/ --remove http://rubygems.org/ && \
gem sources -l && \
gem install redis
RUN wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py && \
python ez_setup.py --insecure && \
easy_install pip && \
pip install supervisor
# checkout the 3.0.6 tag (Will change to 3.2 tag when it is released as stable)
RUN wget http://download.redis.io/releases/redis-3.2.6.tar.gz
RUN tar xzf redis-3.2.6.tar.gz
# Build redis from source
RUN (cd ./redis-3.2.6 && make )
RUN mkdir -p /usr/local/redis/bin && \
mkdir -p /usr/local/redis/etc && \
cd redis-3.2.6/src/ && \
mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-trib.rb /usr/local/redis/bin
ADD ./conf/redis.conf /usr/local/redis/etc/redis.conf
# cp redis conf
RUN cd /usr/local/redis/etc/ && \
cp redis.conf redis-3.0.0_7001.conf && \
cp redis.conf redis-3.0.0_7002.conf && \
cp redis.conf redis-3.0.0_7003.conf && \
cp redis.conf redis-3.0.0_7004.conf && \
cp redis.conf redis-3.0.0_7005.conf && \
cp redis.conf redis-3.0.0_7006.conf
# chage redis port, data and so on
RUN cd /usr/local/redis/etc/ && \
sed -i s/7000/7001/g redis-3.0.0_7001.conf && \
sed -i s/7000/7002/g redis-3.0.0_7002.conf && \
sed -i s/7000/7003/g redis-3.0.0_7003.conf && \
sed -i s/7000/7004/g redis-3.0.0_7004.conf && \
sed -i s/7000/7005/g redis-3.0.0_7005.conf && \
sed -i s/7000/7006/g redis-3.0.0_7006.conf
# redis db dir
RUN mkdir -p /data/redis-3.0.0/7001/db && \
mkdir -p /data/redis-3.0.0/7002/db && \
mkdir -p /data/redis-3.0.0/7003/db && \
mkdir -p /data/redis-3.0.0/7004/db && \
mkdir -p /data/redis-3.0.0/7005/db && \
mkdir -p /data/redis-3.0.0/7006/db
# Add supervisord configuration
ADD ./conf/supervisord.conf /etc/supervisor/supervisord.conf
# Add startup script
ADD ./conf/start.sh /start.sh
RUN chmod 755 /start.sh
EXPOSE 7001 7002 7003 7004 7005 7006
CMD ["/bin/bash", "/start.sh"]
3.2 redis配置文件
config/redis.conf
port 7000
daemonize yes
protected-mode no
pidfile "/logs/redis-3.0.0/redis-3.0.0_7000.pid"
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/logs/redis-3.0.0/redis-3.0.0_7000.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis-3.0.0/7000/db
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
maxclients 10000
maxmemory 6442450944
cluster-require-full-coverage no
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 5000
3.3 启动脚本
config/start.sh
#!/bin/bash
mkdir -p /logs/redis-3.0.0/
mkdir -p /var/log/supervisor
supervisord -c /etc/supervisor/supervisord.conf
sleep 3
ps aux|grep redis
IP=`/sbin/ip addr show eth0 |grep 'inet ' | cut -f2 | awk '{ print $2}'|awk -F "/" '{ print $1}'`
# create cluster with one slave
echo "yes" | ruby /usr/local/redis/bin/redis-trib.rb create --replicas 1 ${IP}:7001 ${IP}:7002 ${IP}:7003 ${IP}:7004 ${IP}:7005 ${IP}:7006
tail -f /var/log/supervisor/redis-1.log
3.4 supervisord配置
config/supervisord.conf
[supervisord]
nodaemon=false
[program:redis-1]
command=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis-3.0.0_7001.conf
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true
[program:redis-2]
command=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis-3.0.0_7002.conf
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true
[program:redis-3]
command=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis-3.0.0_7003.conf
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true
[program:redis-4]
command=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis-3.0.0_7004.conf
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true
[program:redis-5]
command=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis-3.0.0_7005.conf
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true
[program:redis-6]
command=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis-3.0.0_7006.conf
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true