单节点
- 创建配置文件
vim redis.conf
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/tmp/redis.log"
databases 16
always-show-logo no
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
- 创建deployment配置文件
vim redis.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: redis-single-node
name: redis-single-node
spec:
progressDeadlineSeconds: 600 #定义 deploy 升级的最大时间。
replicas: 1
revisionHistoryLimit: 10 #定义保留的升级记录数。
selector:
matchLabels:
app: redis-single-node
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: redis-single-node
spec:
containers:
- command:
- sh
- -c
- redis-server "/mnt/redis.conf"
env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: C.UTF-8
image: redis:5.0.4-alpine
imagePullPolicy: IfNotPresent
lifecycle: {}
livenessProbe:
failureThreshold: 2 #失败的最大次数2次
initialDelaySeconds: 10 #启动容器后10秒开始检测
periodSeconds: 10 #每过10s检测一次
successThreshold: 1 #只要成功了1次,就表示成功了。
tcpSocket:
port: 6379
timeoutSeconds: 2
name: redis-single-node
ports:
- containerPort: 6379
name: web
protocol: TCP
readinessProbe:
failureThreshold: 2
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
tcpSocket:
port: 6379
timeoutSeconds: 2
resources: #资源限制
limits: #最多可使用的资源
cpu: 100m #CPU的计量单位叫毫核(m)。一个节点的CPU核心数量乘以1000,得到的就是节点总的CPU总数量。如,一个节点有两个核,那么该节点的CPU总量为2000m
memory: 339Mi
requests: #代表容器启动请求的资源限制,分配的资源必须要达到此要求
cpu: 10m
memory: 10Mi
securityContext: #上下文参数
privileged: false #特权,最高权限
runAsNonRoot: false #禁止以root用户启动容器 true为禁止
terminationMessagePath: /dev/termination-log #表示容器的异常终止消息的路径,默认在 /dev/termination-log 下。当容器退出时,可以通过容器的状态看到退出信息。
terminationMessagePolicy: File #默认情况容器退出时,退出信息会从文件中读取。 可以修改为 FallbackToLogsOnError 从日志中读取
volumeMounts:
- mountPath: /usr/share/zoneinfo/Asia/Shanghai
name: tz-config
- mountPath: /etc/localtime
name: tz-config
- mountPath: /etc/timezone
name: timezone
- mountPath: /mnt
name: redis-conf
readOnly: true
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30 #在规定的terminationGracePeriodSeconds优雅时间内完成Pod优雅终止动作。默认是30秒
tolerations: #容忍
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 30
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 30
volumes:
- hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
type: ""
name: tz-config
- hostPath:
path: /etc/timezone
type: ""
name: timezone
- configMap:
defaultMode: 420
name: redis-conf
name: redis-conf
启动
kubectl create cm redis-conf --from-file=redis.conf
kubectl create -f redis.yaml
kubectl expose deploy redis-single-node --port 6379
集群部署
集群使用operator部署
operator github地址:https://github.com/operator-framework/awesome-operators
redis-cluster-operator: https://github.com/ucloud/redis-cluster-operator#deploy-redis-cluster-operator
- 拉取代码
git clone https://github.com/ucloud/redis-cluster-operator.git
- 创建crd
cd redis-cluster-operator
kubectl create -f deploy/crds/redis.kun_distributedredisclusters_crd.yaml
kubectl create -f deploy/crds/redis.kun_redisclusterbackups_crd.yaml
- 创建所需资源
kubectl create -f deploy/service_account.yaml
kubectl create -f deploy/cluster/cluster_role.yaml
kubectl create -f deploy/cluster/cluster_role_binding.yaml
kubectl create -f deploy/cluster/operator.yaml
- 等待redis-cluster-operator启动成功后创建redis集群
kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
redis-cluster-operator 1/1 1 1 114s
kubectl apply -f deploy/example/redis.kun_v1alpha1_distributedrediscluster_cr.yaml
- 测试连接
kubectl exec -it drc-example-distributedrediscluster-0-0 -- sh
redis-cli -h example-distributedrediscluster
set a b
#可能会报错,暂时不知道为什么刚装好的集群会报错
(error)CLUSTERDOWN Hash slot not served
#修复操作
redis-cli --cluster fix 127.0.0.1:6379
- 删除集群
kubectl delete -f deploy/example/redis.kun_v1alpha1_distributedrediscluster_cr.yaml
kubectl delete -f deploy/cluster/operator.yaml
kubectl delete -f deploy/cluster/cluster_role_binding.yaml
kubectl delete -f deploy/cluster/cluster_role.yaml
kubectl delete -f deploy/service_account.yaml
kubectl delete -f deploy/crds/redis.kun_redisclusterbackups_crd.yaml
kubectl delete -f deploy/crds/redis.kun_distributedredisclusters_crd.yaml