# Kubernetes CNI Calico GuideLine# 时间:20210820-[20:00-22:00]# 主题:Calico IPAM# 文档:https://www.yuque.com/docs/share/316fe6ba-25c9-4488-8beb-06eec2579b30?# 《20210820-Calico IPAM》# 作业:Calico IPAM
# FAQ:
Calico IPAM
ConceptsIPAM in KubernetesKubernetes uses IPAM plugins to allocate and manage IP addresses assigned to pods. Different IPAM plugins provide different feature sets. Calico provides its own IPAM plugin called calico-ipam which is designed to work well with Calico and includes a number of features.Calico IPAMThe calico-ipam plugin uses Calico’s IP pool resource to control how IP addresses are allocated to pods within the cluster. This is the default plugin used by most Calico installations.By default, Calico uses a single IP pool for the entire Kubernetes pod CIDR, but you can divide the pod CIDR into several pools. You can assign separate IP pools to particular selections of nodes, or to teams, users, or applications within a cluster using namespaces.You can control which pools Calico uses for each pod usingnode selectorsan annotation on the pod’s namespace, oran annotation on the podCalico also supports the host-local IPAM plugin. However, when using the host-local IPAM plugin some Calico features are not available.Calico IPAM blocksIn Calico IPAM, IP pools are subdivided into blocks – smaller chunks that are associated with a particular node in the cluster. Each node in the cluster can have one or more blocks associated with it. Calico will automatically create and destroy blocks as needed as the number of nodes and pods in the cluster grows or shrinks.Blocks allow Calico to efficiently aggregate addresses assigned to pods on the same node, reducing the size of the routing table. By default Calico will try to allocate IP addresses from within an associated block, creating a new block if necessary. Calico can also assign addresses to pods on a node that are not within a block associated with that node. This allows for IP allocations independent of the node on which a pod is launched.By default, Calico creates blocks with room for 64 addresses (a /26), but you can control block sizes for each IP pool.Host-local IPAMThe host-local plugin is a simple IP address management plugin. It uses predetermined CIDRs statically allocated to each node in order to choose addresses for pods. Once set, the CIDR for a node cannot be modified. Pods can be assigned addresses only from within the CIDR allocated to the node.Calico can use the host-local IPAM plugin, using the Node.Spec.PodCIDR field in the Kubernetes API to determine the CIDR to use for each node. However, per-node, per-pod, and per-namespace IP allocation features are not available using the host-local plugin.# https://github.com/projectcalico/libcalico-go/blob/release-v3.9/lib/ipam/ipam.go//这里参考Calico 3.9版本代码,其中,CNI插件的流程就省去了,实际在CNI的ipam插件中会调用libcalico-go的相关代码,主要代码在lib/ipam/ipam.go,由于我们线上是默认的配置,也就是不会定义特殊的IP分配策略,因此主要逻辑集中在AutoAssign(ctx context.Context, args AutoAssignArgs)这个接口,而具体实现在autoAssign函数中://func (c ipamClient) autoAssign(ctx context.Context, num int, handleID *string, attrs map[string]string, requestedPools []net.IPNet, version int, host string, maxNumBlocks int) ([]net.IPNet, error) { ... // 根据当前的节点获取可用的所有IP池 pools, allPools, err := c.determinePools(requestedPools, version, *v3n) if err != nil { return nil, err } // 没有可用的池子就直接返回了 if len(pools) == 0 { return nil, fmt.Errorf("no configured Calico pools for node %v", host) } ... ips := []net.IPNet{} newIPs := []net.IPNet{} // Record how many blocks we own so we can check against the limit later. numBlocksOwned := len(affBlocks) for len(ips) < num { // 所有的可用Block已经尝试完了 if len(affBlocks) == 0 { logCtx.Infof("Ran out of existing affine blocks for host") break } // 选取当前Block列表第一个Block作为当前Block cidr := affBlocks[0] // 把第一个Block去除 affBlocks = affBlocks[1:] for i := 0; i < datastoreRetries; i++ { ... // 尝试从当前的Block里分配一个可用的IP newIPs, err = c.assignFromExistingBlock(ctx, b, num, handleID, attrs, host, true) if err != nil { if _, ok := err.(cerrors.ErrorResourceUpdateConflict); ok { logCtx.WithError(err).Debug("CAS error assigning from affine block - retry") continue } logCtx.WithError(err).Warn("Couldn't assign from affine block, try next one") break } // 成功则添加到IP列表 ips = append(ips, newIPs...) break } logCtx.Infof("Block '%s' provided addresses: %v", cidr.String(), newIPs) } // 获取 Calico IPAM 的配置 // Calico IPAM 有两个全局配置项目(二者之中只能有一个为 true): // StrictAffinity: 严格的一个 host 对应一个地址块,如果地址块耗尽不再分配新的地址 // AutoAllocateBlocks: 自动分配地址块,如果基于 host affine 的地址块耗尽,将分配新的地址块 // 这部分配置没有对外暴露,只能通过人工配置对应 etcd key 值或者编程调用相关接口来进行配置 // 相关讨论可参考 issue: https://github.com/projectcalico/calico/issues/1577 // 直接设置 etcd key:/calico/ipam/v2/config/ => "{\"strict_affinity\":true}" config, err := c.GetIPAMConfig(ctx) if err != nil { return ips, err } // 如果自动分配Block选项打开并且当前IP还不够 if config.AutoAllocateBlocks == true { rem := num - len(ips) retries := datastoreRetries for rem > 0 && retries > 0 { ... // 先看看还有没有没有被绑定到节点的Block subnet, err := c.blockReaderWriter.findUnclaimedBlock(ctx, host, version, pools, *config) if err != nil { if _, ok := err.(noFreeBlocksError); ok { // 没有就中断了 logCtx.Info("No free blocks available for allocation") break } log.WithError(err).Error("Failed to find an unclaimed block") return ips, err } logCtx := log.WithFields(log.Fields{"host": host, "subnet": subnet}) logCtx.Info("Found unclaimed block") for i := 0; i < datastoreRetries; i++ { // 有的话,就绑定到当前节点 pa, err := c.blockReaderWriter.getPendingAffinity(ctx, host, *subnet) if err != nil { if _, ok := err.(cerrors.ErrorResourceUpdateConflict); ok { logCtx.WithError(err).Debug("CAS error claiming pending affinity, retry") continue } logCtx.WithError(err).Errorf("Error claiming pending affinity") return ips, err } // 新绑定了Block,尝试在新Block里分配IP b, err := c.getBlockFromAffinity(ctx, pa) ... newIPs, err := c.assignFromExistingBlock(ctx, b, rem, handleID, attrs, host, config.StrictAffinity) ... // 分配成功 ips = append(ips, newIPs...) rem = num - len(ips) break } } if retries == 0 { return ips, errors.New("Max retries hit - excessive concurrent IPAM requests") } } // 如果IP分配还不够(也就是说已绑定的Block里的IP都分完了,并且也没有没有可绑定的新IPBlock了)并且IP还没分完, // 并且StrictAffinity为false rem := num - len(ips) if config.StrictAffinity != true && rem != 0 { logCtx.Infof("Attempting to assign %d more addresses from non-affine blocks", rem) // Iterate over pools and assign addresses until we either run out of pools, // or the request has been satisfied. logCtx.Info("Looking for blocks with free IP addresses") for _, p := range pools { // 在所有的Pool里,随机选一个Block,在这个Block里找可用的IP地址 newBlock := randomBlockGenerator(p, host) for rem > 0 { // Grab a new random block. blockCIDR := newBlock() if blockCIDR == nil { logCtx.Warningf("All addresses exhausted in pool %s", p.Spec.CIDR) break } for i := 0; i < datastoreRetries; i++ { b, err := c.blockReaderWriter.queryBlock(ctx, *blockCIDR, "") ... newIPs, err := c.assignFromExistingBlock(ctx, b, rem, handleID, attrs, host, false) ... // 分配成功 ips = append(ips, newIPs...) rem = num - len(ips) break } } } } // 最后,如果执行到这里,意味着根据配置,无法再分配IP或者干脆全局都没有IP可用了 logCtx.Infof("Auto-assigned %d out of %d IPv%ds: %v", len(ips), num, version, ips) return ips, nil}
从上面的代码可以看到,Calico分配IP的逻辑为:如果节点有已绑定的IP Block,则从这些IP Block中分配IP如果第1步失败(没有已绑定的IP Block,或者这些绑定的Block里IP耗尽),判断AutoAllocateBlocks为true,则寻找一个没有被绑定的IP Block,并绑定到当前节点,再执行分配逻辑如果第2步失败(AutoAllocateBlocks为false或者没有空闲的IP Block),判断StrictAffinity为false,则从所有IP Blocks中寻找未使用的IP经历前1-3步依然没有分配好IP,则失败阅读了上面的代码,则可以知道在默认配置(StrictAffinity: true, AutoAllocateBlocks: false)下,当节点已有IP Block中没有空闲IP并且也没有空闲IP Block时,就会发生之前所说的情况,而恰好Calico在利用BIRD进行BGP路由广播时,针对每个已绑定的IP Block会设置blackhole路由,从而会导致Pod IP无法访问的问题。根据上面的情况,目前看我们当前使用Calico还是有些问题的,特别是对当前IPPool的处理上。一方面也是Calico的实现并不是特别好,比如这个Issue(https://github.com/projectcalico/calico/issues/2713)里提到的,当前针对节点的IP Block绑定,只有自动绑定的功能,但是没有自动解绑定的功能,解绑只有在删除Calico Node对象的时候才会发生,这会引发一个问题,就是说如果集群中节点有变化了,比如某台机器下线,并有新的节点上线做替换。那如果不手动操作Calico删除对应Node,就会导致之前的IP Block不被释放,也就一直无法没绑定到其他节点。另一方面也是我们的使用问题,没有及时跟进Calico的更新,线上版本相对版本旧一点,导致在分配Block的时候,只能固定以/26的BlockSize,也就是说一个IPBlock包含64个IP,而目前每台节点的Pod数量限制是默认的110,那么在使用2个Block也就是128个IP的时候就会出现比较大的浪费现象。这个问题在Allow the blockSize to be configured for Calico IPAM(https://github.com/projectcalico/libcalico-go/pull/931)中已经得到改进,目标版本v3.3.0。因此,在高于v3.3.0版本的Calico中可以自定义BlockSize,比如定义为30,也就是一个Block 4个IP,这样可以比较好的提升IP的利用率,当然这样带来的问题是对外广播的路由数量的增加,所以需要权衡,找到一个合适的BlockSize。
Assign IP Addresses Based On Topology
# Assign blocks of IP addresses from an IP pool for different topological areas.If you have workloads in different regions, zones, or rack, you may want them to get IP addresses from the same IP pool. This strategy is useful for reducing the number of routes that are required in the network, or to meet requirements imposed by an external firewall device or policy. Calico makes it easy to do this using an IP pool resource with node labels and node selectors.# Concepts# IP address assignmentTopology-based IP address assignment requires addresses to be per-host (node). As such, Kubernetes annotations cannot be used because annotations are only per-namespace and per-pod. And although you can configure IP addresses for nodes in the CNI configuration, you are making changes within the host’s file system. The best option is to use node-selection IP address assignment using IP pools.# Node-selection IP address managementNode selection-based IP address assignment is exactly what it sounds like: node labels are set, and Calico uses node selectors to decide which IP pools to use when assigning IP addresses to the node.# Best practiceNodes only assign workload addresses from IP pools which select them. To avoid having a workload not get an IP and fail to start, it is important to ensure that all nodes are selected by at least one IP pool.# TutorialIn this tutorial, we create a cluster with four nodes across two racks (two nodes/rack). ------------------- | router | ------------------- | |--------------- ---------------| rack-0 | | rack-1 |--------------- ---------------| kube-node-0 | | kube-node-2 |- - - - - - - - - - - - - - - -| kube-node-1 | | kube-node-3 |- - - - - - - - - - - - - - - -Using the pod IP range 192.168.0.0/16, we target the following setup: reserve the 192.168.0.0/24 and 192.168.1.0/24 pools for rack-0, rack-1. Let’s get started.By installing Calico without setting the default IP pool to match, running calicoctl get ippool -o wide shows that Calico created its default IP pool of 192.168.0.0/16:NAME CIDR NAT IPIPMODE DISABLED SELECTORdefault-ipv4-ippool 192.168.0.0/16 true Always false all()#1.Delete the default IP pool.Since the default-ipv4-ippool IP pool resource already exists and accounts for the entire /16 block, we will have to delete this first:calicoctl delete ippools default-ipv4-ippool#2.Label the nodes.To assign IP pools to specific nodes, these nodes must be labelled using kubectl label.kubectl label nodes kube-node-0 rack=0kubectl label nodes kube-node-1 rack=0kubectl label nodes kube-node-2 rack=1kubectl label nodes kube-node-3 rack=1#3.Create an IP pool for each rack.calicoctl create -f -<<EOFapiVersion: projectcalico.org/v3kind: IPPoolmetadata: name: rack-0-ippoolspec: cidr: 192.168.0.0/24 ipipMode: Always natOutgoing: true nodeSelector: rack == "0"EOFcalicoctl create -f -<<EOFapiVersion: projectcalico.org/v3kind: IPPoolmetadata: name: rack-1-ippoolspec: cidr: 192.168.1.0/24 ipipMode: Always natOutgoing: true nodeSelector: rack == "1"EOFWe should now have two enabled IP pools, which we can see when running calicoctl get ippool -o wide:NAME CIDR NAT IPIPMODE DISABLED SELECTORrack-0-ippool 192.168.0.0/24 true Always false rack == "0"rack-1-ippool 192.168.1.0/24 true Always false rack == "1"#4.Verify that the IP pool node selectors are being respected.We will create an nginx deployment with five replicas to get a workload running on each node.kubectl run nginx --image nginx --replicas 5Check that the new workloads now have an address in the proper IP pool allocated for the rack that the node is on with kubectl get pods -owide.NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-5c7588df-prx4z 1/1 Running 0 6m3s 192.168.0.64 kube-node-0 <none> <none>nginx-5c7588df-s7qw6 1/1 Running 0 6m7s 192.168.0.129 kube-node-1 <none> <none>nginx-5c7588df-w7r7g 1/1 Running 0 6m3s 192.168.1.65 kube-node-2 <none> <none>nginx-5c7588df-62lnf 1/1 Running 0 6m3s 192.168.1.1 kube-node-3 <none> <none>nginx-5c7588df-pnsvv 1/1 Running 0 6m3s 192.168.1.64 kube-node-2 <none> <none>The grouping of IP addresses assigned to the workloads differ based on what node that they were scheduled to. Additionally, the assigned address for each workload falls within the respective IP pool that selects the rack that they run on.
Use a specific IP address with a pod
# Before you begin…# Verify that you are using Calico IPAM.To verify, ssh to one of your Kubernetes nodes and view the CNI configuration.cat /etc/cni/net.d/10-calico.conflistLook for the “type” entry: "ipam": { "type": "calico-ipam" }, ## 给pod指定一个特定的ip地址:#1.创建新的ippool:[root@ovs ~]# cat net.yaml apiVersion: projectcalico.org/v3kind: IPPoolmetadata: name: external-poolspec: cidr: 172.16.0.0/26 blockSize: 29 ipipMode: Always natOutgoing: true#2.指定IP地址:[root@ovs ~]# cat calico_static.yaml apiVersion: v1kind: Podmetadata: name: calico-static-pod labels: app: myapp annotations: cni.projectcalico.org/ipAddrs: "[\"172.16.0.2\"]"spec: containers: - name: static-container image: burlyluo/nettoolbox[root@ovs ~]# # show output:[root@k8s-1 ~]# calicoctl get ippool -o wide NAME CIDR NAT IPIPMODE VXLANMODE DISABLED SELECTOR default-ipv4-ippool 10.244.0.0/16 true Always Never false all() external-pool 172.16.0.0/26 true Always Never false all() [root@k8s-1 ~]# # 地址信息:[root@k8s-1 ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATEScalico-static-pod 1/1 Running 0 4m22s 172.16.0.2 k8s-2 <none> <none>[root@k8s-1 ~]# # Deploy DEMO:apiVersion: apps/v1 kind: Deploymentmetadata: name: static-ipspec: selector: matchLabels: app: nginx replicas: 1 # tells deployment to run 1 pods matching the template template: metadata: labels: app: nginx annotations: "cni.projectcalico.org/ipAddrs": "[\"172.16.0.5\"]" spec: containers: - name: nginx image: burlyluo/nettoolbox## Note the use of the escaped \" for the inner double quotes around the addresses.# The address must be within a configured Calico IP pool and not currently in use. The annotation must be present when the pod is created; adding it later has no effect.# Note that currently only a single IP address is supported per-pod using this annotation
Migrate from one IP pool to another
# Step 1: Add a new IP poolWe add a new IPPool with the CIDR range, 10.0.0.0/16.apiVersion: projectcalico.org/v3kind: IPPoolmetadata: name: new-poolspec: cidr: 10.0.0.0/16 ipipMode: Always natOutgoing: true# 查看系统上的ippool信息:[root@k8s-1 ~]# calicoctl get ippool -o wide NAME CIDR NAT IPIPMODE VXLANMODE DISABLED SELECTOR default-ipv4-ippool 10.244.0.0/16 true Always Never false all() external-pool 172.16.0.0/26 true Always Never false all() new-pool 10.0.0.0/16 true Always Never false all() [root@k8s-1 ~]# # 2.Disable the old IP pool: calicoctl get ippool -o yaml > pool.yaml Disable this IP pool by setting: disabled: true [root@k8s-1 ~]# cat pool.yaml apiVersion: projectcalico.org/v3items:- apiVersion: projectcalico.org/v3 kind: IPPool metadata: creationTimestamp: "2021-04-04T04:55:21Z" name: default-ipv4-ippool resourceVersion: "127281" uid: ce563f34-8423-4b39-9653-d98cd77bad8a spec: blockSize: 26 cidr: 10.244.0.0/16 disabled: true ipipMode: Always natOutgoing: true nodeSelector: all() vxlanMode: Never disabled: true # disable this ippool- apiVersion: projectcalico.org/v3 kind: IPPool metadata: creationTimestamp: "2021-08-19T03:05:39Z" name: external-pool resourceVersion: "127282" uid: 9a8de79a-90af-4a46-a4b7-0649ba5b2453 spec: blockSize: 29 cidr: 172.16.0.0/26 ipipMode: Always natOutgoing: true nodeSelector: all() vxlanMode: Never disabled: true # disable this ippool- apiVersion: projectcalico.org/v3 kind: IPPool metadata: creationTimestamp: "2021-08-20T04:53:15Z" name: new-pool resourceVersion: "127286" uid: adddcb37-e7c9-4db4-b25b-d78795f9619f spec: blockSize: 26 cidr: 10.0.0.0/16 ipipMode: Always natOutgoing: true nodeSelector: all() vxlanMode: Neverkind: IPPoolListmetadata: resourceVersion: "127644"#3.Step 3: Delete pods from the old IP pool[root@ovs ~]# kubectl get pods -o wide -wNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATEScni-cgchc 1/1 Terminating 0 3m38s 172.16.0.17 k8s-1 <none> <none>cni-mm9pd 1/1 Terminating 1 137d 10.244.200.194 k8s-2 <none> <none>myapp-pod 1/1 Running 1 90m 172.16.0.2 k8s-2 <none> <none>static-ip-5b7f96bbfd-sztpq 1/1 Running 0 76m 172.16.0.5 k8s-2 <none> <none>cni-cgchc 1/1 Terminating 0 3m50s 172.16.0.17 k8s-1 <none> <none>cni-mm9pd 1/1 Terminating 1 137d 10.244.200.194 k8s-2 <none> <none>cni-cgchc 0/1 Terminating 0 3m51s <none> k8s-1 <none> <none>cni-mm9pd 0/1 Terminating 1 137d <none> k8s-2 <none> <none>cni-cgchc 0/1 Terminating 0 3m52s <none> k8s-1 <none> <none>cni-cgchc 0/1 Terminating 0 3m52s <none> k8s-1 <none> <none>cni-wpg5g 0/1 Pending 0 0s <none> <none> <none> <none>cni-wpg5g 0/1 Pending 0 0s <none> k8s-1 <none> <none>cni-wpg5g 0/1 ContainerCreating 0 0s <none> k8s-1 <none> <none>cni-wpg5g 0/1 ContainerCreating 0 1s <none> k8s-1 <none> <none>cni-mm9pd 0/1 Terminating 1 137d <none> k8s-2 <none> <none>cni-mm9pd 0/1 Terminating 1 137d <none> k8s-2 <none> <none>cni-zc2jl 0/1 Pending 0 0s <none> <none> <none> <none>cni-zc2jl 0/1 Pending 0 0s <none> k8s-2 <none> <none>cni-zc2jl 0/1 ContainerCreating 0 0s <none> k8s-2 <none> <none>cni-zc2jl 0/1 ContainerCreating 0 0s <none> k8s-2 <none> <none>cni-wpg5g 1/1 Running 0 16s 10.0.231.193 k8s-1 <none> <none>cni-zc2jl 1/1 Running 0 16s 10.0.200.193 k8s-2 <none> <none># Step4:Delete the old ippool: calicoctl delete pool default-ipv4-ippool
Specific IPPOOL
ConceptsKubernetes pod CIDRThe Kubernetes pod CIDR is the range of IPs Kubernetes expects pod IPs to be assigned from. It is defined for the entire cluster and is used by various Kubernetes components to determine whether an IP belongs to a pod. For example, kube-proxy treats traffic differently if that traffic is from a pod than if it is not. All pod IPs must be in the CIDR range for Kubernetes to function correctly.IP PoolIP pools are ranges of IP addresses from which Calico assigns pod IPs. By default, Calico creates an IP pool for the entire Kubernetes pod CIDR, but you can change this to break the pod CIDR up into several pools. You can control which pool Calico uses for each pod using node selectors, or annotations on the pod or the pod’s namespace.Before you begin…The features in this How to guide require:Calico IPAMIf you are not sure, ssh to one of your Kubernetes nodes and examine the CNI configuration.cat /etc/cni/net.d/10-calico.conflistLook for the entry: "ipam": { "type": "calico-ipam" },If it is present, you are using the Calico IPAM. If the IPAM is not Calico, or the 10-calico.conflist file does not exist, you cannot use these features in your cluster.Also, cluster administrators must have configured IP pools to define the valid IP ranges to use for allocating pod IP addresses.How toRestrict a pod to use an IP address rangeAnnotate the pod with key cni.projectcalico.org/ipv4pools and/or cni.projectcalico.org/ipv6pools and value set to a list of IP pool names, enclosed in brackets. For example:cni.projectcalico.org/ipv4pools: '["pool-1", "pool-2"]'Note the use of the escaped " for the inner double quotes around the pool names.Restrict all pods within a namespace to use an IP address rangeAnnotate the namespace with key cni.projectcalico.org/ipv4pools and/or cni.projectcalico.org/ipv6pools and value set to a list of IP pool names, enclosed in brackets. For example:cni.projectcalico.org/ipv4pools: '["pool-1", "pool-2"]'Note the use of the escaped \" for the inner double quotes around the pool names.If both the pod and the pod’s namespace have the annotation, the pod annotation takes precedence.The annotation must be present at the time the pod is created. Adding it to an existing pod has no effect.apiVersion: projectcalico.org/v3kind: IPPoolmetadata: name: new-pool-nsspec: cidr: 11.0.0.0/16 ipipMode: Always natOutgoing: true###############################################################################多pod固定IP池需要创建额外IP池(除了默认IP池)。利用注解cni.projectcalico.org/ipv4pools。示例yaml配置如下[root@k8s-master3 ~]# cat ippool1.yaml apiVersion: projectcalico.org/v3kind: IPPoolmetadata: name: new-pool1spec: blockSize: 31 cidr: 10.21.0.0/31 ipipMode: Never natOutgoing: true[root@k8s-master3 ~]# calicoctl create -f ippool1.yaml Successfully created 1 'IPPool' resource(s)[root@k8s-master3 ~]# cat ippool2.yaml apiVersion: projectcalico.org/v3kind: IPPoolmetadata: name: new-pool2spec: blockSize: 31 cidr: 10.21.0.2/31 ipipMode: Never natOutgoing: true[root@k8s-master3 ~]# calicoctl create -f ippool2.yaml Successfully created 1 'IPPool' resource(s)[root@k8s-master3 ~]# [root@k8s-master3 ~]# [root@k8s-master3 ~]# calicoctl get ippoolNAME CIDR SELECTOR default-ipv4-ippool 10.20.0.0/16 all() new-pool1 10.21.0.0/31 all() new-pool2 10.21.0.2/31 all() root@k8s-master3 ~]# cat nginx.yaml apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2kind: Deploymentmetadata: name: nginx-deploymentspec: selector: matchLabels: app: nginx replicas: 4 # tells deployment to run 2 pods matching the template template: metadata: labels: app: nginx annotations: "cni.projectcalico.org/ipv4pools": "[\"new-pool1\",\"new-pool2\"]" spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80[root@k8s-master3 ~]# kubectl create -f nginx.yaml deployment.apps/nginx-deployment created[root@k8s-master3 ~]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-deployment-f49447c5d-4k4px 1/1 Running 0 11s 10.21.0.0 k8s-master4 <none> <none>nginx-deployment-f49447c5d-5sbrx 1/1 Running 0 11s 10.21.0.2 k8s-master4 <none> <none>nginx-deployment-f49447c5d-flfb8 1/1 Running 0 11s 10.21.0.3 k8s-master4 <none> <none>nginx-deployment-f49447c5d-q4945 1/1 Running 0 11s 10.21.0.1 k8s-master4 <none> <none># 指定namespace:kubectl get ns [root@k8s-1 ~]# kubectl edit ns cc # Please edit the object below. Lines beginning with a '#' will be ignored,# and an empty file will abort the edit. If an error occurs while saving this file will be# reopened with the relevant failures.#apiVersion: v1kind: Namespacemetadata: annotations: cni.projectcalico.org/ipv4pools: '["new-pool-ns"]' ### Add this annotations creationTimestamp: "2021-08-20T05:20:44Z" name: cc resourceVersion: "130057" uid: bb8ed59d-04c8-4449-864b-ced54e64bb59spec: finalizers: - kubernetesstatus: phase: Active~创建pod:#kubectl run ccc --image=burlyluo/nettoolbox -ncc# ippool 信息:[root@k8s-1 ~]# calicoctl get ippool -o wide NAME CIDR NAT IPIPMODE VXLANMODE DISABLED SELECTOR default-ipv4-ippool 10.244.0.0/16 true Always Never true all() external-pool 172.16.0.0/26 true Always Never true all() new-pool 10.0.0.0/16 true Always Never false all() new-pool-ns 11.0.0.0/16 true Always Never false all() [root@k8s-1 ~]# [root@k8s-1 ~]# kubectl get pods -o wide -nccNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESccc 1/1 Running 0 57m 11.0.200.193 k8s-2 <none> <none>[root@k8s-1 ~]#