为什么我的Kubernetes服务没有在我指定的端口上发布?

时间:2018-07-14 22:05:19

标签: service kubernetes ports

在过去的几年中,我一直在与Kubernetes进行连接和断开,我不确定是否一直都是这种情况(也许这种行为最近有所改变),但是我似乎无法在我的端口上发布服务意向-他们总是在较高的随机端口(> 30000)上发布。

例如,我正在经历this walkthrough on Ingress,并按照说明创建了以下Deployment和Service对象:

package com.mygdx.manager;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.mygdx.game.MyMainGame;

public class Background {


    private TextureRegion image;
    private OrthographicCamera camera;

    private float scale;

    private float x;
    private float y;
    private int drawX;
    private int drawY;

    private float dx;
    private float dy;

    public Background(TextureRegion image, OrthographicCamera camera, float scale) {
        this.image = image;
        this.camera = camera;
        this.scale = scale;
        drawX = MyMainGame.Width / image.getRegionWidth() + 1;
        drawY = MyMainGame.Height / image.getRegionHeight() + 1;

        System.out.println("Image width"+image.getRegionWidth());
        System.out.println("Image height"+image.getRegionHeight());
    }

    public void setVector(float dx, float dy) {
        this.dx = dx;
        this.dy = dy;
    }

    public void update(float ts) {
        x += (dx * scale) * ts;
        y += (dy * scale) * ts;
    }

    public void render(SpriteBatch sb) {
        float x = ((this.x + camera.viewportWidth / 2 - camera.position.x) * scale) % image.getRegionWidth();
        float y = ((this.y + camera.viewportHeight / 2 - camera.position.y) * scale) % image.getRegionHeight();

        sb.begin();

        int colXOffset = x > 0 ? -1 : 0;
        int rowYOffset = y > 0 ? -1 : 0;

        for (int rowY = 0; rowY < drawY; rowY++) {
            for (int colX = 0; colX < drawX; colX++) {
                sb.draw(image, x + (colX + colXOffset) * image.getRegionWidth(), y + (rowY + rowYOffset) * image.getRegionHeight());
            }
        }

        sb.end();
    }
}

因此,我应该有一个正在侦听8080端口的服务,但是它是一个随机的高端口:

 public void render() {

        //clear screen

        Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
      //
        cam.position.set(playerBody.getPosition().x * PPM +MyMainGame.Width / 4,MyMainGame.Height / 2,0);
        // cam.setToOrtho(false,playerBody.getPosition().x * PPM +MyMainGame.Width / 4,MyMainGame.Height / 2);
        //cam.setPosition(playerBody.getPosition().x * PPM +MyMainGame.Width / 4, MyMainGame.Height / 2);

        cam.setToOrtho(false,10,7);

       cam.update();





        sb.setProjectionMatrix(cam.combined);
        for(int i = 0; i < backgrounds.length; i++) {
            backgrounds[i].render(sb);
       }


        orthomap.setView(cam);
        orthomap.render();
        b2d.render(world,B2DCAM.combined);




    }

我还验证了我的节点都没有在监听8080,但是它们在监听31669。

这不是超级理想-特别是考虑到Ingress部分需要知道正在使用--- apiVersion: apps/v1beta1 kind: Deployment metadata: name: hello-world-deployment spec: replicas: 1 template: metadata: labels: app: hello-world spec: containers: - image: "gokul93/hello-world:latest" imagePullPolicy: Always name: hello-world-container ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: hello-world-svc spec: ports: - port: 9376 protocol: TCP targetPort: 8080 selector: app: hello-world type: NodePort 的情况(本演练在8080处对此进行了引用)。

顺便说一句,当我创建Ingress控制器时,这种行为是相同的-而不是像在一个好的负载均衡器上监听80和443一样,它监听的是随机性较高的端口。

我想念什么吗?我做错了吗?

1 个答案:

答案 0 :(得分:2)

马特

分配随机端口的原因是因为您正在创建NodePort类型的服务。

K8s文档解释了NodePort here

基于您的配置,该服务在端口9376(后端端口为8080)上公开。因此hello-word-svc应该位于:10.109.24.16:9376。本质上,可以通过以下方式之一来获得此服务:

服务ip /端口:-10.109.24.16:9376

节点ip /端口:-[您的计算节点ip]:31669 <-之所以创建它,是因为您的服务属于NodePort类型

您还可以直接查询Pod,以测试Pod实际上是在公开服务。

Pod ip /端口:10.40.0.4:8080

由于您最终的目标是使用入口控制器来实现服务的外部可达性,因此“ type:ClusterIP”可能就足以满足您的要求。