<p>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 :(</p>

<p><strong>PHP-Deployment:</strong></p>

<pre><code>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
</code></pre>

<p><strong>PHP-Service</strong>:</p>

<pre><code>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 
</code></pre>

<p><strong>DB-Deployment:</strong></p>

<pre><code>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
</code></pre>

<p><strong>DB-Service:</strong></p>

<pre><code>kind: Service 
apiVersion: v1 
metadata: 
  name: db-service 
spec: 
  selector: 
    app: db 
  ports: 
  - protocol: TCP 
    port: 3306 
    targetPort: 3306
</code></pre>

<p>I deployed all files on my minikube cluster with <em>kubectl apply -f fileName</em>.</p>

<p>The php application only contains this code:</p>

<pre><code>&lt;?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-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "PDO: Connected successfully&lt;br&gt;";
 }
 catch(PDOException $e) {
   echo "PDO: Connection failed: " . $e-&gt;getMessage() . "&lt;br&gt;";
 }

 phpinfo();
</code></pre>

<p>The database only contains few tables and is named carpoolSystem.</p>

<p>I tried to connect via <strong><a href="http://127.0.0.1:31000/" rel="nofollow noreferrer">http://127.0.0.1:31000/</a></strong> 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".</p>

<p>What are my mistakes, what can I learn by this fail?</p>

<p>The whole application runs perfectly with this docker-compose file:</p>

<pre><code>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"
</code></pre>

<hr>

<h2>UPDATE</h2>

<p>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 : (<a href="http://127.0.0.1:31000/" rel="nofollow noreferrer">http://127.0.0.1:31000/</a>), but still the same response. Any ideas how to troubleshoot it?</p>

<p><a href="https://i.stack.imgur.com/Acu9B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Acu9B.png" alt="PHP App Service"></a></p>

<hr>

<h2>UPDATE</h2>

<p><strong>Dockerfile DB:</strong></p>

<pre><code>FROM mariadb/server:latest

COPY dump.sql /docker-entrypoint-initdb.d/
</code></pre>

<p><strong>Dockerfile PHP:</strong></p>

<pre><code># 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 /
</code></pre>

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

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

<p>I also strongly advice to check <a href="https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/" rel="nofollow noreferrer">Set up Ingress on Minikube with the NGINX Ingress Controller</a>.</p>
