Testing again and now see the following inside minikube post minikube ssh:
$ cd /tmp/hostpath-provisioner/smt-local/code-pv-claim/
$ ls -la
total 12
drwxrwxrwx 2 root root 4096 Dec 7 06:57 .
drwxr-xr-x 5 root root 4096 Dec 7 05:02 ..
-rw-r--r-- 1 root root 1823 Dec 7 07:07 index.php
$ date
Sun Dec 13 03:30:03 UTC 2020
so that index was only added once and I can check in in the Laravel container:
kubectl exec -it app-74b5bdd58d-42rt6 -c laravel -- /bin/bash
root@app-74b5bdd58d-42rt6:/code# ls -la
total 12
drwxrwxrwx 2 root root 4096 Dec 7 06:57 .
drwxr-xr-x 1 root root 4096 Dec 13 03:19 ..
-rw-r--r-- 1 root root 1823 Dec 7 07:07 index.php
root@app-74b5bdd58d-42rt6:/code
# head index.php
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylor@laravel.com>
*/
define('LARAVEL_START', microtime(true));
and though the initContainer does seem to run and terminate successfully it does not seem to add the repository code at all as of yet.
Found a better version of Git Sync Sidecart used once of in initContainer that sort of works:
# Laravel Code Download so we can run Horizon without issues
# and have a codebase to start with For private repo better to add
# code to laravel image https://codepre.com/how-to-perform-git-clone-in-kubernetes-pod-deployment.html
# https://github.com/kubernetes/git-sync/issues/127
# https://github.com/kubernetes/git-sync/issues/286
initContainers:
- image: k8s.gcr.io/git-sync:v3.0.1
name: git-data
# command: [ "sh", "-c", "ls /code/project" ]
volumeMounts:
- name: code-storage
mountPath: /smt-local
env:
- name: GIT_SYNC_REPO
value: https://github.com/smart48/smt-demo.git
# - name: GIT_SYNC_ROOT
# value: /code
- name: GIT_SYNC_BRANCH
value: master
- name: GIT_SYNC_DEPTH
value: "1"
- name: GIT_SYNC_DEST
value: code
- name: GIT_SYNC_ONE_TIME
value: "1"
# - name: GIT_SYNC_USERNAME
# valueFrom:
# secretKeyRef:
# name: git-creds
# key: username
# - name: GIT_SYNC_PASSWORD
# valueFrom:
# secretKeyRef:
# name: git-creds
# key: password
works as in it does at all to symlink code leading to rev-debce33c0a2d34670262dd6ca241b844fd3dbc66/ where the code is stored:
tmp/hostpath-provisioner/smt-local/code-pv-claim
$ ls -la
total 16
drwxrwxrwx 4 root root 4096 Dec 13 07:25 .
drwxr-xr-x 5 root root 4096 Dec 7 05:02 ..
drwxr-xr-x 9 nobody nogroup 4096 Dec 13 07:25 .git
lrwxrwxrwx 1 nobody nogroup 44 Dec 13 07:25 code -> rev-debce33c0a2d34670262dd6ca241b844fd3dbc66
drwxr-xr-x 13 nobody nogroup 4096 Dec 13 07:25 rev-debce33c0a2d34670262dd6ca241b844fd3dbc66
Only no group and user added and… I would prefer a clone with out .git directory and perhaps a different name than rev-.. But perhaps this is a deployment solution like PHP Deployer I could use as well. And then with GIT_SYNC_ONE_TIME false
Will also run only once. That seems to be because after first run there is a conflict that is not shown in the logs because file(s) already exist
Because init containers can be restarted, retried, or re-executed, init container code should be idempotent. In particular, code that writes to files on EmptyDirs should be prepared for the possibility that an output file already exists.
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
namespace: smt-local
spec:
replicas: 2
selector:
matchLabels:
app: web
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
minReadySeconds: 5
template:
metadata:
labels:
app: web
spec:
containers:
- name: laravel
image: smart48/smt-laravel:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9000
resources:
requests:
cpu: 250m
limits:
cpu: 500m
volumeMounts:
- name: code-storage
mountPath: /code
- name: nginx
image: smart48/smt-nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9376
volumeMounts:
- name: code-storage
mountPath: /code
- name: nginx-config
mountPath: /etc/nginx/conf.d
# - name: laravel-horizon
# image: smart48/smt-horizon:latest
# # not sure if we need the port and volume here
# imagePullPolicy: IfNotPresent
# ports:
# - containerPort: 9377
# volumeMounts:
# - name: code-storage
# mountPath: /code
# command: ["/usr/local/bin/php", "artisan", "horizon"]
# lifecycle:
# preStop:
# exec:
# command: ["/usr/local/bin/php", "artisan", "horizon:terminate"]
# Laravel Code Download so we can run Horizon without issues
# and have a codebase to start with For private repo better to add
# code to laravel image https://codepre.com/how-to-perform-git-clone-in-kubernetes-pod-deployment.html
initContainers:
- name: git-cloner
image: alpine/git
args:
- clone
- --single-branch
- --
- https://github.com/smart48/smt-demo
- /data
volumeMounts:
- mountPath: /data
name: code-storage
volumes:
- name: code-storage
persistentVolumeClaim:
claimName: code-pv-claim
- name: nginx-config
persistentVolumeClaim:
claimName: nginx-pv-claim
Did get all the files this time
This worked, but for one of the pods replicas: 2 the init part crashed. Probably because the first pod beat the second one to it:
kubectl get all
NAME READY STATUS RESTARTS AGE
pod/app-7fd554668-hr4cj 2/2 Running 0 2m35s
pod/app-7fd554668-xxxfp 0/2 Init:CrashLoopBackOff 2 2m35s
pod/mysql-686f78b8dd-9f7nx 1/1 Running 0 2m42s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/app 1/2 2 1 2m35s
deployment.apps/mysql 1/1 1 1 2m42s
NAME DESIRED CURRENT READY AGE
replicaset.apps/app-7fd554668 2 2 1 2m35s
replicaset.apps/mysql-686f78b8dd 1 1 1 2m42s
➜ smt-deploy git:(main) ✗ kubectl get pods
NAME READY STATUS RESTARTS AGE
app-7fd554668-hr4cj 2/2 Running 0 3m
app-7fd554668-xxxfp 0/2 Init:CrashLoopBackOff 3 3m
mysql-686f78b8dd-9f7nx 1/1 Running 0 3m7s
Decided to not set replicas and perhaps later on fire a autoscale command instead.
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
namespace: smt-local
spec:
# autoscale using `kubectl autoscale deployment x --cpu-percent=50 --min=1 --max=10` instead of setting replicas
# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
# replicas: 2
selector:
matchLabels:
app: web
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
minReadySeconds: 5
template:
metadata:
labels:
app: web
spec:
containers:
- name: laravel
image: smart48/smt-laravel:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9000
resources:
requests:
cpu: 250m
limits:
cpu: 500m
volumeMounts:
- name: code-storage
mountPath: /code
- name: nginx
image: smart48/smt-nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9376
volumeMounts:
- name: code-storage
mountPath: /code
- name: nginx-config
mountPath: /etc/nginx/conf.d
# - name: laravel-horizon
# image: smart48/smt-horizon:latest
# # not sure if we need the port and volume here
# imagePullPolicy: IfNotPresent
# ports:
# - containerPort: 9377
# volumeMounts:
# - name: code-storage
# mountPath: /code
# command: ["/usr/local/bin/php", "artisan", "horizon"]
# lifecycle:
# preStop:
# exec:
# command: ["/usr/local/bin/php", "artisan", "horizon:terminate"]
# Laravel Code Download so we can run Horizon without issues
# and have a codebase to start with For private repo better to add
# code to laravel image https://codepre.com/how-to-perform-git-clone-in-kubernetes-pod-deployment.html
initContainers:
- name: git-cloner
image: alpine/git
args:
- clone
- --single-branch
- --
- https://github.com/smart48/smt-demo
- /data
volumeMounts:
- mountPath: /data
name: code-storage
volumes:
- name: code-storage
persistentVolumeClaim:
claimName: code-pv-claim
- name: nginx-config
persistentVolumeClaim:
claimName: nginx-pv-claim
And at least for local testing this works now we have Laravel container, Nginx Container and Git Cloner working now though the initContainer only works once now.