需求nfs持久卷 

目录

1、创建pv目录

2、redis.yaml


1、创建pv目录

# master + node
apt-get install nfs-utils(主包提供文件系统)
apt-get install rpcbind(提供rpc协议)
systemctl start rpcbind
systemctl enable nfs-utis --now
 
mkdir -p /data/nfs/Redis/pv
 
vim /etc/exports
/data/nfs/Redis/pv *(rw,nohide,insecure,no_subtree_check,async,no_root_squash)
 
chmod 777 /data/nfs/Redis/*
 
/etc/init.d/nfs-kernel-server restart  //重启 or systemctl restart nfs
 
showmount -e 172.xx.xx.xx  //在其他服务器上查看 (nfs服务器ip)

2、redis.yaml

# redis配置
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config-map
data:
  redis.conf: |
    bind 0.0.0.0
    port 6379
    requirepass 123456   # redis密码
    appendonly yes
    protected-mode no
    dir /var/lib/redis
    rename-command FLUSHALL ""
    rename-command FLUSHDB ""
    rename-command KEYS ""

---
# 数据存储卷
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-pv
spec:
  capacity:
    storage: 7Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: redis
  nfs:
    server: 172.xx.xx.xx
    path: /data/nfs/Redis/pv

---
# redis容器
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  replicas: 1
  serviceName: redis-service
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:latest
        imagePullPolicy: IfNotPresent
        command:
        - sh
        - -c
        - "exec redis-server /usr/local/etc/redis/redis.conf"
        ports:
        - name: redis
          containerPort: 6379
          protocol: TCP
        resources:
          requests:
            cpu: 1
            memory: 512Mi
          limits:
            cpu: 1
            memory: 512Mi
        volumeMounts:
        - name: redis-config
          mountPath: /usr/local/etc/redis
        - name: redis-data
          mountPath: /var/lib/redis
      - name: redis-exporter
        image: oliver006/redis_exporter:latest
        ports:
        - containerPort: 9121
      imagePullSecrets:
        - name: k8s-auth
      volumes:
      - name: redis-config
        configMap:
          name: redis-config-map
  volumeClaimTemplates:
  - metadata:
      name: redis-data
    spec:
      accessModes: ["ReadWriteOnce"]
      volumeMode: Filesystem
      resources:
        requests:
          storage: 7Gi   # 和上面pv匹配
      storageClassName: redis   # 和上面pv的storageClassName匹配

---
# 内部访问的无头服务
apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  selector:
    app: redis
  ports:
  - name: redis
    port: 6379
  clusterIP: None

---
# 外部访问的服务
apiVersion: v1
kind: Service
metadata:
  name: redis-external-service
spec:
  selector:
    app: redis
  ports:
  - name: redis
    protocol: TCP
    port: 6379
    targetPort: 6379
    nodePort: 30103
  type: NodePort

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