tjtjtjのメモ

自分のためのメモです

kubernetes 学習 calico simple policy tutorial

引き続き kubernetes で学習。今回はネットワークポリシー初回

caloco の Simple policy tutorial

Configure namespaces

ネームスペース作成...の前に確認

$ kubectl get ns 
NAME              STATUS   AGE
default           Active   22h
kube-node-lease   Active   22h
kube-public       Active   22h
kube-system       Active   22h

ネームスペース作成

$ kubectl create ns policy-demo
namespace/policy-demo created

確認

$ kubectl get ns
NAME              STATUS   AGE
default           Active   22h
kube-node-lease   Active   22h
kube-public       Active   22h
kube-system       Active   22h
policy-demo       Active   11s

Create demo Pods

NAMESPACE:policy-demo でいろいろやる前に確認

$ kubectl get all --namespace=policy-demo
No resources found.

deployment とか作成。警告されるがスルーで。

$ kubectl run --namespace=policy-demo nginx --replicas=2 --image=nginx
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.
deployment.apps/nginx created

確認。pod, deployment, replicaset ができている。

$ kubectl get all --namespace=policy-demo
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7db9fccd9b-fgd66   1/1     Running   0          66s
pod/nginx-7db9fccd9b-kp2d5   1/1     Running   0          66s

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   2/2     2            2           66s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-7db9fccd9b   2         2         2       66s

pod を wide で確認

$ kubectl get pod --namespace=policy-demo -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE   NOMINATED NODE   READINESS GATES
nginx-7db9fccd9b-fgd66   1/1     Running   0          5m26s   10.244.2.2   kb3    <none>           <none>
nginx-7db9fccd9b-kp2d5   1/1     Running   0          5m26s   10.244.1.2   kb2    <none>           <none>

deployment にサービス:nginxをつける。これでpodからサービス名でアクセスできるようになる、のは以前調べた。

$ kubectl expose --namespace=policy-demo deployment nginx --port=80
service/nginx exposed

access pod をrunする。プロンプトが表示される。

$ 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.
/ #

access pod で nginx サービスにアクセス。nginx がレスポンスする。

/ # 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 href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

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

Enable isolation

policy-demo名前空間で分離を有効にする。Calicoはこの名前空間のポッドへの接続を禁止する。

kind: NetworkPolicy

Network Policies https://kubernetes.io/docs/concepts/services-networking/network-policies/

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

また access pod で wget してみる。タイムアウトする。

/ # wget -q nginx -O -
^C
/ # wget -q --timeout=5 nginx -O -
wget: download timed out

Allow access using a network policy

NetworkPolicyを使ってnginxサービスへのアクセスを有効にする。accessポッドからの着信接続は許可されますが、他の場所からの接続は許可されない。

新しいNetworkPolicy:access-nginxを作成。前のはdefault-deny。

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

また access pod で wget してみる。アクセスできた。

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
:

pod:cant-access から wgetしてみる。タイムアウトした。

$ kubectl run --namespace=policy-demo cant-access --rm -ti --image busybox /bin/sh
:
If you don't see a command prompt, try pressing enter.
/ # wget -q --timeout=5 nginx -O -
wget: download timed out

いろいろ確認

ここでチュートリアルはお掃除して終了だが、もう少し見てみる。

$ kubectl get networkpolicy --namespace=policy-demo
NAME           POD-SELECTOR   AGE
access-nginx   run=nginx      9m54s
default-deny   <none>         15m

default-deny

$ kubectl describe networkpolicy default-deny --namespace=policy-demo
Name:         default-deny
Namespace:    policy-demo
Created on:   2019-04-18 19:28:37 +0900 JST
Labels:       <none>
Annotations:  <none>
Spec:
  PodSelector:     <none> (Allowing the specific traffic to all pods in this namespace)
  Allowing ingress traffic:
    <none> (Selected pods are isolated for ingress connectivity)
  Allowing egress traffic:
    <none> (Selected pods are isolated for egress connectivity)
  Policy Types: Ingress

access-nginx

$ kubectl describe networkpolicy access-nginx --namespace=policy-demo
Name:         access-nginx
Namespace:    policy-demo
Created on:   2019-04-18 19:34:31 +0900 JST
Labels:       <none>
Annotations:  <none>
Spec:
  PodSelector:     run=nginx
  Allowing ingress traffic:
    To Port: <any> (traffic allowed to all ports)
    From:
      PodSelector: run=access
  Allowing egress traffic:
    <none> (Selected pods are isolated for egress connectivity)
  Policy Types: Ingress