파일 설정
- 이전글에서 사용한다. redis -> sentinel 적용시 사용했던 부분에서
하단의 volumeClaimTemplates 부분만이 추가되었다.
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
data:
redis.conf: |
port 6379
bind 0.0.0.0
dir /data
sentinel.conf: |
port 26379
bind 0.0.0.0
# 핵심: redis-0.redis 주소를 사용하며, 쿼럼(2)을 설정합니다.
sentinel monitor mymaster redis-0.redis.default.svc.cluster.local 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
sentinel resolve-hostnames yes
---
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
clusterIP: None # Headless Service: redis-0.redis 주소를 가능하게 함
publishNotReadyAddresses: true
selector:
app: redis
ports:
- name: redis
port: 6379
- name: sentinel
port: 26379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
serviceName: redis
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
# Sentinel은 설정 파일에 쓰기 권한이 필요하므로 초기화 컨테이너로 복사합니다.
initContainers:
- name: config-init
image: redis:6.2-alpine
command:
- sh
- -c
- |
cp /readonly-conf/redis.conf /conf/redis.conf
cp /readonly-conf/sentinel.conf /conf/sentinel.conf
volumeMounts:
- name: readonly-config
mountPath: /readonly-conf
- name: conf
mountPath: /conf
containers:
- name: redis
image: redis:6.2-alpine
command:
- sh
- -c
- |
echo "My host name ${HOSTNAME}"
# 2. 만약 내가 0번 포드가 아니라면, 0번을 마스터로 설정합니다.
if [ "${HOSTNAME}" != "redis-0" ]; then
echo "I am a slave. Pointing to redis-0.redis..."
# --replicaof 옵션을 사용해 실행 시점에 관계를 맺습니다.
exec redis-server /conf/redis.conf --replicaof redis-0.redis 6379
else
echo "I am the master (redis-0)."
exec redis-server /conf/redis.conf
fi
ports:
- containerPort: 6379
volumeMounts:
- name: conf
mountPath: /conf
- name: data
mountPath: /data
- name: sentinel
image: redis:6.2-alpine
command:
- sh
- -c
- |
# redis-0.redis가 IP를 반환할 때까지 무한 루프 (최대 30초 권장)
echo "Waiting for DNS..."
sleep 10
nslookup redis-0.redis.default.svc.cluster.local
echo "DNS is ready! Starting Sentinel..."
exec redis-sentinel /conf/sentinel.conf
ports:
- containerPort: 26379
volumeMounts:
- name: conf
mountPath: /conf
- name: data
mountPath: /data
volumes:
- name: readonly-config
configMap:
name: redis-config
- name: conf
emptyDir: {} # 여기에 설정파일을 복사해서 쓰기 권한을 확보함
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: longhorn # 설치한 Longhorn 스토리지 클래스 이름
resources:
requests:
storage: 1Gi # 용량은 필요에 따라 조절하세요
정상 작동 테스트
- 마찬가지로 이전글에서 longhorn 설정시 대시보드에 접속이 가능해졌다면 대시보드에서 간단하게 확인이 가능하다.
- PVC 상태 확인:
kubectl get pvc
내가 작성한 replica 수만큼 동작이 되고있는지 확인.
STORAGECLASS가 longhorn으로 표시되는지 확인
- 포드와 마운트 확인:
kubectl describe pod redis-0
출력 내용 중 Volumes: 섹션 아래에 data가 pvc-xxxx (Longhorn 볼륨)와 연결되어 있는지 확인
- 데이터 쓰기:
kubectl exec -it redis-0 -- redis-cli set mykey "Longhorn_is_working"
- 포드 삭제 :
kubectl delete pod redis-0
- 포드가 살아난 후 데이터 확인 :
kubectl exec -it redis-0 -- redis-cli get mykey
데이터 저장소 구분
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
data:
redis.conf: |
port 6379
bind 0.0.0.0
dir /data
sentinel.conf: |
port 26379
bind 0.0.0.0
# 핵심: redis-0.redis 주소를 사용하며, 쿼럼(2)을 설정합니다.
sentinel monitor mymaster redis-0.redis.default.svc.cluster.local 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
sentinel resolve-hostnames yes
---
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
clusterIP: None # Headless Service: redis-0.redis 주소를 가능하게 함
publishNotReadyAddresses: true
selector:
app: redis
ports:
- name: redis
port: 6379
- name: sentinel
port: 26379
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: longhorn-redis-special
provisioner: driver.longhorn.io
allowVolumeExpansion: true
parameters:
numberOfReplicas: "3"
staleReplicaTimeout: "2880"
# 핵심: 아까 노드에 설정한 태그를 여기서 지정합니다.
nodeSelector: "redis-data"
# 필요하다면 데이터 디스크 태그도 추가 가능
# diskSelector: "ssd"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
serviceName: redis
replicas: 3
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
# Sentinel은 설정 파일에 쓰기 권한이 필요하므로 초기화 컨테이너로 복사합니다.
initContainers:
- name: config-init
image: redis:6.2-alpine
command:
- sh
- -c
- |
cp /readonly-conf/redis.conf /conf/redis.conf
cp /readonly-conf/sentinel.conf /conf/sentinel.conf
volumeMounts:
- name: readonly-config
mountPath: /readonly-conf
- name: conf
mountPath: /conf
containers:
- name: redis
image: redis:6.2-alpine
command:
- sh
- -c
- |
echo "My host name ${HOSTNAME}"
# 2. 만약 내가 0번 포드가 아니라면, 0번을 마스터로 설정합니다.
if [ "${HOSTNAME}" != "redis-0" ]; then
echo "I am a slave. Pointing to redis-0.redis..."
# --replicaof 옵션을 사용해 실행 시점에 관계를 맺습니다.
exec redis-server /conf/redis.conf --replicaof redis-0.redis 6379
else
echo "I am the master (redis-0)."
exec redis-server /conf/redis.conf
fi
ports:
- containerPort: 6379
volumeMounts:
- name: conf
mountPath: /conf
- name: data
mountPath: /data
- name: sentinel
image: redis:6.2-alpine
command:
- sh
- -c
- |
# redis-0.redis가 IP를 반환할 때까지 무한 루프 (최대 30초 권장)
echo "Waiting for DNS..."
sleep 10
nslookup redis-0.redis.default.svc.cluster.local
echo "DNS is ready! Starting Sentinel..."
exec redis-sentinel /conf/sentinel.conf
ports:
- containerPort: 26379
volumeMounts:
- name: conf
mountPath: /conf
- name: data
mountPath: /data
volumes:
- name: readonly-config
configMap:
name: redis-config
- name: conf
emptyDir: {} # 여기에 설정파일을 복사해서 쓰기 권한을 확보함
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: longhorn-redis-special # 설치한 Longhorn 스토리지 클래스 이름
resources:
requests:
storage: 1Gi # 용량은 필요에 따라 조절하세요