Kubernetes deployment of a php test application

10/16/2019

I'm learning Kubernetes at the moment. I learned first docker and made my own Dockerfiles and built my own images. It's a basic PHP application, which tries to connect to a MariaDB database via PDO and which invokes the phpinfo() function. So via docker-compose, it works fine. The next step for me is to run it in a Kubernetes cluster. I tried it in different ways and it doesn't work. I can't reach the index.php on my browser :(

PHP-Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-app-deployment
  labels:
    app: php-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: php-app
  template:
    metadata:
      labels:
        app: php-app
    spec:
      containers:
      - name: php-app
        image: amannti/my_php_image:1.2
        ports:
        - containerPort: 80

PHP-Service:

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

DB-Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: db-deployment
  labels:
    app: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
      - name: db
        image: amannti/carpool_maria_db:1.1
        ports:
        - containerPort: 3306

DB-Service:

kind: Service 
apiVersion: v1 
metadata: 
  name: db-service 
spec: 
  selector: 
    app: db 
  ports: 
  - protocol: TCP 
    port: 3306 
    targetPort: 3306

I deployed all files on my minikube cluster with kubectl apply -f fileName.

The php application only contains this code:

<?php
$servername = "oldcarpoolsystem_db_1";
$username = "root";
$password = "root";
$dbName = "carpoolSystem";

try {
  $conn = new PDO("mysql:host=$servername;dbname=" . $dbName, 
      $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "PDO: Connected successfully<br>";
 }
 catch(PDOException $e) {
   echo "PDO: Connection failed: " . $e->getMessage() . "<br>";
 }

 phpinfo();

The database only contains few tables and is named carpoolSystem.

I tried to connect via http://127.0.0.1:31000/ to my website. But it says "connection refused" :( On Kubernetes dashboard all services run, but on deployments, pods and replica set the DB part don't run. In pods it says "Waiting: CrashLoopError".

What are my mistakes, what can I learn by this fail?

The whole application runs perfectly with this docker-compose file:

version: '3'

services:
  db:
    image: amannti/carpool_maria_db:1.1
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306" #Left Container | Right Output
  web:
    image: amannti/my_php_image:1.2
    container_name: php_web
    depends_on:
      - db
    ports:
      - "80:80"

UPDATE

In the minikube dashboard all deployments, pods and the rest are green... But I have still no access to my application because of connection is refused :/ I tried to access via : (http://127.0.0.1:31000/), but still the same response. Any ideas how to troubleshoot it?

PHP App Service


UPDATE

Dockerfile DB:

FROM mariadb/server:latest

COPY dump.sql /docker-entrypoint-initdb.d/

Dockerfile PHP:

# This Dockerfile uses the first version of my php image
FROM amannti/my_php_image:1.0

# Copy app's source code to the /src directory
COPY ./src /var/www/html

# The source directory will be the working directory
WORKDIR /
-- Amannti
docker
docker-compose
kubernetes
mariadb
php

2 Answers

10/16/2019

If you do a kubectl describe pod <YOUR DB POD> you'll see a bit more informations on why the pod is crashing.

If that's not enough, try kubectl logs <YOUR DB POD>, you'll have logs with all errors and warning.

-- Corentin Peuvrel
Source: StackOverflow

10/17/2019

You should check on what IP is your minikube configured and use it instead of localhost.

This can be checked in dashboard following Cluster > Nodes > minikube path, or using this command minikube ip and this should be used to check if service is working.

I also strongly advice to check Set up Ingress on Minikube with the NGINX Ingress Controller.

-- Crou
Source: StackOverflow