您好,登錄后才能下訂單哦!
如何解析k8s集群Canal的網(wǎng)絡(luò)控制,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
1 簡介
Canal 是calico和flannel的結(jié)合,我們因為Calico不僅能夠提供很好的網(wǎng)絡(luò)性能,還能有網(wǎng)絡(luò)規(guī)則控制,但是我們很多時候使用的Flannel網(wǎng)絡(luò),他的缺點就是沒有網(wǎng)絡(luò)控制只能提供網(wǎng)絡(luò)服務(wù),所以Canal誕生了,為Falanel提供了網(wǎng)絡(luò)控制。
網(wǎng)絡(luò)策略 控制出站: egress 控制進(jìn)站: ingress 可以以ip地址塊定義規(guī)則,也可以基于命名空間規(guī)則定義,還可以對pod定義規(guī)則
2 部署Canal
curl \ https://docs.projectcalico.org/v3.6/getting-started/kubernetes/installation/hosted/canal/canal.yaml \ -O kubectl apply -f canal.yaml 1)canal默認(rèn)控制的是10.244.0.0/16 ,如果你在安裝k8s集群是設(shè)置的pod網(wǎng)絡(luò)不是此段網(wǎng)絡(luò),則你需要修改配置文件 2)我們采用的和k8s集群公用etcd的方式創(chuàng)建 查看創(chuàng)建的pod # kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE canal-9lmw8 2/2 Running 0 4m50s canal-ln6kg 2/2 Running 0 4m50s canal-mx6tn 2/2 Running 0 4m50s coredns-fb8b8dccf-lfbkh 1/1 Running 2 10d coredns-fb8b8dccf-t2kdz 1/1 Running 2 10d etcd-master-1 1/1 Running 2 10d kube-apiserver-master-1 1/1 Running 2 10d kube-controller-manager-master-1 1/1 Running 2 10d kube-flannel-ds-amd64-df7gk 1/1 Running 3 9d kube-flannel-ds-amd64-dzxfd 1/1 Running 3 9d kube-flannel-ds-amd64-mgw2m 1/1 Running 2 9d kube-proxy-47d6q 1/1 Running 2 10d kube-proxy-jztrs 1/1 Running 3 10d kube-proxy-rt4xx 1/1 Running 3 10d kube-scheduler-master-1 1/1 Running 2 10d kubernetes-dashboard-5f7b999d65-8h79h 1/1 Running 0 21h
3 創(chuàng)建兩個實驗的空間
kubectl create namespace shengchan kubectl create namespace test
4 創(chuàng)建網(wǎng)絡(luò)規(guī)則測試
默認(rèn)ingress 和 engress可以一起使用,當(dāng)這兩規(guī)則同時存在那么將使用policyTypes做具體的規(guī)則指向 例如:創(chuàng)建一個policy默認(rèn)拒絕所有在空間shengchan的入站規(guī)則 # cat ingress-deny.yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-all-ingress namespace: shengchan spec: podSelector: {} policyTypes: - Ingress kubectl apply ingress-deny.yaml 創(chuàng)建一組屬于shengchan空間的pod # cat pod-networkpolicy1.yaml apiVersion: v1 kind: Pod metadata: name: pod-test spec: containers: - name: pod-test-policy image: nginx:1.10 kubectl apply -f pod-networkpolicy1.yaml -n shengchan 創(chuàng)建一組屬于test空間的pod kubectl apply -f pod-networkpolicy1.yaml -n test 分別查看兩組pod的IP地址 # kubectl get pods -o wide -n shengchan NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod-test 1/1 Running 0 16s 10.244.1.2 node2 <none> <none> [root@master-1 ~]# kubectl get pods -o wide -n test NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod-test 1/1 Running 0 9s 10.244.2.2 node1 <none> <none> 在宿主機(jī)上面分別ping這兩個ip # ping 10.244.1.2 -c 1 PING 10.244.1.2 (10.244.1.2) 56(84) bytes of data. ^C --- 10.244.1.2 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms # ping 10.244.2.2 -c 1 PING 10.244.2.2 (10.244.2.2) 56(84) bytes of data. 64 bytes from 10.244.2.2: icmp_seq=1 ttl=63 time=0.660 ms --- 10.244.2.2 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.660/0.660/0.660/0.000 ms 此時發(fā)現(xiàn)屬于shengchan空間的pod是被拒絕訪問的,屬于test空間的pod正常訪問 此時我們是基于名稱空間級別的入站網(wǎng)絡(luò)訪問策略
5 將上面的默認(rèn)拒絕策略放行
修改配置文件 apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-all-ingress namespace: shengchan spec: podSelector: {} ingress: - {} policyTypes: - Ingress kubectl apply -f ingress-deny.yaml
再次測試10.244.1.2
[root@master-1 ~]# curl -I 10.244.1.2 HTTP/1.1 200 OK Server: nginx/1.10.3 Date: Sat, 27 Apr 2019 16:58:04 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT Connection: keep-alive ETag: "5890a6b7-264" Accept-Ranges: bytes [root@master-1 ~]# curl -I 10.244.2.2 HTTP/1.1 200 OK Server: nginx/1.10.3 Date: Sat, 27 Apr 2019 16:58:07 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT Connection: keep-alive ETag: "5890a6b7-264" Accept-Ranges: bytes 說明我們的網(wǎng)絡(luò)策略放行成功
6 放行特定的入站
以標(biāo)簽的形式進(jìn)行劃分pod入站規(guī)則是否被放行
1)回復(fù)默認(rèn)拒絕所有 [root@master-1 ~]# cat ingress-deny.yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-all-ingress namespace: shengchan spec: podSelector: {} policyTypes: - Ingress kubectl apply -f ingress-deny.yaml 2)給shengchan pod打一個標(biāo)簽 kubectl label pods pod-test app=hello -n shengchan 3)創(chuàng)建一個新的規(guī)則 # vim ingress-allow.yml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-hello-ingress namespace: shengchan spec: podSelector: matchLabels: app: hello ingress: - from: - ipBlock: cidr: 10.244.0.0/16 except: - 10.244.2.2/32 ports: - protocol: TCP port: 80 解釋: 我們重新定義了一個policy,通過標(biāo)簽匹配到我們已有的pod(我們一已有的pod是拒絕所有訪問的),定義ingres規(guī)則,如果不通過from方式定義默認(rèn)是所有網(wǎng)段都可以訪問到擁有這個標(biāo)簽的pod為了測試我們使用from方式去拒絕10.244.0.0但是會放行10.244.2.2因為我們有這個ip的pod創(chuàng)建萬郴更可以進(jìn)行測試ports就是指定特定開放的端口 4)創(chuàng)建之前進(jìn)行訪問測試 # kubectl get pods -n shengchan -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod-test 1/1 Running 0 62m 10.244.1.2 node2 <none> <none> # curl 10.244.1.2 說明無法訪問 5)創(chuàng)建之后測試 kubectl apply -f ingress-allow.yml [root@master-1 ~]# curl -I 10.244.1.2 HTTP/1.1 200 OK Server: nginx/1.10.3 Date: Sat, 27 Apr 2019 17:32:15 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT Connection: keep-alive ETag: "5890a6b7-264" Accept-Ranges: bytes 說明已經(jīng)放行 至于對于10.244.0.0網(wǎng)段的限制有興趣可以測試一下
6 egress出站規(guī)則
1)設(shè)置一個拒絕所有出站policy cat egress.yml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-all-egress namespace: test spec: podSelector: {} policyTypes: - Egress kubectl apply -f egress.yml 2)創(chuàng)建一個test命名空間下的pods kubectl apply -f pod-networkpolicy1.yaml -n test 3)查看podIP并進(jìn)去容器去測試ping一個正常的podip [root@master-1 ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES net-test-5764c456cb-2c9df 1/1 Running 24 24h 10.244.2.22 node1 <none> <none> net-test-5764c456cb-ng6vh 1/1 Running 46 6d5h 10.244.1.21 node2 <none> <none> [root@master-1 ~]# kubectl get pods -o wide -n test NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod-test 1/1 Running 0 86m 10.244.2.2 node1 <none> <none> kubectl exec -it pod-test -n test -- /bin/bash root@pod-test:/# ping 10.244.1.21 PING 10.244.1.21 (10.244.1.21): 56 data bytes ^C--- 10.244.1.21 ping statistics --- 3 packets transmitted, 0 packets received, 100% packet loss 說明此時已經(jīng)應(yīng)用策略 4)修改配置文件放行所有出站 # cat egress.yml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-all-egress namespace: test spec: podSelector: {} egress: - {} policyTypes: - Egress kubectl apply -f egress.yml 測試: root@pod-test:/# ping 10.244.1.21 -c 1 PING 10.244.1.21 (10.244.1.21): 56 data bytes 64 bytes from 10.244.1.21: icmp_seq=0 ttl=62 time=0.939 ms --- 10.244.1.21 ping statistics --- 1 packets transmitted, 1 packets received, 0% packet loss round-trip min/avg/max/stddev = 0.939/0.939/0.939/0.000 ms 此時說明已經(jīng)放行成功
7 更加復(fù)雜的policy規(guī)則需要根據(jù)不同的環(huán)境規(guī)劃進(jìn)行深入探究,其所有都規(guī)則都體現(xiàn)在配置文件中
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。