如何在特定Nodeport上公开Kubernetes服务? [英] How to expose a Kubernetes service on a specific Nodeport?
问题描述
我用下面的yaml定义创建了一个pod.
I have create a pod with the below yaml definition.
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: praveensripati/docker-demo:1.2
ports:
- containerPort: 3000
现在我公开了一个pod,它创建了一个服务.
And now I expose the pod, which creates a service.
kubectl expose pod myapp-pod --type=NodePort
容器上的端口3000暴露给节点上的端口31728.而且我可以使用端口31728上的curl来访问页面.
The port 3000 on the container is exposed to port 31728 on the nodes. And I am able to do access the page using curl on port 31728.
kubectl get service myapp-pod
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp-pod NodePort 10.107.254.254 <none> 3000:31728/TCP 5s
这一次,我不想在随机端口上公开该服务,而是在端口80上公开.因此,我使用--port将端口号指定为80.服务细节有点奇怪.它表示容器上的端口80暴露于节点上的端口31316.另外,我能够使用curl在随机端口(在本例中为31316)而不是端口80上访问页面.
This time I wanted to expose the service not a random port, but on port 80. And so I specify the port number as 80, by using --port. The service details are a bit odd. It says that port 80 on the container is exposed to port 31316 on the nodes. Also, I am able to access the page using curl on the random port (31316 in this case) and not port 80.
kubectl expose pod myapp-pod --type=NodePort --target-port=3000 --port=80
kubectl get service myapp-pod
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp-pod NodePort 10.105.123.73 <none> 80:31316/TCP 12s
我无法在特定端口而不是随机端口上公开服务.我尝试了几种组合并阅读了k8s文档,但没有成功.
I am not able to expose a service on a specific port and not on a random port. I tried a few combinations and read the k8s documentation, but no success.
如何在特定端口而不是随机端口上公开服务?
How do I expose a service on a specific port instead of a random port?
推荐答案
我将在这里尝试回答您的查询.
I will try to answer your query here.
此外,我能够使用随机端口上的curl访问页面 (在本例中为31316),而不是端口80.
Also, I am able to access the page using curl on the random port (31316 in this case) and not port 80.
-- Because, kubernetes exposed the port 31316 on the host (maps to the service) and hence it can be accessed on host:31316.
-- Service port is visible only within the kubernetes cluster. You can exec into a pod container and do a curl on servicename:service port instead of the NodePort.
注意术语-container port:
端口容器在监听. Service port:
kubernetes服务在群集内部ip上公开并映射到容器端口的端口. Nodeport:
在主机上公开并映射到kubernetes服务的端口.
Note the terms - container port:
the port container listens on. Service port:
the port where kubernetes service is exposed on cluster internal ip and mapped to the container port. Nodeport:
the port exposed on the host and mapped to kubernetes service.
这篇关于如何在特定Nodeport上公开Kubernetes服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!