Day10-网络通信-calico、flannel

1、centos7.9环境初始化

环境:
centos7.9  
通知:CentOS 8操作系统版本结束了生命周期(EOL),Linux社区已不再维护该操作系统版本。建议您切换到Anolis或Alinux。如果您的业务过渡期仍需要使用CentOS 8系统中的一些安装包,请根据下文切换CentOS 8的源。
所以我们这块要切换一下源
  • 1.
  • 2.
  • 3.
  • 4.
# 切换为华为云的源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/repository/conf/CentOS-7-anon.repo

yum clean all 
yum makecache
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

Day10-网络通信-calico、flannel_centos

# 安装docker依赖包  --后期备注 : 这个下载不了 很多都没有软件包
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum list docker-ce --showduplicates | sort -r

yum install -y docker-ce-18.03.1.ce-1.e17.centos




# 最后采用二进制安装 20.10.10版本
压缩包和对应文件自备
tar -zxvf 
mv docker/* /usr/bin
mv docker.service /usr/lib/systemd/system/
mv daemon.json /etc/docker/

systemctl enable docker
systemctl start docker



  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
安装对应的包和依赖
yum install -y conntrack ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git

yum -y install wget jq psmisc vim net-tools nfs-utils socat telnet device-mapper-persistent-data lvm2 git network-scripts tar curl -y

yum -y install lrzsz git subversion gpm unzip zip curl wget net-tools vim gcc
yum -y install wget jq psmisc vim net-tools nfs-utils telnet yum-tuils device-mapper-persistent-data lvm2 git tar curl -y
yum -y install conntrack socat ipset ipvsadm ebtables
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
关闭防火墙\selinux
vim /etc/selinux/config
service firewalld stop
systemctl disable firewalld.service
  • 1.
  • 2.
  • 3.
  • 4.

Day10-网络通信-calico、flannel_flannel_02
Day10-网络通信-calico、flannel_docker_03

vim /etc/hosts
192.168.0.51 centos7-jichao-051 
192.168.0.52 centos7-jichao-052 
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_docker_04

vim /etc/sysconfig/network-scripts/ifcfg-ens32
  • 1.

Day10-网络通信-calico、flannel_centos_05

克隆机器 更改ip地址和Hostname

Day10-网络通信-calico、flannel_calico_06

flannel

机器51,52  53 centos7.9 其他的需要去编译 时间较长
cat  /etc/hosts

组件:
51 部署etcd,flannel,docker   hostname:centos7-jichao-051  主控端 通过etcd
52 部署flannel,docker       hostname:centos7-jichao-052
53 部署flannel,docker       hostname:centos7-jichao-053
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
flannel 能够让不通的容器上的docker 容器都拥有唯一的Ip地址,不像之前的docker0分配虚拟地址,这样不同的容器可能会出现同样的ip地址
underlay 物理网络
overlay 虚拟网络
flannel 是一种overlay 网络,表示运行在一个网上的网(应用层网络)并不依靠ip地址传递信息,而是采用映射机制,把ip地址和identifiers做映射来资源定位,即把tcp数据包装在另一种网络包里进行理由转发和通信
flannel使用etcd存储配置数据和子网分配信息
flannel启动之后,后台进程首先检索配置和正在使用的子网列表,选择一个可用的子网,去注册它,etcd也存储每个主机对应的Ip
flannel使用etcd的watch机制监视/coreos.com/network/subnets下面所有元素的变化信息
并且根据它来维护一个路由表,为提高性能,flannel优化了universal TAP/TUN设备
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

Day10-网络通信-calico、flannel_calico_07

这属于二层网络,基于下一跳-网关
三层网络宣告
  • 1.
  • 2.
打开中继路由
打开中继路由
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
  • 1.
  • 2.

Day10-网络通信-calico、flannel_calico_08

防火墙
# 防火墙规则
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F
iptables -L -n
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Day10-网络通信-calico、flannel_vim_09
Day10-网络通信-calico、flannel_docker_10

机器51部署etcd
机器51部署etcd
yum -y install etcd
vim /etc/etcd/etcd.conf
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_flannel_11
Day10-网络通信-calico、flannel_docker_12

客户端网络要注入到flannel 地址里面去 下面是访问地址 把路由放到etcd里面去
上面是监听

service etcd start
chkconfig etcd on
ps -ef | grep etcd

这个是单机的etcd
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

Day10-网络通信-calico、flannel_docker_13

查看端口是否正常
etcdctl -C http://192.168.0.51:4001 cluster-health
etcdctl -C http://192.168.0.51:2379 cluster-health
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_flannel_14

查看etcd 成员
etcdctl member list
  • 1.
  • 2.

Day10-网络通信-calico、flannel_docker_15

所有机器部署flannel
yum -y install flannel
  • 1.

Day10-网络通信-calico、flannel_flannel_16

cp /etc/sysconfig/flanneld /etc/sysconfig/flanneld.bak
vim /etc/sysconfig/flanneld
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_17

定义网络
# 注意!!!!!!!!!!!!!!!!!!!!!!!!!!
这里我们出错了  我们这个网段不能和宿主机网段相同 不然会出现flanneld无法启动的情况
我们设置的网段和宿主机是一个网段,所以会报错,flannel无法启动
etcdctl mk /atomic.io/network/config '{"Network:"192.123.0.0/16","SubnetMin": "192.123.1.0","SubnetMax": "192.123.254.0"}'


更改为
etcdctl mk /atomic.io/network/config '{"Network:"172.27.0.0/16","SubnetMin": "172.27.1.0","SubnetMax": "172.27.254.0"}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
启动flanneld
service flanneld start
chkconfig flanneld on
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_docker_18
Day10-网络通信-calico、flannel_centos_19

# 注意,docker 也要进行重启
systemctl restart docker
ifconfig
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_centos_19

cat /run/flannel/subnet.env
cat /run/flannel/docker
vim /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl restart docker.service
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Day10-网络通信-calico、flannel_calico_21
Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

机器52、53
systemctl start flanneld
systemctl enable flanneld
ps -ef |grep flannel
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

52

Day10-网络通信-calico、flannel_centos_19

vim /usr/lib/systemd/system/docker.service
  • 1.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

53

Day10-网络通信-calico、flannel_centos_19

vim /usr/lib/systemd/system/docker.service
  • 1.
# 完成之后都进行重启
systemctl daemon-reload
systemctl restart docker
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

测试是否通信成功?

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

51   by1
  • 1.

Day10-网络通信-calico、flannel_centos_19

52 by2
  • 1.

Day10-网络通信-calico、flannel_centos_19

53 by3
  • 1.

Day10-网络通信-calico、flannel_centos_19

 通信成功!
  • 1.

Day10-网络通信-calico、flannel_centos_19

如果有一台新的机器,要加入 如何操作?

1、 打开中继路由
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf

2、# 防火墙规则
iptables -P INPUT ACCEPTiptables -P FORWARD ACCEPTiptables -Fiptables -L -n

3、 yum -y install flannel

4、 更改配置文件 
vim /etc/sysconfig/flanneld  改为etcd地址

5、启动flannel systemctl start flanneld
systemctl enable flanneld

6、cat /run/flannel/subnet.env
cat /run/flannel/docker

7、vim /usr/lib/systemd/system/docker.service
把启动后加入  /run/flannel/docker 后面的bip 

8、重启docker

9、ifconfig

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

补充

对等网,物理网-underlay network
flannel  overlay network 虚拟网络
思考: k8s - 三个网络 :1、物理网络 2、svc网络 3、pod网络
那这三个网络我全弄成物理网络那个Ip 可以嘛? 就不用走那么多规则,是不是更加高效呢?两个集群之间的Pod不就直接可以相互通信了嘛?
答案是:可以 
那安全怎么解决? -> 安全加固,低运行时 runc!  kata + gvisor  + 扫描器
资源限制-> 限制cpu 限制内存


可以一个集群有状态服务 , 一个集群无状态服务
# 扩展
daocloud   https://docs.daocloud.io/network/intro/what/#cilium-macvlansr-iovipvlan-spiderpool-multus
kubesphere   https://kubesphere.io/zh/docs/v3.4/release/release-v341/
rancher     https://www.rancher.cn/products/
rainbond   https://www.rainbond.com/
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

calico

calico的原理理解起来,不是那么容易,我们可以先进行搭建,在进行理解
下图是calico的跨主机通信
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19

系统 centos7.9
# 这边复习下初始化,操作就不列举了
主机 
51   centos-jichao-101  安装dockder+etcd(集成) + calicoctl
52   centos-jichao-102  安装dockder+etcd(集成) + calicoctl
53   centos-jichao-103  安装dockder+etcd(集成) + calicoctl
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
开启中继路由、转发
vim /etc/sysctl.conf
net.ipv4.conf.all.rp_filter=1
net.ipv4.ip_forward=1
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_centos_19

防火墙
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F
iptables -L -n
  • 1.
  • 2.
  • 3.
  • 4.

Day10-网络通信-calico、flannel_centos_19

scp /etc/sysctl.conf root@192.168.0.102:/etc/
scp /etc/sysctl.conf root@192.168.0.103:/etc/
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19

部署etcd集群
yum -y install etcd
  • 1.

Day10-网络通信-calico、flannel_centos_19

# 备份etcd.conf  + 置空etcd.conf
cp /etc/etcd/etcd.conf /etc/etcd/etcd.conf.bak
> /etc/etcd/etcd.conf
cat > /etc/etcd/etcd.conf  << EOF
#[Member]
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"

ETCD_NAME="centos-jichao-101"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.0.101:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.0.101:2379"
ETCD_INITIAL_CLUSTER="centos-jichao-101=http://192.168.0.101:2380,centos-jichao-102=http://192.168.0.102:2380,centos-jichao-103=http://192.168.0.103:2380"
EOF
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

Day10-网络通信-calico、flannel_centos_19

scp /etc/etcd/etcd.conf root@192.168.0.102:/etc/etcd/etcd.conf
scp /etc/etcd/etcd.conf root@192.168.0.103:/etc/etcd/etcd.conf
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19

102,103 机器
vim /etc/etcd/etcd.conf 改一下主机名 ETCD_NAME="centos-jichao-102\103"
更改ip地址
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_centos_19

这三台机器就组成了etcd集群
cat /etc/hosts文件是要注意要写的
  • 1.
  • 2.
# 启动etcd
systemctl start etcd
systemctl enable etcd
#三个节点同时启动
  • 1.
  • 2.
  • 3.
  • 4.

Day10-网络通信-calico、flannel_centos_19

查看成员
etcdctl memebet list
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19

ps -ef | grep etcd 
netstat -nultp | grep 2379
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19

# 查看健康
etcdctl cluster-health
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19

集成docker 和 etcd
在三台机器的docker.service中添加  --cluster-store=etcd://机器ip:2379
vim /usr/lib/systemd/system/docker.service
# 如下图所示,都是一样的更改
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_centos_19

systemctl daemon-reload
systemctl restart docker.service
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19

# 我们这里重启报错了,所以我们把原来docker除了我们添加的部分删除,即可重启成功
  • 1.

Day10-网络通信-calico、flannel_centos_19

ps -ef | grep docker
  • 1.

Day10-网络通信-calico、flannel_centos_19

下载calicoctl
# 下载calicoctl
https://github.com/projectcalico/calicoctl/releases
下载3.16的
  • 1.
  • 2.
  • 3.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

chmod +x calicoctl
mv calicoctl /usr/local/bin
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19

calicoctl version
  • 1.

Day10-网络通信-calico、flannel_centos_19

docker pull quay.io/calico/node:v2.6.10
  • 1.

Day10-网络通信-calico、flannel_centos_19

启动容器

启动容器

docker run --net=host --privileged --name=calico-node -d --restart=always -e NODENAME=centos-jichao-101 -e CALICO_NETWORKING_BACKEND=bird -e CALICO_LIBNETWORK_ENABLED=true -e IP=192.168.0.101 -e ETCD_ENDPOINTS=http://127.0.0.1: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 quay.io/calico/node:v2.6.10

  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19

docker exec -it calico-node env
  • 1.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

查看集群状态
# 查看calico集群状态
calicoctl node status
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

添加calico子网
ipPool.yaml
apiVersion: v1
kind: ipPool
metedate:
  cidr: 10.244.10.0/24
spec:
  ipip:
    enabled: true
  nat-outgoing: true
  disabled: false
  
  # 再把scp给102 103机器
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

calicoctl create -f ipPool.yaml
calicoctl get ipPool -o wide
  • 1.
  • 2.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

创建ip池子网
在创建的ipPool中创建子网络
docker network create --driver calico --ipam-driver calico-ipam --subnet 10.244.10.0/24 calico-net1
docker network create --driver calico --ipam-driver calico-ipam --subnet 10.244.20.0/24 calico-net2
docker network create --driver calico --ipam-driver calico-ipam --subnet 10.244.30.0/24 calico-net3
  • 1.
  • 2.
  • 3.
  • 4.

Day10-网络通信-calico、flannel_centos_19

--driver calico 网络使用calico驱动
--ipam-driver calico-ipam : 指定calico 的ipam 驱动管理ip
-subnet:指定calico-net1、calico-net2、calico-net3
这三个网段是global网段,etcd会将它们三个同步到所有的机器
  • 1.
  • 2.
  • 3.
  • 4.
# 开启ipv6转发
echo 1 > /proc/sys/net/ipv6/conf/default/disable_ipv6
echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6

101 
docker run -itd --net calico-net1 --name docker-test1 busybox
docker run -itd --net calico-net2 --name docker-test11 busybox
docker run -itd --net calico-net3 --name docker-test111 busybox


102
docker run -itd --net calico-net1 --name docker-test2 busybox
docker run -itd --net calico-net2 --name docker-test22 busybox
docker run -itd --net calico-net3 --name docker-test222 busybox

103
docker run -itd --net calico-net1 --name docker-test3 busybox
docker run -itd --net calico-net2 --name docker-test33 busybox
docker run -itd --net calico-net3 --name docker-test333 busybox
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

Day10-网络通信-calico、flannel_centos_19

查看节点Ip
101
docker exec -it docker-test1 ifconfig |grep inet
docker exec -it docker-test11 ifconfig |grep inet
docker exec -it docker-test111 ifconfig |grep inet
102
docker exec -it docker-test2 ifconfig |grep inet
docker exec -it docker-test22 ifconfig |grep inet
docker exec -it docker-test222 ifconfig |grep inet
103
docker exec -it docker-test3 ifconfig |grep inet
docker exec -it docker-test33 ifconfig |grep inet
docker exec -it docker-test333 ifconfig |grep inet

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

测试联通
docker exec -it docker-test1 ping -c 2 docker-test2.calico-net1
docker exec -it docker-test1 ping -c 2 docker-test3.calico-net1
docker exec -it docker-test11 ping -c 2 docker-test22.calico-net2
docker exec -it docker-test11 ping -c 2 docker-test33.calico-net2
docker exec -it docker-test111 ping -c 2 docker-test222.calico-net3
docker exec -it docker-test111 ping -c 2 docker-test333.calico-net3
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19
Day10-网络通信-calico、flannel_centos_19

 route -n
  • 1.

Day10-网络通信-calico、flannel_centos_19

总结:
1、同一网段里面容器互相Ping的通(即使不在同一个节点上,只要创建容器时使用的是同一个子网段)
2、不在同一个子网段的容器是Ping不通的
3、宿主机能Ping通他自身的所有容器ip,但不能ping通其他节点上的容器Ip
4、所有节点的容器都能Ping通其他节点的宿主机ip 



即-在calico默认设置下,只允许位于同一网络中的容器之间通信,这样就实现了容器的跨主机互连,也可以更好实现网络隔离效果,位于不通网络下的容器想要通信,就要依赖calico的policy策略来实现了
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.