k8s运行redis

redis-persistentvolume.yaml

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-datadir-pv-1
  namespace: wgs-redis
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /data/k8s-data/redis/redis-test 
    server: 192.168.174.102

redis-persistentvolumeclaim.yaml

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-datadir-pvc-1 
  namespace: wgs-redis
spec:
  volumeName: redis-datadir-pv-1 
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

创建namespace

~# kubectl create ns wgs-redis

redis.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: devops-redis 
  name: deploy-devops-redis
  namespace: wgs-redis
spec:
  replicas: 1 
  selector:
    matchLabels:
      app: devops-redis
  template:
    metadata:
      labels:
        app: devops-redis
    spec:
      containers:
        - name: redis-container
          image: redis:7.0.7 
          imagePullPolicy: Always
          volumeMounts:
          - mountPath: "/data"
            name: redis-datadir
      volumes:
        - name: redis-datadir
          persistentVolumeClaim:
            claimName: redis-datadir-pvc-1 

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: devops-redis
  name: srv-devops-redis
  namespace: wgs-redis
spec:
  type: NodePort
  ports:
  - name: http
    port: 6379 
    targetPort: 6379
    nodePort: 36379 
  selector:
    app: devops-redis
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10800

创建资源

创建pv资源

# kubectl apply -f redis-persistentvolume.yaml 
persistentvolume/redis-datadir-pv-1 created

创建pvc资源

# kubectl apply -f redis-persistentvolumeclaim.yaml 
persistentvolumeclaim/redis-datadir-pvc-1 created

创建redis资源

# kubectl apply -f redis.yaml 
deployment.apps/deploy-devops-redis created
service/srv-devops-redis created

查看资源

查看pv资源

~# kubectl get pv -n wgs-redis
NAME                 CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                           STORAGECLASS   REASON   AGE
redis-datadir-pv-1   1Gi        RWO            Retain           Bound    wgs-redis/redis-datadir-pvc-1                           72s

查看pvc资源

~# kubectl get pvc -n wgs-redis
NAME                  STATUS   VOLUME               CAPACITY   ACCESS MODES   STORAGECLASS   AGE
redis-datadir-pvc-1   Bound    redis-datadir-pv-1   1Gi        RWO                           64s

查看redis资源

~# kubectl get pod -n wgs-redis -o wide
NAME                                   READY   STATUS    RESTARTS   AGE     IP               NODE              NOMINATED NODE   READINESS GATES
deploy-devops-redis-6c9bfc49f9-h6jzk   1/1     Running   0          2m54s   172.20.154.214   192.168.174.106   <none>           <none>

测试redis服务

# redis-cli -h 192.168.174.106 -p 36379
192.168.174.106:36379> keys *
(empty array)
192.168.174.106:36379> set wgs wgs1
OK
192.168.174.106:36379> get wgs
"wgs1"
192.168.174.106:36379> save
OK
192.168.174.106:36379> 
# tail -f dump.rdb 
REDIS0010	redis-ver7.0.7
redis-bits󿿀򳨭eãT©used-memaof-base~󂶧swgs1ÿH^[₃°dXshellXshell

 

posted @ 2022-12-26 16:04  小吉猫  阅读(263)  评论(0编辑  收藏  举报