在阿里云用ECS测试K8S的时候 ,Calico这个网络插件安装不了,受限制,所以决定用flannel的网络插件,之前文章介绍过用二进制安装的方式,具体参考前面写过的文章


今天重点介绍用官网提供的yaml 文件部署

https://github.com/coreos/flannel

For Kubernetes v1.17+ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

在部署之前需要注意几个问题,一个是CNI工具包的安装:

--cni-bin-dir string                                                                                       
<Warning: Alpha feature> A comma-separated list of full paths of directories in which to search for CNI plugin binaries.
This docker-specific flag only works when container-runtime is set to docker. (default "/opt/cni/bin")
默认在/opt/cni/bin 下面,根据你实际规划自己配置。
cd /opt/cni/bin 
wget https://github.com/containernetworking/plugins/releases/download/v0.8.7/cni-plugins-linux-amd64-v0.8.7.tgz
tar -zxvf cni-plugins-linux-amd64-v0.8.7.tgz
如果没有提前安装好工具包 Pod启动会有下面的包错
Failed to create pod sandbox: rpc error: code = Unknown desc = failed to setup 
network for sandbox "53a26d45c9eb3668ffc0e2716b8adb9d3a1a78c89ed3c2cffd980d370f253865":
failed to find plugin "bridge" in path [/opt/cni/bin]kubelet 配置
--network-plugin=cnikube-controller-manager 配置
--allocate-node-cidrs=true \       Should CIDRs for Pods be allocated and set on the cloud provider.
--cluster-cidr=192.168.0.0/16 \  这个一定要和kube-proxy  kube-flannel.yml  里面的地址保持一样  --cluster-cidr string                      CIDR Range for Pods in cluster. Requires --allocate-node-cidrs to be truekube-proxy

k8s安装mysql nacos k8s安装flannel网络插件_ci

kube-flannel.yml

 

 

k8s安装mysql nacos k8s安装flannel网络插件_k8s安装mysql nacos_02

这两参数必须增加,要不会有报错
Error registering network: failed to acquire lease: node "k8s-node1" pod cidr not assigned

kubectl  apply -f  kube-flannel.yml

k8s安装mysql nacos k8s安装flannel网络插件_github_03

 

k8s安装mysql nacos k8s安装flannel网络插件_ci_04

证明节点分配IP 正常

启动测试POD ,在各节点ping相应的IP 地址看是否互通

k8s安装mysql nacos k8s安装flannel网络插件_k8s安装mysql nacos_05

查看节点的路由

k8s安装mysql nacos k8s安装flannel网络插件_ci_06

如果是同一局域网的集群,可以使用host-gw 这种模式提升性能

 

host-gw 模式

修改文件kube-flannel.yml文件里面的net-conf.json
net-conf.json: |
{
"Network": "192.168.0.0/16",
"Backend": {
"Type": "vxlan",#当然,若你很确定自己的集群以后也不可能跨网段,你完全可以直接设置为 host-gw.
"Directrouting": true默认是false,修改为true就是可以让VxLAN自适应是使用VxLAN还是使用host-gw了
}
}

DirectRouting: 这种是混合自适应的方式, 即它会自动判断,若当前是相同二层网络
       (即:不垮路由器,二层广播可直达),则直接使用Host-GW方式工作,若发现目标是需要跨网段
       (即:跨路由器)则自动转变为使用VxLAN的方式。

 

 

k8s安装mysql nacos k8s安装flannel网络插件_github_07

 

直接修改有时候不生效,需要强制删除在启动,生产环境建议在部署前配置好

注:发现阿里云也不支持这种模式的,只能强制换回VXLAN,隧道模式就没问题,建议如果在阿里云用K8S 还是用他们提供的容器服务吧,自建的还是不要尝试了,后面还有什么坑,就不测试了

这里还涉及到一个问题就是后续安装 metrics-server  因为master 没有安装网络插件,所以k8s-API 不管是和metrics-server POD 通信还是svc通信都是有问题的,这里涉及到二进制安装网络插件,而且最好不要单独用etcd作为源

因为我们集群的节点是通过POD 运行的,POD 运行的flannel 是通过K8S 来获取网络信息的,这里就需要我们用k8s做数据源

Flannel can now use the kubernetes api as storage backend, instead of etcd.

This is useful especially when deploying flannel with kubernetes where you - otherwise - either need to allow multi-user access to a single etcd instance or setup a secondary etcd instance just to hold flannel config.

When configuring flannel with kubernetes as storage backend, you can isolate the access to etcd by authorizing only kube-apiserver.

Flannel现在可以使用kubernetes api作为存储后端,而不是etcd

这在使用kubernetes部署flannel时特别有用,否则,你要么需要允许多用户访问单个etcd实例,要么需要设置一个辅助etcd实例来保存flannel配置。

当使用kubernetes作为存储后端配置flannel时,您可以通过只授权kube-apiserver来隔离对etcd的访问

具体安装步骤:

下载二进制flanneld 的包

curl -o /usr/bin/flanneld https://github.com/coreos/flannel/releases/download/v0.13.0/flanneld-amd64

服务的配置文件

[Unit]
Description=Network fabric for containers
Documentation=https://github.com/coreos/flannel
After=network.target
After=network-online.target
Wants=network-online.target[Service]
Type=notify
Restart=always
RestartSec=5
# This is needed because of this: https://github.com/coreos/flannel/issues/792
# Kubernetes knows the nodes by their FQDN so we have to use the FQDN
Environment=NODE_NAME=k8s-master1
# Note that we don't specify any etcd option. This is because we want to talk
# to the apiserver instead. The apiserver then talks to etcd on flannel's
# behalf.
ExecStart=/usr/bin/flanneld \
  --kube-subnet-mgr=true \
  --kubeconfig-file=/root/.kube/config \
  --v=1 \
  --ip-masq=true \
  --iface=eth0
[Install]
WantedBy=multi-user.target

k8s安装mysql nacos k8s安装flannel网络插件_ci_08

这里说明一下master 节点必须在k8s注册过,所以需要我们安装kubelet 服务来达到注册的目标,如果自己了解整个数据规则可以按规则写到etcd,但是不推荐这样干,因为不知道还会有什么问题,目前这点是比较难受的,不知道后续会不会改进,但是安装了kubelet 启动起来,以后自动创建ds 相关发flannel POD,

感觉还是用另一个推荐的网络插件吧,K8S 后续版本也推荐用这个网络插件,下面是非集群节点的安装方式完全符合我们的需求

https://docs.projectcalico.org/getting-started/bare-metal/installation/

 

如果不注册这个节点就会报下面的错,

etcdctl --cacert="/etc/ssl/kubernetes/ca.pem" --cert="/etc/ssl/kubernetes/kubernetes.pem" --key="/etc/ssl/kubernetes/kubernetes-key.pem" get / --prefix --keys-only

这个是查看etcd所有的key 

k8s安装mysql nacos k8s安装flannel网络插件_ci_09

I now get: Error registering network: failed to acquire lease: node "k8s-master" not found

Before switching from etcd to kubernetes configuration backend, I had flannel running on my k8s master node, which haven't got an active kubelet service on it - i.e. the master node is not a cluster node as such.