简体   繁体   中英

Clear Kubernetes persistent volume on new image

I have a deployment of wordpress with a custom docker image with a custom theme copied into the image that gets deployed into a kubernetes cluster with a persistent volume.

The initial deployment works great and the website presents as expected. The problem comes when I update the theme and deploy a new docker image, because of the persistent volume the theme files don't seem to be updated to the new version of the theme in the docker image.

Is there a to clear/reset the wp-content/themes/my-theme directory when I deploy the new image?

Any help is appreciated and code samples below.

Dockerfile:

FROM wordpress:latest
COPY ./my-thtme /usr/src/wordpress/wp-content/themes/my-theme

Persistent Volume Claim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress
spec:
    accessModes:
        - ReadWriteMany
    resources:
        requests:
            storage: 2Gi
    storageClass: "nfs"

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      imagePullSecrets:
        - name: gitlab-auth
      containers:
        - name: wordpress
          image: registry.gitlab.com/user/wordpress:1234
          imagePullPolicy: IfNotPresent
          env:
            - name: WORDPRESS_DB_HOST
              value: wordpress-mysql
            - name: WORDPRESS_DB_USER
              value: mysql_wordpress
            - name: WORDPRESS_DB_NAME
              value: wordpress
            - name: WORDPRESS_DB_TABLE_PREFIX
              value: _wp
            - name: WORDPRESS_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: mysql-password
          volumeMounts:
          - name: wordpress-data
            mountPath: /var/www/html/wp-content
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
      volumes:
      - name: wordpress-data
        persistentVolumeClaim:
          claimName: wordpress

There is more than one way to do it. You could add an initContainer to the Deployment spec to remove existing files from thePersistent Volume before app containers in the Pod are started. For example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      imagePullSecrets:
        - name: gitlab-auth
      initContainers:
      - name: init-theme
        image: "alpine:3"
        command: ["sh", "-c", "if [ -d /var/www/html/wp-content/my-theme ]; then rm -rf /var/www/html/wp-content/my-theme; fi"]
        volumeMounts:
          - name: workdpress-data
            mountPath: /var/www/html/wp-content
      containers:
      ...
      volumes:
      - name: wordpress-data
        persistentVolumeClaim:
          claimName: wordpress

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM