简体   繁体   English

Kubernetes - minikube 服务连接超时

[英]Kubernetes - minikube service connection timeout

I have a Docker environment with 3 containers: nginx, PHP with Laravel and a MySQL database. I have a Docker environment with 3 containers: nginx, PHP with Laravel and a MySQL database. It works fine, and I'm now trying to learn Kubernetes.它工作正常,我现在正在尝试学习 Kubernetes。

I was hoping to create a deployment and a service just for the nginx container to make it simple to start with:我希望仅为 nginx 容器创建部署和服务,以使其易于开始:

Here is the deployment.yaml:这是部署。yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
    name: toolkit-app-deployment
spec:
    replicas: 1
    selector:
        matchLabels:
            container: toolkit-server
    template:
        metadata:
            labels:
                container: toolkit-server
        spec:
            containers:
                - name: toolkit-server
                  image: my/toolkit-server:test
                  ports:
                      - containerPort: 8000
            imagePullSecrets:
                - name: my-cred

Here is the service.yaml:这是服务。yaml:

apiVersion: v1
kind: Service
metadata:
    name: backend
spec:
    selector:
        container: toolkit-server
    ports:
        - protocol: "TCP"
          port: 80
          targetPort: 8000
    type: LoadBalancer

And just incase it's needed, here is the nginx part of the docker-compose.yaml:如果需要,这里是 docker-compose.yaml 的 nginx 部分:

version: "3.8"

services:
    server:
        build:
            context: .
            dockerfile: dockerfiles/nginx.dockerfile
        ports:
            - "8000:80"
        volumes:
            - ./src:/var/www/html
            - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
        container_name: toolkit-server

The deployment is created successfully and I can see that 1/1 pods are running.部署创建成功,我可以看到 1/1 的 Pod 正在运行。

However when I run minikube service backend , the URL I get just times out.但是,当我运行minikube service backend时,我得到的 URL 只是超时。

I was expecting to see some sort of nginx page, maybe an nginx error - but with a time out I'm not sure what the next step is.我期待看到某种 nginx 页面,可能是 nginx 错误 - 但是超时我不确定下一步是什么。

I'm brand new to Kubernetes to theres a good chance I've messed the ports up or something basic.我是 Kubernetes 的新手,很有可能我把端口弄乱了或者一些基本的东西。 Any help appreciated.任何帮助表示赞赏。

Edit:编辑:

As advised by @david-maze I changed the following in the deployment.yaml:正如@david-maze 所建议的,我在deployment.yaml 中更改了以下内容:

ports:
  - containerPort: 80

And the following in service.yaml:以及以下服务中的yaml:

targetPort: 80

This gave me an nginx error page when viewed in the browser, as expected, but crucially no longer times out.正如预期的那样,当在浏览器中查看时,这给了我一个 nginx 错误页面,但至关重要的是不再超时。

This is a community wiki answer posted for better visibility.这是为更好的可见性而发布的社区 wiki 答案。 Feel free to expand it.随意扩展它。

As discussed in the comments the issue was due to wrong port configuration.正如评论中所讨论的,问题是由于端口配置错误造成的。

TargetPort is the port on which the service will send requests to, that your pod will be listening on. TargetPort是服务将向其发送请求的端口,您的 pod 将在该端口上进行侦听。 Your application in the container will need to be listening on this port also.您在容器中的应用程序也需要在此端口上进行侦听。

ContainerPort defines the port on which app can be reached out inside the container. ContainerPort定义了可以在容器内访问应用程序的端口。

So in your use case the deployment should have:因此,在您的用例中,部署应该具有:

ports:
  - containerPort: 80

and the service:和服务:

targetPort: 80

This will make the connection not to timeout anymore.这将使连接不再超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM