简介
SPEC 规范:IP Address Management (IPAM) Interface
IPAM作用/职责
背景
以dhcp ipam 为例
和 CNI 的关系
To lessen the burden(负担) and make IP management strategy be orthogonal(正交) to the type of CNI plugin, we define a second type of plugin – IP Address Management Plugin (IPAM plugin)
The IPAM plugin must determine the interface IP/subnet, Gateway and Routes and return this information to the “main” plugin to apply. The IPAM plugin may obtain the information via a protocol (e.g. dhcp), data stored on a local filesystem, the “ipam” section of the Network Configuration file or a combination of the above.
几个重要的点
- Like CNI plugins, the IPAM plugins are invoked by running an executable. CNI和ipam 都是可执行文件
- The executable is searched for in a predefined list of paths(说白了就是跟CNI 插件一个地址 默认
/opt/cni/bin
), indicated to the CNI plugin via CNI_PATH - The IPAM Plugin must receive all the same environment variables that were passed in to the CNI plugin. Just like the CNI plugin, IPAM plugins receive the network configuration via stdin. CNI/IPAM通过 stdin 和 stdout 和外界交互, 环境变量也是共享的
- Success must be indicated by a zero return code and the JSON being printed to stdout
接口规范
k8s 提供了对CNI 和 ipam 的代码 skeleton,Package skel provides skeleton code for a CNI/IPAM plugin.In particular, it implements argument parsing and validation.
官方提供的ipam:host-local/static/dhcp,其main 方法都是
func main() {
skel.PluginMain(cmdAdd, cmdCheck, cmdDel, version.All, bv.BuildString("static-ipam"))
}
只需实现cmdAdd, cmdCheck, cmdDel 即可
CNI | IPAM | |
---|---|---|
cmdAdd | Add container to network | 返回一个可用的ip |
cmdCheck | Check container’s networking is as expected | |
cmdDel | Delete container from network | 将传入的ip回收 |
cmdAdd
请求参数model
// github.com/containernetworking/cni/pkg/skel/skel.go
type CmdArgs struct {
ContainerID string
Netns string
IfName string
Args string
Path string
StdinData []byte
}
Args 包括了一些额外的KeyValue,分号分割
IgnoreUnknown=1;
K8S_POD_NAMESPACE=default;
K8S_POD_NAME=fm-barge-backend-stable-5b498b6ff8-6bn2w;
K8S_POD_INFRA_CONTAINER_ID=3f1abf20f3060a67a5e49f75847eb91d2feb6cbe1e76dd52db2242063fb0e178
cmdAdd 的返回
// github.com/containernetworking/cni/pkg/types/current/types.go
type Result struct {
CNIVersion string `json:"cniVersion,omitempty"`
Interfaces []*Interface `json:"interfaces,omitempty"`
IPs []*IPConfig `json:"ips,omitempty"`
Routes []*types.Route `json:"routes,omitempty"`
DNS types.DNS `json:"dns,omitempty"`
}
calico-ipam 支持静态ip
docker中部署服务一个很常见的问题 每次部署,pod的ip 都会发生改变,这对mysql、redis等基础服务就很不友好,因此有必要将ip“固定”。常见的是:使用servie 解决内部连通性, 使用ingress + service 解决外部连通性。但对于一些网络来说,使用service 很不方便,比如macvlan等,性能也很差,换个思路:将一个服务与ip绑定起来 ==> 指定服务所属的ip
Requesting a specific IP address
代码 calico-ipam 实现了pod.yaml 上使用一个annotation 来指定ip,calico-ipam 即可返回指定ip 的功能。
annotations:
"cni.projectcalico.org/ipAddrs": "[\"192.168.0.1\"]"
基本原理是
- 从 CmdArgs.Args拿到K8S_POD_NAME (github.com/projectcalico/cni-plugin/internal/pkg/utils/utils.go/GetIdentifiers)
- 进而查询k8s 集群api 拿到 pod的annotations 信息(github.com/projectcalico/cni-plugin/pkg/k8s/k8s.go/CmdAddK8s)
- 将ip 作为ipam 的返回结果,用于CNI 插件设定容器
笔者依照该思想自己实现了一个topsli/static-ipam