Im new to Kubernetes and i try to run Laravel application from persistent volume. I got it working and i will have now the code running from persistent volume and i can scale it.
I have nginx running in own pods and share the same persistent volume.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
tier: backend
app: nginx
spec:
replicas: 4
selector:
matchLabels:
app: nginx
tier: backend
template:
metadata:
labels:
app: nginx
tier: backend
spec:
volumes:
- name: laravel-pv-volume
persistentVolumeClaim:
claimName: laravel-pv-claim
- name: config
configMap:
name: nginx-config
items:
- key: config
path: site.conf
containers:
- name: nginx
image: nginx
volumeMounts:
- name: laravel-pv-volume
mountPath: /code
- name: config
mountPath: /etc/nginx/conf.d
ports:
- containerPort: 80
name: http
protocol: TCP
My Laravel deployment includes initContainers which has command to copy Laravel source code from /var/www to /code which is the persistent volume path
apiVersion: apps/v1
kind: Deployment
metadata:
name: php
labels:
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: php
tier: backend
template:
metadata:
labels:
app: php
tier: backend
spec:
volumes:
- name: laravel-pv-volume
persistentVolumeClaim:
claimName: laravel-pv-claim
containers:
- name: php
image: registry.digitalocean.com/xxx/laravel-test:3.0
volumeMounts:
- name: laravel-pv-volume
mountPath: /code
initContainers:
- name: install
imagePullPolicy: Always
image: registry.digitalocean.com/xxx/laravel-test:3.0
command: ["/bin/sh", "-c", "cp -R /var/www/. /code && chown -R www-data:www-data /code"]
volumeMounts:
- name: laravel-pv-volume
mountPath: /code
How can i create new pods with new code after the Laravel image changes and code will be updated, somehow i think that after this i should make new persistent volume path for mount that will run the code for the new pods, and when old pods will be terminated, last one will delete the "old" code from persistent volume. But also i don't know that will the pods now that which one is last?
My workaround this could be that if my Laravel image updates to next version, i will add command to clear the /code folder, but that will not be the best practice and will couse downtime.
command: ["/bin/sh", "-c", "rm -rf /code/*" && "cp -R /var/www/. /code && chown -R www-data:www-data /code"]
I understand from where these practices come. But you should do differently on Kubernetes.
When you updated your PHP code, don't put it on a PersistentVolume, instead, build a new Docker image, with e.g. docker build -t <myapp>:<new version> .
and when you have pushed the image to a registry, you can update the image:
field in the Deployment
. Avoid putting code on Persistent Volumes, and most likely you will not need a Persistent Volume Claim in your Deployment
at all.
Most likely you want a COPY
command in your Dockerfile
to put your code in the container, something like (don't know your paths):
COPY code/ opt/app/myapp