Kubernetes服务IP是否会更改? [英] Do Kubernetes service IPs change?

查看:682
本文介绍了Kubernetes服务IP是否会更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是kubernetes/docker的新手,如果这是一个愚蠢的问题,我们深表歉意.

我有一个正在访问一些服务的Pod.在我的容器中,我正在运行python脚本,需要访问该服务.目前,我正在使用服务的IP地址进行此​​操作.

服务IP地址是稳定的还是使用环境变量?如果是这样,那么一些这样做的技巧会很棒.

服务文档的开头段落为暗示稳定的服务提供了动力. IP地址,但我从未看到它明确声明过:

虽然每个Pod都有自己的IP地址,但即使这些IP地址也无法长期保持稳定.这就带来了一个问题:如果某个Pod集(我们称其为后端)为Kubernetes集群中的其他Pod(我们称其为前端)提供了功能,那么这些前端如何找出并跟踪该集中的哪些后端?

输入服务.

我的Pod规格供参考:

kind: Pod
apiVersion: v1
metadata:
  name: fetchdataiso 
  labels:
    name: fetchdataiso
spec:
  containers:
    - name: fetchdataiso 
      image: 192.111.1.11:5000/ncllc/fetch_data
      command: ["python"]
      args: ["feed/fetch_data.py", "-hf", "10.222.222.51", "-pf", "8880", "-hi", "10.223.222.173", "-pi","9101"]

解决方案

简短的回答是是的,服务IP可以更改"

$ kubectl apply -f test.svc.yml
service "test" created

$ kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.12.0.1       <none>        443/TCP   10d
test         10.12.172.156   <none>        80/TCP    6s

$ kubectl delete svc test
service "test" deleted

$ kubectl apply -f test.svc.yml
service "test" created

$ kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.12.0.1       <none>        443/TCP   10d
test         10.12.254.241   <none>        80/TCP    3s

长的答案是,如果正确使用它,则不会有任何问题.在您的问题范围内,更重要的是ENV变量要比DNS/IP耦合差得多. 您应该通过 service service.namespace 甚至是完整路径(例如类似于 test.default.svc.cluster.local 的东西)来引用您的服务强>.这将解决为ClusterIP服务的问题,并且与您的ENV相反,它可以被重新解析为新的IP(除非您明确删除并重新创建服务,否则将永远不会发生),而不会更改正在运行的进程的ENV

I'm very new to kubernetes/docker, so apologies if this is a silly question.

I have a pod that is accessing a few services. In my container I'm running a python script and need to access the service. Currently I'm doing this using the services' IP addresses.

Is the service IP address stable or is it better to use environment variables? If so, some tips on doing that would be great.

The opening paragraph of the Services Documentation gives a motivation for services which implies stable IP addresses, but I never see it explicitly stated:

While each Pod gets its own IP address, even those IP addresses cannot be relied upon to be stable over time. This leads to a problem: if some set of Pods (let’s call them backends) provides functionality to other Pods (let’s call them frontends) inside the Kubernetes cluster, how do those frontends find out and keep track of which backends are in that set?

Enter Services.

My pod spec for reference:

kind: Pod
apiVersion: v1
metadata:
  name: fetchdataiso 
  labels:
    name: fetchdataiso
spec:
  containers:
    - name: fetchdataiso 
      image: 192.111.1.11:5000/ncllc/fetch_data
      command: ["python"]
      args: ["feed/fetch_data.py", "-hf", "10.222.222.51", "-pf", "8880", "-hi", "10.223.222.173", "-pi","9101"]

解决方案

The short answer is "Yes, the service IP can change"

$ kubectl apply -f test.svc.yml
service "test" created

$ kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.12.0.1       <none>        443/TCP   10d
test         10.12.172.156   <none>        80/TCP    6s

$ kubectl delete svc test
service "test" deleted

$ kubectl apply -f test.svc.yml
service "test" created

$ kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.12.0.1       <none>        443/TCP   10d
test         10.12.254.241   <none>        80/TCP    3s

The long answer is that if you use it right, you will have no problem with it. What is even more important in scope of your question is that ENV variables are way worse then DNS/IP coupling. You should refer to your service by service or service.namespace or even full path like something along the lines of test.default.svc.cluster.local. This will get resolved to service ClusterIP, and in opposite to your ENVs it can get re-resolved to a new IP (which will probably never happen unless you explicitly delete and recreate service) while ENV of a running process will not be changed

这篇关于Kubernetes服务IP是否会更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