简体   繁体   English

如何使用 Kubernetes 为 Pod 分配静态 IP

[英]How to assign a static IP to a pod using Kubernetes

currently kubectl assigns the IP address to a pod and that is shared within the pod by all the containers.目前 kubectl 将 IP 地址分配给一个 Pod,该 IP 地址在 Pod 内由所有容器共享。

I am trying to assign a static IP address to a pod ie in the same network range as the one assigned by kubectl, I am using the following deployment file我正在尝试为 pod 分配一个静态 IP 地址,即在与 kubectl 分配的网络范围相同的网络范围内,我正在使用以下部署文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: redis
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: rediscont
    spec:
      containers:
      - name: redisbase
        image: localhost:5000/demo/redis
        ports:
        - containerPort: 6379
          hostIP: 172.17.0.1
          hostPort: 6379

On the dockerhost where its deployed i see the following:在部署它的 dockerhost 上,我看到以下内容:

CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                       NAMES
4106d81a2310        localhost:5000/demo/redis            "/bin/bash -c '/root/"   28 seconds ago      Up 27 seconds                                   k8s_redisbase.801f07f1_redis-1139130139-jotvn_default_f1776984-d6fc-11e6-807d-645106058993_1b598062
71b03cf0bb7a        gcr.io/google_containers/pause:2.0   "/pause"                 28 seconds ago      Up 28 seconds       172.17.0.1:6379->6379/tcp   k8s_POD.99e70374_redis-1139130139-jotvn_default_f1776984-d6fc-11e6-807d-645106058993_8c381981

The IP tables-save gives the following output IP 表保存提供以下输出

-A DOCKER -d 172.17.0.1/32 ! -i docker0 -p tcp -m tcp --dport 6379 -j DNAT --to-destination 172.17.0.3:6379

Even with this, from other pods the IP 172.17.0.1 is not accessible.即使这样,也无法从其他 pod 访问 IP 172.17.0.1。 Basically the question is how to assign static IP to a pod so that 172.17.0.3 doesn't get assigned to it基本上问题是如何将静态 IP 分配给 pod,以便 172.17.0.3 不会被分配给它

Generally, assigning a Pod a static IP address is an anti-pattern in Kubernetes environments. 通常,为Pod分配静态IP地址是Kubernetes环境中的反模式。 There are a couple of approaches you may want to explore instead. 您可能想要探索几种方法。 Using a Service to front-end your Pods (or to front-end even just a single Pod) will give you a stable network identity, and allow you to horizontally scale your workload (if the workload supports it). 使用服务前端您的Pod(或前端甚至只是一个Pod)将为您提供稳定的网络身份,并允许您水平扩展您的工作负载(如果工作负载支持它)。 Alternately, using a StatefulSet may be more appropriate for some workloads, as it will preserve startup order, host name, PersistentVolumes, etc., across Pod restarts. 或者,使用StatefulSet可能更适合某些工作负载,因为它将在Pod重新启动之间保留启动顺序,主机名,PersistentVolumes等。

I know this doesn't necessarily directly answer your question, but hopefully it provides some additional options or information that proves useful. 我知道这不一定直接回答你的问题,但希望它提供了一些证明有用的附加选项或信息。

When you created Deployment with one replica and defined hostIP and hostPort you basically bounded hostIP and hostPort of your host machine with your pod IP and container port, so that traffic is routed from hostIP: port to podIP: port. 当您使用一个副本创建部署并定义了hostIPhostPort时,您基本上将主机的hostIP和hostPort与您的pod IP和容器端口绑定在一起,以便将流量从hostIP:port路由到podIP:port。 Created pod (and container inside of it ) was assigned the ip address from the IP range that is available to it. 创建的pod(及其内部的容器)被分配了可用的IP范围的IP地址。 Basically, the IP range depends on the CNI networking plugin used and how it allocates IP range to each node. 基本上,IP范围取决于所使用的CNI网络插件以及它如何为每个节点分配IP范围。 For instance flannel, by default, provides a /24 subnet to hosts, from which Docker daemon allocates IPs to containers. 例如,flannel默认情况下为主机提供/ 24子网,Docker守护程序从中将IP分配给容器。 So hostIP: 172.17.0.1 option in a spec has nothing to do with assigning IP address to a pod. 因此,规范中的hostIP:172.17.0.1选项与为pod分配IP地址无关。

