calico ipam使用

2023-11-01 16:36
文章标签 使用 calico ipam

本文主要是介绍calico ipam使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

calico ipam使用
前面的文章pod获取ip地址的过程中提到过calico使用的IP地址的管理模块是其自己开发的模块calico-ipam,本篇文章来讲述下其具体用法。

一、环境信息

  • 版本信息
本环境使用版本是k8s 1.25.3
[root@node1 ~]# kubectl get node 
NAME    STATUS   ROLES                  AGE    VERSION
node1   Ready    control-plane,worker   206d   v1.25.3
node2   Ready    worker                 206d   v1.25.3
node3   Ready    worker                 206d   v1.25.3###集群已经部署了calico cni
[root@node1 ~]# kubectl get po -n kube-system   | grep calico 
calico-kube-controllers-75c594996d-x49mw   1/1     Running   5 (12d ago)    206d
calico-node-htq5b                          1/1     Running   1 (12d ago)    206d
calico-node-x6xwl                          1/1     Running   1 (12d ago)    206d
calico-node-xdx46                          1/1     Running   1 (12d ago)    206d
[root@node1 ~]# ####查看calico的默认配置
[root@node1 ~]# cat /etc/cni/net.d/10-calico.conflist 
{"name": "k8s-pod-network","cniVersion": "0.3.1","plugins": [{"type": "calico",   ###插件类型"log_level": "info","log_file_path": "/var/log/calico/cni/cni.log","datastore_type": "kubernetes","nodename": "node1","mtu": 0,"ipam": {"type": "calico-ipam"    ####ipam类型是calico-ipam},"policy": {"type": "k8s"},"kubernetes": {"kubeconfig": "/etc/cni/net.d/calico-kubeconfig"}},{"type": "portmap","snat": true,"capabilities": {"portMappings": true}},{"type": "bandwidth","capabilities": {"bandwidth": true}}]
}[root@node1 ~]# 
  • 网络模式
###查看目前使用的IP地址池
[root@node1 ~]# calicoctl  get ippool -o wide 
NAME                  CIDR             NAT    IPIPMODE   VXLANMODE   DISABLED   DISABLEBGPEXPORT   SELECTOR   
default-ipv4-ippool   10.233.64.0/18   true   Always     Never       false      false              all()      ###查看网路详细信息
[root@node1 ~]# calicoctl  get ippool default-ipv4-ippool -o yaml 
apiVersion: projectcalico.org/v3
kind: IPPool
metadata:creationTimestamp: "2023-04-08T17:18:59Z"name: default-ipv4-ippoolresourceVersion: "647"uid: 7b9d84e1-ac69-4660-b298-c52e2267ab08
spec:allowedUses:- Workload- TunnelblockSize: 24   ###每个block的大小是24为掩码cidr: 10.233.64.0/18ipipMode: Always  ###网络模式是ipipnatOutgoing: truenodeSelector: all()  ###所有节点可用vxlanMode: Never

二、环境物理拓扑

每个物理机位于不同的机架上,每个物理节点分配不同的ippool

               -------------------|    router       |-------------------|                 |
---------------   ---------------   ---------------
| rack-1      |   | rack-2      |   | rack-3      |
---------------   ---------------   ---------------
| node-1      |   | node-2      |   | node-3      |
- - - - - - - -   - - - - - - - -   - - - - - - - -

三、为节点分配网络

  • 为node打label
[root@node1 ~]# kubectl label node node1 rack=1
node/node1 labeled
[root@node1 ~]# kubectl label node node2 rack=2
node/node2 labeled
[root@node1 ~]# kubectl label node node3 rack=3
node/node3 labeled
[root@node1 ~]# 
  • 为node创建ippool
1:首先要禁用环境中默认的ippool,因为我环境中有使用默认ippool的pod,不做删除操作
[root@node1 ~]# calicoctl  get ippool -o wide 
NAME                  CIDR             NAT    IPIPMODE   VXLANMODE   DISABLED   DISABLEBGPEXPORT   SELECTOR   
default-ipv4-ippool   10.233.64.0/18   true   Always     Never       false      false              all()  ########################
2:使用patch命令修改disabled=true
[root@node1 ~]# calicoctl patch ipPool default-ipv4-ippool --patch '{"spec":{"disabled": true}}'
Successfully patched 1 'IPPool' resource
[root@node1 ~]# calicoctl  get ippool -o wide
NAME                  CIDR             NAT    IPIPMODE   VXLANMODE   DISABLED   DISABLEBGPEXPORT   SELECTOR   
default-ipv4-ippool   10.233.64.0/18   true   Always     Never       true       false              all()      
[root@node1 ~]# ########################
3:为三个node创建ippool,注意不要和其他网路冲突
[root@node1 ~]# calicoctl create -f -<<EOF
> apiVersion: projectcalico.org/v3
> kind: IPPool
> metadata:
>   name: rack-1-ippool
> spec:
>   cidr: 172.16.1.0/24
>   ipipMode: Always
>   natOutgoing: true
>   nodeSelector: rack == "1"     #####此处标签与之前为node打的label 对应
> EOF
Successfully created 1 'IPPool' resource(s)
[root@node1 ~]# calicoctl create -f -<<EOF
> apiVersion: projectcalico.org/v3
> kind: IPPool
> metadata:
>   name: rack-2-ippool
> spec:
>   cidr: 172.16.2.0/24
>   ipipMode: Always
>   natOutgoing: true
>   nodeSelector: rack == "2"
> EOF
Successfully created 1 'IPPool' resource(s)
[root@node1 ~]# calicoctl create -f -<<EOF
> apiVersion: projectcalico.org/v3
> kind: IPPool
> metadata:
>   name: rack-3-ippool
> spec:
>   cidr: 172.16.3.0/24
>   ipipMode: Always
>   natOutgoing: true
>   nodeSelector: rack == "3"
> EOF
Successfully created 1 'IPPool' resource(s)
[root@node1 ~]# ###########
4:查看创建好的ippool
[root@node1 ~]# calicoctl  get ippool -o wide 
NAME                  CIDR             NAT    IPIPMODE   VXLANMODE   DISABLED   DISABLEBGPEXPORT   SELECTOR      
default-ipv4-ippool   10.233.64.0/18   true   Always     Never       true       false              all()         
rack-1-ippool         172.16.1.0/24    true   Always     Never       false      false              rack == "1"   
rack-2-ippool         172.16.2.0/24    true   Always     Never       false      false              rack == "2"   
rack-3-ippool         172.16.3.0/24    true   Always     Never       false      false              rack == "3" 

四、验证网络

1:编辑yaml文件
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: nginx
spec:replicas: 10selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: docker.io/library/nginx:latestimagePullPolicy: IfNotPresentports:- containerPort: 80#############################
2:启动pod,查看pod获取ip情况
[root@node1 ~]# kubectl apply -f yaml/nginx.yaml 
deployment.apps/nginx created
[root@node1 ~]# kubectl get po -o wide 
NAME                                      READY   STATUS    RESTARTS       AGE    IP             NODE    NOMINATED NODE   READINESS GATES
nginx-5977dc5756-22kfl                    1/1     Running   0              7s     172.16.1.131   node1   <none>           <none>
nginx-5977dc5756-4lvpq                    1/1     Running   0              7s     172.16.3.129   node3   <none>           <none>
nginx-5977dc5756-59jkh                    1/1     Running   0              7s     172.16.1.129   node1   <none>           <none>
nginx-5977dc5756-9lm7p                    1/1     Running   0              7s     172.16.3.132   node3   <none>           <none>
nginx-5977dc5756-jdcqf                    1/1     Running   0              7s     172.16.1.130   node1   <none>           <none>
nginx-5977dc5756-jvwkf                    1/1     Running   0              7s     172.16.2.1     node2   <none>           <none>
nginx-5977dc5756-nq46g                    1/1     Running   0              7s     172.16.2.3     node2   <none>           <none>
nginx-5977dc5756-tsjf7                    1/1     Running   0              7s     172.16.3.131   node3   <none>           <none>
nginx-5977dc5756-xqmwz                    1/1     Running   0              7s     172.16.2.2     node2   <none>           <none>
nginx-5977dc5756-xt648                    1/1     Running   0              7s     172.16.3.130   node3   <none>           <none>
[root@node1 ~]# 
以上可以看到每个pod在对应的节点获取到的ip和ippool对应##############################
3:测试网络联通性,在node1可以ping通其他两个节点的pod ip
[root@node1 ~]# ping 172.16.1.131
PING 172.16.1.131 (172.16.1.131) 56(84) bytes of data.
64 bytes from 172.16.1.131: icmp_seq=1 ttl=64 time=0.306 ms
^C
--- 172.16.1.131 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.306/0.306/0.306/0.000 ms
[root@node1 ~]# ping 172.16.2.1
PING 172.16.2.1 (172.16.2.1) 56(84) bytes of data.
64 bytes from 172.16.2.1: icmp_seq=1 ttl=63 time=1.25 ms
64 bytes from 172.16.2.1: icmp_seq=2 ttl=63 time=0.906 ms
^C
--- 172.16.2.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.906/1.080/1.255/0.177 ms
[root@node1 ~]# ping 172.16.3.131
PING 172.16.3.131 (172.16.3.131) 56(84) bytes of data.
64 bytes from 172.16.3.131: icmp_seq=1 ttl=63 time=2.26 ms
64 bytes from 172.16.3.131: icmp_seq=2 ttl=63 time=1.52 ms
^C
--- 172.16.3.131 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 1.528/1.898/2.269/0.373 ms
[root@node1 ~]# ######################################
注意:Calico IPAM不会将IP地址重新分配给已经运行的pod。若要使用新配置的IP池中的IP地址,需要更新正在运行的pod,需要重建它们。

五、迁移ip到新ippool

1: 使用旧的ip新建pod,以便后续测试
[root@node1 ~]# kubectl apply -f yaml/nginx.yaml 
deployment.apps/nginx created[root@node1 ~]# kubectl get po -o wide 
NAME                                      READY   STATUS    RESTARTS       AGE    IP             NODE    NOMINATED NODE   READINESS GATES
nginx-5977dc5756-8jm8g                    1/1     Running   0              48s    10.233.90.36   node1   <none>           <none>
nginx-5977dc5756-8vz6r                    1/1     Running   0              48s    10.233.96.68   node2   <none>           <none>
nginx-5977dc5756-c6ltc                    1/1     Running   0              48s    10.233.92.61   node3   <none>           <none>
nginx-5977dc5756-gmr27                    1/1     Running   0              48s    10.233.96.69   node2   <none>           <none>
nginx-5977dc5756-h7tz5                    1/1     Running   0              48s    10.233.92.60   node3   <none>           <none>
nginx-5977dc5756-k7jpx                    1/1     Running   0              48s    10.233.92.59   node3   <none>           <none>
nginx-5977dc5756-kzfpm                    1/1     Running   0              48s    10.233.92.62   node3   <none>           <none>
nginx-5977dc5756-nnzxt                    1/1     Running   0              48s    10.233.90.34   node1   <none>           <none>
nginx-5977dc5756-ppcxz                    1/1     Running   0              48s    10.233.90.35   node1   <none>           <none>
nginx-5977dc5756-rk9nk                    1/1     Running   0              48s    10.233.96.70   node2   <none>           <none>###########################
2:新建ippool
apiVersion: projectcalico.org/v3
kind: IPPool
metadata:name: new-ipv4-ippool
spec:allowedUses:- Workload- TunnelblockSize: 24cidr: 172.16.0.0/16ipipMode: AlwaysnatOutgoing: truenodeSelector: all()vxlanMode: Never[root@node1 ~]# calicoctl apply -f  ippool.yaml 
Successfully applied 1 'IPPool' resource(s)
[root@node1 ~]# calicoctl  get ippool -o wide 
NAME                  CIDR             NAT    IPIPMODE   VXLANMODE   DISABLED   DISABLEBGPEXPORT   SELECTOR   
default-ipv4-ippool   10.233.64.0/18   true   Always     Never       false      false              all()      
new-ipv4-ippool       172.16.0.0/16    true   Always     Never       false      false              all() ###############################
3:禁用旧的ippool,不会影响现有 pod 的网络
[root@node1 ~]# calicoctl patch ipPool default-ipv4-ippool --patch '{"spec":{"disabled":  true}}'
Successfully patched 1 'IPPool' resource查看默认的IPPOOL  DISABLED 为 true
[root@node1 ~]# calicoctl  get ippool -o wide 
NAME                  CIDR             NAT    IPIPMODE   VXLANMODE   DISABLED   DISABLEBGPEXPORT   SELECTOR   
default-ipv4-ippool   10.233.64.0/18   true   Always     Never       true       false              all()      
new-ipv4-ippool       172.16.0.0/16    true   Always     Never       false      false              all()      [root@node1 ~]# ###################################
4:重启新建好的pod,看获取ip情况
[root@node1 ~]# kubectl rollout restart  deploy nginx
deployment.apps/nginx restarted
[root@node1 ~]# kubectl get po -owide 
NAME                                      READY   STATUS    RESTARTS       AGE    IP             NODE    NOMINATED NODE   READINESS GATES
nginx-8499ccc976-6q5d8                    1/1     Running   0              8s     172.16.154.1   node1   <none>           <none>
nginx-8499ccc976-8mw42                    1/1     Running   0              8s     172.16.44.1    node2   <none>           <none>
nginx-8499ccc976-9x84p                    1/1     Running   0              8s     172.16.28.2    node3   <none>           <none>
nginx-8499ccc976-f8n28                    1/1     Running   0              8s     172.16.44.2    node2   <none>           <none>
nginx-8499ccc976-fxfft                    1/1     Running   0              6s     172.16.28.3    node3   <none>           <none>
nginx-8499ccc976-jj8hg                    1/1     Running   0              6s     172.16.44.3    node2   <none>           <none>
nginx-8499ccc976-kjf75                    1/1     Running   0              8s     172.16.28.1    node3   <none>           <none>
nginx-8499ccc976-rms74                    1/1     Running   0              6s     172.16.154.2   node1   <none>           <none>
nginx-8499ccc976-trcn8                    1/1     Running   0              5s     172.16.28.4    node3   <none>           <none>
nginx-8499ccc976-z28fw                    1/1     Running   0              5s     172.16.154.3   node1   <none>           <none>
可以看到pod重启后获取到了新ippool的ip################################
5:测试网络连通性
[root@node1 ~]# ping 172.16.44.1
PING 172.16.44.1 (172.16.44.1) 56(84) bytes of data.
64 bytes from 172.16.44.1: icmp_seq=1 ttl=63 time=1.32 ms
^C
--- 172.16.44.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.327/1.327/1.327/0.000 ms
[root@node1 ~]# ping 172.16.28.2
PING 172.16.28.2 (172.16.28.2) 56(84) bytes of data.
64 bytes from 172.16.28.2: icmp_seq=1 ttl=63 time=2.66 ms
64 bytes from 172.16.28.2: icmp_seq=2 ttl=63 time=1.07 ms
^C
--- 172.16.28.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 1.076/1.868/2.660/0.792 ms
[root@node1 ~]# ping 172.16.154.2
PING 172.16.154.2 (172.16.154.2) 56(84) bytes of data.
64 bytes from 172.16.154.2: icmp_seq=1 ttl=64 time=0.263 ms
64 bytes from 172.16.154.2: icmp_seq=2 ttl=64 time=0.125 ms
^C
--- 172.16.154.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.125/0.194/0.263/0.069 ms

这篇关于calico ipam使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/324320

相关文章

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

pdfmake生成pdf的使用

实际项目中有时会有根据填写的表单数据或者其他格式的数据,将数据自动填充到pdf文件中根据固定模板生成pdf文件的需求 文章目录 利用pdfmake生成pdf文件1.下载安装pdfmake第三方包2.封装生成pdf文件的共用配置3.生成pdf文件的文件模板内容4.调用方法生成pdf 利用pdfmake生成pdf文件 1.下载安装pdfmake第三方包 npm i pdfma

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

git使用的说明总结

Git使用说明 下载安装(下载地址) macOS: Git - Downloading macOS Windows: Git - Downloading Windows Linux/Unix: Git (git-scm.com) 创建新仓库 本地创建新仓库:创建新文件夹,进入文件夹目录,执行指令 git init ,用以创建新的git 克隆仓库 执行指令用以创建一个本地仓库的

【北交大信息所AI-Max2】使用方法

BJTU信息所集群AI_MAX2使用方法 使用的前提是预约到相应的算力卡,拥有登录权限的账号密码,一般为导师组共用一个。 有浏览器、ssh工具就可以。 1.新建集群Terminal 浏览器登陆10.126.62.75 (如果是1集群把75改成66) 交互式开发 执行器选Terminal 密码随便设一个(需记住) 工作空间:私有数据、全部文件 加速器选GeForce_RTX_2080_Ti

【Linux 从基础到进阶】Ansible自动化运维工具使用

Ansible自动化运维工具使用 Ansible 是一款开源的自动化运维工具,采用无代理架构(agentless),基于 SSH 连接进行管理,具有简单易用、灵活强大、可扩展性高等特点。它广泛用于服务器管理、应用部署、配置管理等任务。本文将介绍 Ansible 的安装、基本使用方法及一些实际运维场景中的应用,旨在帮助运维人员快速上手并熟练运用 Ansible。 1. Ansible的核心概念