LoginSignup
8
7

More than 5 years have passed since last update.

Kubernetesクラスタ 名前空間のアクセス制御

Posted at

ネットワークのアドオンとして、IBM Cloud Kubenetesサービスでは、Calicoが採用されています。このCalicoはネットワークのポリシーを設定できるので、ネームスペース間のアクセスの分離を検証しました。

Kubernetesのクラスタ・ネットワークを Flannelで構築している場合は、動作しません。

ネームスペースの準備

テスト環境と本番環境のそれぞれに名前空間を作る

$ kubectl create namespace testing
namespace "testing" created

$ kubectl create namespace production
namespace "production" created

アクセス・ポリシーを設定

他の名前空間からのアクセスを禁止するネットワークポリシーをセットする。

deny-from-other-namespaces.yml
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: deny-from-other-namespaces
spec:
  podSelector:
    matchLabels:
  ingress:
  - from:
    - podSelector: {}

本番環境へ適用する

$ kubectl apply -f deny-from-other-namespaces.yml -n production
networkpolicy "deny-from-other-namespaces" created

アプリの準備

本番環境とテスト環境に、ウェブサーバーをデプロイする。

imac:bluemix-k8s maho$ kubectl apply -f webserver.yml -n production
deployment "web" created
service "web" created
imac:bluemix-k8s maho$ kubectl apply -f webserver.yml -n testing
deployment "web" created
service "web" created

サービスとポッドのIPアドレスをリストする

$ kubectl get svc -n production
NAME      TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
web       ClusterIP   172.21.194.171   <none>        80/TCP    37s

imac:bluemix-k8s maho$ kubectl get pod -n production -o wide
NAME                  READY     STATUS    RESTARTS   AGE       IP              NODE
web-6d95c64d8-kw88r   1/1       Running   0          8m        172.30.26.204   10.132.253.17


$ kubectl get svc -n testing
NAME      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
web       ClusterIP   172.21.52.107   <none>        80/TCP    37s


imac:bluemix-k8s maho$ kubectl get pod -n testing -o wide
NAME                  READY     STATUS    RESTARTS   AGE       IP               NODE
web-6d95c64d8-77ldv   1/1       Running   0          7m        172.30.184.125   10.132.253.30

テスト環境内にUbuntuのポッドを起動して、サービスとポッドの両方にアクセスできる。

$ kubectl run ubuntu -n testing --image=ubuntu --restart=Never -- /bin/sh -c "sleep 3600"
pod "ubuntu" created

$ kubectl exec -it -n testing ubuntu bash
root@ubuntu:/# apt-get update
root@ubuntu:/# apt-get install -y dnsutils iproute2 iputils-ping curl
...

同一名前空間 テスト環境で、サービスとポッドのアドレスにアクセスすると、正常応答が帰る。

root@ubuntu:/# curl -I --connect-timeout 3 172.21.52.107
HTTP/1.1 200 OK
...

root@ubuntu:/# curl -I --connect-timeout 3 172.30.184.125 
HTTP/1.1 200 OK
...

本番環境へのサービスとポッドへアクセスすると、ブロックされる。

root@ubuntu:/# curl -I --connect-timeout 3 172.21.194.171
curl: (28) Connection timed out after 3000 milliseconds

root@ubuntu:/# curl -I --connect-timeout 3 172.30.26.204 
curl: (28) Connection timed out after 3001 milliseconds

参考資料

[1] Calicoプロジェクト https://docs.projectcalico.org/v2.6/getting-started/kubernetes/
[2] Kubernetes Network Policy Recipes https://github.com/ahmetb/kubernetes-network-policy-recipes
[3] ネットワーク・ポリシーによるトラフィックの制御 https://console.bluemix.net/docs/containers/cs_network_policy.html#network_policies

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7