Minikube部署多容器应用遇端口占用问题求助
问题描述
在Minikube上部署多容器应用时,mariadb部署可正常启动,但另外两个部署出现报错:
0/1 nodes are available: 1 node(s) didn't have free ports for the requested pod ports. preemption: 0/1 nodes are available
该应用在Docker Compose中可正常运行,使用Kompose工具生成Kubernetes配置文件。(注:个人测试项目,数据库密码仅用于测试)
附上相关配置文件:
UI Service配置
apiVersion: v1 kind: Service metadata: annotations: kompose.version: 1.31.2 (a92241f79) creationTimestamp: null labels: io.kompose.service: ui name: ui spec: ports: - name: "8888" port: 8888 targetPort: 80 selector: io.kompose.service: ui status: loadBalancer: {}
UI Deployment配置
apiVersion: apps/v1 kind: Deployment metadata: annotations: kompose.version: 1.31.2 (a92241f79) creationTimestamp: null labels: io.kompose.service: ui name: ui spec: hostNetwork: false replicas: 1 selector: matchLabels: io.kompose.service: ui strategy: {} template: metadata: annotations: kompose.version: 1.31.2 (a92241f79) creationTimestamp: null labels: io.kompose.network/docker-compose-rgs-frontend: "true" io.kompose.service: ui spec: containers: - image: ui name: ui ports: - containerPort: 80 hostPort: 8888 protocol: TCP resources: {} restartPolicy: Always status: {}
MariaDB Service配置
apiVersion: v1 kind: Service metadata: annotations: kompose.version: 1.31.2 (a92241f79) creationTimestamp: null labels: io.kompose.service: mariadb name: mariadb spec: ports: - name: "3307" port: 3307 targetPort: 3306 selector: io.kompose.service: mariadb status: loadBalancer: {}
MariaDB Deployment配置
apiVersion: apps/v1 kind: Deployment metadata: annotations: kompose.version: 1.31.2 (a92241f79) creationTimestamp: null labels: io.kompose.service: mariadb name: mariadb spec: replicas: 1 selector: matchLabels: io.kompose.service: mariadb strategy: type: Recreate template: metadata: annotations: kompose.version: 1.31.2 (a92241f79) creationTimestamp: null labels: io.kompose.network/docker-compose-rgs-backend: "true" io.kompose.service: mariadb spec: containers: - env: - name: MYSQL_DATABASE value: rgs - name: MYSQL_ROOT_PASSWORD value: root image: mariadb name: mariadb ports: - containerPort: 3306 hostPort: 3307 protocol: TCP resources: {} volumeMounts: - mountPath: /var/lib/mysql name: db restartPolicy: Always volumes: - name: db persistentVolumeClaim: claimName: db status: {}
尝试查阅资料但不确定方案适用性,尤其不清楚hostNetwork: false参数的添加位置。
问题分析
报错的核心原因是宿主机端口被占用。Kompose转换Docker Compose配置时,会将ports字段的宿主机端口映射(如8888:80)自动转为Kubernetes Deployment中的hostPort配置。但Minikube是单节点集群,宿主机端口资源有限,若其他部署复用相同hostPort,或该端口被Minikube/宿主机其他进程占用,就会导致Pod无法调度。
另外,你在Deployment的spec层级已经设置了hostNetwork: false(这也是默认值),所以问题不在该参数上。
修复方案
1. 删除Deployment中的hostPort配置
Kubernetes的服务暴露逻辑是通过Service层实现,而非直接绑定宿主机端口。hostPort属于特殊场景用法,常规部署中应移除,避免端口冲突。
修改UI Deployment的容器端口部分,删除hostPort: 8888:
containers: - image: ui name: ui ports: - containerPort: 80 protocol: TCP resources: {}
其他报错的部署若有hostPort配置,需一并删除。
2. 通过Minikube访问服务
移除hostPort后,使用以下命令访问UI服务:
minikube service ui
该命令会自动创建端口转发并打开浏览器,直接访问应用。
3. (可选)固定端口访问(NodePort)
若需要固定端口,可将UI Service的类型改为NodePort:
spec: type: NodePort ports: - name: "8888" port: 8888 targetPort: 80 nodePort: 30088 # 可选,指定30000-32767范围的端口 selector: io.kompose.service: ui
执行minikube ip获取节点IP后,通过http://<Minikube节点IP>:30088访问。
4. (可选)检查端口占用
若怀疑端口被占用,可进入Minikube节点排查:
minikube ssh netstat -tulpn | grep 8888
查看是否有进程占用目标端口。
内容的提问来源于stack exchange,提问作者hellodarkness

