cannot access a service via nodeport in kubernetes for docker desktop windows

3/20/2019

Hello everyone i am trying kubernetes and have the version that comes with docker desktop for windows and i cant seem to access a service which has type nodeport . Following are the related info

docker version:

Client: Docker Engine - Community
 Version:           18.09.2
 API version:       1.39
 Built:             Sun Feb 10 04:12:31 2019
 OS/Arch:           windows/amd64
 Experimental:      false



Server: Docker Engine - Community
 Engine:
  Version:          18.09.2
  API version:      1.39 (minimum version 1.12)
  Built:            Sun Feb 10 04:13:06 2019
  OS/Arch:          linux/amd64
  Experimental:     false



kubernetes version:

Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.11", GitCommit:"637c7e288581ee40ab4ca210618a89a555b6e7e9", GitTreeState:"clean", BuildDate:"2018-11-26T14:38:32Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.11", GitCommit:"637c7e288581ee40ab4ca210618a89a555b6e7e9", GitTreeState:"clean", BuildDate:"2018-11-26T14:25:46Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

my deployment file:(deployment.yml)

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
  name: tomcat-app
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: tomcat-app
    spec:
      containers:
        - name: tomcat-app
          image: tomcatapp:v1.0.0
          ports:
            - containerPort: 80


---
kind: Service
apiVersion: v1
metadata:
  name: tomcatappservice
spec:

  type: NodePort
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 80
  selector:
    app: tomcat-app

ran it with

kubectl create -f deployment.yml

cant seem to access the tomcat server at localhost: given by kubernetes also tried explicitly giving a port in deployment file but still cant get it to work

-- JayD
kubectl
kubernetes

2 Answers

3/20/2019

Your deployment file does not have any selector for Pods which would be used by service!

If you want specific port, update the port info in the service section itself. Otherwise k8s will assign some random ports which you get by running kubectl get svc

Try this file. Access the application at port 30080

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: tomcat-app
  name: tomcat-app
spec:
  replicas: 5
  selector:
    matchLabels:
      app: tomcat-app
  template:
    metadata:
      labels:
        app: tomcat-app
    spec:
      containers:
      - image: tomcatapp:v1.0.0
        name: tomcat-app
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: tomcat-app
  name: tomcat-app
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30080
  selector:
    app: tomcat-app
  type: NodePort
-- KitKarson
Source: StackOverflow

4/12/2020
  1. You must have two tags nodePort: 30007 and type: NodePort Your service yaml should be like this:
kind: Service
apiVersion: v1
metadata:
  name: tomcatappservice
spec:
  type: NodePort
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 80
      nodePort: 30007
  selector:
    app: tomcat-app
  1. Test it by kubectl get services, and if it succeed, you will see something like 80:30007/TCP
$ kubectl get services
NAME           TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
hello          NodePort       10.96.92.53    <none>        80:30007/TCP   3d3h
  1. Access your service.
$ kubectl describe nodes | grep InternalIP -n1
26-Addresses:
27:  InternalIP:  172.17.0.3
28-  Hostname:    kind2-control-plane

or

$ kubectl get nodes --output json
{
    "apiVersion": "v1",
    "items": [
            ...
            "status": {
                "addresses": [
                    {
                        "address": "172.17.0.3",
                        "type": "InternalIP"
                    },
                    {
                        "address": "kind2-control-plane",
                        "type": "Hostname"
                    }
                ],
    ...
}

$ kubectl get nodes --output jsonpath='{.items[*].status.addresses}'
[map[address:172.17.0.3 type:InternalIP] map[address:kind2-control-plane type:Hostname]]

In the above examples, URL is http://127.17.0.3:30007.

-- kyakya
Source: StackOverflow