K8S Redis 连接

K8S Redis 连接

我已经为 HA Redis 安装了 Bitnami Helm Chart: https://bitnami.com/stack/redis/helm

aleg@Azure:~$ kubectl get pods | grep redis
redis-1580896952-master-0                              1/1     Running   0         67m
redis-1580896952-slave-0                               1/1     Running   0          67m
redis-1580896952-slave-1                               1/1     Running   0          65m

aleg@Azure:~$ kubectl get svc | grep redis
redis-1580896952-headless             ClusterIP      None           <none>          6379/TCP                     67m
redis-1580896952-master               ClusterIP      10.0.244.169   <none>          6379/TCP                     67m
redis-1580896952-slave                ClusterIP      10.0.250.136   <none>          6379/TCP                     67m

我该如何连接它?连接到服务或主 pod 还是其他?请提供建议。

答案1

这很好地解释了示例:使用 Redis 部署 PHP Guestbook 应用程序

解释了整个设置过程,然后设置并公开留言簿前端描述如何公开用于处理 HTTP 请求的 Web 前端。它配置为连接到redis-master用于写入请求的服务以及redis-slave用于读取请求的服务。

示例deployment如下:

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: frontend
  labels:
    app: guestbook
spec:
  selector:
    matchLabels:
      app: guestbook
      tier: frontend
  replicas: 3
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google-samples/gb-frontend:v4
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # Using `GET_HOSTS_FROM=dns` requires your cluster to
          # provide a dns service. As of Kubernetes 1.3, DNS is a built-in
          # service launched automatically. However, if the cluster you are using
          # does not have a built-in DNS service, you can instead
          # access an environment variable to find the master
          # service's host. To do so, comment out the 'value: dns' line above, and
          # uncomment the line below:
          # value: env
        ports:
        - containerPort: 80

通过该设置,您只需创建前端服务:

apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # comment or delete the following line if you want to use a LoadBalancer
  type: NodePort 
  # if your cluster supports it, uncomment the following to automatically create
  # an external load-balanced IP for the frontend service.
  # type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: guestbook
    tier: frontend

我建议你阅读有关Redis

相关内容