k8s修改IP地址池

平台安装默认K8S集群,地址池为:172.20.0.0/16,本文将按照新的网络规划,将地址池网络段修改为:172.17.0.0/24
1. 修改k8s相关配置
  • kube-apiserver: --pod-network-cidr
  • kube-proxy: --cluster-cidr
  • kube-controller-manager: --cluster-cidr

2. 修改calico IP pool

配置calico连接etcd:
calicoctl get ippool -o wide --config /etc/calico/calicoctl.cfg
返回结果:
apiVersion: projectcalico.org/v3
kind: CalicoAPIConfig
metadata:
spec:
  etcdEndpoints: https://192.168.0.5:2379,https://192.168.0.6:2379,https://192.168.0.7:2379
  etcdKeyFile: /etc/calico/certs/key.pem
  etcdCertFile: /etc/calico/certs/cert.crt
  etcdCACertFile: /etc/calico/certs/ca_cert.crt

2.1 增加新的 ip pool(k8s组件中设置的--pod-network-cidr --cluster-cidr):

calicoctl create -f -<<EOF
apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  name: new-pool
spec:
  cidr: 172.17.0.0/24
  ipipMode: Always
  natOutgoing: true
EOF

 2.2 禁用旧的 IP pool

导出旧的IP pool的配置yaml文件
calicoctl get ippool -o yaml --config /etc/calico/calicoctl.cfg > pool.yaml
在旧的ip pool的spec中增加一个字段:disabled: true
apiVersion: projectcalico.org/v3
items:
- apiVersion: projectcalico.org/v3
  kind: IPPool
  metadata:
    creationTimestamp: 2018-12-07T13:17:31Z
    name: default-pool
    resourceVersion: "3807"
    uid: 73fd5867-fa22-11e8-b0e7-5254c80ff311
  spec:
    blockSize: 26
    cidr: 10.233.64.0/18
    ipipMode: Always
    natOutgoing: true
    nodeSelector: all()
    disabled: true   # new add
- apiVersion: projectcalico.org/v3
  kind: IPPool
  metadata:
    creationTimestamp: 2019-10-20T07:45:14Z
    name: new-pool
    resourceVersion: "704829464"
    uid: 8d40e24e-f30d-11e9-a071-5254c80ff311
  spec:
    blockSize: 26
    cidr: 172.17.0.0/24
    ipipMode: Always
    natOutgoing: true
    nodeSelector: all()
    vxlanMode: Never
kind: IPPoolList
metadata:
  resourceVersion: "704836168"
部署使新配置生效
calicoctl apply -f pool.yaml
calicoctl get ippool -o wide

 查看

 calicoctl get wep --all-namespaces
 删除旧的IP池
calicoctl delete pool default-ipv4-ippool

3. 重置k8s集群节点 podCIDR

获取节点yaml文件
# 获取节点yaml文件
kubectl get node node名称 -o yaml > node1.yaml
修改yaml文件中spec.podCIDR字段,按照规划的CIDR地址块进行修改
# 修改yaml文件中spec.podCIDR字段
# 按照规划的CIDR地址块进行修改
spec:
  podCIDR: 172.17.0.0/24

 修改yaml文件后,由于node对象不能直接覆盖创建,需要先删除节点,再重新创建

# 修改yaml文件后,由于node对象不能直接覆盖创建,需要先删除节点,再重新创建
kubectl delete nodes node名称
kubectl create -f node1.yaml

 

posted @ 2023-05-09 10:08  王竹笙  阅读(456)  评论(0编辑  收藏  举报