kubernetes环境部署单节点redis

kubernetes部署redis数据库(单节点)

redis简介

Redis 是咱们经常使用的非关系型数据库,在项目开发、测试、部署到生成环境时,常常须要部署一套 Redis 来对数据进行缓存。这里介绍下如何在 Kubernetes 环境中部署用于开发、测试的环境的 Redis 数据库,固然,部署的是单节点模式,并不是用于生产环境的主从、哨兵或集群模式。单节点的 Redis 部署简单,且配置存活探针,能保证快速检测 Redis 是否可用,当不可用时快速进行重启。html

redis 参数配置

在使用 Kubernetes 部署应用后,通常会习惯与将应用的配置文件外置,用 ConfigMap 存储,而后挂载进入镜像内部。这样,只要修改 ConfigMap 里面的配置,再重启应用就能很方便就可以使应用从新加载新的配置,很方便。redis

部署redis

建立configmap存储redis配置文件

redis-config.yaml数据库

kind: ConfigMap
apiVersion: v1
metadata:
  name: redis-config
  namespace: zisefeizhu
  labels:
    app: redis
data:
  redis.conf: |-
    dir /data
    port 6379
    bind 0.0.0.0
    appendonly yes
    protected-mode no
    requirepass zisefeizhu
    pidfile /data/redis-6379.pid

Redis 数据存储

Kubernetes 部署的应用通常都是无状态应用,部署后下次重启极可能会漂移到不一样节点上,因此不能使用节点上的本地存储,而是使用网络存储对应用数据持久化,PV 和 PVC 是 Kubernetes 用于与储空关联的资源,可与不一样的存储驱动创建链接,存储应用数据,因此接下来咱们要建立 Kubernetes PV、PVC 资源。api

请参考:https://www.cnblogs.com/zisefeizhu/p/13564547.html缓存

建立 Deployment 部署 Redis

建立用于 Kubernetes Deployment 来配置部署 Redis 的参数,须要配置 Redis 的镜像地址、名称、版本号,还要配置其 CPU 与 Memory 资源的占用,配置探针监测应用可用性,配置 Volume 挂载以前建立的 PV、PVC、ConfigMap 资源等等,内容以下:
redis-deployment.yaml网络

---
apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    app: redis
spec:
  type: ClusterIP
  ports:
    - name: redis
      port: 6379
  selector:
    app: redis
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: production-pppharmapack
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      # 进行初始化操做,修改系统配置,解决 Redis 启动时提示的警告信息
      initContainers:
        - name: system-init
          image: busybox:1.32
          imagePullPolicy: IfNotPresent
          command:
            - "sh"
            - "-c"
            - "echo 2048 > /proc/sys/net/core/somaxconn && echo never > /sys/kernel/mm/transparent_hugepage/enabled"
          securityContext:
            privileged: true
            runAsUser: 0
          volumeMounts:
          - name: sys
            mountPath: /sys
      containers:
        - name: redis
          image: redis:5.0.8
          command:
            - "sh"
            - "-c"
            - "redis-server /usr/local/etc/redis/redis.conf"
          ports:
            - containerPort: 6379
          resources:
            limits:
              cpu: 1000m
              memory: 1024Mi
            requests:
              cpu: 1000m
              memory: 1024Mi
          livenessProbe:
            tcpSocket:
              port: 6379
            initialDelaySeconds: 300
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            tcpSocket:
              port: 6379
            initialDelaySeconds: 5
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          volumeMounts:
            - name: data
              mountPath: /data
            - name: config
              mountPath: /usr/local/etc/redis/redis.conf
              subPath: redis.conf
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: zisefeizhu
        - name: config
          configMap:
            name: redis-config
        - name: sys
          hostPath:
            path: /sys

测试redis是否能够正常使用

# ctl get pod -n production-pppharmapack | grep redis
redis-7768dc9c56-4kp8l                    1/1     Running   0          8m43s
ctl exec -it  redis-7768dc9c56-4kp8l -n production-pppharmapack -- /bin/sh
# redis-cli
127.0.0.1:6379> auth zisefeizhu
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "zisefeizhu"
相关文章
相关标签/搜索