简体   繁体   English

如何从节点 ip 开始使用 docker 运行访问容器

[英]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.我有一个 web 应用程序,我试图在 k8s 集群上运行。 I build the image from DockerFile and was testing locally till now using the command:我从 DockerFile 构建图像并使用以下命令在本地进行测试:

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

and I was able to access the GUI using http://localhost:8888.我能够使用 http://localhost:8888 访问 GUI。

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.现在,我试图在 k8s 集群中运行它,所以我在我的 k8s 集群中执行了相同的 docker 运行命令,当我尝试从我的集群中访问 curl http://localhost:8888 时,我得到了 o/p。 But this is not I want in production, I want to access the web application using node ip like http://<node_ip>:8888.但这不是我想要的生产环境,我想使用节点 ip 访问 web 应用程序,例如 http://<node_ip>:8888。 I tried a few times but not able to access it using node ip.我尝试了几次但无法使用节点 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?我需要在 docker 运行命令中进行哪些更改才能使用节点 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.我还尝试使用 helm 图表运行容器并创建 NodePort 类型的服务,但它也不起作用。 Is there anything we need to take care while running any frontend web application using helm install?使用 helm install 运行任何前端 web 应用程序时,我们需要注意什么吗?

For NodePort type service, you need to hit in <node_ip>:<nodePort> .对于 NodePort 类型的服务,您需要输入<node_ip>:<nodePort> NodePort is the port which you have exposed in you service maniest. NodePort 是您在服务中暴露最多的端口。

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

For example, in this service manifest the nodePort is 31000 .例如,在此服务清单中,节点端口为31000 Remember one thing nodePort range is from 30000 to 32767.请记住一件事, nodePort范围是从 30000 到 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.如果您在 kube.netes 中部署应用程序,则不需要运行 docker。 Have you created a deployment of your application in k8s?您是否在 k8s 中创建了应用程序的部署? If yes then you can enter your application via kubectl exec -it <pod_nam> sh .如果是,那么您可以通过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> .如果你想从集群外部进行通信,你需要像我说的那样创建 se NodePort 类型的服务,然后你可以通过http://<node_ip>:<node_port>获得访问权限。

Here I have given a yaml for deployment as an example.这里我以部署一个yaml为例。

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.顺便说一句,您在部署中的containerPort必须等于服务中的targetPort

it seems like you are trying with the wrong port.看来您正在尝试使用错误的端口。 8888 this is not a NodePort . 8888这不是NodePort NodePort range is (30000-32767) . NodePort范围是(30000-32767) better you look into the exact nodeport .最好查看确切的nodeport

try this command kubectl get svc .试试这个命令kubectl get svc output will be something like this. output 将是这样的。

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 .对于test-srv服务,节点端口是31553 And you can find your node's IP with你可以找到你的节点的 IP

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 .对我来说,nodeIp 是172.18.0.2 Now just try with curl -k 172.18.0.2:31553 .现在试试curl -k 172.18.0.2:31553 The template is like <node_ip>:<node_port>模板类似于<node_ip>:<node_port>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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