Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Deploy a PHP App with Kubernetes on Ubuntu 16.04

This guide shows the main steps to run a simple PHP app in Kubernetes. It needs root access, so it fits a VPS or dedicated server.

How-To Guides2 min read9 steps
Tip: Ubuntu 16.04 is old. Newer Ubuntu versions work in a similar way, but package names may differ.

Some words first

A container is a small sealed box that holds your app and everything it needs. Docker is a tool that builds containers. Kubernetes is a manager that runs many containers and restarts them if they fail. A pod is the smallest unit in Kubernetes. It holds one or more containers. A deployment tells Kubernetes how many copies of a pod to keep running.

You need a working Kubernetes cluster and the kubectl tool. A cluster is a group of servers that Kubernetes controls. Setting one up is a bigger job. If you have only one server, a small tool such as minikube can help for learning.

Steps

  1. Install Docker:
sudo apt-get update
sudo apt-get install -y docker.io

This installs Docker from the Ubuntu package list.

  1. Make a folder for your app and add a file index.php:
<?php echo "Hello from PHP on Kubernetes"; ?>
  1. In the same folder, create a file named Dockerfile:
FROM php:7.4-apache
COPY index.php /var/www/html/

This starts from a ready-made PHP and Apache image and adds your file.

  1. Build the image:
sudo docker build -t my-php-app:1.0 .
  1. Make the image available to the cluster. Push it to a registry (an online store for images) that your cluster can reach. Local clusters may use the local image directly.
  2. Create a file deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: php-app
  template:
    metadata:
      labels:
        app: php-app
    spec:
      containers:
      - name: php-app
        image: my-php-app:1.0
        ports:
        - containerPort: 80

This asks for two copies of your app.

  1. Apply it:
kubectl apply -f deployment.yaml
  1. Expose the app so people can reach it:
kubectl expose deployment php-app --type=NodePort --port=80
  1. Check the result:
kubectl get pods
kubectl get services

The pods should show Running. The service shows the port number to visit.

Quick recap

  • Put your PHP app in a Docker image.
  • Describe it in a deployment file.
  • Apply the file with kubectl.
  • Expose it with a service and check the pods.