Search code examples
containerskubernetesibm-cloud

App deployed on Kubernetes cannot be accessed from Internet


I am new to Kubernetes & Docker. I created a simple nodejs application and deployed on BlueMix Kubernetes. But I am unable to accesses the application on internet. The ip & port mentioned in the kubernetes is not accessible. Can somebody help me.

I tried to http://10.76.193.146:31972, but it did not go through. I am not sure if this the public ip as its 10. series.

I also tried the public ip ( http://184.173.1.79:31972 ) mentioned in the blue mix kubernetes cluster - screenshot below. But that too failed.

This are steps I followed.

  1. Created a nodejs app locally. It ran as desired on the local
// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

---------- package.json

{
  "name": "helloworld-nodejs",
  "version": "0.0.1",
  "description": "First Docker",
  "main": "app.js",
  "scripts": {
    "start": "PORT=8000 node ./app.js"
  },
  "author": "",
  "license": "ISC"
}
  1. Created a docker container locally and ran the docker. It worked properly

  2. Uploaded the docker container on Bluemix registry as

    registry.ng.bluemix.net/testkubernetes/helloworld-nodejs:0.0.1

  3. Created the Nodes and Services in Kubernetes, using the following YAML file

----------Node YAML file

apiVersion: v1
kind: Pod
metadata:
  name: helloworld-nodejs
  labels:
    name: helloworld-nodejs
spec:
  containers:
    - name: helloworld-nodejs
      image: registry.ng.bluemix.net/testkubernetes/helloworld-nodejs:0.0.1
      ports:
        - containerPort: 8000

---------- Services YAML

apiVersion: v1
kind: Service
metadata:
  name: helloworld-nodejs
  labels:
    name: helloworld-nodejs
spec:
  type: NodePort
  selector:
    name: helloworld-nodejs
  ports:
  - port: 8080
  1. The application gets deployed properly and is also running, which I can confirm from the logs

enter image description here enter image description here

Result of kubectl get services & kubectl get nodes command

Result of kubectl get services

Result of kubectl get nodes

enter image description here


Solution

  • Since your service's port is different from your pod's containerPort, you will have to specify targetPort in your service.

    spec:
      type: NodePort
      selector:
        name: helloworld-nodejs
      ports:
        - port: 8080
          targetPort: 8000
    

    According to the Kubernetes documentation on targetPort, it is the:

    Number or name of the port to access on the pods targeted by the service. .... If this is not specified, the value of the 'port' field is used (an identity map).