Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CodingMonkTech/cafec3a17d2d29f595b01d5b394b0478 to your computer and use it in GitHub Desktop.
Save CodingMonkTech/cafec3a17d2d29f595b01d5b394b0478 to your computer and use it in GitHub Desktop.
Deploying laravel on kubernetes cluster - Ready to use configuration Files
FROM php:7.2-fpm
COPY app /var/www/
EXPOSE 9000
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
labels:
tier: backend
data:
config : |
server {
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code/app/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
tier: backend
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
tier: backend
template:
metadata:
labels:
app: nginx
tier: backend
spec:
volumes:
- name: dir
hostPath:
path: /code
- name: config
configMap:
name: nginx-config
items:
- key: config
path: site.conf
containers:
- name: nginx
image: nginx
volumeMounts:
- name: dir
mountPath: /code
- name: config
mountPath: /etc/nginx/conf.d
ports:
- containerPort: 80
name: http
protocol: TCP
kind: Service
apiVersion: v1
metadata:
name: nginx
labels:
tier: backend
app: nginx
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 32380
selector:
tier: backend
app: nginx
type: LoadBalancer
apiVersion: apps/v1
kind: Deployment
metadata:
name: php
labels:
tier: backend
spec:
replicas: 5
selector:
matchLabels:
app: php
tier: backend
template:
metadata:
labels:
app: php
tier: backend
spec:
volumes:
- name: dir
hostPath:
path: /code
containers:
- name: php
image: hirenkavad/laravel-k8:1.5.1
volumeMounts:
- name: dir
mountPath: /code
initContainers:
- name: install
image: busybox
volumeMounts:
- name: dir
mountPath: /code
command:
- cp
- "-r"
- "/var/www/."
- "/code/app"
apiVersion: v1
kind: Service
metadata:
name: php
labels:
tier: backend
spec:
selector:
app: php
tier: backend
ports:
- protocol: TCP
port: 9000
@jasperf
Copy link

jasperf commented May 22, 2020

Should we not use something like

FROM php:7.3-fpm

# Update packages and install composer and PHP dependencies.
RUN apt-get update && \
  DEBIAN_FRONTEND=noninteractive apt-get install -y \
    postgresql-client \
    libpq-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libmcrypt-dev \
    libpng12-dev \
    libbz2-dev \
    cron \
    && pecl channel-update pecl.php.net \
    && pecl install apcu

# PHP Extensions
RUN docker-php-ext-install mcrypt zip bz2 mbstring pdo pdo_pgsql pdo_mysql pcntl \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd

# Memory Limit
RUN echo "memory_limit=2048M" > $PHP_INI_DIR/conf.d/memory-limit.ini
RUN echo "max_execution_time=900" >> $PHP_INI_DIR/conf.d/memory-limit.ini
RUN echo "extension=apcu.so" > $PHP_INI_DIR/conf.d/apcu.ini
RUN echo "post_max_size=20M" >> $PHP_INI_DIR/conf.d/memory-limit.ini
RUN echo "upload_max_filesize=20M" >> $PHP_INI_DIR/conf.d/memory-limit.ini

# Time Zone
RUN echo "date.timezone=${PHP_TIMEZONE:-UTC}" > $PHP_INI_DIR/conf.d/date_timezone.ini

# Display errors in stderr
RUN echo "display_errors=stderr" > $PHP_INI_DIR/conf.d/display-errors.ini

# Disable PathInfo
RUN echo "cgi.fix_pathinfo=0" > $PHP_INI_DIR/conf.d/path-info.ini

# Disable expose PHP
RUN echo "expose_php=0" > $PHP_INI_DIR/conf.d/path-info.ini

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

ADD . /var/www/html
WORKDIR /var/www/html

RUN mkdir storage/logs
RUN touch storage/logs/laravel.log

RUN composer install
#RUN php artisan cache:clear
#RUN php artisan view:clear
#RUN php artisan route:cache

RUN chmod -R 777 /var/www/html/storage

RUN touch /var/log/cron.log

ADD deploy/cron/artisan-schedule-run /etc/cron.d/artisan-schedule-run
RUN chmod 0644 /etc/cron.d/artisan-schedule-run
RUN chmod +x /etc/cron.d/artisan-schedule-run
RUN touch /var/log/cron.log

CMD ["/bin/sh", "-c", "php-fpm -D | tail -f storage/logs/laravel.log"]

like used by Li0nel https://github.com/li0nel/laravel-terraform/blob/master/Dockerfile so we can add PHP FPM with needed extensions, composer, memory limit and so on . Did check https://hub.docker.com/r/hirenkavad/laravel-k8 but cannot see what it contains nor is a Github repo attached to it..

@jasperf
Copy link

jasperf commented May 22, 2020

Also

docker pull hirenkavad/laravel-k8
Using default tag: latest
Error response from daemon: manifest for hirenkavad/laravel-k8:latest not found: manifest unknown: manifest unknown

So did have to pick a version

Error response from daemon: manifest for hirenkavad/laravel-k8:latest not found: manifest unknown: manifest unknown
➜  laravel-k8 git:(master) docker pull hirenkavad/laravel-k8:1.5.1
1.5.1: Pulling from hirenkavad/laravel-k8
8ec398bc0356: Pull complete
85cf4fc86478: Pull complete
970dadf4ccb6: Extracting [=====>                                             ]  8.913MB/76.65MB
8c04561117a4: Download complete
1ddb7fee2951: Download complete
3b816b60f4bf: Download complete
6a671cae4104: Download complete
e9350f4c473a: Download complete
fc574b27512e: Download complete
ca7b19921b25: Waiting
da6a45de7db0: Waiting
64b88a35d7ad: Waiting
..

Also, creating a Dockerfile from an image is a pain ... https://stackoverflow.com/questions/19104847/how-to-generate-a-dockerfile-from-an-image

@jasperf
Copy link

jasperf commented May 28, 2020

Realized that you need to connect your Docker Hub repo to Github and build to load the README.mdand Dockerfile. I did now and have my first Laravel App image here https://hub.docker.com/r/smart48/laravel-app/dockerfile. Only tagging did not work so I also only have the latest now. But will work that out as well I am sure.

@faridmmv
Copy link

faridmmv commented Oct 3, 2023

The question is how source code appears in busybox image to copy in mounted /code directory. It doesn't have "/var/www/." directory.

@jasperf
Copy link

jasperf commented Oct 3, 2023

The question is how source code appears in busybox image to copy in mounted /code directory. It doesn't have "/var/www/." directory.

If you could share your updated code that would be great for all @faridmmv . Thanks in advance for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment