跨主机容器间通信-calico

docker/calico

Posted by Xiaolei.liang on May 13, 2019 本文总阅读量

安装etcd

  1. 下载etcdv3 版本: wget https://github.com/etcd-io/etcd/releases/download/v3.3.13/etcd-v3.3.13-linux-amd64.tar.gz -O /tmp/etcd-v3.3.13-linux-amd64.tar.gz
  2. 启动etcd:
    • node0
      cd /opt
      tar -xzvf /tmp/etcd-v3.3.13-linux-amd64.tar.gz
      cd etcd-v3.3.13-linux-amd64
      nohup ./etcd --name node0 --initial-advertise-peer-urls http://192.168.17.200:2380 --listen-peer-urls http://0.0.0.0:2380 --listen-client-urls http://0.0.0.0:2379,http://127.0.0.1:4001 --advertise-client-urls http://0.0.0.0:2379 --initial-cluster-token etcd-cluster --initial-cluster node0=http://192.168.17.200:2380,node1=http://192.168.17.201:2380,node2=http://192.168.17.202:2380 --initial-cluster-state new >> /tmp/etcd.log 2>&1 &
      
    • node1
      cd /opt
      tar -xzvf /tmp/etcd-v3.3.13-linux-amd64.tar.gz
      cd etcd-v3.3.13-linux-amd64
      nohup ./etcd --name node1 --initial-advertise-peer-urls http://192.168.17.201:2380 --listen-peer-urls http://0.0.0.0:2380 --listen-client-urls http://0.0.0.0:2379,http://127.0.0.1:4001 --advertise-client-urls http://0.0.0.0:2379 --initial-cluster-token etcd-cluster --initial-cluster node0=http://192.168.17.200:2380,node1=http://192.168.17.201:2380,node2=http://192.168.17.202:2380 --initial-cluster-state new >> /tmp/etcd.log 2>&1 &
      
    • node2
      cd /opt
      tar -xzvf /tmp/etcd-v3.3.13-linux-amd64.tar.gz
      cd etcd-v3.3.13-linux-amd64
      nohup ./etcd --name node2 --initial-advertise-peer-urls http://192.168.17.202:2380 --listen-peer-urls http://0.0.0.0:2380 --listen-client-urls http://0.0.0.0:2379,http://127.0.0.1:4001 --advertise-client-urls http://0.0.0.0:2379 --initial-cluster-token etcd-cluster --initial-cluster node0=http://192.168.17.200:2380,node1=http://192.168.17.201:2380,node2=http://192.168.17.202:2380 --initial-cluster-state new >> /tmp/etcd.log 2>&1 &
      

创建bridge

  1. 执行命令
  2. Node0:
    $ yum install -y vim docker bridge-utils && yum clean all
    $ systemctl disable firewalld.service
    $ systemctl stop firewalld.service
    $ ip link set dev docker0 down
    $ brctl delbr docker0
    $ brctl addbr bridge0
    $ ip addr add 192.168.10.1/24 dev bridge0
    $ ip link set dev bridge0 up
    $ sed -i 's/--selinux-enabled --log-driver=journald/--selinux-enabled --log-driver=journald -b=bridge0/g' /etc/sysconfig/docker
    $ systemctl start docker
    
  3. Node1:
    $ yum install -y vim docker bridge-utils && yum clean all
    $ systemctl disable firewalld.service
    $ systemctl stop firewalld.service
    $ ip link set dev docker0 down
    $ brctl delbr docker0
    $ brctl addbr bridge0
    $ ip addr add 192.168.20.1/24 dev bridge0
    $ ip link set dev bridge0 up
    $ sed -i 's/--selinux-enabled --log-driver=journald/--selinux-enabled --log-driver=journald -b=bridge0/g' /etc/sysconfig/docker
    $ systemctl start docker
    
  4. Node2:
    $ yum install -y vim docker bridge-utils && yum clean all
    $ systemctl disable firewalld.service
    $ systemctl stop firewalld.service
    $ ip link set dev docker0 down
    $ brctl delbr docker0
    $ brctl addbr bridge0
    $ ip addr add 192.168.30.1/24 dev bridge0
    $ ip link set dev bridge0 up
    $ sed -i 's/--selinux-enabled --log-driver=journald/--selinux-enabled --log-driver=journald -b=bridge0/g' /etc/sysconfig/docker
    $ systemctl start docker
    

使用calicoctl部署calico

  1. Wget http://www.projectcalico.org/builds/calicoctl -O /tmp/calicoctl
  2. Cp /tmp/calicoctl /usr/local/bin && chmod u+x /usr/local/bin/calicoctl
  3. 执行命令:
    • Node0: ETCD_ENDPOINTS=http://node0:2379,http://node1:2379,http://node2:2379 calicoctl node run --ip=192.168.17.200
    • Node1: ETCD_ENDPOINTS=http://node0:2379,http://node1:2379,http://node2:2379 calicoctl node run --ip=192.168.17.201
    • Node2: ETCD_ENDPOINTS=http://node0:2379,http://node1:2379,http://node2:2379 calicoctl node run --ip=192.168.17.202
  4. Calicoctl node status

使用calicoctl创建ippool

  1. Node0:
    $ cat ipPool0.yaml
    apiVersion: projectcalico.org/v3
    kind: IPPool
    metadata:
      name: new-pool1
    spec:
      cidr: 192.168.10.0/24
      ipipMode: Never
      natOutgoing: true
    $ ETCD_ENDPOINTS=http://node0:2379,http://node1:2379,http://node2:2379 calicoctl create -f ipPool0.yaml
    $ calicoctl get ippool -o wide
    
  2. Node1:
    $ cat ipPool1.yaml
    apiVersion: projectcalico.org/v3
    kind: IPPool
    metadata:
      name: new-pool2
    spec:
      cidr: 192.168.20.0/24
      ipipMode: Never
      natOutgoing: true
    $  ETCD_ENDPOINTS=http://node0:2379,http://node1:2379,http://node2:2379 calicoctl create -f ipPool1.yaml
    $ calicoctl get ippool -o wide
    
  3. Node2:
    apiVersion: projectcalico.org/v3
    kind: IPPool
    metadata:
      name: new-pool3
    spec:
      cidr: 192.168.30.0/24
      ipipMode: Never
      natOutgoing: true
    $ ETCD_ENDPOINTS=http://node0:2379,http://node1:2379,http://node2:2379 calicoctl create -f ipPool2.yaml
    $ calicoctl get ippool -o wide
    

不同机器上创建容器验证

在不同主机的容器上ping互通