无法使用 Kubernetes 公开本地镜像的端口

Can't expose port of local image with Kubernetes

我有一个PHPLaravel项目,我有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" ]

我用 docker build -t laravel-app . 构建图像,用 docker run -d -p 8000:8000 --name backend app 构建图像 运行,在 http://localhost:8000 上我可以正确访问 api。


问题:

我正在为这个项目尝试使用 Kubernetes,我已经写了一个 laravel-deployment.yaml 文件:

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

当我尝试部署它时kubectl apply -f laravel-deployment.yaml,部署成功并创建了pod,但我无法访问http://localhost:8000 我以前做过什么:

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

您可以使用 kubectl port-forward <backend-deployment-xxxx-xxxx> 8000:8000 访问 http://localhost:8000。您还可以公开为 NodePort or LoadBalancer,其中不需要 port-forward。