前言
本文包括如下部分:
1)單機(jī)版
2)開發(fā)環(huán)境快速啟動(dòng)redis的一個(gè)方案。
3)容器快速啟動(dòng)一個(gè)偽集群
我的其它相關(guān)文檔:
《kubernetes啟動(dòng)單機(jī)redis》
《helm啟動(dòng)radis-ha》
1. 單機(jī)版
1.1 docker-compose 文件
創(chuàng)建redis目錄狡逢,目錄下創(chuàng)建docker-compose.yml文件
version: "3.1"
services:
redis:
image: redis:4
environment:
TZ: Asia/Shanghai
ports:
- 6379:6379
command: ["redis-server","/etc/redis/redis.conf"]
restart: always
volumes:
- ./conf:/etc/redis/
- ./data:/data
1.2 redis.conf
redis目錄下創(chuàng)建conf目錄,下邊創(chuàng)建redis.cof文件如下
bind 0.0.0.0
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
#loglevel notice
#logfile "/redis/logs/redis.log"
databases 16
requirepass liubei@2021
1.3 啟動(dòng)
在redis目錄下執(zhí)行如下命令啟動(dòng)redis
docker-compose up -d
啟動(dòng)之后颠锉,redis就可以正常使用了。
2 自動(dòng)創(chuàng)建腳本
說明:
1)在宿主機(jī)中執(zhí)行如下腳本史汗,自動(dòng)啟動(dòng)一個(gè)容器供開發(fā)測(cè)試使用琼掠。
2)執(zhí)行過程中需要一些交互式輸入:安裝位置、密碼停撞、使用端口瓷蛙。
3)開發(fā)環(huán)境可從FTP或Http服務(wù)器上調(diào)用該腳本直接本地啟動(dòng)一個(gè)redis實(shí)例。
#!/bin/bash
########## 定義變量 ##########
read -p "輸入安裝的位置(回車默認(rèn)/usr/local/redis ) " home_dir
if [ -z "${home_dir}" ];then
home_dir=/usr/local/redis
fi
read -p "輸入密碼(回車默認(rèn)liubei@2021):" redis_passwd
if [ -z "${redis_passwd}" ];then
redis_passwd=liubei@2021
fi
read -p "輸入端口(默認(rèn)6379):" redis_port
if [ -z "${redis_port}" ];then
redis_port=6379
fi
############## yml文件 ##################
mkdir ${home_dir} -p
cat > ${home_dir}/docker-compose.yml << EOF
version: "3.1"
services:
redis:
image: 10.252.xxx.xxx/public/redis:4
environment:
TZ: Asia/Shanghai
ports:
- ${redis_port}:6379
command: ["redis-server","/etc/redis/redis.conf"]
restart: always
volumes:
- ./conf:/etc/redis
- ./data:/data
EOF
mkdir ${home_dir}/conf -p
chmod 755 ${home_dir}/conf
cat > ${home_dir}/conf/redis.conf << EOF
bind 0.0.0.0
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
#loglevel notice
#logfile "/redis/logs/redis.log"
databases 16
requirepass ${redis_passwd}
EOF
chmod 644 ${home_dir}/conf/redis.conf
cd ${home_dir}
docker-compose up -d
docker ps
3. redis啟動(dòng)一個(gè)偽集群
- docker-compose.yml
創(chuàng)建redis-cluster目錄戈毒,目錄下創(chuàng)建docker-compose.yml文件如下
version: '3.4'
x-image:
&default-image
publicisworldwide/redis-cluster
x-restart:
&default-restart
always
# 網(wǎng)絡(luò)我用本地模式啟動(dòng)的你用橋接也沒有問題艰猬,端口映射就可以了。
x-netmode:
&default-netmode
host
services:
redis1:
image: *default-image
network_mode: *default-netmode
restart: *default-restart
volumes:
- ./7001/data:/data
environment:
- REDIS_PORT=7001
redis2:
image: *default-image
network_mode: *default-netmode
restart: *default-restart
volumes:
- ./7002/data:/data
environment:
- REDIS_PORT=7002
redis3:
image: *default-image
network_mode: *default-netmode
restart: *default-restart
volumes:
- ./7003/data:/data
environment:
- REDIS_PORT=7003
redis4:
image: *default-image
network_mode: *default-netmode
restart: *default-restart
volumes:
- ./7004/data:/data
environment:
- REDIS_PORT=7004
redis5:
image: *default-image
network_mode: *default-netmode
restart: *default-restart
volumes:
- ./7005/data:/data
environment:
- REDIS_PORT=7005
redis6:
image: *default-image
network_mode: *default-netmode
restart: *default-restart
volumes:
- ./7006/data:/data
environment:
- REDIS_PORT=7006
- 啟動(dòng)
# docker-compose up -d
- 查看結(jié)果
[root@dev-python redis-cluster]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------
redis-cluster_redis1_1 /usr/local/bin/entrypoint. ... Up
redis-cluster_redis2_1 /usr/local/bin/entrypoint. ... Up
redis-cluster_redis3_1 /usr/local/bin/entrypoint. ... Up
redis-cluster_redis4_1 /usr/local/bin/entrypoint. ... Up
redis-cluster_redis5_1 /usr/local/bin/entrypoint. ... Up
redis-cluster_redis6_1 /usr/local/bin/entrypoint. ... Up