请写出在k8s集群中,部署一个redis集群的yaml文件,要求是一主两从的架构
时间: 2023-06-01 22:08:10 浏览: 131
手写yaml文件创建k8s的deployment和service
好的,以下是部署一个一主两从的Redis集群的YAML文件:
```yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-cluster
spec:
serviceName: redis
replicas: 3
selector:
matchLabels:
app: redis-cluster
template:
metadata:
labels:
app: redis-cluster
spec:
containers:
- name: redis
image: redis:6.0.0
ports:
- containerPort: 6379
command: ["redis-server"]
args: ["--appendonly", "yes"]
volumeMounts:
- name: redis-data
mountPath: /data
env:
- name: REDIS_REPLICATION_MODE
value: "cluster"
volumes:
- name: redis-data
persistentVolumeClaim:
claimName: redis-pvc
---
apiVersion: v1
kind: Service
metadata:
name: redis
labels:
app: redis-cluster
spec:
selector:
app: redis-cluster
ports:
- name: redis
port: 6379
targetPort: 6379
protocol: TCP
clusterIP: None
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
name: redis-pdb
spec:
selector:
matchLabels:
app: redis-cluster
maxUnavailable: 1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
name: redis-headless
spec:
clusterIP: None
selector:
app: redis-cluster
ports:
- port: 6379
name: db
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-deploy
spec:
selector:
matchLabels:
app: redis-cluster
replicas: 1
template:
metadata:
labels:
app: redis-cluster
spec:
containers:
- name: redis
image: redis:6.0.0
ports:
- containerPort: 6379
command: ["redis-server"]
args: ["--appendonly", "yes"]
volumeMounts:
- name: redis-data
mountPath: /data
env:
- name: REDIS_REPLICATION_MODE
value: "cluster"
- name: REDIS_CLUSTER_NODE_TIMEOUT
value: "5000"
- name: REDIS_CLUSTER_ANNOUNCE_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "redis-cli -a $(cat /run/secrets/redis-pass/redis-pass) SHUTDOWN"]
# This secret must exist before the pod is started.
volumeMounts:
- name: redis-data
mountPath: /data
- name: redis-pass
mountPath: "/run/secrets/redis-pass"
readOnly: true
volumes:
- name: redis-data
emptyDir: {}
- name: redis-pass
secret:
secretName: redis-pass
```
以上YAML文件中部署了一个由一主两从组成的Redis集群,并且采用了StatefulSet、Service、Deployment和PersistentVolumeClaim等Kubernetes资源对象。请根据实际情况修改其中的名称和配置参数。
阅读全文