1. 程式人生 > >[k8s]docker calico網絡

[k8s]docker calico網絡

releases protoc blog ati .project /usr 通信 cde name

#!/usr/bin/env bash


docker stats

vim /usr/lib/systemd/system/docker.service
# /etc/systemd/system/docker.service
--cluster-store=etcd://192.168.14.132:2379

systemctl daemon-reload
systemctl restart docker.service

[root@node1 ~]# ps -ef|grep docker
root       8122      1  0 Nov07 ?        00:01:01 /usr/bin/dockerd --cluster-store=etcd://192.168.14.132:2379

etcdctl ls
/docker


cd /usr/local/bin
wget https://github.com/projectcalico/calicoctl/releases/download/v1.6.1/calicoctl
chmod +x calicoctl

[root@node1 ~]# rpm -qa|grep etcd
etcd-3.2.5-1.el7.x86_64

cat >> /etc/calico/calicoctl.cfg <<EOF
apiVersion: v1
kind: calicoApiConfig
metadata:
spec:
  datastoreType: "etcdv2"
  etcdEndpoints: "http://192.168.14.132:2379"
EOF

calicoctl node run
calicoctl node run --ip=192.168.14.132

1,開啟ip_forward
2,下載calico-node鏡像,並啟動
3,寫入etcd信息

iptables -P FORWARD ACCEPT
etcdctl rm --recursive /calico
etcdctl rm --recursive /docker

# 可以看到bgp鄰居已經建立起來了(14.132 14.133)
calicoctl node status

# 任意一臺機器創建網絡,另一臺機器會同步過去的
docker network rm cal_net1
docker network create --driver calico --ipam-driver calico-ipam cal_net1

#+++++++++++++++++++++++++++
#  測試
#+++++++++++++++++++++++++++
# 14.132
docker container run --net cal_net1 --name bbox1 -tid busybox
docker exec bbox1 ip address
docker exec bbox1 route -n

# 14.133
docker container run --net cal_net1 --name bbox2 -tid busybox


docker exec bbox2 ip address
docker exec bbox2 ping  192.168.108.128


#+++++++++++++++++++++++++++
#  參考
#+++++++++++++++++++++++++++
https://mp.weixin.qq.com/s/VL72aVjU4KB3c2UTihl-DA
http://blog.csdn.net/felix_yujing/article/details/55213239


#+++++++++++++++++++++++++++
#  創建網段
#+++++++++++++++++++++++++++
calicoctl node status
calicoctl get ipPool
- apiVersion: v1
  kind: ipPool
  metadata:
    cidr: 10.20.0.0/24
  spec:
    ipip:
      enabled: true
    nat-outgoing: true


另外一個測試
docker network create --driver calico --ipam-driver calico-ipam  --subnet 10.30.0.0/24 net1

docker network create --driver calico --ipam-driver calico-ipam  --subnet 10.30.0.0/24 net1
docker network create --driver calico --ipam-driver calico-ipam  --subnet 10.30.0.0/24 net2
docker network create --driver calico --ipam-driver calico-ipam  --subnet 10.30.0.0/24 net3

#node1
docker run --net net1 --name workload-A -tid busybox
docker run --net net2 --name workload-B -tid busybox
docker run --net net1 --name workload-C -tid busybox
#node2
docker run --net net3 --name workload-D -tid busybox
docker run --net net1 --name workload-E -tid busybox



#同一網絡內的容器(即使不在同一節點主機上)可以使用容器名來訪問
docker exec workload-A ping -c 4 workload-C.net1
docker exec workload-A ping -c 4 workload-E.net1
#不同網絡內的容器需要使用容器ip來訪問(使用容器名會報:bad address)
docker exec workload-A ping -c 2  `docker inspect --format "{{ .NetworkSettings.Networks.net2.IPAddress }}" workload-B`


#calico默認策略,同一網絡內的容器是能相互通信的;不同網絡內的容器相互是不通的。不同節點上屬於同一網絡的容器也是能相互通信的,這樣就實現了容器的跨主機互連。



#+++++++++++++++++++++++++++
#  修改默認策略
#+++++++++++++++++++++++++++

cat << EOF | calicoctl apply -f -
- apiVersion: v1
  kind: profile
  metadata:
    name: cal_net12icmp
    labels:
      role: database
  spec:
    ingress:
    - action: allow
      protocol: icmp
      source:
        tag: net1
      destination:
        tag: net2
EOF





https://docs.projectcalico.org/v2.2/reference/public-cloud/aws
$ calicoctl apply -f - << EOF
apiVersion: v1
kind: ipPool
metadata:
  cidr: 192.168.0.0/16
spec:
  ipip:
    enabled: true
    mode: cross-subnet
  nat-outgoing: true
EOF

參考:
Docker網絡解決方案-Calico部署記錄
https://allgo.cc/2015/04/16/centos7%E7%BD%91%E5%8D%A1%E6%A1%A5%E6%8E%A5/
yum install bridge-utils
calico原理
http://www.cnblogs.com/kevingrace/p/6864804.html



#!/usr/bin/env bash
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-udp-ingress-controller
  labels:
    k8s-app: nginx-udp-ingress-lb
  namespace: kube-system
spec:
  replicas: 1
  selector:
    k8s-app: nginx-udp-ingress-lb
  template:
    metadata:
      labels:
        k8s-app: nginx-udp-ingress-lb
        name: nginx-udp-ingress-lb
    spec:
      hostNetwork: true
      terminationGracePeriodSeconds: 60
      containers:
      #- image: gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.8
      - image: 192.168.1.103/k8s_public/nginx-ingress-controller:0.9.0-beta.5
        name: nginx-udp-ingress-lb
        readinessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
        livenessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          timeoutSeconds: 1
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
        ports:
        - containerPort: 81
          hostPort: 81
        - containerPort: 443
          hostPort: 443
        - containerPort: 53
          hostPort: 53
        args:
        - /nginx-ingress-controller
        - --default-backend-service=$(POD_NAMESPACE)/default-http-backend
        - --udp-services-configmap=$(POD_NAMESPACE)/nginx-udp-ingress-configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: udp-configmap-example
data:
  53: "kube-system/kube-dns:53"

[k8s]docker calico網絡