Kubernetes使用calico固定pod ip

colico官网参考
主要涉及两个注解:
1、cni.projectcalico.org/ipAddrs #单个pod ip固定
2、cni.projectcalico.org/ipv4pools #从指定ippool获取

一、检查Calico IPAM

如果 IPAM 设置为其他值,或者 10-calico.conflist 文件不存在,则无法在集群中使用这些功能
cat /etc/cni/net.d/10-calico.conflist

"ipam": {
              "type": "calico-ipam"
          },

二、设置pod单个固定ip

注解 cni.projectcalico.org/ipAddrs,注意双引号和转义 不要少

apiVersion: apps/v1 
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1 
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        "cni.projectcalico.org/ipAddrs": "["10.244.126.10"]"  #表示固定单个pod ip
    spec:
      containers:
      - name: nginx
        image: nginx

三、从ippool获取ip

calico官网ippoll详解
ippool1.yaml

apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  name: ippool-1
spec:
  blockSize: 20
  cidr: 10.244.1.0/32
  ipipMode: Always
  natOutgoing: true

ippool2.yaml

apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  name: ippool-2
spec:
  blockSize: 20
  cidr: 10.244.2.0/32
  ipipMode: Always
  natOutgoing: true

calicoctl get ippool -o wide #查看ippool

apiVersion: apps/v1 
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        "cni.projectcalico.org/ipv4pools": "["ippool-1","ippool-2"]"
    spec:
      containers:
      - name: nginx
        image: nginx

kubectl get pods -o wide #查看IP分配情况