k8s部署单节点Redis

给服务器创建目录

# 添加目录
mkdir -p /home/redis/log /home/redis/conf /home/redis/data

编辑部署文件redis.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: k8s-redis

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-conf
  namespace: k8s-redis
data:
  redis.conf: |
    bind 0.0.0.0
    port 6379
    pidfile .pid
    appendonly yes
    cluster-config-file nodes-6379.conf
    databases 48
    pidfile /home/redis/log/redis-6379.pid
    cluster-config-file /home/redis/conf/redis.conf
    dir /home/redis/data/
    logfile "/home/redis/log/redis-6379.log"
    cluster-node-timeout 5000
    protected-mode no

---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: redis
  name: redis
  namespace: k8s-redis
spec:
  type: NodePort
  ports:
    - port: 6379
      targetPort: 6379
      protocol: TCP
      nodePort: 30268
  selector:
    app: redis

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: redis
  name: redis
  namespace: k8s-redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:6.2.1
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - -c
            - "exec redis-server /home/redis/conf/redis.conf"
          ports:
            - containerPort: 6379
              name: redis
              protocol: TCP
          volumeMounts:
            - name: redis-config
              mountPath: /home/redis/conf/
            - name: redis-data
              mountPath: /home/redis/
      volumes:
        - name: redis-config
          configMap:
            name: redis-conf
        - name: redis-data
          hostPath:
            path: /home/redis/
      nodeSelector:
        "kubernetes.io/os": linux
        "kubernetes.io/hostname": sd-redis

执行部署

kubectl apply -f redis.yaml