k8s statefulSet实践

3月 27, 2023 |

我使用的是minikube version: v1.28.0,PersistentVolume的storageClassName属性非常重要,如果没有指定,那么volumeClaimTemplates 会使用默认的provisioner k8s.io/minikube-hostpath, 使用本地的目录映射
docker.io/library/hellok8s:v1.0 是我自己编译的镜像,看君可以使用nginx试验

使用docker 创建nfs服务器

docker run -d --net=host \
   --privileged --name nfs-server \
   katacoda/contained-nfs-server:centos7 \
   /exports/data-0001 /exports/data-0002

statefulset.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-0001
spec:
  storageClassName: javacoder
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    server: 192.168.140.141
    path: /exports/data-0001
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-0002
spec:
  storageClassName: javacoder
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    server: 192.168.140.141
    path: /exports/data-0002
---
apiVersion: v1
kind: Service
metadata:
  name: hellok8s
  labels:
    app: hellok8s
spec:
  ports:
  - port: 8080
    name: web
  clusterIP: None
  selector:
    app: hellok8s
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "hellok8s"
  replicas: 2
  selector:
    matchLabels:
      app: hellok8s
  template:
    metadata:
      labels:
        app: hellok8s
    spec:
      containers:
      - name: hellok8s
        image: docker.io/library/hellok8s:v1.0
        ports:
        - containerPort: 8080
          name: web
        volumeMounts:
        - name: www
          mountPath: /opt/logs
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      storageClassName: javacoder
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

Posted in: Linux

Comments are closed.