1、首先查看calico的CIDR地址范围
[root@nccztsjb-node-17 ~]# calicoctl get ippool NAME CIDR SELECTOR default-pool 172.23.0.0/16 all()
2、然后呢,在这个地址范围内,给pod选择一个固定的IP地址
比如:172.23.45.27
通过在pod中加入annotation来实现:
annotations: "cni.projectcalico.org/ipAddrs": "[\"172.23.45.27\"]"
使用如下的yaml创建一个pod
apiVersion: apps/v1 kind: Deployment metadata: name: pod-static-ip labels: app: pod-static-ip spec: selector: matchLabels: app: pod-static-ip template: metadata: labels: app: pod-static-ip annotations: "cni.projectcalico.org/ipAddrs": "[\"172.23.45.27\"]" spec: containers: - name: pod-static-ip image: 172.20.58.152/middleware/nginx:1.21.4
查看创建的pod的ip
[root@nccztsjb-node-01 ~]# kubectl get pod -o wide | grep static pod-static-ip-75779bcc95-hxj7f 1/1 Running 0 10s 172.23.45.27 node177 <none> <none> [root@nccztsjb-node-01 ~]#
确实是我们固定的IP地址172.23.45.27
3、重启一下,看pod的ip地址是否会变化
[root@nccztsjb-node-01 ~]# kubectl delete pod pod-static-ip-75779bcc95-hxj7f pod "pod-static-ip-75779bcc95-hxj7f" deleted [root@nccztsjb-node-01 ~]# [root@nccztsjb-node-01 ~]# kubectl get pod -o wide | grep static pod-static-ip-75779bcc95-7996b 1/1 Running 0 3s 172.23.45.27 node177 <none> <none> [root@nccztsjb-node-01 ~]#
OK,重启pod之后,ip地址,还是没变,目的达成。
标签:插件,ip,static,nccztsjb,IP地址,pod,root,172.23 From: https://www.cnblogs.com/chuanzhang053/p/17584488.html