前提条件

Nginx サービスがポッドへアクセスが可能か確認する

  1. 以下のコマンドを実行し、Nginx アプリケーションを作成して、サービス名 "Nginx" を通じてアプリケーションを公開します。
    $ kubectl run nginx --image=nginx
    deployment.apps/nginx created
    $ kubectl get pod
    NAME                     READY   STATUS    RESTARTS   AGE
    nginx-64f497f8fd-znbxb   1/1     Running   0          45s
    
    $ kubectl expose deployment nginx --port=80
    service/nginx exposed
    $ kubectl get service
    NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
    kubernetes   ClusterIP   172.19.0.1    <none>        443/TCP   3h
    nginx        ClusterIP   172.19.8.48   <none>        80/TCP    10s
  2. 以下のコマンドを実行し、"busybox" という名称のポッドを作成し、ステップ 1 で作成した Nginx サービスへのアクセスに作成したポッドを使用します。
    $ kubectl run busybox --rm -ti --image=busybox /bin/sh
    kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
    If you don't see a command prompt, try pressing enter.
    / # wget nginx
    Connecting to nginx (172.19.8.48:80)
    index.html           100% |***************************************************************************************************|   612  0:00:00 ETA
    / #

Nginx サービスを具体的にラベル付けされたアプリケーションにのみアクセス可能にするためネットワークポリシーを利用する

  1. 以下のコマンドを実行し、policy.yaml ファイルを作成します。
    $ vim policy.yaml
    kind: NetworkPolicy
    apiVersion: networking.k8s.io/v1
    metadata:
      name: access-nginx
    spec:
      podSelector:
        matchLabels:
          run: nginx
      ingress:
      - from:
        - podSelector:
            matchLabels:
              access: "true"
  2. 以下のコマンドを実行し、ステップ 1 で作成した policy.yaml ファイルからネットワークポリシーを作成します。
    $ kubectl apply -f policy.yaml 
    networkpolicy.networking.k8s.io/access-nginx created
  3. 以下のコマンドを実行し、このコマンド上でアクセスラベルを定義しない場合に、Nginx サービスにアクセスできないことを確認します。
    $ kubectl run busybox --rm -ti --image=busybox /bin/sh
    If you don't see a command prompt, try pressing enter.
    / # wget nginx
    Connecting to nginx (172.19.8.48:80)
    wget: can't connect to remote host (172.19.8.48): Connection timed out
    / #
  4. 以下のコマンドを実行し、コマンド上でアクセスラベルが定義された場合、Nginx サービスにアクセスできることを確認します。
    $ kubectl run busybox --rm -ti --labels="access=true" --image=busybox /bin/sh
    If you don't see a command prompt, try pressing enter.
    / # wget nginx
    Connecting to nginx (172.19.8.48:80)
    index.html           100% |***************************************************************************************************|   612  0:00:00 ETA
    / #

