在 arm64 上运行 calico libnetwork-plugin

本文主要指导在arm64(raspberry pi 3b+)上安装部署calico v2/v3 版本,并安装使用 docker calico 插件联通多个节点。

运行环境

环境 版本 备注
硬件 raspberry pi 3b+ -
操作系统 ubuntu server 18.04.4 arm64
docker 19.03 arm64

树莓派系统安装

mac os 下:

# 镜像下载
% wget http://cdimage.ubuntu.com/releases/18.04.4/release/ubuntu-18.04.4-preinstalled-server-arm64+raspi3.img.xz
# 格式化SD卡为 MS-DOS (FAT32)格式,非必须要求
% sudo diskutil eraseDisk FAT32 "UBUNTU" MBRFormat  /dev/disk2
% sudo diskutil unmountDisk /dev/disk2
# 烧写启动数据
% sudo sh -c 'gunzip -c ~/Downloads/ubuntu-18.04.4-preinstalled-server-arm64+raspi3.img.xz | sudo dd of=/dev/disk2  bs=32m'
0+40048 records in
0+40048 records out
2624517120 bytes transferred in 913.454427 secs (2873178 bytes/sec)

cloudinit 配置(可选)

ubuntu server 提供了 cloud-init 进行服务器配置,可以编辑简单的文件进行配置服务器初始状态。

打开上面烧写完成的SD卡挂载,可以看到许多文件。查看README文件以了解cloud-init的配置内容与初始化流程的执行方式。

其中 meta-data 用于配置 配置 cloudinit初始操作,以及数据源等。我们用来设置了 hostname (但是目前好像不生效)。
其中 user-meta 用于配置 ssh file 等,我们主要用它来配置ssh初始密码。
其中 network-config 用于配置服务器网络。

meta-data:

# This is the meta-data configuration file for cloud-init. Typically this just
# contains the instance_id. Please refer to the cloud-init documentation for
# more information:
#
# https://cloudinit.readthedocs.io/

instance_id: cloud-image
local_hostname: ubuntu-001

user-meta:

#cloud-config

# This is the user-data configuration file for cloud-init. By default this sets
# up an initial user called "ubuntu" with password "ubuntu", which must be
# changed at first login. However, many additional actions can be initiated on
# first boot from this file. The cloud-init documentation has more details:
#
# https://cloudinit.readthedocs.io/
#
# Some additional examples are provided in comments below the default
# configuration.

# Enable password authentication with the SSH daemon
ssh_pwauth: true

# On first boot, set the (default) ubuntu user's password to "ubuntu" and
# expire user passwords
chpasswd:
  expire: false
  list:
    - ubuntu:password
    - root:password

network-config:

# This file contains a netplan-compatible configuration which cloud-init
# will apply on first-boot. Please refer to the cloud-init documentation and
# the netplan reference for full details:
#
# https://cloudinit.readthedocs.io/
# https://netplan.io/reference
#
# Some additional examples are commented out below

version: 2
ethernets:
  eth0:
    dhcp4: false
    optional: true
    addresses:
      - 192.168.2.31/16
    gateway4: 192.168.0.1
    nameservers:
      addresses:
        - 223.5.5.5
        - 8.8.8.8

docker 安装

Get Docker Engine - Community for Ubuntu

官方提供一键安装:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun

手动安装:

sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
sudo apt-get install -y\
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
# 这里注意架构为 arm64
sudo add-apt-repository \
   "deb [arch=arm64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo usermod -aG docker $USER
sudo systemctl enable docker
sudo systemctl start docker

etcd 安装

参考文档: running-a-single-node-etcd

其中,由于树莓派架构为arm64. 需要使用对应的docker image quay.azk8s.cn/coreos/etcd:v3.3.18-arm64

ETCD_IMAGE=quay.azk8s.cn/coreos/etcd:v3.3.18-arm64
# 注意!!! 这里换成当前机器的IP
NODE1=192.168.2.31
DATA_DIR=etcd-data

docker pull ${ETCD_IMAGE}
docker volume create --name ${DATA_DIR}
docker run \
  -d \
  -p 2379:2379 \
  -p 2380:2380 \
  --restart always \
  --volume=${DATA_DIR}:/etcd-data \
  -e ETCD_UNSUPPORTED_ARCH=arm64 \
  --name etcd ${ETCD_IMAGE} \
  /usr/local/bin/etcd \
  --data-dir=/etcd-data --name node1 \
  --initial-advertise-peer-urls http://${NODE1}:2380 --listen-peer-urls http://0.0.0.0:2380 \
  --advertise-client-urls http://${NODE1}:2379 --listen-client-urls http://0.0.0.0:2379 \
  --initial-cluster node1=http://${NODE1}:2380

calico 安装

截止该文档编写时 calico 目前最新版本为 v3.12.0.

docker 使用 calico/libnetwork-plugin 支持在 v2 版本和 v3 版本进行安装。

