flannel网络配置
实验环境
Master:192.168.220.200/24 | kube-apiserver kube-controller-manager kube-scheduler etcd |
---|---|
Node01:192.168.220.202/24 | kubelet kube-proxy docker flannel etcd |
Node02:192.168.220.203/24 | kubelet kube-proxy docker flannel etcd |
flannel部署
注:这里要接着etcd做完之后继续做,详情参考etcd部署
两个node节点都必须要部署docker引擎,详情参考docker引擎部署
做完之后就可以接下来的操作,里边所有的包都可以在官网里边去下载k8s官网
————————master————————
[root@localhost /opt/etcd/ssl] # /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.220.200:2379,https://192.168.220.202:2379,https://192.168.220.203:2379" set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}
[root@localhost /opt/etcd/ssl] # cd
[root@localhost ~] # cd k8s/
[root@localhost ~/k8s] # ls
etcd-cert etcd-v3.3.10-linux-amd64 flannel-v0.10.0-linux-amd64.tar.gz
etcd.sh etcd-v3.3.10-linux-amd64.tar.gz kubernetes-server-linux-amd64.tar.gz
[root@localhost ~/k8s] # /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.220.200:2379,https://192.168.220.202:2379,https://192.168.220.203:2379" get /coreos.com/network/config
open server.pem: no such file or directory
[root@localhost ~/k8s] # ls
etcd-cert etcd-v3.3.10-linux-amd64 flannel-v0.10.0-linux-amd64.tar.gz
etcd.sh etcd-v3.3.10-linux-amd64.tar.gz kubernetes-server-linux-amd64.tar.gz
[root@localhost ~/k8s] # cd etcd-cert/
[root@localhost ~/k8s/etcd-cert] # /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.220.200:2379,https://192.168.220.202:2379,https://192.168.220.203:2379" get /coreos.com/network/config
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}
[root@localhost ~/k8s/etcd-cert] # cd ../
[root@localhost ~/k8s] # scp flannel-v0.10.0-linux-amd64.tar.gz root@192.168.220.202:/root
root@192.168.220.202's password:
flannel-v0.10.0-linux-amd64.tar.gz 100% 9479KB 45.3MB/s 00:00
[root@localhost ~/k8s] # scp flannel-v0.10.0-linux-amd64.tar.gz root@192.168.220.203:/root
root@192.168.220.203's password:
flannel-v0.10.0-linux-amd64.tar.gz 100% 9479KB 45.9MB/s 00:00
[root@localhost ~/k8s] # cd etcd-cert/
[root@localhost ~/k8s/etcd-cert] # ls
ca-config.json ca-csr.json ca.pem server-csr.json server.pem
ca.csr ca-key.pem server.csr server-key.pem
——————node1———————
[root@localhost ~] # tar zxvf flannel-v0.10.0-linux-amd64.tar.gz
flanneld
mk-docker-opts.sh
README.md
[root@localhost ~] # mkdir /opt/kubernetes/{cfg,bin,ssl} -p
[root@localhost ~] # mv mk-docker-opts.sh flanneld /opt/kubernetes/bin/
[root@localhost ~] # vim flannel.sh
#!/bin/bash
ETCD_ENDPOINTS=${1:-"http://127.0.0.1:2379"}
cat <<EOF >/opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} \
-etcd-cafile=/opt/etcd/ssl/ca.pem \
-etcd-certfile=/opt/etcd/ssl/server.pem \
-etcd-keyfile=/opt/etcd/ssl/server-key.pem"
EOF
cat <<EOF >/usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service
[Service]
Type=notify
EnvironmentFile=/opt/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq \$FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable flanneld
systemctl restart flanneld
[root@localhost ~] # bash flannel.sh https://192.168.220.200:2379,https://192.168.220.202:2379,https://192.168.220.203:2379
Created symlink from /etc/systemd/system/multi-user.target.wants/flanneld.service to /usr/lib/systemd/system/flanneld.service.
[root@localhost ~] # vim /usr/lib/systemd/system/docker.service
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
EnvironmentFile=/run/flannel/subnet.env
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
[root@localhost ~] # cat /run/flannel/subnet.env
DOCKER_OPT_BIP="--bip=172.17.45.1/24"
DOCKER_OPT_IPMASQ="--ip-masq=false"
DOCKER_OPT_MTU="--mtu=1450"
DOCKER_NETWORK_OPTIONS=" --bip=172.17.45.1/24 --ip-masq=false --mtu=1450"
[root@localhost ~] # systemctl daemon-reload
[root@localhost ~] # systemctl restart docker
[root@localhost ~] # ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.45.1 netmask 255.255.255.0 broadcast 172.17.45.255
ether 02:42:d5:27:8a:28 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.220.202 netmask 255.255.255.0 broadcast 192.168.220.255
inet6 fe80::2672:566:3ec9:a6b2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9f:a9:4e txqueuelen 1000 (Ethernet)
RX packets 1371460 bytes 1126496022 (1.0 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 898410 bytes 99740718 (95.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.45.0 netmask 255.255.255.255 broadcast 0.0.0.0
inet6 fe80::8cef:b6ff:fec9:eeb2 prefixlen 64 scopeid 0x20<link>
ether 8e:ef:b6:c9:ee:b2 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 36 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 2969 bytes 200340 (195.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2969 bytes 200340 (195.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:bf:11:dd txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~] # docker run -it centos:7 /bin/bash
Unable to find image 'centos:7' locally
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
[root@a4c74ac279d5 /]# yum install -y net-tools
[root@a4c74ac279d5 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.45.2 netmask 255.255.255.0 broadcast 172.17.45.255
ether 02:42:ac:11:2d:02 txqueuelen 0 (Ethernet)
RX packets 20147 bytes 15437343 (14.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7845 bytes 427029 (417.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@a4c74ac279d5 /]# ping 172.17.95.2
PING 172.17.95.2 (172.17.95.2) 56(84) bytes of data.
64 bytes from 172.17.95.2: icmp_seq=1 ttl=62 time=0.541 ms
64 bytes from 172.17.95.2: icmp_seq=2 ttl=62 time=1.02 ms
^C
--- 172.17.95.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.541/0.784/1.027/0.243 ms
[root@a4c74ac279d5 /]#
——————node2(和node1操作一样)——————
[root@localhost ~] # tar zxvf flannel-v0.10.0-linux-amd64.tar.gz
flanneld
mk-docker-opts.sh
README.md
[root@localhost ~] # mkdir /opt/kubernetes/{cfg,bin,ssl} -p
[root@localhost ~] # mv mk-docker-opts.sh flanneld /opt/kubernetes/bin/
[root@localhost ~] # ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 视频 文档 音乐
flannel-v0.10.0-linux-amd64.tar.gz README.md 模板 图片 下载 桌面
[root@localhost ~] # vim flannel.sh
#!/bin/bash
ETCD_ENDPOINTS=${1:-"http://127.0.0.1:2379"}
cat <<EOF >/opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} \
-etcd-cafile=/opt/etcd/ssl/ca.pem \
-etcd-certfile=/opt/etcd/ssl/server.pem \
-etcd-keyfile=/opt/etcd/ssl/server-key.pem"
EOF
cat <<EOF >/usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service
[Service]
Type=notify
EnvironmentFile=/opt/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq \$FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable flanneld
systemctl restart flanneld
[root@localhost ~] # bash flannel.sh https://192.168.220.200:2379,https://192.168.220.202:2379,https://192.168.220.203:2379
Created symlink from /etc/systemd/system/multi-user.target.wants/flanneld.service to /usr/lib/systemd/system/flanneld.service.
[root@localhost ~] # vim /usr/lib/systemd/system/docker.service
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
EnvironmentFile=/run/flannel/subnet.env
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
[root@localhost ~] # cat /run/flannel/subnet.env
DOCKER_OPT_BIP="--bip=172.17.95.1/24"
DOCKER_OPT_IPMASQ="--ip-masq=false"
DOCKER_OPT_MTU="--mtu=1450"
DOCKER_NETWORK_OPTIONS=" --bip=172.17.95.1/24 --ip-masq=false --mtu=1450"
[root@localhost ~] # systemctl daemon-reload
[root@localhost ~] # systemctl restart docker
[root@localhost ~] # ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.95.1 netmask 255.255.255.0 broadcast 172.17.95.255
ether 02:42:18:d8:15:95 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.220.203 netmask 255.255.255.0 broadcast 192.168.220.255
inet6 fe80::1c50:ea1c:7df5:dda2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2e:96:d1 txqueuelen 1000 (Ethernet)
RX packets 1468834 bytes 1136097173 (1.0 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1054174 bytes 113578451 (108.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.95.0 netmask 255.255.255.255 broadcast 0.0.0.0
inet6 fe80::783f:d8ff:fe51:118a prefixlen 64 scopeid 0x20<link>
ether 7a:3f:d8:51:11:8a txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 34 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 2996 bytes 177569 (173.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2996 bytes 177569 (173.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:85:de:42 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~] # docker run -it centos:7 /bin/bash
Unable to find image 'centos:7' locally
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
[root@d91883a6f331 /]# yum install -y net-tools
[root@d91883a6f331 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.95.2 netmask 255.255.255.0 broadcast 172.17.95.255
ether 02:42:ac:11:5f:02 txqueuelen 0 (Ethernet)
RX packets 20180 bytes 15437378 (14.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9942 bytes 540149 (527.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@d91883a6f331 /]# ping 172.17.45.2
PING 172.17.45.2 (172.17.45.2) 56(84) bytes of data.
64 bytes from 172.17.45.2: icmp_seq=1 ttl=62 time=1.52 ms
64 bytes from 172.17.45.2: icmp_seq=2 ttl=62 time=0.742 ms
^C
--- 172.17.45.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.742/1.133/1.525/0.392 ms
[root@d91883a6f331 /]#