简体   繁体   English

无法通过 IP:PORT 端口转发访问 Kubernetes pod

[英]Can't access Kubernetes pod via IP:PORT port forwarding

Background- I've deployed a docker image ( https://hub.docker.com/r/digitorus/eramba-db ) to Kubernetes. Background- I've deployed a docker image ( https://hub.docker.com/r/digitorus/eramba-db ) to Kubernetes. By pulling the image and using the below.yaml file to deploy to a separate namespace (eramba-1)通过拉取镜像并使用下面的.yaml 文件部署到单独的命名空间 (eramba-1)

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: eramba
      namespace: eramba-1
      labels:
         app: eramba               
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: eramba
      template:
        metadata:
          labels:
            app: eramba
        spec:
          containers:
          - name: eramba
            image: docker.io/digitorus/eramba:latest
            ports:
            - containerPort: 80

PROBLEM - there's a problem when accessing the application via the IP:port I've also checked the <exposed: 80> port in the sockerfile, the same port is seen in the describe pod output.问题 - 通过 IP:port 访问应用程序时出现问题我还检查了 sockerfile 中的 <exposed: 80> 端口,在描述 pod output 中看到了相同的端口。 Tried running a port forwarding rule to forward 80 to 8888. When accessing localhost:8888 via the browser, an error is seen below.尝试运行端口转发规则将 80 转发到 8888。通过浏览器访问 localhost:8888 时,出现以下错误。 I still have to config services to expose the Application.我仍然需要配置服务来公开应用程序。

root@osboxes:/home/osboxes/manifests# kubectl port-forward --namespace eramba-1 pod/eramba-7455b5bb8-fnw7v 8888:80
Forwarding from 127.0.0.1:8888 -> 80
Forwarding from [::1]:8888 -> 80
Handling connection for 8888
E0106 22:31:17.651396   32811 portforward.go:406] an error occurred forwarding 8888 -> 80: error forwarding port 80 to pod 659bd926eb494d74aec7bd4b86b3d1f293e42aececff758f7a565d708078d0d6, uid : exit status 1: 2022/01/06 22:31:17 socat[32837] E connect(5, AF=2 127.0.0.1:80, 16): Connection refused
E0106 22:31:17.652649   32811 portforward.go:234] lost connection to pod

I also noticed the pod is crashing a lot (CrashLoopBackOff).我还注意到 pod 经常崩溃(CrashLoopBackOff)。 Here's the describe pod output这是描述吊舱 output

root@osboxes:/home/osboxes/manifests# kubectl describe pods eramba -n eramba-1
Name:         eramba-7455b5bb8-fnw7v
Namespace:    eramba-1
Priority:     0
Node:         osboxes/172.16.42.135
Start Time:   Thu, 06 Jan 2022 21:35:22 -0500
Labels:       app=eramba
              pod-template-hash=7455b5bb8
Annotations:  <none>
Status:       Running
IP:           10.20.0.55
IPs:
  IP:           10.20.0.55
