Giter VIP home page Giter VIP logo

bb-networkpolicy's Introduction

BB Network Policy

Create Cluster

We must enable network policy, choose a pod subnet that is different than your local subnet.

cat  >config.yaml <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
  - role: control-plane
    extraPortMappings:
      - containerPort: 30001
        hostPort: 80
networking:
  disableDefaultCNI: true
  podSubnet: "172.16.0.0/12"
EOF

kind create cluster --config config.yaml
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml
kubectl create -f -<<EOF
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: null
  name: blue
  labels:
    name: blue
spec: {}
status: {}
---
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: blue
    pepr.dev/netpol: enabled
    ingress/1: pod-runEQgreenALSOpod-runEQredANDns-nameEQblue
    egress/test: pod-runEQgreenALSOpod-runEQredANDns-nameEQblue
  name: blue
spec:
  containers:
  - image: nginx
    name: blue
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
---
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: red
  name: red
  namespace: blue
spec:
  containers:
  - image: nginx
    name: red
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
---
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: green
  name: green
spec:
  containers:
  - image: nginx
    name: green
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
EOF
# ---
# k create -f -<<EOF
# kind: NetworkPolicy
# apiVersion: networking.k8s.io/v1
# metadata:
#   namespace: default
#   name: blue
# spec:
#   podSelector:
#     matchLabels:
#       run: blue
#   ingress:
#   - from:
#     - podSelector:
#         matchLabels:
#           run: red
#           run: green
# ---
# kubectl create -f -<<EOF
# kind: NetworkPolicy
# apiVersion: networking.k8s.io/v1
# metadata:
#   name: blue
#   # ingress/1: pod###run=green,pod###run=red&ns###name=blue
#   # egress/1: pod###run=green,pod###run=red&ns###name=blue
# spec:
#   podSelector:
#     matchLabels:
#       run: blue
#   egress:
#   - to:
#       - podSelector:
#           matchLabels:
#             run: red
#         namespaceSelector:
#           matchLabels:
#             name: blue
#       - podSelector:
#           matchLabels:
#             run: green
#   ingress:
#   - from:
#       - podSelector:
#           matchLabels:
#             run: red
#         namespaceSelector:
#           matchLabels:
#             name: blue
#       - podSelector:
#           matchLabels:
#             run: green
EOF

Test the network policy

Curl against blue pod from red pod in blue namespace

BLUE_IP=$(kubectl get po blue --template='{{.status.podIP}}')
k exec -it red -n blue -- curl -I $BLUE_IP

k exec it green  -- curl -I $BLUE_IP

Create a purple pod to make sure it's blocked

k run purple --image=nginx:alpine --rm -it --restart=Never  -- curl $BLUE_IP

bb-networkpolicy's People

Contributors

cmwylie19 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.