从外部访问kubernetes服务 [英] Access kubernetes service externally
问题描述
我正在关注spring和kubernetes集成教程:
I'm following the spring and kubernetes integration tutorial:
https://spring.io/guides/gs/spring-boot-kubernetes/
在当前情况下,我有1个主服务器和2个工作服务器.
In my current scenario, I have 1 master and 2 workers servers.
当我使用命令 kubectl apply -f deployment.yaml
部署以下文件时,我可以使用 kubectl port-forward svc/demo 8080从主服务器内发出请求:8080
和 curl localhost:8080/actuator/health
.
When I deploy the file below using the command kubectl apply -f deployment.yaml
, I can make a request from within the master server using kubectl port-forward svc/demo 8080:8080
and curl localhost:8080/actuator/health
.
我想要做的是一个外部请求(一台公用计算机-我的计算机)来访问我创建的服务( kubernetes_master_ip:8080/actuator
),但是当我尝试这样做时,我得到了连接被拒绝".
What I want to do is an external request (a public computer - my computer) to access the service that I created (kubernetes_master_ip:8080/actuator
), but when I try this, I get "connection refused".
缺少什么?
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: demo
name: demo
spec:
replicas: 1
selector:
matchLabels:
app: demo
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: demo
spec:
containers:
- image: springguides/demo
name: demo
resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: demo
name: demo
spec:
ports:
- name: 8080-8080
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: demo
type: ClusterIP
status:
loadBalancer: {}
推荐答案
您需要更改服务类型以公开应用程序.有两种方法:-LoadBalancer类型:(仅适用于云提供商)-NodePort类型:可以在本地或微型kube中完成.
You need to change the type of service to expose the application. There are two ways: - LoadBalancer type: (Only on cloud providers) - NodePort type: Can be done on-premise or minikube.
将您的服务Yaml更改为以下内容:
Change your service yaml to below:
apiVersion: v1
kind: Service
metadata:
labels:
app: demo
name: demo
spec:
ports:
- name: 8080-8080
port: 8080
nodePort: 31234
protocol: TCP
targetPort: 8080
selector:
app: demo
type: NodePort
服务一旦执行.检查在其上创建容器的应用程序节点IP.
Once the service is executed. check the application Node IP on which container is created.
kubectl get pods -o wide
然后尝试通过以下位置访问该应用程序: http://node_ip:31234/actuator
then try to access the application at: http://node_ip:31234/actuator
这篇关于从外部访问kubernetes服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!