狂神--Redis-Docker启动脚本

1.docker 版本 20

docker info

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
  scan: Docker Scan (Docker Inc., v0.9.0)

Server:
 Containers: 22
  Running: 22
  Paused: 0
  Stopped: 0
 Images: 53
 Server Version: 20.10.11
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc version: v1.0.2-0-g52b36a2
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.4.0-90-generic
 Operating System: Ubuntu 20.04.3 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 62.79GiB
 Name: S-SI-APP-GRGGFW2
 ID: ZML7:RXBJ:UIBL:H26O:VFLK:BIZX:PXF5:MICC:UMHL:XQGK:5TX4:5EOE
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

2.创建文件夹

mkdir -p /work/redis-cluster/redis/{bin,data,conf,logs}
mkdir -p /work/redis-cluster/redis2/{bin,data,conf,logs}
mkdir -p /work/redis-cluster/redis3/{bin,data,conf,logs}
mkdir -p /work/redis-cluster/sentinel/{bin,conf,logs}

3.添加配置文件

??为啥容器里面没有默认的配置文件 我还想着从容器内拷出来 =下载了一个windows拷过来的=

#修改的配置 redis.conf   6379
port 6379      #端口
logfile "/var/log/redis/redis-server.log"  #日志文件
masterauth passwrod  #主机密码
requirepass passwrod  #本机密码
#修改的配置 redis.conf   6380
port 6380      #端口
logfile "/var/log/redis2/redis-server.log"  #日志文件
masterauth passwrod  #主机密码
requirepass passwrod  #本机密码
#修改的配置 redis.conf   6381
port 6381      #端口
logfile "/var/log/redis3/redis-server.log"  #日志文件
masterauth passwrod  #主机密码
requirepass passwrod  #本机密码
#sentinel 配置文件
sentinel monitor mymaster 127.0.0.1 6379 1   #监控79主机
sentinel auth-pass mymaster password
requirepass password

4.创建日志文件

??为啥自己没有自动生成

#log 目录下
touch redis-server.log
chmod 666 redis-server.log

·······················20230316························

原因: 容器内的用户没有创建文件和写入文件的权限【当然root账号不会出现这种问题】

1.容器内的用户和用户组和宿主机是一致的,容器内的用户redis 在宿主机没有相应的权限

容器内的用户:
root@480b143fee75:/# cat /etc/passwd | grep redis
redis:x:999:999::/home/redis:/bin/sh

宿主机文件所属用户
cat /etc/passwd | grep ubuntu
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash


解决办法
1.启动容器时加入 -u参数  【推荐】
 -u 可以指定宿主机运行docker命令的用户, -u指定的uid就是docker实际运行的进程拥有者。
 eg:
 	docker run -d --restart=always -p 6379:6379 \
        --log-opt max-size=10m  -u 1000 \
        -v /etc/localtime:/etc/localtime:ro \
        -v /etc/timezone:/etc/timezone \
        -v /work/redis-cluster/redis/data:/data \
        -v /work/redis-cluster/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
        --privileged=true  -v /work/redis-cluster/redis/logs:/var/log/redis   \ 
        --name redis redis:latest  redis-server /usr/local/etc/redis/redis.conf
2.更改成所有用户都有读写的权限
    touch redis-server.log
    chmod 666 redis-server.log
3.把logs目录更改成容器内用户的uid
	chown 999 /work/redis-cluster/redis/logs/

5.启动脚本

docker pull redis
docker run -d --restart=always -p 6379:6379 \
        --log-opt max-size=10m \
        -v /etc/localtime:/etc/localtime:ro \
        -v /etc/timezone:/etc/timezone \
        -v /work/redis-cluster/redis/data:/data \
        -v /work/redis-cluster/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
        --privileged=true  -v /work/redis-cluster/redis/logs:/var/log/redis   \ 
        --name redis redis:latest  redis-server /usr/local/etc/redis/redis.conf
docker run -d --restart=always -p 6380:6380 \
        --log-opt max-size=10m \
        -v /etc/localtime:/etc/localtime:ro \
        -v /etc/timezone:/etc/timezone \
        -v /work/redis-cluster/redis2/data:/data \
        -v /work/redis-cluster/redis2/conf/redis.conf:/usr/local/etc/redis/redis.conf \
        --privileged=true  -v /work/redis-cluster/redis2/logs:/var/log/redis    \
        --name redis2 redis:latest  redis-server /usr/local/etc/redis/redis.conf

	docker run -d --restart=always -p 6381:6381 \
        --log-opt max-size=10m \
        -v /etc/localtime:/etc/localtime:ro \
        -v /etc/timezone:/etc/timezone \
        -v /work/redis-cluster/redis3/data:/data \
        -v /work/redis-cluster/redis3/conf/redis.conf:/usr/local/etc/redis/redis.conf \
        --privileged=true  -v /work/redis-cluster/redis3/logs:/var/log/redis    \
        --name redis3 redis:latest  redis-server /usr/local/etc/redis/redis.conf
docker run --network=host --name sentinel \
        --log-opt max-size=10m \
        --restart=always \
        -v /etc/localtime:/etc/localtime:ro \
        -v /etc/timezone:/etc/timezone \
        -v /work/redis-cluster/sentinel/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
        -d redis:latest  redis-sentinel /usr/local/etc/redis/sentinel.conf

6.选两个从机执行命令

SLAVEOF 127.0.0.1 6379 #认主
posted @ 2023-03-15 21:08  When?  阅读(86)  评论(0编辑  收藏  举报