06走进云计算-Docker跨主机通讯

file

Docker跨主机网络类型:

静态路由
flannel (k8s)
overlay
macvlan
calico

静态路由

file

flannel

file

部署etcd

## 1.安装etcd
yum install -y etcd

## 2.修改配置文件
#[Member]
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://10.0.0.51:2379,http://127.0.0.1:2379"
ETCD_NAME="default"

#[Clustering]
ETCD_ADVERTISE_CLIENT_URLS="http://10.0.0.51:2379"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

## 3.启动etcd
[root@db01 ~]# systemctl start etcd

## 4.测试
[root@db01 ~]# etcdctl -C http://10.0.0.51:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://10.0.0.51:2379
cluster is healthy
[root@db01 ~]# etcdctl -C http://10.0.0.51:2379 set /testdir/testkey "hello world"
hello world
[root@db01 ~]# etcdctl -C http://10.0.0.51:2379 get /testdir/testkey
hello world

部署flannel

## 安装flannel
[root@db01 ~]# yum install -y flannel
[root@db02 ~]# yum install -y flannel

## 修改flannel配置
[root@db02 ~]# vim /etc/sysconfig/flanneld
FLANNEL_ETCD_ENDPOINTS="http://10.0.0.51:2379"
FLANNEL_ETCD_PREFIX="/atomic.io/network"

[root@db01 ~]# vim /etc/sysconfig/flanneld
FLANNEL_ETCD_ENDPOINTS="http://10.0.0.51:2379"
FLANNEL_ETCD_PREFIX="/atomic.io/network"

## 创建etcd配置
etcdctl mk /atomic.io/network/config '{"Network":"172.17.0.0/16"}'

## 启动flannel
[root@db01 ~]# systemctl start flanneld.service 
[root@db02 ~]# systemctl start flanneld.service

关联docker和flannel

[root@db01 ~]# vim /usr/lib/systemd/system/docker.service
EnviromentFile=/run/flannel/docker
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_NETWORK_OPTIONS

[root@docker ~]# cat /run/flannel/docker 
DOCKER_OPT_BIP="--bip=172.16.89.1/24"
DOCKER_OPT_IPMASQ="--ip-masq=true"
DOCKER_OPT_MTU="--mtu=1472"
DOCKER_NETWORK_OPTIONS=" --bip=172.16.89.1/24 --ip-masq=true --mtu=1472"

## 开启forward转发
echo 1 > /proc/sys/net/ipv4/ip_forward

# 如果开启转发不行,就执行以下命令
## iptables开启forward功能
[root@node-1 ~]# iptables -P FORWARD ACCEPT
[root@node-1 ~]# iptables -F

Dcoker跨主机容器通信之overlay

file

http://www.cnblogs.com/CloudMan6/p/7270551.html

docker03上: consul存储ip地址的分配

docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap

设置容器的主机名

consul:kv类型的存储数据库(key:value)

docker01、02上:
vim  /etc/docker/daemon.json
{
  "cluster-store": "consul://10.0.0.13:8500",
  "cluster-advertise": "10.0.0.11:2376"
}

vim /usr/lib/systemd/system/docker.service
systemctl daemon-reload 
systemctl restart docker

2)创建overlay网络
docker network create -d overlay --subnet 172.16.2.0/24 --gateway 172.16.2.254 ol1

3)启动容器测试

docker run -it --network ol1 --name oldboy01 busybox /bin/sh
每个容器有两块网卡,eth0实现容器间的通讯,eth1实现容器访问外网

Docker跨主机容器之间的通信macvlan

默认一个物理网卡,只有一个物理mac地址,虚拟多个mac地址

## 创建macvlan网络
docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=eth0 macvlan_1

## 设置eth0的网卡为混杂模式
ip link set eth0 promisc on

## 创建使用macvlan网络的容器
docker run -it --network macvlan_1 --ip=10.0.0.200 busybox

山林不向四季起誓 荣枯随缘