- 常规安装
- docker-compose安装
- # 其它网络也可以访问,这是限制redis只能本地访问
- bind 0.0.0.0 # 注释限制本地访问
- requirepass 123456
- # 默认yes,开启保护模式,限制为本地访问
- protected-mode no
- # 默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方式启动redis失败
- daemonize no
- # 数据库个数(可选)
- databases 16
- # 输入本地redis数据库存放文件夹(可选)
- dir ./
- # redis持久化(可选)
- appendonly yes
- logfile “access.log”
- StatefulSet方式(推荐)
常规安装
1.下载安装并解压到你想要安装的路径,我下的是redis-4.0.9
2.进入目录,修改redis.conf
daemonize yes # 以守护进程方式启动,使用本启动方式,redis将以服务的形式存在
3.执行make 进行安装后,进入src目录 执行启动服务命令(docker中的目录是:/usr/local/bin)
./redis-server /data/util/redis/redis-4.0.9/redis.conf
4.使用客户端连接。同样在src目录下执行命令连接
/redis-cli -p 6379 #ping pong响应连接
#关闭redis 服务
/redis-cli -p 端口 shutdown
- redis-cli -p 6379 shutdown 关闭客户端连接
- 设置密码 config set requirepass 123456 下次连接数 输入 auth 123456即可
需要注意的如果你的是外网访问,需要将redis.conf的配置绑定的地址有bind 127.0.0.1改成0.0.0.0
docker-compose安装
单节点安装
前提:拉取一个redis镜像,要知道版本号,我用的是6.0。这个很关键,涉及到之后使用的redis.conf,要使用相同版本的。redis.conf下载地址:http://download.redis.io/releases/
主节点docker-compose文件
version: "3"
services:
redis:
container_name: my-redis-container
image: redis:6.0
environment:
- TZ=Asia/Shanghai
ports:
- 6380:6379
volumes:
- ./volume/data:/data:rw
- ./volume/conf/redis.conf:/etc/redis/redis.conf:rw
command: redis-server /etc/redis/redis.conf
restart: always
privileged: true
在执行docker-compose up -d 之前,需要我们把conf文件放到对应的挂在目录下,我这里是当前conf文件夹下创建redis.conf即可 ```
# 其它网络也可以访问,这是限制redis只能本地访问
bind 0.0.0.0 # 注释限制本地访问
requirepass 123456
# 默认yes,开启保护模式,限制为本地访问
protected-mode no
# 默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方式启动redis失败
daemonize no
# 数据库个数(可选)
databases 16
# 输入本地redis数据库存放文件夹(可选)
dir ./
# redis持久化(可选)
appendonly yes
logfile “access.log”
bind 0.0.0.0 requirepass 123456 protected-mode yes port 6379 tcp-backlog 511 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile “” databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb replica-serve-stale-data yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-diskless-load disabled repl-disable-tcp-nodelay no replica-priority 100 acllog-max-len 128 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no lazyfree-lazy-user-del no oom-score-adj no oom-score-adj-values 0 200 800 appendonly no 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 aof-use-rdb-preamble 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-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes jemalloc-bg-thread yes
> 备注:
> 1. 因为在我这台宿主机上,通过另外一个docker-compose.yml开着另外一个redis服务名相同,导致服务冲突,会挤掉另外一个服务
>
services: redis: 这个名字最好设置不一样的。现在改掉就可以正常启动两个了
> container_name: my-redis-container
> image: redis:6.0
> 2.切记要以redis.conf文件形式启动,否则你redis挂掉可能数据没有按照你的规则持久化到rdb文件中,使得你下一次启动出现数据缺失情况(数据还是之前的老数据)。如果你想同步其它redis机器数据(数据想保持一致),直接将它的rdb文件放到你的挂在目录即可。
> 3.出现Fatal error, can't open config file '/etc/redis/redis.conf': Permission denied 启动报错情况,权限不足
> **权限问题,因为容器主机是以redis用户去执行cmd脚本,但是它跑的是宿主机root权限的文件 给我们宿主机赋权 chmod 777 ./volume/conf/redis.conf 你要挂在的文件**
<a name="kX1OV"></a>
### 从节点docker-compose文件
搭建从服务器,当然可以多台,我这里只新建了一台,如果多台从机器配置类似,开启后即可实现主从同步。以此来实现一主多从效果
version: “3” services: redis: container_name: my-redis-container-slave6381 image: redis:6.0 environment:
- TZ=Asia/Shanghai
ports:
- 6381:6379
volumes:
- ./volume/redis.conf:/etc/redis/redis.conf
- ./volume/data:/data
command: redis-server /etc/redis/redis.conf --slaveof 192.168.1.148 6380
restart: always
privileged: true
<a name="rEwba"></a>
# k8s安装
<a name="gLOjt"></a>
## 单节点安装
下面方式采用的是deployment方式进行,并且直接使用的宿主机目录(pvc和pv数据卷也没创建),因为想到后期可能会多节点部署,因为目前就单个节点所以没搞的很复杂。其次是在部署之前需要将redis.conf文件提前放到指定目录,否则容器会创建失败。推荐使用 StatefulSet 方式安装
<a name="KRNrc"></a>
### Deployment方式
<a name="Rwio1"></a>
#### Deployment文件
```yaml
apiVersion: apps/v1
kind: Deployment # 推荐使用这种 StatefulSet
metadata:
name: redis-master
namespace: prod
labels:
app: redis-master
spec:
replicas: 1
selector:
matchLabels:
app: redis-master
template:
metadata:
labels:
app: redis-master
spec:
containers:
- args:
- redis-server
- --requirepass
- "123456"
env:
- name: TZ
value: Asia/Shanghai
image: redis:6.0
name: redis-container-master
ports:
- containerPort: 6379
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 500m
memory: 512Mi
volumeMounts:
- mountPath: /data
name: redis-master-dir
- mountPath: /etc/redis/redis.conf
name: redis-master-file
restartPolicy: Always # RollingUpdate滚动升级,先创建新版的pod将流量导入到新pod上后销毁原来的旧的pod
volumes:
- name: redis-dir
hostPath:
path: /root/project-server/backend/redis/redis-master/volume/data
type: DirectoryOrCreate
- name: redis-file
hostPath:
path: /root/project-server/backend/redis/redis-master/volume/conf/redis.conf
type: FileOrCreate
Service文件
apiVersion: v1
kind: Service
metadata:
name: redis-master
namespace: prod
labels:
app: redis-master
spec:
type: NodePort #指定类型为NodePort好让外部访问
ports:
- protocol: TCP
nodePort: 30000 #NodePort默认范围是30000-32767,会映射道容器的对应端口
port: 6379
targetPort: 6379
selector:
app: redis-master
StatefulSet方式(推荐)
StatefulSet文件
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
namespace: prod
labels:
app: redis
spec:
serviceName: redis-headless
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis-container
image: redis:6.0
ports:
- containerPort: 6379
command:
- redis-server
- /etc/redis/redis.conf
env:
- name: TZ
value: Asia/Shanghai
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 500m
memory: 512Mi
volumeMounts:
- mountPath: /data
name: redis-dir
- mountPath: /etc/redis/redis.conf
name: redis-file
restartPolicy: Always # RollingUpdate滚动升级,先创建新版的pod将流量导入到新pod上后销毁原来的旧的pod
volumes:
- name: redis-dir
hostPath:
path: /root/project-server/backend/redis/redis-master/volume/data
type: DirectoryOrCreate
- name: redis-file
hostPath:
path: /root/project-server/backend/redis/redis-master/volume/conf/redis.conf
type: FileOrCreate
Service文件
apiVersion: v1
kind: Service
metadata:
name: redis-headless
namespace: prod
labels:
app: redis
spec:
type: NodePort #指定类型为NodePort好让外部访问
ports:
- protocol: TCP
nodePort: 30000 #NodePort默认范围是30000-32767,会映射道容器的对应端口
port: 6379
targetPort: 6379
selector:
app: redis