简体   繁体   中英

How to access a container from node ip started using docker run

I have one web application which I am trying to run on k8s cluster. I build the image from DockerFile and was testing locally till now using the command:

docker run --name web-app -d -p 8888:80 web-app

and I was able to access the GUI using http://localhost:8888.

Now, I am trying to run it in k8s cluster, so I executed the same docker run command in my k8s cluster and I am getting the o/p when I am trying to curl http://localhost:8888 from my cluster. But this is not I want in production, I want to access the web application using node ip like http://<node_ip>:8888. I tried a few times but not able to access it using node ip.

Now, I have 2 questions:

  1. What changes I need to make in docker run command in order to access the application using node IP?
  2. I also tried running the container using helm charts and by creating a service of type NodePort but it is also not working. Is there anything we need to take care while running any frontend web application using helm install?

For NodePort type service, you need to hit in <node_ip>:<nodePort> . NodePort is the port which you have exposed in you service maniest.

  • kubectl get nodes -o wide : To get the NodeIP

For example, in this service manifest the nodePort is 31000 . Remember one thing nodePort range is from 30000 to 32767.

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 31000

You don't need docker run if you deploy your application in kube.netes. Have you created a deployment of your application in k8s? If yes then you can enter your application via kubectl exec -it <pod_nam> sh . If you want to communicate from outside the cluster you need to create se NodePort type service as i said and then you can get the access by http://<node_ip>:<node_port> .

Here I have given a yaml for deployment as an example.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.14.1
          ports:
            - containerPort: 80

By the way, your containerPort in deployment must need to be equal to the targetPort in service.

it seems like you are trying with the wrong port. 8888 this is not a NodePort . NodePort range is (30000-32767) . better you look into the exact nodeport .

try this command kubectl get svc . output will be something like this.

kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          17h
test-srv    NodePort    10.102.92.219   <none>        4000:31553/TCP   9s

For test-srv service, the nodeport is 31553 . And you can find your node's IP with

kubectl get nodes -o wide
NAME                 STATUS   ROLES    AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE       KERNEL-VERSION     CONTAINER-RUNTIME
kind-control-plane   Ready    master   17h   v1.18.2   172.18.0.2    <none>        Ubuntu 19.10   5.8.0-41-generic   containerd://1.3.3-14-g449e9269

for me, nodeIp is 172.18.0.2 . Now just try with curl -k 172.18.0.2:31553 . The template is like <node_ip>:<node_port>

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