简体   繁体   中英

Can't connect to Kubernetes Cluster on external IP

I'm trying to access .NET Web API which I docker-ized and mounted in an Kubernet Cluster on Microsoft Azure.

The application works fine on local docker machine. The cluster is running, my deployment was correct and the pods where created. Everything I check is fine, but I cannot access my application through the external cluster IP (Load Balancer). This is my YAML deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ohmioapi-deployment
spec:
  selector:
    matchLabels:
      app: ohmioapi
  replicas: 1
  template:
    metadata:
      labels:
        app: ohmioapi
    spec:
      containers:
      - name: ohmioapi
        image: ohmiocontainers.azurecr.io/ohmioapi:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 15200
      imagePullSecrets:
        - name: acr-auth
---
apiVersion: v1
kind: Service
metadata:
  name: ohmioapi
  labels:
    app: ohmioapi
spec:
  selector:
    app: ohmioapi
  ports:
  - port: 15200
    nodePort: 30200
    protocol: TCP
  type: LoadBalancer

Can anyone give a hint of where to start looking for? Thanks!

I would give the deployment/pods port a name (eg http ) and then make the service serve off port 80 but target the pod port by name... that way you don't have to worry about port numbers when connecting to a service.

Also, you shouldn't need or want to use nodePort if you are using type of LoadBalancer .

Eg

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ohmioapi-deployment
spec:
  selector:
    matchLabels:
      app: ohmioapi
  replicas: 1
  template:
    metadata:
      labels:
        app: ohmioapi
    spec:
      containers:
      - name: ohmioapi
        image: ohmiocontainers.azurecr.io/ohmioapi:latest
        imagePullPolicy: Always
        ports:
        - name: http
          containerPort: 15200
      imagePullSecrets:
        - name: acr-auth

---
apiVersion: v1
kind: Service
metadata:
  name: ohmioapi
  labels:
    app: ohmioapi
spec:
  selector:
    app: ohmioapi
  ports:
  - name: http
    port: 80
    targetPort: http
    protocol: TCP
  type: LoadBalancer

You can use the command kubectl get service to get all the information of services and check your service ohmioapi , the result will like this:

在此处输入图片说明

Or you can use the command kubectl describe service serviceName to get more details about your service, the result will like this:

在此处输入图片说明

You can check the port mapping in the load balancer and access from the browser via the external IP and port.

You also can use the command kubectl edit service serviceName to edit and check from the config file created by the Kunernetes, the result will like this:

在此处输入图片说明

Pretty sure you need 'targetport' instead of 'nodeport' (or just drop it if the port is the same).

https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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