常规安装

1.下载安装并解压到你想要安装的路径,我下的是redis-4.0.9
2.进入目录,修改redis.conf

  1. daemonize yes # 以守护进程方式启动,使用本启动方式,redis将以服务的形式存在

3.执行make 进行安装后,进入src目录 执行启动服务命令(docker中的目录是:/usr/local/bin)

  1. ./redis-server /data/util/redis/redis-4.0.9/redis.conf

4.使用客户端连接。同样在src目录下执行命令连接

  1. /redis-cli -p 6379 #ping pong响应连接
  2. #关闭redis 服务
  3. /redis-cli -p 端口 shutdown
  1. redis-cli -p 6379 shutdown 关闭客户端连接
  2. 设置密码 config set requirepass 123456 下次连接数 输入 auth 123456即可
  3. 需要注意的如果你的是外网访问,需要将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文件

    1. version: "3"
    2. services:
    3. redis:
    4. container_name: my-redis-container
    5. image: redis:6.0
    6. environment:
    7. - TZ=Asia/Shanghai
    8. ports:
    9. - 6380:6379
    10. volumes:
    11. - ./volume/data:/data:rw
    12. - ./volume/conf/redis.conf:/etc/redis/redis.conf:rw
    13. command: redis-server /etc/redis/redis.conf
    14. restart: always
    15. privileged: true
  4. 在执行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. > 备注:
  2. > 1. 因为在我这台宿主机上,通过另外一个docker-compose.yml开着另外一个redis服务名相同,导致服务冲突,会挤掉另外一个服务
  3. >
  4. services: redis: 这个名字最好设置不一样的。现在改掉就可以正常启动两个了
  5. > container_name: my-redis-container
  6. > image: redis:6.0
  7. > 2.切记要以redis.conf文件形式启动,否则你redis挂掉可能数据没有按照你的规则持久化到rdb文件中,使得你下一次启动出现数据缺失情况(数据还是之前的老数据)。如果你想同步其它redis机器数据(数据想保持一致),直接将它的rdb文件放到你的挂在目录即可。
  8. > 3.出现Fatal error, can't open config file '/etc/redis/redis.conf': Permission denied 启动报错情况,权限不足
  9. > **权限问题,因为容器主机是以redis用户去执行cmd脚本,但是它跑的是宿主机root权限的文件 给我们宿主机赋权 chmod 777 ./volume/conf/redis.conf 你要挂在的文件**
  10. <a name="kX1OV"></a>
  11. ### 从节点docker-compose文件
  12. 搭建从服务器,当然可以多台,我这里只新建了一台,如果多台从机器配置类似,开启后即可实现主从同步。以此来实现一主多从效果

version: “3” services: redis: container_name: my-redis-container-slave6381 image: redis:6.0 environment:

  1. - TZ=Asia/Shanghai
  2. ports:
  3. - 6381:6379
  4. volumes:
  5. - ./volume/redis.conf:/etc/redis/redis.conf
  6. - ./volume/data:/data
  7. command: redis-server /etc/redis/redis.conf --slaveof 192.168.1.148 6380
  8. restart: always
  9. privileged: true
  1. <a name="rEwba"></a>
  2. # k8s安装
  3. <a name="gLOjt"></a>
  4. ## 单节点安装
  5. 下面方式采用的是deployment方式进行,并且直接使用的宿主机目录(pvc和pv数据卷也没创建),因为想到后期可能会多节点部署,因为目前就单个节点所以没搞的很复杂。其次是在部署之前需要将redis.conf文件提前放到指定目录,否则容器会创建失败。推荐使用 StatefulSet 方式安装
  6. <a name="KRNrc"></a>
  7. ### Deployment方式
  8. <a name="Rwio1"></a>
  9. #### Deployment文件
  10. ```yaml
  11. apiVersion: apps/v1
  12. kind: Deployment # 推荐使用这种 StatefulSet
  13. metadata:
  14. name: redis-master
  15. namespace: prod
  16. labels:
  17. app: redis-master
  18. spec:
  19. replicas: 1
  20. selector:
  21. matchLabels:
  22. app: redis-master
  23. template:
  24. metadata:
  25. labels:
  26. app: redis-master
  27. spec:
  28. containers:
  29. - args:
  30. - redis-server
  31. - --requirepass
  32. - "123456"
  33. env:
  34. - name: TZ
  35. value: Asia/Shanghai
  36. image: redis:6.0
  37. name: redis-container-master
  38. ports:
  39. - containerPort: 6379
  40. resources:
  41. limits:
  42. cpu: 1000m
  43. memory: 1Gi
  44. requests:
  45. cpu: 500m
  46. memory: 512Mi
  47. volumeMounts:
  48. - mountPath: /data
  49. name: redis-master-dir
  50. - mountPath: /etc/redis/redis.conf
  51. name: redis-master-file
  52. restartPolicy: Always # RollingUpdate滚动升级,先创建新版的pod将流量导入到新pod上后销毁原来的旧的pod
  53. volumes:
  54. - name: redis-dir
  55. hostPath:
  56. path: /root/project-server/backend/redis/redis-master/volume/data
  57. type: DirectoryOrCreate
  58. - name: redis-file
  59. hostPath:
  60. path: /root/project-server/backend/redis/redis-master/volume/conf/redis.conf
  61. type: FileOrCreate

Service文件

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: redis-master
  5. namespace: prod
  6. labels:
  7. app: redis-master
  8. spec:
  9. type: NodePort #指定类型为NodePort好让外部访问
  10. ports:
  11. - protocol: TCP
  12. nodePort: 30000 #NodePort默认范围是30000-32767,会映射道容器的对应端口
  13. port: 6379
  14. targetPort: 6379
  15. selector:
  16. app: redis-master

StatefulSet方式(推荐)

StatefulSet文件

  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. name: redis
  5. namespace: prod
  6. labels:
  7. app: redis
  8. spec:
  9. serviceName: redis-headless
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. app: redis
  14. template:
  15. metadata:
  16. labels:
  17. app: redis
  18. spec:
  19. containers:
  20. - name: redis-container
  21. image: redis:6.0
  22. ports:
  23. - containerPort: 6379
  24. command:
  25. - redis-server
  26. - /etc/redis/redis.conf
  27. env:
  28. - name: TZ
  29. value: Asia/Shanghai
  30. resources:
  31. limits:
  32. cpu: 1000m
  33. memory: 1Gi
  34. requests:
  35. cpu: 500m
  36. memory: 512Mi
  37. volumeMounts:
  38. - mountPath: /data
  39. name: redis-dir
  40. - mountPath: /etc/redis/redis.conf
  41. name: redis-file
  42. restartPolicy: Always # RollingUpdate滚动升级,先创建新版的pod将流量导入到新pod上后销毁原来的旧的pod
  43. volumes:
  44. - name: redis-dir
  45. hostPath:
  46. path: /root/project-server/backend/redis/redis-master/volume/data
  47. type: DirectoryOrCreate
  48. - name: redis-file
  49. hostPath:
  50. path: /root/project-server/backend/redis/redis-master/volume/conf/redis.conf
  51. type: FileOrCreate

Service文件

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: redis-headless
  5. namespace: prod
  6. labels:
  7. app: redis
  8. spec:
  9. type: NodePort #指定类型为NodePort好让外部访问
  10. ports:
  11. - protocol: TCP
  12. nodePort: 30000 #NodePort默认范围是30000-32767,会映射道容器的对应端口
  13. port: 6379
  14. targetPort: 6379
  15. selector:
  16. app: redis