Deploying laravel application to Digital ocean kubernetes — Part 3

Deploying laravel application on Cloud Kubernetes.

We will be using digital ocean cloud managed kubernetes, so if you don’t have account on Digital ocean, you can use this link https://m.do.co/c/37201cd07b6d to create account in Digital Ocean get $100 credit.

We will be using all the configuration files which we have used in previous tutorial. So i would suggest you to look into once.

Now let’s create kubernetes cluster on Digital Ocean. Select Clusters from create menu.

Select Kubernetes version and Region

Select your cluster configuration, Number of nodes and it’s configurations.

Give a name to your cluster.

Digital ocean will start creating Kubernetes cluster and it will take few minutes.

Till then download kubernetes config file.

Once cluster gets ready, you can perform all command which you have been performing on local kubernetes.

Now let’s apply configuration file one by one. We are assuming you already have your application inside docker image with php inside.

Dockerfile

FROM php:7.2-fpm
COPY app /var/www/
EXPOSE 9000

Kubernetes configuration files uses yaml format. So we will have to define all our configuration files into .yaml format.

nginx_configMap.yaml

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;
}
}

nginx_deployment.yaml

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

nginx_service.yaml

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

php_deployment.yaml

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

php_service.yaml

apiVersion: v1
kind: Service
metadata:
name: php
labels:
tier: backend
spec:
selector:
app: php
tier: backend
ports:
- protocol: TCP
port: 9000

We will be exposing our service on 9000 Port.

Now with following command you can apply all this configurations to kubernetes cluster.

kubectl -kubeconfig="laravel-k8-config.yaml" apply -f nginx_configMap.yamlkubectl -kubeconfig="laravel-k8-config.yaml" apply -f php_deployment.yamlkubectl -kubeconfig="laravel-k8-config.yaml" apply -f php_service.yamlkubectl -kubeconfig="laravel-k8-config.yaml" apply -f nginx_deployment.yamlkubectl -kubeconfig="laravel-k8-config.yaml" apply -f nginx_service.yaml

You will see pods and deployments will be started.

When you deploy nginx service of LoadBalancer type, it will create Load Balancer in Digital ocean by default.

Once load balancer is created you can go to LoadBalancer IP and access your application. But we need to do one more thing. As our nginx image doesn’t have application code, so we will need to copy code from php pod to shared volume, so our nginx server can access our application.

We will also need to change permissions of some directory, as some directory of laravel needs certain permissions, eg. storage and bootstrap.

Live application

And your application is live, on kubernetes cluster. You can scale up and down easily by changing number of replicas from config file or from kubernetes dashboard. You can also increase number of nodes very easily from digital ocean cluster configuration.

Accessing kubernetes dashboard from digital ocean
Scaling from Kubernetes dashboard

So this much it’s easy to deploy laravel application to kubernetes cluster. I hope you find it helpful.

If you want to follow along with video, here is youtube tutorial.

Thank you for reading. :)

We are here for quick tutorials, it may be server administration, server setup, dev ops, simple programming tips, simple hacks for server and many other things.