Deploy PHP Applications using Kubernetes

Sameer Mehta
Akeo
Published in
4 min readJan 13, 2020

Today we will learn how to deploy the sample PHP web application using Kubernetes and automated deployment through jenkins.

PHP aka Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases.It’s a widely-used open source general purpose scripting language that is especially suited for web development and can be embedded into HTML.

Before we go ahead, do check that you have a sample PHP web application ready with you. In case you don’t, a sample PHP application is also available here: https://github.com/akeotech/devops-kubernetes/tree/master/php

Note: We assume that you have the basic knowledge of how to setup Dockers, Kubernetes and Jenkins deployment. (if not, you can refer to our blog How to setup Dockers, Kubernetes and Jenkins)

Step 1: Creating simple dockerfile

  • In order to host and orchestrate our application on Kubernetes, we have to create a Docker image to build and run the application from the project repository.
  • Create a Docker file with the below mentioned contents and place it in the root location of the project repository. The Docker file can also be downloaded from here: https://github.com/akeotech/devops-kubernetes/blob/master/php/dockerfile)
##DockerFile#Pulling base php-apache image from dockerhubFROM php:7.0-apache#Run php commands to build and serve the applicationRUN apt-get update && \    apt-get clean# The PHP application code goes in sample-php/COPY sample-php /var/www/html/

Step 2: Creating Kubernetes yaml file

apiVersion: apps/v1kind: Deploymentmetadata:  name: sample-php  labels:        app: sample-php
spec:
selector: matchLabels: app: sample-php template: metadata: labels: app: sample-php spec: containers: - image: ip_address_of_the_machine:5000/sample-php:latest name: php-sample imagePullPolicy: Always ports: - containerPort: 80 — -apiVersion: v1kind: Servicemetadata: name: sample-php-svcspec: ports: - name: “sample-php” targetPort: 80 port: 80 nodePort: 30270 protocol: TCP selector: app: sample-php type: NodePort

To help you understand better, allow us to elaborate the Kubernetes terminology from the above snippet:

  • apiVersion — which version of the Kubernetes API being used to create this object
  • kind — what kind of object is being created
  • metadata — data that helps to uniquely identify the object, including a name string, UID, and optional namespace
  • spec — what state is being desired for the object

The selector field defines how the deployment find the pods to manage, it is defined in the template (app: sample-php). The template.spec defines the docker image being used to create the pod. Replace the ip_address_of_the_machine:5000 with the private docker registry details or the global dockerhub repository. The container port defines the port on which application will run inside the pod.

The Kubernetes service section

The Kubernetes services is responsible for enabling network access to a set of pods.

  • port: defines the port on which application is running inside the pod
  • nodePort: defines the port to be bind to the base machine
  • protocol: TCP default protocol for network communication
  • selector: to identify the deployment created above

So far we have done all the configurations and our repository should like the image below:

Step 3: Jenkins Configuration

  • Creating Jenkins job
  • Open the Jenkins URL and click on “New Item”
  • Provide a name for the job and select freestyle job
  • Provide the git repository URL and credentials for cloning the project. Don’t forget to specify the branch.
  • Select “Execute Shell” from the add build step
  • Provide the steps as shown in the image below
  • Save the job and execute it
  • This will create a docker image and push to the private Docker registry. Also, the kubectl apply command will configure the pod with sample-php application running inside it. The purpose of pushing the Docker image to the Docker registry is that this image can be used on any other Ubuntu machine to run the application.
  • Access the sample application on the url http://ip_address_of_machine:30270

With this article we have tried to enable you to how to deploy the sample-php web application using Kubernetes and automated deployment through Jenkins. Hopefully, you have gained enough knowledge to do the same and will take it to the next level by launching your own app with Kubernetes.

--

--