Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

OpenShift – Practical Guide to NetworkPolicy


βœ… Validated for OpenShift 4.16.30 (Kubernetes v1.29.10)


🧱 Objective

  • Deploy a simple Apache HTTP server using openshift/httpd
  • Deploy a test pod to simulate network access
  • Apply NetworkPolicy to:
    • Allow or deny access based on pod labels and namespaces

πŸ“¦ Step 1: Pre-Requisites

  • You must have:
    • A running OpenShift 4.16+ cluster
    • Access to the oc CLI
    • Permissions to create NetworkPolicy in your namespace
oc login --token=xxx --server=https://your-cluster-api:6443
oc new-project netpol-demo

🌐 Step 2: Deploy Apache HTTP Server

oc new-app --name=my-httpd --image-stream=openshift/httpd

This will:

  • Deploy the httpd pod
  • Create a Service named my-httpd

πŸ” Confirm deployment:

oc get pods
oc get svc

πŸ§ͺ Step 3: Deploy a Test Pod to Simulate Access

oc run test-client --image=registry.access.redhat.com/ubi8/ubi --command -- sleep 3600

Wait a few seconds, then exec into it:

oc exec -it pod/test-client -- bash
curl http://my-httpd:8080

βœ… You should see an HTTP response (e.g., HTML page)


πŸ”’ Step 4: Apply a Deny-All NetworkPolicy

Now apply a default deny-all ingress policy:

# deny-all.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-ingress
spec:
  podSelector: {}
  policyTypes:
  - Ingress
oc apply -f deny-all.yaml

πŸ” Test again:

oc exec -it pod/test-client -- curl http://my-httpd:8080

❌ It should now fail with timeout β€” all ingress is blocked.


βœ… Step 5: Allow Access Only from Labeled Pods

Label your test-client pod:

oc label pod test-client access=httpd-client

Create a NetworkPolicy to allow only pods with that label:

# allow-httpd-from-labeled-pods.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-httpd-from-specific-pod
spec:
  podSelector:
    matchLabels:
      app: my-httpd
  ingress:
  - from:
    - podSelector:
        matchLabels:
          access: httpd-client
  policyTypes:
  - Ingress
oc apply -f allow-httpd-from-labeled-pods.yaml

πŸ” Re-test:

oc exec -it pod/test-client -- curl http://my-httpd:8080

βœ… Access is restored only for labeled pod.


πŸ›‘οΈ Step 6: Add a Pod in Another Namespace to Prove Isolation

Create another namespace:

oc new-project outside-namespace

Run a test pod:

oc run outsider --image=registry.access.redhat.com/ubi8/ubi --command -- sleep 3600

Try accessing:

oc exec -it pod/outsider -n outside-namespace -- curl http://my-httpd.netpol-demo.svc:8080

❌ Should fail β€” external namespace is blocked by policy


βœ… Summary of What You Experienced

ScenarioAccess Status
Any pod (no policy)βœ… Allowed
After Deny-All❌ Blocked
Labeled pod with allow policyβœ… Allowed
External namespace pod❌ Blocked

πŸ“˜ Key Concepts Practiced

ConceptWhat You Did
NetworkPolicyControlled access to httpd service
PodSelectorMatched specific workloads by label
NamespaceScopeIsolated access from other namespaces
Testing accessUsed curl from test pods

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x