kubernetes - Kubernetes,k8s如何制作服务url?
问题描述
我正在学习k8s。我的问题是,如何让 k8s 获取服务 url 作为 minikube 命令“minikube get service xxx --url”呢?为什么我问是因为当 pod 关闭并再次启动/创建/启动时,无需通过访问服务 url 来更改 url。当我将 pod 部署为 NodePort 时,我可以使用主机 IP 和端口访问 pod,但如果再次重新启动/创建它,则端口会更改。
我的情况如下图所示:我有
one master(172.16.100.91) and
one node(hostname node3, 172.16.100.96)
我如下创建 pod 和服务,hellocomm 部署为 NodePort,helloext 部署为 ClusterIP。hellocomm 和 helloext 都是 spring boot hello world 应用程序。
docker build -t jshenmaster2/hellocomm:0.0.2 .
kubectl run hellocomm --image=jshenmaster2/hellocomm:0.0.2 --port=8080
kubectl expose deployment hellocomm --type NodePort
docker build -t jshenmaster2/helloext:0.0.1 .
kubectl run helloext --image=jshenmaster2/helloext:0.0.1 --port=8080
kubectl expose deployment helloext --type ClusterIP
[root@master2 shell]# kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
hellocomm NodePort 10.108.175.143 <none> 8080:31666/TCP 8s run=hellocomm
helloext ClusterIP 10.102.5.44 <none> 8080/TCP 2m run=helloext
[root@master2 hello]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
hellocomm-54584f59c5-7nxp4 1/1 Running 0 18m 192.168.136.2 node3
helloext-c455859cc-5zz4s 1/1 Running 0 21m 192.168.136.1 node3
在上面,我的 pod 部署在 node3(172.16.100.96),所以我可以通过 172.16.100.96:31666/hello 访问 hellocomm,在这种情况下,可以很容易地看到当 node3 关闭时,会创建/启动一个新的 pod ,端口也发生变化。这样我的客户就失去了连接。我不想要这个解决方案。
我目前的问题是,由于 helloext 部署为 ClusteriP,它也是如上所示的服务。这是否意味着 ClusterIP 10.102.5.44 和端口 8080 将是服务 url,http ://10.102.5.44:8080/hello ?
我是否需要再次通过 yaml 文件创建服务?命令创建的服务与 yaml 文件创建的服务有什么区别?如果我必须通过 yaml 创建服务,如何编写以下 yaml 文件?
下面是我需要填写的yaml定义模板,如何填写?
apiVersion: v1
kind: Service
matadata:
name: string helloext
namespace: string default
labels:
- name: string helloext
annotations:
- name: string hello world
spec:
selector: [] ?
type: string ?
clusterIP: string anything I could give?
sessionAffinity: string ? (yes or no)
ports:
- name: string helloext
protocol: string tcp
port: int 8081? (port used by host machine)
targetPort: int 8080? (spring boot uses 8080)
nodePort: int ?
status: since I am not using loadBalancer in deploymennt, I could forget this.
loadBalancer:
ingress:
ip: string
hostname: string
解决方案
NodePort,顾名思义,直接在节点上打开一个端口(实际上是在集群中的所有节点上),这样你就可以访问你的服务了。默认情况下它是随机的——这就是为什么当一个 pod 死掉时,它会为你生成一个新的。但是,您也可以指定一个端口(此处为第 3 段) - 即使重新创建了 pod,您也可以在同一端口上访问。
clusterIP 只能在集群内部访问,因为它是私有 IP。这意味着,在默认情况下,您可以从集群内的另一个容器/节点访问此服务。您可以exec
/ssh
进入任何正在运行的容器/节点并尝试一下。
Yaml 文件可以进行版本控制、文档化、模板化 ( Helm ) 等。
查看https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#servicespec-v1-core了解每个字段的详细信息。
编辑:有关服务的更多详细信息:https ://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0
推荐阅读
- .net-core - EasyNetQ:我们可以使用相同的连接来订阅和发布内部消息吗?
- c# - 有没有办法覆盖地图类中流畅的 nhibernate 配置的 DefaultSchema?
- css-grid - 使用试剂中的 css 网格
- c - 在 CLion 中找不到 Conio.h
- java - 如何使用 try 和 catch 验证用户输入?
- react-native - 如何在 return() 中更新状态?
- c - 使用字符串进行插值搜索
- python - DjangoCMS 产品和目录
- mysql - 在关系数据库表中插入余额数据
- java - Java:为什么在Java中用当前调用对象自动调用另一个方法内部调用的方法?