インターネットを介した SLB サービスにより公開されたサービスへのアクセスを可能にするソース IP CIDR ブロックを指定するためにネットワークポリシーを利用

  1. 以下のコマンドを実行し、前出の Nginx アプリケーション用の Alibaba Cloud SLB サービスを作成します。つまり、Nginx サービスをインターネットへ公開するためにtype=LoadBalancer を指定します。
    $ vim nginx-service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        run: nginx
      name: nginx-slb
    spec:
      externalTrafficPolicy: Local
      ports:
      - port: 80
        protocol: TCP
        targetPort: 80
      selector:
        run: nginx
      type: LoadBalancer
    
    $ kubectl apply -f nginx-service.yaml 
    service/nginx-slb created
    
    $ kubectl get service nginx-slb
    NAME        TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)        AGE
    nginx-slb   LoadBalancer   172.19.12.254   47.110.200.119   80:32240/TCP   8m
  2. 以下のコマンドを実行し、作成された SLB サービスの IP アドレスを確認します。ここでは、"47.110.200.119" となり、アクセスできません。
    $ wget 47.110.200.119
    --2018-11-21 11:46:05--  http://47.110.200.119/
    Connecting to 47.110.200.119:80... failed: Connection refused.
    以下の理由により、アクセスが失敗します。

    解決法: ネットワークポリシーを変更し、Nginx サービスへのアクセスを許可するソース IP CIDR ブロックを追加します。

  3. 以下のコマンドを実行し、お使いのローカル IP アドレスを参照します。
    $ curl myip.ipip.net
     
    IP address: 10.0.0.1 from: China Beijing Beijing        #The local IP address varies by devices.
  4. 以下のコマンドを実行し、作成された policy.yaml ファイルを変更します。
    $ vim policy.yaml
    kind: NetworkPolicy
    apiVersion: networking.k8s.io/v1
    metadata:
      name: access-nginx
    spec:
      podSelector:
        matchLabels:
          run: nginx
      ingress:
      - from:
        - podSelector:
            matchLabels:
              access: "true"
        - ipBlock:
            cidr: 100.64.0.0/10
        - ipBlock:
            cidr: 10.0.0.1/24      # ローカル IP アドレスが属する CIDR ブロックを設定します。 例 お使いのデバイスに応じて必要なパラメーターを設定します。
    
    $ kubectl apply -f policy.yaml 
    networkpolicy.networking.k8s.io/access-nginx unchanged
    • ネットワークの発信インターフェイスが複数の IP アドレスを持つことがあります。 CIDR ブロック全体を指定することを推奨します。
    • SLB ヘルスチェックアドレスは、100.64.0.0/10 CIDR ブロックに属します。 そのため、100.64.0.0/10 ブロックを指定する必要があります。
  5. 以下のコマンドを実行し、Nginx サービスにアクセスできることを確認します。
    $  kubectl run busybox --rm -ti --labels="access=true" --image=busybox /bin/sh
    
    If you don't see a command prompt, try pressing enter.
    / # wget 47.110.200.119
    Connecting to 47.110.200.119 (47.110.200.119:80)
    index.html           100% |***********************************************************|   612  0:00:00 ETA
    / #

www.aliyun.com へのみアクセス可能なポッドの設定のためのネットワークポリシーを利用する

  1. 以下のコマンドを実行し、www.aliyun.com のドメイン名から解決される IP アドレスリストを取得します。
    $ dig +short www.aliyun.com
    www-jp-de-intl-adns.aliyun.com.
    www-jp-de-intl-adns.aliyun.com.gds.alibabadns.com.
    v6wagbridge.aliyun.com.
    v6wagbridge.aliyun.com.gds.alibabadns.com.
    106.11.93.21
    140.205.32.4
    140.205.230.13
    140.205.34.3
  2. 以下のコマンドを実行し、busybox-policy ファイルを作成します。
    $ vim busybox-policy.yaml
    kind: NetworkPolicy
    apiVersion: networking.k8s.io/v1
    metadata:
      name: busybox-policy
    spec:
      podSelector:
        matchLabels:
          run: busybox
      egress:
      - to:
        - ipBlock:
            cidr: 106.11.93.21/32
        - ipBlock:
            cidr: 140.205.32.4/32
        - ipBlock:
            cidr: 140.205.230.13/32
        - ipBlock:
            cidr: 140.205.34.3/32
      - to:
        - ipBlock:
            cidr: 0.0.0.0/0
        ports:
        - protocol: UDP
          port: 53
    前出の busybox-policy で、送信ルールを設定し、クラスターアプリケーションによるアクセスが可能となる CIDR ブロックを指定します。 UDP リクエストを許可する条件の設定が必要です。 設定しない場合、DNS の解決が失敗します。
  3. 以下のコマンドを実行し、busybox-policy ファイルによるネットワークポリシーを作成します。
    $ kubectl apply -f busybox-policy.yaml 
    networkpolicy.networking.k8s.io/busybox-policy created
  4. 以下のコマンドを実行し、www.aliyun.com 以外のどの Web サイト (例として、www.google.com) からもアクセスが出来ないことを確認します。
    $ kubectl run busybox --rm -ti --image=busybox /bin/sh
    If you don't see a command prompt, try pressing enter.
    / # wget www.google.com
    Connecting to www.google.com (64.13.192.74:80)
    wget: can't connect to remote host (64.13.192.74): Connection timed out
  5. 以下のコマンドを実行し、www.aliyun.com からアクセスできることを確認します。
    / # wget www.aliyun.com
    Connecting to www.aliyun.com (140.205.34.3:80)
    Connecting to www.aliyun.com (140.205.34.3:443)
    wget: note: TLS certificate validation not implemented
    index.html           100% |***********************************************************|  462k  0:00:00 ETA
    / #