How to Deploy Php, Redis Application to Kubernetes using minikube

Docker

Docker is a computer program that performs operating-system-level virtualization. Docker is used to run software packages called containers. To run a docker container you will create a docker image, embed your code then run the image using docker.

Kubernetes

Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications. It groups containers that make up an application into logical units for easy management, deployment and discovery.

In this tutorial we will run a sample php-redis application on kubernetes locally using minikube and in next tutorial we will deploy the same application on Google Cloud using GKE. We will run the application using docker containers. Kubernetes support many other containerization technologies, here we will use Docker as the runtime.

Source code for this project can be found on Github

Steps

First of all lets create directory for our project in any of your favorite location

mkdir kubernetes-php
cd kubernetes-php
# also create a src directory for our source code inside the directory
mkdir src                        

now create a php file src/index.php and add sample php code


Now create a Docker file in your project root named Docker-php

#Docker-php
FROM php:7.3-apache

#This is for future use when we connect with redis server
RUN pecl install redis && docker-php-ext-enable redis

COPY ./src /var/www/html/

Now create a deployment and a service for your Application. create php-apache.yaml file and add the following content

#php-apache.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache
  labels:
    app: php-apache
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-apache
  template:
    metadata:
      labels:
        app: php-apache
    spec:
      containers:
        - name: php-apache
          image: php-apache-gke:latest
          imagePullPolicy: Never
          ports:
            - name: http
              containerPort: 80
          env:
            - name: REDIS_HOST
              valueFrom:
                configMapKeyRef:
                  name: redishost
                  key: REDIS_HOST
            - name: REDIS_PORT
              valueFrom:
                configMapKeyRef:
                  name: redishost
                  key: REDIS_PORT

---
apiVersion: v1
kind: Service
metadata:
  name: php-apache
spec:
  type: NodePort
  selector:
    app: php-apache
  ports:
    - port: 80
      targetPort: http
      nodePort: 30510
      protocol: TCP

Now we have our source code, Dockerfile and kubernetes Deployment ready let's start minikube cluster locally and deploy the application

#start minikube
minikube start

#Configure your docker client to use minikube docker daemon
eval $(minikube docker-env)

#Build your docker Image
docker build -t php-apache-gke -f Dockerfile-php .

#make sure your newly build docker image is available
docker images

#Deploy your deployment and service to minikube cluster using kubectl
kubectl apply -f php-apache.yaml

#to make sure your pod is running
kubectl get pods

#to make sure your service is running
kubectl get services

now if everything is running smoothly you will be able to access your deployed application using

curl $(minikube service php-apache --url)

or you can get the url to your application using following command and open the url in your browser

minikube service php-apache --url

Our Php application is running on the cluster now lets deploy a redis client and connect to it from our php application.

Create a php file src/redis.php and add the following content



Create Docker file for redis Dockerfile-redis and add following


FROM redis:latest
VOLUME /data
EXPOSE 6379
CMD ["redis-server"]

Create a redis Deployment and server redis.yaml


apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          # this is for minikube only, for gke you have to push the docker image to any container registry
          # then use that image like
          # image: docker.io/username/imagename
          image: redis-local:latest
          # only for minikube, for gke remove this line or set any desired policy
          imagePullPolicy: Never
          ports:
             - containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  ports:
    - port: 6379
      targetPort: 6379
  selector:
    app: redis
                        
Create a configMap to store redis server information

kubectl create configmap redishost --from-literal=REDIS_HOST=redis --from-literal=REDIS_PORT=6379

Our php-apache.yaml Deployment already use the above configmap to set env variables

Create your docker images using

docker build -t redis-local -f Dockerfile-redis .

docker build -t php-apache-gke -f Dockerfile-php .

Deploy your Deployment and Services using

kubectl apply -f redis.yaml

kubectl apply -f php-apache.yaml

Make sure they are running using kubectl get pods and kubectl get services

Find your service url using

minikube service php-apache --url

Check your application using curl

curl $(minikube service php-apache --url)/redis.php

or open it in your browser at

SERVICE_URL/redis.php

In this tutorial we deployed a php redis application in minikube in next tutorial we will deploy it into Google Cloud GKE and will use Cloud Memorystore instead of redis container.

Hope You enjoyed this, If you have any question don't hesitate to reach out on twitter

You can follow me on twitter @ilyash00 where I share some cool stuff about programming and occasionally create some zines

Copyright ©