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
- Install Docker:
sudo apt-get update
sudo apt-get install -y docker.io
This installs Docker from the Ubuntu package list.
- Make a folder for your app and add a file
index.php:
<?php echo "Hello from PHP on Kubernetes"; ?>
- 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.
- Build the image:
sudo docker build -t my-php-app:1.0 .
- 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.
- 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.
- Apply it:
kubectl apply -f deployment.yaml
- Expose the app so people can reach it:
kubectl expose deployment php-app --type=NodePort --port=80
- 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.