Can't expose port of local image with Kubernetes

2/18/2022

I have a PHP Laravel project, I have Dockerfile:

FROM php:7.4-fpm

COPY . /myapp
WORKDIR /myapp

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

RUN curl -sS https://getcomposer.org/installer | php
RUN php composer.phar update

EXPOSE 8000
CMD [ "php", "artisan", "serve", "--host", "0.0.0.0" ]

I build the image with docker build -t laravel-app . and run it with docker run -d -p 8000:8000 --name backend app, on http://localhost:8000 I can access the api correctly.


The issue:

I am trying to use Kubernetes for this project, I've written a laravel-deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend-laravel
  template:
    metadata:
      labels:
        app: backend-laravel
    spec:
      containers:
      - name: laravel-app
        image: laravel-app
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8000

When I try to deploy it kubectl apply -f laravel-deployment.yaml, the deployment is successful and the pod is created, but I can't access with http://localhost:8000 What I previously did:

  • I've set docker to point to minikube with eval $(minikube docker-env)
  • Create the service kubectl expose -f laravel-deployment.yaml --port=8000 --target-port=8000
-- Hamza Ince
docker
kubernetes
minikube

1 Answer

2/18/2022

...can't access with http://localhost:8000 What I previously did

You can access http://localhost:8000 with kubectl port-forward <backend-deployment-xxxx-xxxx> 8000:8000. You can also expose as NodePort or LoadBalancer where port-forward will not be required.

-- gohm&#39;c
Source: StackOverflow