【问题标题】:kompose not exposing NodePort - Docker for Desktop Windowskompose 不公开 NodePort - 用于桌面 Windows 的 Docker
【发布时间】:2020-01-07 19:29:41
【问题描述】:

我有这个 docker-compose.yml 和 Docker for Desktop Windows 运行本地 kubernetes 集群(默认 docker-desktop 上下文):

version: '3'
services:
  web:
    image: customnode
    ports:
    - "3000:3000"
    labels:
      kompose.service.type: nodeport

  datastore:
    image: custommongo
    ports:
    - "27017:27017"

docker-compose up -d 完美运行并将我的 NodeJS 暴露在 127.0.0.1 上的端口 3000 上。

我正在尝试迁移我的 k8s 集群,所以关注https://kompose.io/getting-started/

上面的页面说“如果您还没有运行 Kubernetes 集群,minikube 是最好的入门方式”...我已经在运行 OOTB Docker for Desktop 集群,所以我假设我不需要 minikube .

kompose convert
INFO Kubernetes file "datastore-service.yaml" created
INFO Kubernetes file "web-service.yaml" created
INFO Kubernetes file "datastore-deployment.yaml" created
INFO Kubernetes file "web-deployment.yaml" created

这里是 web-deployment 和 web-service YAMLS:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.service.type: nodeport
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: web
  name: web
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: web
    spec:
      containers:
        image: customnode
        name: web
        ports:
        - containerPort: 3000
        resources: {}
      restartPolicy: Always
status: {}

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.service.type: nodeport
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: web
  name: web
spec:
  ports:
  - name: "3000"
    port: 3000
    targetPort: 3000
  selector:
    io.kompose.service: web
  type: NodePort
status:
  loadBalancer: {}

最后,运行kompose up

kompose up
[36mINFO[0m We are going to create Kubernetes Deployments, Services and PersistentVolumeClaims for your Dockerized application. If you need different kind of resources, use the 'kompose convert' and 'kubectl create -f' commands instead.

INFO Deploying application in "default" namespace
INFO Successfully created Service: datastore
INFO Successfully created Service: web
INFO Successfully created Deployment: datastore
INFO Successfully created Deployment: web

kubectl get svc的输出:

kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
datastore    ClusterIP   10.103.***.***   <none>        27017/TCP        76s
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          23m
web          NodePort    10.106.***.***     <none>        3000:32033/TCP   76s

如您所见,没有我期望的外部 IP。我确定这是我缺乏知识,而不是错误,所以我错过了什么?

【问题讨论】:

    标签: docker kubernetes docker-compose kompose


    【解决方案1】:

    外部 IP 仅分配给 LoadBalancer 类型的服务。 LoadBalancer 控制器必须安装在集群上,LoadBalancer 服务才能工作。否则 LoadBalancer 服务的行为与 NodePort 服务完全相同。大多数云提供商都支持LoadBalancer 服务。

    对于NodePort 类型的服务,该服务绑定到所有节点上节点端口范围内的一个随机端口。在您的情况下,您可以看到,该服务绑定到端口 32033 - 3000:32033/TCP

    节点端口范围配置为 Kubernetes API 服务器的参数,选项为 --service-node-port-range(默认为 30000-32767)。当你创建一个 NodePort 类型的服务时,会从这个范围中选择一个随机的空闲端口。如果要选择自定义端口,可以在Port 对象中指定nodePort 属性。

    例如:

    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kompose.cmd: kompose convert
        kompose.service.type: nodeport
        kompose.version: 1.16.0 (0c01309)
      creationTimestamp: null
      labels:
        io.kompose.service: web
      name: web
    spec:
      ports:
      - name: "3000"
        port: 3000
        targetPort: 3000
        nodePort: 30002         ###### You can choose node port here if needed
      selector:
        io.kompose.service: web
      type: NodePort           ####### Change this line to LoadBalancer if you want an external IP
    status:
      loadBalancer: {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 2022-01-05
      相关资源
      最近更新 更多