Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes nodeport not working

Tags:

kubernetes

I've created an YAML file with three images in one pod (they need to communicate with eachother over 127.0.0.1) It seems that it's all working. I've defined a nodeport in the yaml file.

There is one deployment defined applications it contains three images:

  • contacts-db (A MySQL database)
  • front-end (An Angular website)
  • net-core (An API)

I've defined three services, one for every container. In there I've defined the type NodePort to access it.

So I retrieved the services to get the port numbers:

NAME          CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
contacts-db   10.103.67.74     <nodes>       3306:30241/TCP   1d
front-end     10.107.226.176   <nodes>       80:32195/TCP     1d
net-core      10.108.146.87    <nodes>       5000:30245/TCP   1d

And I navigate in my browser to http://:32195 and it just keeps loading. It's not connecting. This is the complete Yaml file:

---
apiVersion: v1
kind: Namespace
metadata:
  name: three-tier
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: applications
  labels:
    name: applications
  namespace: three-tier
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: applications
    spec:
      containers:
      - name: contacts-db
        image: mysql/mysql-server #TBD
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: quintor
          - name: MYSQL_DATABASE
            value: quintor #TBD
        ports:
        - name: mysql
          containerPort: 3306
      - name: front-end
        image: xanvier/angularfrontend #TBD
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 80
      - name: net-core
        image: xanvier/contactsapi #TBD
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: contacts-db
  labels:
    name: contacts-db
  namespace: three-tier
spec:
  type: NodePort
  ports:
    # the port that this service should serve on
  - port: 3306
    targetPort: 3306
  selector:
    name: contacts-db
---
apiVersion: v1
kind: Service
metadata:
  name: front-end
  labels:
    name: front-end
  namespace: three-tier
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80 #nodePort: 30001
  selector:
    name: front-end
---
apiVersion: v1
kind: Service
metadata:
  name: net-core
  labels:
    name: net-core
  namespace: three-tier
spec:
  type: NodePort
  ports:
  - port: 5000
    targetPort: 5000 #nodePort: 30001
  selector:
    name: net-core
---
like image 782
Ravenix Avatar asked Feb 09 '17 10:02

Ravenix


People also ask

How do you check NodePort in Kubernetes?

To specify a NodePort and see which NodePorts are already in use, run the kubectl get svc command. Any NodePorts in use appear under the Ports field.

How does NodePort work in Kubernetes?

A NodePort is an open port on every node of your cluster. Kubernetes transparently routes incoming traffic on the NodePort to your service, even if your application is running on a different node.

Can I use NodePort with ingress?

NodePort can be used with an Ingress Controller. Using NodePort over the LoadBalancer type may be a requirement for certain configurations or architectures. When using cloud based solutions it may be more beneficial to use a LoadBalancer as they will use the standard ports of 80/443.


1 Answers

The selector of a service is matching the labels of your pod. In your case the defined selectors point to the containers which leads into nothing when choosing pods.

You'd have to redefine your services to use one selector or split up your containers to different Deployments / Pods.

To see whether a selector defined for a services would work, you can check them with:

kubectl get pods -l key=value

If the result is empty, your services will run into the void too.

like image 138
pagid Avatar answered Oct 12 '22 01:10

pagid