Deploying laravel application to Digital ocean kubernetes — Part 2

Hiren Kavad
Coding Monk
Published in
4 min readApr 26, 2020

As we know kubernetes makes scaling super easy, but deploying your application on kubernetes is easy ? Well we will make it super easy.

In Previous tutorial we have already learned how you can build docker image of your application. Following is my project structure.

We will deploy our application with php-7.2 and nginx. As you can see we are choosing php-7.2 as base image.

Dockerfile

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

app directory contains our laravel app code. On line second we are copying app(Laravel application) to /var/www/ into our dokcer image and we are exposing php on 9000 Port.

Above step builds image which our application and push it to docker hub repository.

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

nginx_configMap.yaml

When we configure nginx for domain, we have to write .conf file for site. Above file is for storing configuration file.

Type of this configurations is ConfigMap which uses api version 1. name of the configuration is nginx-config and we are also giving a labels, as it is server configuration we are assigning tier:backend.

Another key is data, put nginx configuration in config object. And you are done. specify your code path and php service port, our php service will be running on 9000 Port.

nginx_deployment.yaml

Deployment file specifies all the things related to application we will be using, we want to configure nginx, so we have to specify all things related to nginx. abobe deployment will create 2 replicas of nginx pod. it will also mount volume to /code in node(host server.) we will also have to mount our config file to appropriate location, finally we will expose this deployment on 80 port. This is backend worker so we are assigning label to — tier:backend

nginx_service.yaml

To access or expose nginx server to outside world we will need to create service, which will be exposed to us.

We are specifying 80 port because we want to access our application on that port, we want to target nginx deployment which is running on 80 port, finally type of this service is LoadBalancer, because load balance our application automatically.

php_deployment.yaml

We want 4 replicas of php service, this is also backend service so we have specified label accordingly. we will be using our own docker image which contains our application — hirenkavad/laravel-k8:1.5.1, Lets mount volume, Note this is Normal volume mount so, it will not persist on two nodes, create persistant volume claim to access multiple node wide.

php_service.yaml

Now we want to expose our php deployment to outside so our nginx server can use php.

We will be exposing our service on 9000 Port.

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

kubectl apply -f nginx_configMap.yaml
kubectl apply -f nginx_deployment.yaml
kubectl apply -f nginx_service.yaml
kubectl apply -f php_deployment.yaml
kubectl apply -f php_service.yaml

That’s it, all things are configured.

if you are on minikube run following command to get Local IP on which you will be able to access application.

minikube service nginx

Above command will provide, URL to your application.

One last very important thing. When you open your application you will get 404 Not found.

It’s because our php image have application code but nginx image doesn’t. So we will need to copy application code to volume which both deployment are sharing. This is not a best practice as we need to put our code in image at a time of building docker image, but as we are using official nginx image without modification, we have to do it manually.

So login into ssh of your PHP pod,

kubectl get po

Above command will list all php pods, select one of the pod id

kubectl exec -it php_pod_id bash

Using above command you will logged into php pod shell.

Now copy code from image to volume directory, on which nginx config file is pointing root of Laravel Application.

cp -R /var/www/. /code/app

And open nginx service URL. Your laravel application is live on kubernetes

If you want to see all above things in action you can watch this youtube tutorial on our youtube channel

Thank you for reading. :)

--

--