区别在于:

  • v3 版本 calico 官方默认支持多架构,可以直接获取 arm64 等异构平台的release。
  • v3 版本 不再将 calico libnetwork-plugin 网络插件作为核心插件打包进入release。
  • v3 版本 /etc/calico/calico.cfg 使用的apiVersion: projectcalico/v3 配置。
  • v2 版本 默认将 libnetwork-plugin 打包并在运行时启用。
  • v2 版本 /etc/calico/calico.cfg 使用的apiVersion: v1 配置。

下文展示了如何安装两个版本的 calico 。

calico v3

安装calicoctl

方式一: 从github上release页面下载arm64calicoctl

# 从github release页面下载`arm64`架构的`calicoctl`。
sudo wget -O /usr/local/bin/calicoctl https://github.com/projectcalico/calicoctl/releases/download/v3.12.0/calicoctl-linux-arm64

由于github release 后段使用的s3过于缓慢。

方法二:

CALICO_CTL_IMAGE=quay.azk8s.cn/calico/ctl:release-v3.12-arm64
docker pull ${CALICO_CTL_IMAGE}
docker  create --name ctl  ${CALICO_CTL_IMAGE}
sudo docker cp ctl:/calicoctl  /usr/bin/calicoctl
docker rm -f ctl

配置calicoctl

# 这里配置calicoctl连接数据源,根据实际情况变化
sudo mkdir -p /etc/calico
sudo sh -c "cat > /etc/calico/calicoctl.cfg" << EOF
apiVersion: projectcalico.org/v3
kind: CalicoAPIConfig
metadata:
spec:
  datastoreType: etcdv3
  etcdEndpoints: "http://192.168.2.31:2379"
EOF

安装calico node

calico node 镜像同样由于架构不同,需要使用镜像calico/node:v3.12.0-arm64,或者直接在 arm64 机器上执行 docker pull calico/node:v3.12.0 docker 会自动推断并下载当前机器所符合的版本,前提是该镜像支持异构。

如果下载过慢可以使用 quay.azk8s.cn/calico/node:v3.12.0-arm64 代理地址。

方式一:使用 calicoctl node run 方式安装

CALICO_NODE_IMAGE=calico/node:v3.12.0-arm64
docker pull ${CALICO_NODE_IMAGE}
sudo calicoctl node run --node-image=${CALICO_NODE_IMAGE}

方式二:以 systemd 方式安装 calico node (docker base)

sudo sh -c "cat > /etc/calico/calico.env" << EOF
#!按需更改地址配置
ETCD_ENDPOINTS=http://192.168.2.31:2379
ETCD_CA_CERT_FILE=""
ETCD_CERT_FILE=""
ETCD_KEY_FILE=""
CALICO_NODENAME=""
CALICO_NO_DEFAULT_POOLS=""
CALICO_IP=""
CALICO_IP6=""
CALICO_AS=""
CALICO_NETWORKING_BACKEND=bird
EOF
sudo sh -c "cat > /etc/systemd/system/calico-node.service" << EOF'
[Unit]
Description=calico-node
After=docker.service
Requires=docker.service

[Service]
EnvironmentFile=/etc/calico/calico.env
ExecStartPre=-/usr/bin/docker rm -f calico-node
ExecStart=/usr/bin/docker run --net=host --privileged \
--name=calico-node \
-e NODENAME=\${CALICO_NODENAME} \
-e IP=\${CALICO_IP} \
-e IP6=\${CALICO_IP6} \
-e CALICO_NETWORKING_BACKEND=\${CALICO_NETWORKING_BACKEND} \
-e AS=\${CALICO_AS} \
-e NO_DEFAULT_POOLS=\${CALICO_NO_DEFAULT_POOLS} \
-e ETCD_ENDPOINTS=\${ETCD_ENDPOINTS} \
-e ETCD_CA_CERT_FILE=\${ETCD_CA_CERT_FILE} \
-e ETCD_CERT_FILE=\${ETCD_CERT_FILE} \
-e ETCD_KEY_FILE=\${ETCD_KEY_FILE} \
-v /var/log/calico:/var/log/calico \
-v /run/docker/plugins:/run/docker/plugins \
-v /lib/modules:/lib/modules \
-v /var/run/calico:/var/run/calico \
calico/node:v3.12.0-arm64

ExecStop=-/usr/bin/docker stop calico-node

Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target'
EOF
sudo systemctl enable calico-node
sudo systemctl start calico-node

问题: 在当前系统上 EnvironmentFile 不生效,原因不详.

启动后发现 /run/docker/plugins下依旧没有calico相关插件。也印证了官方在v3之后不再默认支持docker network plugin。

calico libnetwork-plugin 安装

calico/libnetwork-plugin 是docker网络插件的calico 实现。

# calico node v3 默认不提供该插件。
$ docker network create --driver calico --ipam-driver calico-ipam cali
Error response from daemon: plugin "calico" not found

查看官方仓库,发现了 libnetwork-plugin。其中有commit显示,已经支持了calicov3。
于是开始按照指引手动编译。

