MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

OpenShift – Practical Guide to NetworkPolicy


URL – https://www.devopsschool.com/blog/kubernetes-cks-network-policy-example-code/



๐Ÿ” OpenShift NetworkPolicies Tutorial (with httpd in test2 namespace)


๐ŸŽฏ Goal

You will:

โœ… Deploy an httpd server
โœ… Launch test clients to access it
โœ… Apply NetworkPolicy to:

  • โŒ Block all traffic
  • โœ… Allow traffic only from specific labeled pods

๐Ÿ”ง Prerequisites

You already have:

  • httpd deployed using ImageStream (oc new-app httpd -n test2)
  • oc expose svc/httpd -n test2 run (optional, for browser access)

โœ… Step-by-Step Guide


โœ… Step 1: Check Internal Access to httpd

Create a PSA-compliant test pod and try connecting to the httpd service:

oc run test-client \
  --rm -it \
  --restart=Never \
  --image=busybox:1.35 \
  -n test2 \
  --overrides='
{
  "apiVersion": "v1",
  "spec": {
    "securityContext": {
      "runAsNonRoot": true,
      "seccompProfile": { "type": "RuntimeDefault" }
    },
    "containers": [{
      "name": "test-client",
      "image": "busybox:1.35",
      "command": ["sh"],
      "stdin": true,
      "tty": true,
      "securityContext": {
        "allowPrivilegeEscalation": false,
        "capabilities": {
          "drop": ["ALL"]
        },
        "runAsNonRoot": true
      }
    }]
  }
}'
Code language: PHP (php)

Inside the pod:

wget -qO- httpd

โœ… This should return a response โ€” all traffic is allowed by default.

Exit:

exit
Code language: PHP (php)

๐Ÿšซ Step 2: Block All Ingress Traffic to httpd

Create a deny-all NetworkPolicy:

cat <<EOF | oc apply -n test2 -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-to-httpd
spec:
  podSelector:
    matchLabels:
      deployment: httpd
  policyTypes:
    - Ingress
EOF

This blocks all ingress to httpd pods.


๐Ÿ” Step 3: Retest (Should Fail Now)

Run the same test-client pod again and try:

wget -qO- httpd

โŒ It should now fail โ€” because ingress to httpd is blocked.

Exit:

exit
Code language: PHP (php)

โœ… Step 4: Allow Labeled Pods to Access httpd

Deploy a new client pod with access=allowed label:

oc run allowed-client \
  --rm -it \
  --restart=Never \
  --image=busybox:1.35 \
  --labels="access=allowed" \
  -n test2 \
  --overrides='
{
  "apiVersion": "v1",
  "spec": {
    "securityContext": {
      "runAsNonRoot": true,
      "seccompProfile": { "type": "RuntimeDefault" }
    },
    "containers": [{
      "name": "allowed-client",
      "image": "busybox:1.35",
      "command": ["sh"],
      "stdin": true,
      "tty": true,
      "securityContext": {
        "allowPrivilegeEscalation": false,
        "capabilities": {
          "drop": ["ALL"]
        },
        "runAsNonRoot": true
      }
    }]
  }
}'
Code language: PHP (php)

Now create a policy to allow only that pod label:

cat <<EOF | oc apply -n test2 -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-from-allowed
spec:
  podSelector:
    matchLabels:
      deployment: httpd
  ingress:
    - from:
        - podSelector:
            matchLabels:
              access: allowed
  policyTypes:
    - Ingress
EOF
Code language: JavaScript (javascript)

Inside the pod:

wget -qO- httpd

โœ… This should now succeed โ€” because the pod is allowed.

Exit:

exit
Code language: PHP (php)

โŒ Step 5: Verify Denial from Unlabeled Pods

Run another test pod without label:

oc run denied-client \
  --rm -it \
  --restart=Never \
  --image=busybox:1.35 \
  -n test2 \
  --overrides='
{
  "apiVersion": "v1",
  "spec": {
    "securityContext": {
      "runAsNonRoot": true,
      "seccompProfile": { "type": "RuntimeDefault" }
    },
    "containers": [{
      "name": "denied-client",
      "image": "busybox:1.35",
      "command": ["sh"],
      "stdin": true,
      "tty": true,
      "securityContext": {
        "allowPrivilegeEscalation": false,
        "capabilities": {
          "drop": ["ALL"]
        },
        "runAsNonRoot": true
      }
    }]
  }
}'
Code language: PHP (php)

Then:

wget -qO- httpd

โŒ This should fail โ€” the pod is not allowed by the NetworkPolicy.

Exit:

exit
Code language: PHP (php)

โœ… Summary Table

StepResult
No policyAll pods can access httpd
Deny-all policyNo pod can access httpd
Allow from access=allowedOnly labeled pods can access
Unlabeled podsAccess denied

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

OpenShift Lab 10: View Pod, Workload, and Namespace Performance Metrics Using the OpenShift Web Console

This lab uses the OpenShift web console to view CPU, memory, network and pod metrics for one namespace and its workloads. It then checks the same numbers…

Read More

Kubernetes ResourceQuota & LimitRange Example Programs

Kubernetes ResourceQuota and LimitRange are two important mechanisms for managing resource allocation and usage within a Kubernetes cluster. Here’s a comparison of ResourceQuota and LimitRange features and…

Read More

Kubernetes API Reference: Complete Guide

Namespaces Working with Kubernetes Cluster using Kubectl Part โ€“ 6 โ€“ Namespaces Pod Kubernetes Pod Example Programs Working with Kubernetes Pods & Deployment Kubernetes POD observability and…

Read More

Kubernetes Interview Questions and Answer Part โ€“ 2

How you will link when the docker containers is in different virtual machine is there any configuration in docker compose file are any command or any variable?…

Read More

Kubernetes Assignment and Project

This page is a five-day Kubernetes assignment. It starts with architecture and Pods, then moves through ReplicaSets, DaemonSets, Deployments, Services, CoreDNS service discovery and StatefulSets, and ends…

Read More

Helm Tutorials: How to create Basic Custom Charts

This tutorial shows how to build a minimal custom Helm chart: scaffold it with helm create, remove the default templates, add your own Pod template, set chart…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x