網絡策略--簡單示例

本示例使用基于calico的網絡策略實驗。

實驗目的,使用策略規(guī)則,建立簡單的網絡隔離。

創(chuàng)建命名空間 policy-demo

kubectl create ns policy-demo

創(chuàng)建 demo pod

1、 在命名空間中創(chuàng)建nginx pod

kubectl create deployment --namespace=policy-demo nginx --image=nginx

2、開放service端口

kubectl expose --namespace=policy-demo deployment nginx --port=80

3、確認nginx service能夠訪問
創(chuàng)建一個busybox,使用wget命令驗證

kubectl run --namespace=policy-demo access --rm -ti --image busybox /bin/sh
[root@k8s-master ~]# kubectl run --namespace=policy-demo access --rm -ti --image busybox /bin/sh
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
If you don't see a command prompt, try pressing enter.
/ # wget -q nginx -O -
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

使用策略隔離

創(chuàng)建一個命名空間policy-demo中所有pod都默認給拒絕的行為。

kubectl create -f - <<EOF
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: default-deny
  namespace: policy-demo
spec:
  podSelector:
    matchLabels: {}
EOF

隔離驗證

阻止所有要訪問nginx service

kubectl run --namespace=policy-demo access --rm -ti --image busybox /bin/sh
wget -q --timeout=5 nginx -O -
wget: download timed out

允許使用網絡策略訪問

kubectl create -f - <<EOF
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: access-nginx
  namespace: policy-demo
spec:
  podSelector:
    matchLabels:
      app: nginx
  ingress:
    - from:
      - podSelector:
          matchLabels:
            run: access
EOF

這個策略規(guī)則允許流量從帶標簽run: access的pod到達帶標簽app: nginx。

現(xiàn)在能夠從access的pod訪問service

kubectl run --namespace=policy-demo cant-access --rm -ti --image busybox /bin/sh
wget -q --timeout=5 nginx -O -
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • https://edu.aliyun.com/course/1651 第一章 云原生課 1.1 云原生技術發(fā)展歷程...
    MichelleZm閱讀 1,889評論 0 0
  • 1、Network 接下來就要說到跟Kubernetes網絡通信相關的內容,我們都知道K8S最小的操作單位是Pod...
    Suny____閱讀 609評論 0 3
  • 久違的晴天,家長會。 家長大會開好到教室時,離放學已經沒多少時間了。班主任說已經安排了三個家長分享經驗。 放學鈴聲...
    飄雪兒5閱讀 7,805評論 16 22
  • 創(chuàng)業(yè)是很多人的夢想,多少人為了理想和不甘選擇了創(chuàng)業(yè)來實現(xiàn)自我價值,我就是其中一個。 創(chuàng)業(yè)后,我由女人變成了超人,什...
    亦寶寶閱讀 1,993評論 4 1
  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友。感恩相遇!感恩不離不棄。 中午開了第一次的黨會,身份的轉變要...
    余生動聽閱讀 10,805評論 0 11

友情鏈接更多精彩內容