Basically, the question is how to assign static IP to a pod so that 172.17.0.3 doesn't get assigned to it 基本上,问题是如何将静态IP分配给一个pod,以便172.17.0.3不会被分配给它

As far as I know, all major networking plugins, provide a range of IPs to hosts, so that a pod's IP will be assigned from that range. 据我所知,所有主要的网络插件都为主机提供了一系列IP,因此将从该范围分配一个pod的IP。 You can explore different networking plugins and look at how each of them deals with IPAM(IP Address Management), maybe some plugin provides that functionality or offers some tweaks to implement that, but overall its usefulness would be quite limited. 您可以探索不同的网络插件并查看每个插件如何处理IPAM(IP地址管理),也许某些插件提供该功能或提供一些调整来实现它,但总体而言它的用处将非常有限。

Below is useful info on "hostIP, hostPort" from official K8 docs: 以下是官方K8文档中“hostIP,hostPort”的有用信息:

Don't specify a hostPort for a Pod unless it is absolutely necessary. 除非绝对必要,否则不要为Pod指定hostPort。 When you bind a Pod to a hostPort, it limits the number of places the Pod can be scheduled, because each combination must be unique. 将Pod绑定到hostPort时,它会限制Pod可以调度的位置数,因为每个组合必须是唯一的。 If you don't specify the hostIP and protocol explicitly, Kubernetes will use 0.0.0.0 as the default hostIP and TCP as the default protocol. 如果未明确指定hostIP和协议,Kubernetes将使用0.0.0.0作为默认hostIP,并使用TCP作为默认协议。

If you only need access to the port for debugging purposes, you can use the apiserver proxy or kubectl port-forward. 如果您只需要访问端口以进行调试,则可以使用apiserver proxy或kubectl port-forward。

If you explicitly need to expose a Pod's port on the node, consider using a NodePort Service before resorting to hostPort. 如果您明确需要在节点上公开Pod的端口,请考虑在使用hostPort之前使用NodePort服务。 Avoid using hostNetwork, for the same reasons as hostPort. 避免使用hostNetwork,原因与hostPort相同。 Orignal info link to config best practices . Orignal信息链接以配置最佳实践

Assigning static IP addresses to PODs is not possible in OSS Kubernetes.在 OSS Kubernetes 中无法为 POD 分配静态 IP 地址。 But it is possible to configure via some CNI plugins.但是可以通过一些 CNI 插件进行配置。 For instance, Calico provides a way to override IPAM and use fixed addresses by annotating pod.例如,Calico 提供了一种通过注释 pod 来覆盖 IPAM 并使用固定地址的方法。 The address must be within a configured Calico IP pool and not currently in use.该地址必须在已配置的 Calico IP 池中,并且当前未使用。

https://docs.projectcalico.org/networking/use-specific-ip https://docs.projectcalico.org/networking/use-specific-ip

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

相关问题 部署时如何使用Kubernetes将静态IP分配给Pod - How to assign a static IP to a pod using Kubernetes on deployment Kubernetes:如何将 static IP 地址或主机名分配给节点和 POD - Kubernetes : How to assign static IP address or HostName to node and POD 如何使用 Terraform 为 Kubernetes 入口分配静态 IP 地址? - How to assign a static ip address to an Kubernetes ingress using Terraform? 当使用具有多个 static IP 地址的 Cloud NAT 时,GKE 是否可以将特定的出站 IP 地址分配给 Pod? - Is it possible in GKE to assign a specific outbound IP Address to a Pod when using Cloud NAT with multiple static IP adresses? 如何将 IP 分配给 Kube.netes 集群? - How to assign an IP to Kubernetes cluster? Kubernetes + GCP TCP 负载平衡:如何为 Kubernetes 服务分配静态 IP? - Kubernetes + GCP TCP Load balancing: How can I assign a static IP to a Kubernetes Service? 如何从 Kubernetes Pod 连接到私有 IP - How to connect to a private IP from Kubernetes Pod Kubernetes:如何配置hostnetwork pod IP 地址? - Kubernetes: How to configure hostnetwork pod IP address? 如何在 Kubernetes 中修改 Pod 的源 IP? - How to modify source IP for a Pod in Kubernetes? 如何在kubernetes上获取所有Pod的IP - How to get all Pod's IP on a kubernetes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM