19.網絡插件calico

19.網絡插件calico

官網: https://docs.projectcalico.org/v3.8/introduction/
calico默認工做在192.168.0.0/16 的網絡
calico還不支持ipvshtml

====node

Egres 出站
Ingress 入站
networkpolicy 網絡策略,簡寫 netpolapi

查看網絡策略幫助:
kubectl explain networkpolicy網絡

查看某名稱空間的網絡策略:
[root@k8s-master networkpoliy]# kubectl get netpol -n dev
NAME POD-SELECTOR AGE
deny-all-ingress 73s app

====
calico有多種部署方法,咱們這裏介紹的是使用flannel和calico混用的方法。這裏也比較推薦這種方法。
flannel負責網絡部分的功能,而calico咱們只使用它的網絡策略的功能。curl

calico部署:
1. 另外單獨部署一個etcd
官方文檔:
https://docs.projectcalico.org/v3.8/getting-started/kubernetes/installation/flannelide

2. 公用集羣的etcd
wget https://docs.projectcalico.org/v3.8/manifests/canal.yaml
kubectl apply -f canal.yaml  

[root@k8s-master networkpoliy]# kubectl get pods -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
canal-j9mlh                          2/2     Running   0          97m
canal-jgj2j                          2/2     Running   0          97m

=====================測試

calico實例:url

[root@k8s-master networkpoliy]# kubectl create namespace dev
namespace/dev created
[root@k8s-master networkpoliy]# kubectl create namespace prod
namespace/prod created


[root@k8s-master networkpoliy]# cat ingress.yaml 
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-ingress
  namespace: dev

spec:
  podSelector: {}  #空表示選擇該名稱空間的全部pod
  policyTypes:       #當這裏寫了Ingress,則Ingress中定義的規則生效,若是規則爲空則爲所有拒絕。這裏沒有寫Egres,則表示Egres規則不生效,默認全開。
  - Ingress


[root@k8s-master networkpoliy]# cat pod-a.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1


[root@k8s-master networkpoliy]# kubectl apply -f  pod-a.yaml -n dev
pod/pod1 created

[root@k8s-master networkpoliy]# kubectl apply -f  pod-a.yaml -n prod
pod/pod1 created

[root@k8s-master networkpoliy]# kubectl get pods -n dev -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
pod1   1/1     Running   0          31s   10.244.1.2   k8s-node1   <none>           <none>


[root@k8s-master networkpoliy]# kubectl get pods -n prod -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
pod1   1/1     Running   0          11s   10.244.1.3   k8s-node1   <none>           <none>

[root@k8s-master networkpoliy]# curl 10.244.1.2 
^C
[root@k8s-master networkpoliy]# curl 10.244.1.3
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

dev的ns咱們應用了策略,因此經過curl請求不到數據,prod的ns中咱們沒有應用網絡策略,因此咱們能夠經過curl正常請求到。

添加入站規則

ingress:
  - {}       #這個{}寫法表示容許全部的入站,allow all

[root@k8s-master networkpoliy]# cat ingress.yaml 
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-ingress
  namespace: dev

spec:
  podSelector: {}  #空表示選擇該名稱空間的全部pod
  ingress:
  - {}       #這個{}寫法表示容許全部的入站,allow all
  policyTypes:       #當這裏寫了Ingress,則Ingress中定義的規則生效,若是規則爲空則爲所有拒絕。這裏沒有寫Egres,則表示Egres規則不生效,默認全開。

[root@k8s-master networkpoliy]# kubectl apply -f ingress.yaml -n dev
networkpolicy.networking.k8s.io/deny-all-ingress configured

[root@k8s-master networkpoliy]# curl 10.244.1.2 
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

當咱們修改策略容許dev上全部能夠入站後能夠正常請求到10.244.1.2了。

添加一個入站規則,容許10.244.0.0/16這個網段的除了10.244.1.3這個ip,均可以訪問pod的80端口,使用podSelector來匹配pod

[root@k8s-master networkpoliy]# cat allow-netpol.demo.yaml 
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-myapp-ingress
spec:
  podSelector:
    matchLabels:
      app: myapp
  ingress:
  - from:       #這裏不寫from則容許全部能夠訪問
    - ipBlock:         
        cidr: 10.244.0.0/16               #指定網段
        except: 
        - 10.244.1.3/32           #除開這個ip
    ports:
    - protocol: TCP
      port: 80

[root@k8s-master networkpoliy]# kubectl apply -f allow-netpol.demo.yaml -n dev

[root@k8s-master networkpoliy]#  telnet 10.244.1.2 80
Trying 10.244.1.2...
Connected to 10.244.1.2.
Escape character is '^]'.

在本地測試,能夠正常鏈接

[root@k8s-master networkpoliy]# kubectl exec pod1 -it -n prod -- /bin/sh
/ # telnet 10.244.1.2 80

登陸到10.244.1.3這個pod上,則測試telnet 80端口不通,說明策略生效。

添加一個出站策略:

拒絕全部出站策略:

[root@k8s-master networkpoliy]# cat egress.yaml 
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-egress
  namespace: prod

spec:
  podSelector: {}  #空表示選擇該名稱空間的全部pod
  policyTypes:       #當這裏寫了Ingress,則Ingress中定義的規則生效,若是規則爲空則爲所有拒絕。這裏沒有寫Egres,則表示Egres規則不生效,默認全開。
  - Egress


[root@k8s-master networkpoliy]# kubectl apply -f egress.yaml  -n prod

[root@k8s-master networkpoliy]# kubectl exec pod1 -it -n prod -- /bin/sh
/ # ping 10.250.0.89
PING 10.250.0.89 (10.250.0.89): 56 data bytes


ping k8s-master上的kube-scheduler-k8s-master 的ip發現ping不通,策略生效。


容許全部出去的策略:
[root@k8s-master networkpoliy]# cat egress.yaml 
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-egress
  namespace: prod

spec:
  podSelector: {}  #空表示選擇該名稱空間的全部pod
  egress:
  - {}
  policyTypes:       #當這裏寫了Ingress,則Ingress中定義的規則生效,若是規則爲空則爲所有拒絕。這裏沒有寫Egres,則表示Egres規則不生效,默認全開。
  - Egress

[root@k8s-master networkpoliy]# kubectl apply -f egress.yaml  -n prod
networkpolicy.networking.k8s.io/deny-all-egress configured

[root@k8s-master networkpoliy]# kubectl exec pod1 -it -n prod -- /bin/sh
/ # ping 10.250.0.89
PING 10.250.0.89 (10.250.0.89): 56 data bytes
64 bytes from 10.250.0.89: seq=119 ttl=63 time=0.384 ms
64 bytes from 10.250.0.89: seq=120 ttl=63 time=0.505 ms
64 bytes from 10.250.0.89: seq=121 ttl=63 time=0.373 ms
64 bytes from 10.250.0.89: seq=122 ttl=63 time=0.360 ms
64 bytes from 10.250.0.89: seq=123 ttl=63 time=0.504 ms

ping通了,策略生效。

通常對於網絡策略設置的規範:spa

對於網絡策略來說: 名稱空間: 拒絕全部出站,入站規則 方形全部出站目標名稱空間內的全部Pod

相關文章
相關標籤/搜索