K8S集群固定Pod IP

原理

主要利用 Calico 组件的两个kubernetes注解

(1)cni.projectcalico.org/ipAddrs
(2)cni.projectcalico.org/ipAddrs

单个Pod固定IP

利用注解 cni.projectcalico.org/ipAddrs
示例 Yaml如下

apiVersion: apps/v1
kind: Deployment
metadata:
  name: front-end
  labels:
    app: front-end
spec:
  replicas: 1
  selector:
    matchLabels:
      app: front-end
  template:
    metadata:
      labels:
        app: front-end
      annotations:
        "cni.projectcalico.org/ipAddrs": "[\"10.233.126.33\"]"
    spec:
      nodeSelector:
        app: demo3
      containers:
      - name: nginx
        image: autoregistry.rd.tp-link.net/auto/nginx:1.18
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: 100m
          limits:
            cpu: 500m
        ports:
        - name: http
          containerPort: 80
          protocol: TCP

多个Pod固定使用IP池

利用注解 cni.projectcalico.org/ipv4pools
需要创建额外IP池(除了默认的default-pool)
查看默认的IP池

[root@iamdemo1 test]# calicoctl get ippool

image

apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  name: new-pool2
spec:
  blockSize: 31
  cidr: 10.21.0.2/31
  ipipMode: Never
  natOutgoing: true
apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  name: new-pool1
spec:
  blockSize: 31 ##指定IP地址块大小
  cidr: 10.21.0.0/31  ##指定了IP地址池的CIDR地址范围为10.21.0.0/31
  ipipMode: Never ##不使用IPIP隧道模式
  natOutgoing: true ## 出站NAT(网络地址转换)为启用状态,出站会将私有网络的ip转为公共ip地址

deployment定义中需要定义annotations

annotations:
        "cni.projectcalico.org/ipv4pools": "[\"new-pool1\",\"new-pool2\"]"

在固定单个Pod IP实践中
删除掉原来的Pod发现 IP仍为定义的IP没有变化
image

posted @ 2024-02-23 15:28  付同學  阅读(138)  评论(0编辑  收藏  举报