如何使Kuberbetes外部可以访问Redis集群?

时间:2018-08-24 06:15:36

标签: redis kubernetes

我在kubernets中建立了一个Redis集群。 Redis集群在kubernetes内部被恶意访问,但是当我尝试在kubernetes外部访问Redis集群时,发生以下问题,Redis只能读写当前连接的Redis。当我使用cluster nodes查看Redis集群时信息,则可以正确显示。

这是我的statefulset.yaml

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: redis-cluster-node
spec:
  serviceName: "redis-cluster-svc"
  replicas: 6
  template:
    metadata:
      labels:
        name: redis-cluster-node
        app: redis
    spec:
      hostNetwork: false
      dnsPolicy: ClusterFirstWithHostNet
      terminationGracePeriodSeconds: 10
      imagePullSecrets:
        - name: registrykey
      containers:
      - name: redis-cluster-node
        image: redis-cluster:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 6380
        env:
        - name: MODE
          value: "cluster"
        - name: REDIS_CLUSTER_REPLICAS
          value: 6
        - name: LOG_LEVEL
          value: 0
        - name: REDIS_PORT
          value: 6380
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace

这是我的服务

apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-svc-np
  labels:
    name: redis-cluster-svc-np
    app: redis
spec:
  ports:
  - port: 6380
    targetPort: 6380
    nodePort: 30079
  type: NodePort
  selector:
    name: redis-cluster-node 

Cluster internal access redis

Cluster external access redis

那么,如何使kuberbetes外部也可以访问redis集群?

1 个答案:

答案 0 :(得分:0)

在服务清单中,将type更改为LoadBalancer,并添加loadBalancerSourceRanges字段以限制对某些IP地址的访问。 CIDR表示法用于列出IP地址范围。

apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-svc-np
  labels:
    name: redis-cluster-svc-np
    app: redis
spec:
  ports:
  - port: 6380
    targetPort: 6380
    nodePort: 30079
  type: LoadBalancer
  selector:
    name: redis-cluster-node
  loadBalancerSourceRanges:
    - 100.100.100.100/32
    - 10.1.1.1/24

确保添加群集IP范围以允许从群集内部访问Redis。