Controlled By:  ReplicaSet/eramba-7455b5bb8
Containers:
  eramba:
    Container ID:   docker://ea6b299219ff4477e7a6992d36e9d7ca8a4fa4eb1aec83b31db93e4599f1f91d
    Image:          docker.io/digitorus/eramba:latest
    Image ID:       docker-pullable://digitorus/eramba@sha256:5f27f83fb46e4760f51bed9c8b8f8abecf1c11402595ca87ecee78ccc017a532
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Thu, 06 Jan 2022 22:29:19 -0500
      Finished:     Thu, 06 Jan 2022 22:32:34 -0500
    Ready:          False
    Restart Count:  13
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-x5fnh (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  kube-api-access-x5fnh:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason            Age                  From               Message
  ----     ------            ----                 ----               -------
  Warning  FailedScheduling  59m (x31 over 90m)   default-scheduler  0/1 nodes are available: 1 node(s) had taint {node.kubernetes.io/not-ready: }, that the pod didn't tolerate.
  Normal   Scheduled         58m                  default-scheduler  Successfully assigned eramba-1/eramba-7455b5bb8-fnw7v to osboxes
  Normal   Pulled            58m                  kubelet            Successfully pulled image "docker.io/digitorus/eramba:latest" in 709.654139ms
  Normal   Pulled            55m                  kubelet            Successfully pulled image "docker.io/digitorus/eramba:latest" in 706.000948ms
  Normal   Pulled            52m                  kubelet            Successfully pulled image "docker.io/digitorus/eramba:latest" in 689.074695ms
  Normal   Pulled            49m                  kubelet            Successfully pulled image "docker.io/digitorus/eramba:latest" in 677.201277ms
  Normal   Started           49m (x4 over 58m)    kubelet            Started container eramba
  Normal   Killing           45m (x4 over 55m)    kubelet            Stopping container eramba
  Normal   Pulling           45m (x5 over 58m)    kubelet            Pulling image "docker.io/digitorus/eramba:latest"
  Normal   Pulled            45m                  kubelet            Successfully pulled image "docker.io/digitorus/eramba:latest" in 762.977828ms
  Normal   Created           45m (x5 over 58m)    kubelet            Created container eramba
  Warning  BackOff           18m (x48 over 32m)   kubelet            Back-off restarting failed container
  Normal   SandboxChanged    105s (x17 over 55m)  kubelet            Pod sandbox changed, it will be killed and re-created.

kubectl version output kubectl 版本 output

root@osboxes:/home/osboxes/manifests# kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.1", GitCommit:"86ec240af8cbd1b60bcc4c03c20da9b98005b92e", GitTreeState:"clean", BuildDate:"2021-12-16T11:41:01Z", GoVersion:"go1.17.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.1", GitCommit:"86ec240af8cbd1b60bcc4c03c20da9b98005b92e", GitTreeState:"clean", BuildDate:"2021-12-16T11:34:54Z", GoVersion:"go1.17.5", Compiler:"gc", Platform:"linux/amd64"}

The logs output日志 output

root@osboxes:/home/osboxes/manifests# kubectl logs eramba-7455b5bb8-hw2k7 -n eramba-1
[i] pre-exec.d - processing /scripts/pre-exec.d/010-apache.sh
tail: can't open '/var/log/apache2/*log': No such file or directory
[i] pre-exec.d - processing /scripts/pre-exec.d/020-eramba-initdb.sh
[i] Waiting for database to setup...
[i] Trying to connect to database: try 1...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 2...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 3...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 4...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 5...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 6...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 7...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 8...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 9...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 10...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 11...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 12...
ERROR 2005 (HY000): Unknown MySQL server host 'db' (-3)
[i] Trying to connect to database: try 13...

As noted in the DockerHub page for your image, you need to provide database environmental variables to connect to a seperate MySQL db instance.如镜像的 DockerHub 页面中所述,您需要提供数据库环境变量以连接到单独的 MySQL 数据库实例。 It's a bit more complicated in Kubernetes, but you can use something like this instead:在 Kubernetes 中它有点复杂,但你可以使用类似这样的东西:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: eramba
  namespace: eramba-1
  labels:
   app: eramba               
spec:
  replicas: 1
  selector:
    matchLabels:
      app: eramba
  template:
    metadata:
      labels:
        app: eramba
    spec:
      containers:
      - name: eramba
        image: docker.io/digitorus/eramba:latest
        ports:
        - containerPort: 80
        env:
        - name: DB_ENV_MYSQL_DATABASE
          value: "eramba-db"
        - name: DB_ENV_MYSQL_USER
          value: "eramba"
        - name: DB_ENV_MYSQL_PASSWORD
          value: "password"
        - name: DB_ENV_MYSQL_ROOT_PASSWORD
          value: "password"
        - name: ERAMBA_HOSTNAME
          value: localhost
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: eramba-db
  namespace: eramba-1
  labels:
   app: eramba-db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: eramba-db
  template:
    metadata:
      labels:
        app: eramba-db
    spec:
      containers:
      - name: eramba-db
        image: docker.io/digitorus/eramba-db:latest
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_DATABASE
          value: "eramba-db"
        - name: MYSQL_USER
          value: "eramba"
        - name: MYSQL_PASSWORD
          value: "password"
        - name: MYSQL_ROOT_PASSWORD
          value: "password"
---
apiVersion: v1
kind: Service
metadata:
  name: db
  namespace: eramba-1
spec:
  selector:
    app: eramba-db
  ports:
    - name: sql
      port: 3306
      targetPort: 3306
---
apiVersion: v1
kind: Service
metadata:
  name: eramba-np
  namespace: eramba-1
spec:
  type: NodePort
  selector:
    app: eramba
  ports:
    - name: http
      port: 80
      targetPort: 80
      nodePort: 30045

This creates the app and database with the required environmental variables according to the configuration files in the repositories.这将根据存储库中的配置文件创建具有所需环境变量的应用程序和数据库。 It then creates a ClusterIP service "db" in the same namespace (so the DNS name from the app to hit that is "db") which connects to port 3306 on the database.然后它在同一命名空间中创建一个 ClusterIP 服务“db”(因此应用程序中的 DNS 名称是“db”),它连接到数据库上的端口 3306。

The app will take a minute to start while it waits on the database to initialise.该应用程序在等待数据库初始化时将需要一分钟时间才能启动。 I've added a NodePort service on port 30045. You should be able to reach the UI on http://localhost:30045 and login with admin/admin.我在端口 30045 上添加了一个 NodePort 服务。您应该能够访问http://localhost:30045上的 UI 并使用 admin/admin 登录。

I would recommend learning a bit more about how containers run and communicate with eachother using Docker and Docker Compose, as Kubernetes is difficult to jump right into without that prior knowledge.我建议使用 Docker 和 Docker Compose 了解更多关于容器如何运行和相互通信的信息,因为如果没有这些先验知识,很难直接了解 Kubernetes。 For example here, the database and the app are run in seperate containers (eramba, eramba-db) and use a service to connect with each other.例如这里,数据库和应用程序在单独的容器(eramba、eramba-db)中运行,并使用服务相互连接。 You were running just the app with no database and no configuration.您只运行没有数据库且没有配置的应用程序。

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

相关问题 我无法通过端口转发从公共 IP 访问 Docker 容器。 我可以检查请求停止的位置吗? - I can't access Docker container from public IP via port forwarding. Can i check where the request stops? Kubernetes:端口转发后,我可以通过“localhost”但不能通过“主机名”访问服务 - Kubernetes: After port-forwarding, I can access a service via "localhost" but not via "hostname" 我无法访问端口转发容器 - I can't access port forwarding containers 如何从Kubernetes中的群集外部访问Pod? 我将其作为节点端口服务公开,但是无法通过该节点端口访问它 - How to access pod from outside of my cluster in kubernetes? I exposed it as node port service but can't access it with that node port 无法访问 kubernetes + rancher 中的 KubeAPI 端口 - Can't access KubeAPI port in kubernetes + rancher 无法访问 kubernetes minikube 上的 IP 和 PORT - Cannot Access IP and PORT on kubernetes minikube 通过Docker在本地Kubernetes:为什么我们需要端口转发? - Kubernetes locally via Docker: why do we need port forwarding? Kubernetes 部署动态端口转发 - Kubernetes Deployment dynamic port forwarding 双端口转发 kubernetes + docker - Double port forwarding kubernetes + docker 无法在本地 Kubernetes 集群上公开端口(通过 Docker 桌面) - Can't expose port on local Kubernetes cluster (via Docker Desktop)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM