Kubernets Redis Sentinel Тип службы Nodeport Получить главный IP-адрес - PullRequest
0 голосов
/ 06 мая 2020

Я пытаюсь развернуть Redis Sentinel в Kubernetes.

Это ссылка, которую я взял https://github.com/kubernetes/examples/tree/master/staging/storage/redis

Я использую тип службы NodePort. Проблема в том, что я не могу найти мастер redis и мастер дозорного: ниже мои файлы развертывания и службы.

Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    name: redis
  namespace: {{K8S_NAMESPACE}}
  name: {{DEPLOYMENT_NAME}}-master
spec:
  replicas: 1
  selector:
    matchLabels:
      name: {{DEPLOYMENT_NAME}}
  strategy:
    rollingUpdate:
      maxSurge: 100%
      maxUnavailable: 30%
    type: RollingUpdate
  template:
    metadata:
      labels:
        name: {{DEPLOYMENT_NAME}}
        redis-sentinel: "true"
        role: master
        version: v1
      annotations:
        sla: high
        tier: application
        role: frontend-api
        quality: {{ENVIRONMENT}}
    spec:
      containers:
      - name: {{APP_NAME}}
        image: {{DOCKER_IMAGE}}
        env:
          - name: MASTER
            value: "true"
        ports:
          - containerPort: 6379
        volumeMounts:
          - mountPath: /redis-master-data
            name: data
      - name: sentinel
        image: {{DOCKER_IMAGE}}
        env:
          - name: SENTINEL
            value: "true"
        ports:
          - containerPort: 26379  
      nodeSelector:
        tier: {{NODE_SELECTOR}}
      imagePullSecrets:
      - name: {{IMAGE_PULL_SECRETS}}
      volumes:
        - name: data
          emptyDir: {}

redis-controller.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    name: redis
  namespace: {{K8S_NAMESPACE}}
  name: {{DEPLOYMENT_NAME}}
spec:
  replicas: 1
  revisionHistoryLimit: {{REVISION_HISTORY_LIMIT}}
  selector:
    matchLabels:
      name: {{DEPLOYMENT_NAME}}
  strategy:
    rollingUpdate:
      maxSurge: 100%
      maxUnavailable: 30%
    type: RollingUpdate    
  template:
    metadata:
      labels:
        name: {{DEPLOYMENT_NAME}}
        role: master
    spec:
      containers:
      - name: {{APP_NAME}}
        image: {{DOCKER_IMAGE}}
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: 1.5
        volumeMounts:
        - mountPath: /redis-master-data
          name: data
      nodeSelector:
        tier: {{NODE_SELECTOR}}  
      imagePullSecrets:
      - name: {{IMAGE_PULL_SECRETS}} 
      volumes:
        - name: data
          emptyDir: {}

Sentinel Controller

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    name: redis-sentinel
  namespace: {{K8S_NAMESPACE}}
  name: {{DEPLOYMENT_NAME}}-sentinel
spec:
  replicas: 1
  selector:
    matchLabels:
      redis-sentinel: "true"
  template:
    metadata:
      labels:
        name: redis-sentinel
        redis-sentinel: "true"
        role: sentinel
    spec:
      containers:
      - name: sentinel
        image: {{DOCKER_IMAGE}}
        env:
          - name: SENTINEL
            value: "true"
        ports:
          - containerPort: 26379
      nodeSelector:
        tier: {{NODE_SELECTOR}}    
      imagePullSecrets:
      - name: {{IMAGE_PULL_SECRETS}}
      volumes:
        - name: data
          emptyDir: {}

Service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    name: {{APP_NAME}}
  namespace: {{K8S_NAMESPACE}}
  name: {{APP_NAME}}-{{ENVIRONMENT}}
spec:
  ports:
  - name: http
    port: 26379
    protocol: TCP
    targetPort: 26379
  selector:
    redis-sentinel: "true"
  sessionAffinity: None
  type: NodePort

Docker Файл

FROM alpine:3.8

RUN apk add --no-cache redis sed bash

COPY redis-master.conf /redis-master/redis.conf
COPY redis-slave.conf /redis-slave/redis.conf
COPY run.sh /run.sh
RUN chmod 777 /run.sh

CMD [ "/run.sh" ]

ENTRYPOINT [ "bash", "-c" ]

run. sh

function launchmaster() {
  if [[ ! -e /redis-master-data ]]; then
    echo "Redis master data doesn't exist, data won't be persistent!"
    mkdir /redis-master-data
  fi
  redis-server /redis-master/redis.conf --protected-mode no
}

function launchsentinel() {
  while true; do
    master=$(redis-cli -h ${REDIS_SENTINEL_SERVICE_HOST} -p ${REDIS_SENTINEL_SERVICE_PORT} --csv SENTINEL get-master-addr-by-name mymaster | tr ',' ' ' | cut -d' ' -f1)
    if [[ -n ${master} ]]; then
      master="${master//\"}"
    else
      master=$(hostname -i)
    fi

    redis-cli -h ${master} INFO
    if [[ "$?" == "0" ]]; then
      break
    fi
    echo "Connecting to master failed.  Waiting..."
    sleep 10
  done

  sentinel_conf=sentinel.conf

  echo "sentinel monitor mymaster ${master} 6379 2" > ${sentinel_conf}
  echo "sentinel down-after-milliseconds mymaster 60000" >> ${sentinel_conf}
  echo "sentinel failover-timeout mymaster 180000" >> ${sentinel_conf}
  echo "sentinel parallel-syncs mymaster 1" >> ${sentinel_conf}
  echo "bind 0.0.0.0" >> ${sentinel_conf}

  redis-sentinel ${sentinel_conf} --protected-mode no
}

function launchslave() {
  while true; do
    master=$(redis-cli -h ${REDIS_SENTINEL_SERVICE_HOST} -p ${REDIS_SENTINEL_SERVICE_PORT} --csv SENTINEL get-master-addr-by-name mymaster | tr ',' ' ' | cut -d' ' -f1)
    if [[ -n ${master} ]]; then
      master="${master//\"}"
    else
      echo "Failed to find master."
      sleep 60
      exit 1
    fi
    redis-cli -h ${master} INFO
    if [[ "$?" == "0" ]]; then
      break
    fi
    echo "Connecting to master failed.  Waiting..."
    sleep 10
  done
  sed -i "s/%master-ip%/${master}/" /redis-slave/redis.conf
  sed -i "s/%master-port%/6379/" /redis-slave/redis.conf
  redis-server /redis-slave/redis.conf --protected-mode no
}

if [[ "${MASTER}" == "true" ]]; then
  launchmaster
  exit 0
fi

if [[ "${SENTINEL}" == "true" ]]; then
  launchsentinel
  exit 0
fi

launchslave

Вот проблема, с которой я столкнулся при запуске . sh

master=$(redis-cli -h ${REDIS_SENTINEL_SERVICE_HOST} -p ${REDIS_SENTINEL_SERVICE_PORT} --csv SENTINEL get-master-addr-by-name mymaster | tr ',' ' ' | cut -d' ' -f1)

Поскольку мой тип службы - NodePort. Я не уверен, как запросить мастер Redis у часового?

Кто-нибудь может мне помочь?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...