Flanneld 安装配置

介绍

Flannel是CoreOS团队针对Kubernetes设计的一个网络规划服务,目的就是不同的宿主机的容器有自己的ip地址并且可以相互通信,需要注意的是容器的ip不能与宿主机同一网段;

原理

实践

环境

OS:Centos7     
flanneld network:172.20.0.0/16 主机名 ip地址        部署服务 node
-1 10.105.10.241 etcd,flannel,docker node-2 10.105.10.241 flannel,docker

安装配置

node1操作

[root@localhost ~]# hostnamectl --static set-hostname  node-1
[root@localhost ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.105.10.241    node-1
10.105.10.241    etcd
10.105.10.242    node-2
:wq
[root@localhost ~]#  systemctl disable firewalld.service
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# yum install -y docker
。。。。。。。省略
作为依赖被升级:
  libsemanage.x86_64 0:2.5-8.el7                libsemanage-python.x86_64 0:2.5-8.el7               
  policycoreutils.x86_64 0:2.5-17.1.el7         policycoreutils-python.x86_64 0:2.5-17.1.el7        

完毕!
[root@localhost ~]#  yum install etcd -y
[root@localhost ~]# vim /etc/etcd/etcd.conf

# [member]
ETCD_NAME=default
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR=""
#ETCD_SNAPSHOT_COUNT="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
#ETCD_LISTEN_PEER_URLS="http://localhost:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"

#ETCD_CORS=""
#
#[cluster]
#ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
# if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
#ETCD_INITIAL_CLUSTER="default=http://localhost:2380"
#ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://etcd:2379,http://etcd:4001"
#ETCD_DISCOVERY=""
:x
[root@localhost ~]# systemctl start etcd
[root@localhost ~]# lsof -i:2379;lsof -i:4001
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
etcd    4807 etcd    6u  IPv6  35997      0t0  TCP *:2379 (LISTEN)
etcd    4807 etcd   12u  IPv4  37018      0t0  TCP localhost:36180->localhost:2379 (ESTABLISHED)
etcd    4807 etcd   15u  IPv6  37022      0t0  TCP localhost:2379->localhost:36180 (ESTABLISHED)
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
etcd    4807 etcd    7u  IPv6  35998      0t0  TCP *:newoak (LISTEN)
etcd    4807 etcd   13u  IPv4  37019      0t0  TCP localhost:39224->localhost:newoak (ESTABLISHED)
etcd    4807 etcd   14u  IPv6  37020      0t0  TCP localhost:newoak->localhost:39224 (ESTABLISHED)
验证etcd 服务是否正常
[root@localhost ~]#  etcdctl set testdir/testkey0 0
0
[root@localhost ~]# etcdctl get testdir/testkey0
0
[root@localhost ~]#  etcdctl -C http://etcd:4001 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://etcd:2379
cluster is healthy
[root@localhost ~]# etcdctl -C http://etcd:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://etcd:2379
安装flanneld
[root@localhost ~]# yum install flannel -y
[root@localhost ~]# vim /etc/sysconfig/flanneld

# Flanneld configuration options  

# etcd url location.  Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS="http://etcd:2379"

# etcd config key.  This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_PREFIX="/atomic.io/network"
x:
[root@localhost ~]#  etcdctl mk /atomic.io/network/config '{ "Network": "172.20.0.0/16" }' //配置flanneld 网络
{ "Network": "172.20.0.0/16" } 
####[root@localhost ~]#  etcdctl rm /atomic.io/network/config 删除配置 在这制作说明
启动[root@localhost ~]#  systemctl enable flanneld.service
Created symlink from /etc/systemd/system/multi-user.target.wants/flanneld.service to /usr/lib/systemd/system/flanneld.service.
Created symlink from /etc/systemd/system/docker.service.requires/flanneld.service to /usr/lib/systemd/system/flanneld.service.
[root@localhost ~]#  systemctl start flanneld.service
[root@localhost ~]# ps -ef|grep flannel
root      5017     1  0 15:37 ?        00:00:00 /usr/bin/flanneld -etcd-endpoints=http://etcd:2379 -etcd-prefix=/atomic.io/network
root      5066  4176  0 15:37 pts/0    00:00:00 grep --color=auto flannel
重启docker ,让docker网络重新初始化
[root@localhost ~]# systemctl restart docker

 node2操作

[root@localhost ~]# hostnamectl --static set-hostname  node-2
[root@localhost ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.105.10.241    node-1
10.105.10.241    etcd
10.105.10.242    node-2
x:
[root@localhost ~]#  systemctl disable firewalld.service
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# yum install -y docker
[root@localhost ~]# yum install flannel -y
[root@localhost ~]# vim /etc/sysconfig/flanneld

# Flanneld configuration options  

# etcd url location.  Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS="http://etcd:2379"

# etcd config key.  This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_PREFIX="/atomic.io/network"

# Any additional options that you want to pass
x:
[root@localhost ~]# ping etcd 检查网络通信
PING etcd (10.105.10.241) 56(84) bytes of data.
64 bytes from node-1 (10.105.10.241): icmp_seq=1 ttl=64 time=0.445 ms
[root@localhost ~]# systemctl start flanneld.service
[root@localhost ~]# systemctl restart docker

 验证

在每个节点部署一个容器

node-1 node-2

[root@node-1~]# docker pull centos
[root@node-1 ~]# docker run -itd --name=node1.sys centos
91bd5a287b942c0b096307c189a4c82858fc70d0b3ced401872a73d6ebda356e
[root@node-1 ~]# 
[root@node-1 ~]# 
[root@node-1 ~]# 
[root@node-1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
91bd5a287b94        centos              "/bin/bash"         6 seconds ago       Up 5 seconds                            node1.sys
[root@node-1 ~]# docker attach node1.sys 
[root@91bd5a287b94 /]# ifconfig 
bash: ifconfig: command not found
[root@91bd5a287b94 /]# yum -y install net-tools //安装ifconfig 命令
Loaded plugins: fastestmirror, ovl

。。。略
[root@91bd5a287b94 /]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1472
        inet 172.20.83.2  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe14:5302  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:14:53:02  txqueuelen 0  (Ethernet)
        RX packets 5979  bytes 10890074 (10.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6038  bytes 442770 (432.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@node-2 ~]# docker pull centos
[root@node-2 ~]# docker run -itd --name=node2.sys  centos
46b36dbea6efbc31c27c78d14b63f6271649407ee739656adc84490a39695778
[root@node-2 ~]# 
[root@node-2 ~]# 
[root@node-2 ~]# docker attach node2.sys 
[root@46b36dbea6ef /]# yum -y install net-tools
[root@46b36dbea6ef /]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1472
        inet 172.20.57.2  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe14:3902  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:14:39:02  txqueuelen 0  (Ethernet)
        RX packets 6104  bytes 11023441 (10.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6173  bytes 454525 (443.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 ping测试

node-1

[root@91bd5a287b94 /]# ping 172.20.57.2
PING 172.20.57.2 (172.20.57.2) 56(84) bytes of data.
64 bytes from 172.20.57.2: icmp_seq=1 ttl=60 time=0.548 ms
64 bytes from 172.20.57.2: icmp_seq=2 ttl=60 time=0.282 ms

 node-2

[root@46b36dbea6ef /]# ping 172.20.83.2
PING 172.20.83.2 (172.20.83.2) 56(84) bytes of data.
64 bytes from 172.20.83.2: icmp_seq=1 ttl=60 time=0.255 ms
64 bytes from 172.20.83.2: icmp_seq=2 ttl=60 time=0.327 ms
posted @ 2017-11-28 17:19  91King  阅读(642)  评论(0编辑  收藏  举报