#查看版本
uname -r
#Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。
#Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。
rpm -iUvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum -y install docker-io
service docker start
#yum下载的docker版本可能比较低
#方法1:
vim /usr/bin/yum-config-manager #确定使用的yum是py2版本
yum remove docker-io
参考: https://docs.docker.com/engine/installation/linux/docker-ce/centos/#install-docker-ce-1
systemctl start docker
#方法2
#下载最新的
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.12.0.ce-1.el7.centos.x86_64.rpm
一、安装elrepo的yum源--http://elrepo.org/tiki/tiki-index.php
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
#如果出现问题:curl: (60) SSL certificate problem: unable to get local issuer certificate
#wget http://mirror.centos.org/centos/6/os/x86_64/Packages/ca-certificates-2016.2.10-65.4.el6.noarch.rpm
#rpm2cpio ca-certificates-2016.2.10-65.4.el6.noarch.rpm | cpio -idmv
#cp -pi ./etc/pki/tls/certs/ca-bundle.* /etc/pki/tls/certs/
#yum reinstall ca-certificates
#yum reinstall openssl
#如果还是不行,直接不用rpm安装
#wget --no-check-certificate https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
#rpm --import RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
二、升级内核
#在yum的elrepo源中有ml和lt两种内核,其中ml(mainline)为最新版本的内核,lt为长期支持的内核。
#如果要安装ml内核,使用如下命令:
yum --enablerepo=elrepo-kernel -y install kernel-ml
#如果要安装lt内核,使用如下命令:
yum --enablerepo=elrepo-kernel -y install kernel-lt
三、修改grub.conf文件
vim /etc/grub.conf
default=0
docker官方安装
docker search centos
#使用docker运行centos后输出,会自杀,因为docker认为没什么事情可以做了
docker pull centos /bin/echo "Hello world"
#docker build -t csphere/centos:7.1 .
#后台运行docker
docker run -dit centos
#查看容器列表
docker ps
docker ps -a
#进入容器
docker attach 44fc0f0582d9 # 使用了exit命令,容器就会退出后台运行
docker exec -it 44fc0f0582d9 /bin/sh #不会退出
#在宿主机器查看docker的cpu 内存使用状态
docker stats 44fc0f0582d9
参考
vim Dockerfile
# 将官方 Python 运行时用作父镜像
FROM python:2.7-slim
# 将工作目录设置为 /app。指定RUN、CMD与ENTRYPOINT命令的工作目录
WORKDIR /app
# 将当前目录内容复制到位于 /app 中的容器中。复制文件指令。它有两个参数
ADD . /app
# 安装 requirements.txt 中指定的任何所需软件包。在shell或者exec的环境下执行的命令
RUN pip install -r requirements.txt
# 使端口 80 可供此容器外的环境使用
EXPOSE 80
# 定义环境变量
ENV NAME World
# 在容器启动时运行 app.py。提供了容器默认的执行命令。 Dockerfile只允许使用一次CMD指令。 使用多个CMD会抵消之前所有的指令,只有最后一个指令生效。
CMD ["python", "app.py"]
vim requirements.txt
Flask
Redis
app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "cannot connect to Redis, counter disabled"
html = "Hello {name}!
" \
"Hostname: {hostname}
" \
"Visits: {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
docker build -t friendlyhello .
#查看构建的镜像
docker images
#运行构建的镜像(将本机的4000映射到docker的80端口)
docker run -p 4000:80 friendlyhello
curl http://localhost:4000
输出:
#登录 jhcoder 19901108 [email protected]
docker login
#标记
docker tag friendlyhello jhcoder/get-started:part1
docker images
#REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
#friendlyhello latest bb72871e4fe5 14 hours ago 149.1 MB
#jhcoder/get-started part1 bb72871e4fe5 14 hours ago 149.1 MB
#push上去仓库
docker push jhcoder/get-started:part1
#可以在任何地方运行这个镜像
docker run -p 4000:80 jhcoder/get-started:part1
#查看日志
docker logs -f sentry_redis_6301
vim docker-compose.yml
#####文件注意空格 ## mapping values are not allowed in this context
version: "3"
services:
web:
# 将 username/repo:tag 替换为您的名称和镜像详细信息
image: jhcoder/get-started:part1
deploy:
replicas:5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
docker swarm init
#部署
docker stack deploy -c docker-compose.yml getstartedlab
#查看
docker stack ps getstartedlab
#删除
docker stack rm getstartedlab
docker pull hub.c.163.com/library/redis:latest
docker run --name redis-6379 -p 16379:6379 -d hub.c.163.com/library/redis
docker ps
vim redis-6301.conf
vim redis-6302.conf
vim redis-6303.conf
vim sentry-16301.conf
vim sentry-16302.conf
vim sentry-16303.conf
// redis-6301.conf
daemonize yes
pidfile /var/run/redis-6301.pid
port 6301
requirepass jhcoder
// sentry-16301.conf
port 16301
dir /data
sentinel monitor mymaster 192.168.1.10 6301 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster jhcoder
// redis-6302.conf
daemonize yes
pidfile /var/run/redis-6302.pid
port 6302
requirepass jhcoder
slaveof 192.168.1.10 6301
masterauth jhcoder
// sentry-16302.conf
port 16302
dir /data
sentinel monitor mymaster 192.168.1.10 6301 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster jhcoder
// redis-6303.conf
daemonize yes
pidfile /var/run/redis-6303.pid
port 6303
requirepass jhcoder
slaveof 192.168.1.10 6301
masterauth jhcoder
### sentry-16303.conf ###
port 16303
dir /data
sentinel monitor mymaster 192.168.1.10 6301 2
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster jhcoder
使用shell脚本生成配置文件
#!/bin/sh
redis6301="
daemonize yes \n
pidfile /var/run/redis-6301.pid \n
port 6301 \n
requirepass jhcoder \n"
sentry16301="
port 16301 \n
dir /data \n
sentinel monitor mymaster 192.168.1.10 6301 2 \n
sentinel down-after-milliseconds mymaster 30000 \n
sentinel failover-timeout mymaster 60000 \n
sentinel parallel-syncs mymaster 1 \n
sentinel auth-pass mymaster jhcoder \n
sentinel announce-ip 192.168.1.10 \n
sentinel announce-port 16301"
redis6302="
daemonize yes \n
pidfile /var/run/redis-6302.pid \n
port 6302 \n
requirepass jhcoder \n
slaveof 192.168.1.10 6301 \n
slave-announce-ip 192.168.1.10 \n
slave-announce-port 6302 \n
masterauth jhcoder \n"
sentry16302="
port 16302 \n
dir /data \n
sentinel monitor mymaster 192.168.1.10 6301 2 \n
sentinel down-after-milliseconds mymaster 30000 \n
sentinel failover-timeout mymaster 60000 \n
sentinel parallel-syncs mymaster 1 \n
sentinel auth-pass mymaster jhcoder \n
sentinel announce-ip 192.168.1.10 \n
sentinel announce-port 16302"
redis6303="
daemonize yes \n
pidfile /var/run/redis-6303.pid \n
port 6303 \n
requirepass jhcoder \n
slaveof 192.168.1.10 6301 \n
slave-announce-ip 192.168.1.10 \n
slave-announce-port 6303 \n
masterauth jhcoder \n"
sentry16303="
port 16303 \n
dir /data \n
sentinel monitor mymaster 192.168.1.10 6301 2 \n
sentinel down-after-milliseconds mymaster 30000 \n
sentinel failover-timeout mymaster 60000 \n
sentinel parallel-syncs mymaster 1 \n
sentinel auth-pass mymaster jhcoder \n
sentinel announce-ip 192.168.1.10 \n
sentinel announce-port 16303"
echo -e ${redis6301} > ../config.d/redis-6301.conf echo -e ${redis6302} > ../config.d/redis-6302.conf
echo -e ${redis6303} > ../config.d/redis-6303.conf echo -e ${sentry16301} > ../config.d/sentry-16301.conf
echo -e ${sentry16302} > ../config.d/sentry-16302.conf echo -e ${sentry16303} > ../config.d/sentry-16303.conf
这里的配置相比之前改的都是ip的配置
1、docker宿主现在使用host模式。
2、配置貌似不识别域名!!!!!!!!!!!
3、从机标明主机的通讯ip和port,这个配置会自动生成在配置文件中,最后redisson使用这个ip和port,所以需要注意
slave-announce-ip 宿主ip \n
slave-announce-port 6303 \n
docker run -d -ti \
-p 6301:6301 -p 16301:16301 \ -v /data/docker-redis/sentry/config.d/redis-6301.conf:/etc/redis/redis-6301.conf \ -v /data/docker-redis/sentry/config.d/sentry-16301.conf:/etc/redis/sentry-16301.conf \ -v /data/docker-redis/sentry/data/6301:/data \ -m 1024m \ --cpuset-cpus="0" \ --network="host" \ --dns="223.5.5.5" \ --name sentry_redis_6301 \ --sysctl net.core.somaxconn=511 \ hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6301.conf && redis-sentinel /etc/redis/sentry-16301.conf'
docker run -d -ti \
-p 6302:6302 -p 16302:16302 \ -v /data/docker-redis/sentry/config.d/redis-6302.conf:/etc/redis/redis-6302.conf \ -v /data/docker-redis/sentry/config.d/sentry-16302.conf:/etc/redis/sentry-16302.conf \ -v /data/docker-redis/sentry/data/6302:/data \ -m 1024m \ --cpuset-cpus="1" \ --network="host" \ --dns="223.5.5.5" \ --name sentry_redis_6302 \ --sysctl net.core.somaxconn=511 \ hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6302.conf && redis-sentinel /etc/redis/sentry-16302.conf'
docker run -d -ti \
-p 6303:6303 -p 16303:16303 \ -v /data/docker-redis/sentry/config.d/redis-6303.conf:/etc/redis/redis-6303.conf \ -v /data/docker-redis/sentry/config.d/sentry-16303.conf:/etc/redis/sentry-16303.conf \ -v /data/docker-redis/sentry/data/6303:/data \ -m 1024m \ --cpuset-cpus="2" \ --network="host" \ --name sentry_redis_6303 \ --sysctl net.core.somaxconn=511 \ hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6303.conf && redis-sentinel /etc/redis/sentry-16303.conf'
redis-cli -p 6301 -a jhcoder
#在主机进行hset, 在从机进行hget
#启动日志查看
docker logs -f sentry_redis_6302
#进入主机 docker ps
docker exec -ti b4ee02d90d18 /bin/sh
# kill 主机redis-server
ps -ef | grep redis
kill -9 xxx
#查看日志输出
#进入哨兵客户端
redis-cli -p 16301 -a jhcoder
redis-cli -p 16302 -a jhcoder
#查看
SENTINEL masters
#java客户端可以通过自己写命令支持哨兵
以上的做法通过客户端是可以连接上的, 但是通过redisson连接不上,因为redisson需要连接哨兵的6302,但是这里提供的是内网的ip,外网不能直接访问到,所以尝试吧哨兵放在宿主机器上运行。
docker run -d -ti \
-p 6301:6301 \ -v /data/docker-redis/sentry/config.d/redis-6301.conf:/etc/redis/redis-6301.conf \ -v /data/docker-redis/sentry/data/6301:/data \ -m 1024m \ --cpuset-cpus="0" \ --name sentry_redis_6301 \ --sysctl net.core.somaxconn=511 \ hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6301.conf'
docker run -d -ti \
-p 6302:6302 \ -v /data/docker-redis/sentry/config.d/redis-6302.conf:/etc/redis/redis-6302.conf \ -v /data/docker-redis/sentry/data/6302:/data \ -m 1024m \ --cpuset-cpus="1" \ --name sentry_redis_6302 \ --sysctl net.core.somaxconn=511 \ hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6302.conf'
docker run -d -ti \
-p 6303:6303 \ -v /data/docker-redis/sentry/config.d/redis-6303.conf:/etc/redis/redis-6303.conf \ -v /data/docker-redis/sentry/data/6303:/data \ -m 1024m \ --cpuset-cpus="2" \ --name sentry_redis_6303 \ --sysctl net.core.somaxconn=511 \ hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6303.conf'
vim /data/docker-redis/official/config.d/redis-6401.conf ……
port 6401 # 6402 6403 6404 6405 6406
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
docker run -d -ti \
-p 6401:6401 -p 16401:16401 \
-v /data/docker-redis/official/config.d/redis-6401.conf:/etc/redis/redis-6401.conf \
-v /data/docker-redis/official/data/6401:/data \
--name official_redis_6401 \
--sysctl net.core.somaxconn=511 \
hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6401.conf'
docker run -d -ti \
-p 6402:6402 -p 16402:16402 \
-v /data/docker-redis/official/config.d/redis-6402.conf:/etc/redis/redis-6402.conf \
-v /data/docker-redis/official/data/6402:/data \
--name official_redis_6402 \
--sysctl net.core.somaxconn=511 \
hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6402.conf'
docker run -d -ti \
-p 6403:6403 -p 16403:16403 \
-v /data/docker-redis/official/config.d/redis-6403.conf:/etc/redis/redis-6403.conf \
-v /data/docker-redis/official/data/6403:/data \
--name official_redis_6403 \
--sysctl net.core.somaxconn=511 \
hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6403.conf'
docker run -d -ti \
-p 6404:6404 -p 16404:16404 \
-v /data/docker-redis/official/config.d/redis-6404.conf:/etc/redis/redis-6404.conf \
-v /data/docker-redis/official/data/6404:/data \
--name official_redis_6404 \
--sysctl net.core.somaxconn=511 \
hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6404.conf'
docker run -d -ti \
-p 6405:6405 -p 16405:16405 \
-v /data/docker-redis/official/config.d/redis-6405.conf:/etc/redis/redis-6405.conf \
-v /data/docker-redis/official/data/6405:/data \
--name official_redis_6405 \
--sysctl net.core.somaxconn=511 \
hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6405.conf'
docker run -d -ti \
-p 6406:6406 -p 16406:16406 \
-v /data/docker-redis/official/config.d/redis-6406.conf:/etc/redis/redis-6406.conf \
-v /data/docker-redis/official/data/6406:/data \
--name official_redis_6406 \
--sysctl net.core.somaxconn=511 \
hub.c.163.com/library/redis \
/bin/sh -c \
'redis-server /etc/redis/redis-6406.conf'
ruby下载地址
ruby安装教程
使用rvm形式安装
//安装大于2.2.2的ruby,然后安装redis模块
gem install redis
// !!!!! 不要用127.0.0.1,这里使用的是宿主机器的ip
//这里的目的是使用redis官方自带的ruby工具构建起集群间的通讯
./redis-trib.rb create --replicas 1 172.18.0.1:6401 172.18.0.1:6402 \
172.18.0.1:6403 172.18.0.1:6404 172.18.0.1:6405 172.18.0.1:6406