git clone https://github.com/projectcalico/libnetwork-plugin.git
cd libnetwork-plugin
# make help 可以看到更多信息,也可以针对特定平台进行编译
make image BUILDOS=linux BUILDARCH=arm64

该过程需要下载一个镜像,并在该镜像环境里面执行glide下载vendor,如果本地有glide 可以考虑使用本地glide glide install -strip-vendor

由于众所周知的原因glide vendor多次失败,转而切换依赖工具使用 go module。

go mod init git.ghostcloud.cn/newben/newben
# Makefile 中未指定 GOOS 如果在非 linux 下进行交叉编译还需要修改 Makefile 在 go build 时增加 GOOS=${BUILDOS} 环境变量
make binary BUILDOS=linux BUILDARCH=arm64
# 用于生成 `calico/libnetwork-plugin:latest-arm64`,生成的镜像需要自己改成易于标志的名称,不建议使用latest。
make image  BUILDOS=linux BUILDARCH=arm64

传输镜像到目标机器(arm64),手动运行calico插件

docker run -d --privileged \
  --name calico-docker-network-plugin \
  --net host  \
  --restart always \
  -v /run/docker/plugins:/run/docker/plugins \
  -e ETCD_ENDPOINTS=http://192.168.2.31:2379 \
  calico/libnetwork-plugin:v2.6-arm64

运行成功后有日志输出,并且会在 /run/docker/plugins 下生成两个sock文件 calico.sock calico-ipam.sock

$ docker logs -f calico-docker-network-plugin
time="2020-02-18T03:09:40Z" level=info msg="calico-ipam has started."
time="2020-02-18T03:09:40Z" level=info msg="calico-net has started."
$ sudo ls -l -a  /run/docker/plugins
total 0
drwx------ 2 root root  80 Feb 18 03:10 .
drwx------ 8 root root 180 Feb 18 03:10 ..
srw-rw---- 1 root root   0 Feb 18 03:10 calico-ipam.sock
srw-rw---- 1 root root   0 Feb 18 03:10 calico.sock

继续运行创建 docker calico 网络:

在 docker service 中修改:
在ExecStart配置项后加 --cluster-store=etcd://192.168.2.31:2379

该操作是让docker将已经运行的docker容器信息等均存储置etcd,编译calico libnetwork-plugin执行信息采集同步等。

# 创建docker calico网络
docker network create --driver calico --ipam-driver calico-ipam cali

calico 集群默认规则不允许容器间网络访问,需要配置允许所有流量规则:

sudo sh -c "cat > /etc/calico/global-network-policy-allow-all.yaml" << EOF
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
  name: allow-all
spec:
  selector: all()
  ingress:
  - action: Allow
  egress:
  - action: Allow
EOF
sudo calicoctl apply -f /etc/calico/global-network-policy-allow-all.yaml

完成。

calico v2

如果使用 2.6.X 版本 calico node。

在libnetwork-plugin#repository-status
中有说到

The libnetwork plugin integration is no longer included as a core component in Calico releases, and is not being actively developed or supported by the core Calico team. The latest release of Calico which includes this plugin is the v2.6.x series of releases.

目前仅在 v2.6.x 系列的calico 发布版本才包含此组件作为核心组件。其他版本均需要手动编译。此外该工程已经向社区寻找维护人员,官方不再维护。

即calico node 2.6.x 系列是默认开启 libnetwork的。使用预编译好的calico/node:v2.6.10-arm64

由于缺少 arm64 架构的calicoctl 暂时不使用 calicoctl node run ... 改为手动执行。

在 docker service 中修改:
在ExecStart配置项后加 --cluster-store=etcd://192.168.2.31:2379

CALICO_NODE_IMAGE=calico/node:v2.6.10-arm64
# docker pull ${CALICO_NODE_IMAGE}
sudo calicoctl node run --node-image=${CALICO_NODE_IMAGE}
# 这里的配置文件和 v3 版本的有很大区别
sudo sh -c "cat > /etc/calico/calicoctl.cfg" << EOF
apiVersion: v1
kind: calicoApiConfig
spec:
  datastoreType: "etcdv2"
  etcdEndpoints: "http://192.168.2.31:2379"
EOF
# 手动运行 calico-node
docker run --net=host --privileged \
--name=calico-node -d --restart=always \
-e NODENAME=ubuntu-001 \
-e CALICO_NETWORKING_BACKEND=bird \
-e CALICO_LIBNETWORK_ENABLED=true \
-e ETCD_ENDPOINTS=http://192.168.2.31:2379 \
-v /var/log/calico:/var/log/calico \
-v /var/run/calico:/var/run/calico \
-v /lib/modules:/lib/modules \
-v /run:/run \
-v /run/docker/plugins:/run/docker/plugins \
-v /var/run/docker.sock:/var/run/docker.sock \
calico/node:v2.6.10-arm64
docker network create --driver calico --ipam-driver calico-ipam cali_net

你可能感兴趣的:(在 arm64 上运行 calico libnetwork-plugin)