# singlenode-redis

* 数据存储采用 hostpath 方式
* Pod 多节点反亲和性调度，单节点失效
* Node 调度， 只运行在指定的节点上



```yaml
[root@master test]# cat singlenode-redis-statefulset.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: singlenode-redis-conf
data:
  redis.conf: |
    bind 0.0.0.0
    port 6379
    requirepass 123456
    pidfile .pid
    appendonly yes
    cluster-config-file nodes-6379.conf
    pidfile /data/k8s/redis/log/redis-6379.pid
    cluster-config-file /data/k8s/redis/conf/redis.conf
    dir /data/k8s/redis/data/
    logfile "/data/k8s/redis/log/redis-6379.log"
    cluster-node-timeout 5000
    protected-mode no


---
apiVersion: v1
kind: Service
metadata:
  name: singlenode-redis
  labels:
    name: singlenode-redis
spec:
  type: NodePort
  ports:
    - name: singlenode-redis
      port: 6379
      targetPort: 6379
      nodePort: 31379
  selector:
    name: singlenode-redis


---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: singlenode-redis
spec:
  replicas: 1
  serviceName: singlenode-redis
  selector:
    matchLabels:
      name: singlenode-redis
  template:
    metadata:
      labels:
        name: singlenode-redis
    spec:
      # 节点打上中间件标签并污染：kubectl taint nodes <node-name> middleware=true:NoSchedule
      #tolerations:
      #  - key: "middleware"
      #    operator: "Equal"
      #    value: "true"
      #    effect: "NoSchedule"
      
      # 当多节点生效：不允许同一个pod调度到同一个节点
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - middleware
              topologyKey: "kubernetes.io/hostname"
      initContainers:
        - name: init-singlenode-redis
          image: busybox
          command: ['sh', '-c', 'mkdir -p /data/k8s/redis/log/; mkdir -p /data/k8s/redis/conf/; mkdir -p /data/k8s/redis/data/']
          volumeMounts:
            - name: data
              mountPath: /data/k8s/redis/
      containers:
        - name: singlenode-redis
          image: redis:7.2.3
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - -c
            - "exec redis-server /data/k8s/redis/conf/redis.conf"
          ports:
            - containerPort: 6379
              name: redis
              protocol: TCP
          volumeMounts:
            - name: redis-config
              mountPath: /data/k8s/redis/conf/
            - name: data
              mountPath: /data/k8s/redis/
      volumes:
        - name: redis-config
          configMap:
            name: singlenode-redis-conf
        - name: data
          hostPath:
            path: /data/k8s/redis/
```
