【Django-CI系统】Volume解法1--SQLite持久化保存到虚拟机hostPath-20220516

【Django-CI系统】Volume解法1--SQLite持久化保存到虚拟机hostPath-20220516,第1张

效果:deployment的pod重新启动后,SQLite可以持久化保存数据。 9K. SQLite可保存至hostPath

步骤1:应用启动时,会将数据从持久卷同步到应用中。因为hostpath: /data为空,故第一次部署时,数据库是空的。
步骤2:应用启动后,数据会从应用同步到持久卷。故在部署后的应用网站上传数据后,数据会同步到hostpath: /data。

步骤3:应用关闭后,数据会保存在持久卷中。
步骤4:将重启pod或deployment后,持久卷会重复步骤1-----应用启动时,会将数据从持久卷同步到应用中。

重新设定SQLite的位置到training_system\SQL,方便挂hostPath的volume

settings.py

# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

print(Path(__file__).resolve().parent)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        #'NAME': BASE_DIR / 'db.sqlite3',
        'NAME': Path(__file__).resolve().parent /'SQL' /'db.sqlite3',
    }
}

yamal设定PVC

yaml档案

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ci-dp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ci-pod
  template:
    metadata:
      labels:
        app: ci-pod
    spec:
      containers:
      - name: ci
        image: seasonzhang/civ2:5.0
        imagePullPolicy: Always
        resources:
          requests:
            cpu: 500m
            memory: 500Mi
          limits:
            cpu: 800m
            memory: 1000Mi
        ports:
        - containerPort: 8000
          name: ci-pod
          protocol: TCP
        volumeMounts:
        - name: ci-pvcc
          mountPath: "/code/package/training_system/SQL"
      volumes:
       - name: ci-pvcc
         persistentVolumeClaim:
         hostPath:
            # directory location on host
            path: /data
            # this field is optional
            type: Directory
---
apiVersion: v1
kind: Service
metadata:
  name: ci-svc
spec:
  type: LoadBalancer
  ports:
   - port: 8330
     targetPort: 8000
     protocol: TCP
  selector:
    app: ci-pod

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/942862.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存