Title: Restricting Container Access to Specific Ports in Kubernetes

Introduction:
In this article, we will learn about how to restrict container access to specific ports in Kubernetes. We will explain the step-by-step process and provide code examples along the way. This tutorial is aimed at beginners who are new to Kubernetes and want to understand how to achieve this functionality.

Step-by-Step Process:

| Step | Description |
|------|-------------|
| 1 | Create a Kubernetes Cluster |
| 2 | Create a Namespace |
| 3 | Create a ServiceAccount |
| 4 | Define a Pod |
| 5 | Apply Pod Security Policies |
| 6 | Deploy the Pod |
| 7 | Test the Pod's Connectivity |

Step 1: Create a Kubernetes Cluster
First, we need to set up a Kubernetes cluster to work with. You can use any cloud provider or a local setup like Minikube. Follow the documentation of your chosen platform to create a cluster.

Step 2: Create a Namespace
A namespace is a logical boundary within a cluster that allows you to separate and organize your resources. To create a namespace, execute the following command:

```shell
kubectl create namespace my-namespace
```

Step 3: Create a ServiceAccount
A ServiceAccount provides an identity for processes running in a pod. To create a ServiceAccount, run the following command:

```shell
kubectl create serviceaccount my-serviceaccount -n my-namespace
```

Step 4: Define a Pod
Create a pod definition file (e.g., pod.yaml) and define the necessary specifications. Ensure that you specify the created namespace and service account in the pod definition file. Here's an example:

```yaml
apiVersion: v1
kind: Pod
metadata:
name: restricted-pod
namespace: my-namespace
spec:
serviceAccountName: my-serviceaccount
containers:
- name: my-container
image: my-container-image
ports:
- containerPort: 8080
name: http
```

Step 5: Apply Pod Security Policies
Pod Security Policies (PSPs) are used to control the security-related aspects of pods. Create a PSP and define the necessary policies. For example, to disallow containers from accessing any ports other than port 8080, create a policy file (e.g., psp.yaml) with the following content:

```yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: restricted-psp
spec:
privileged: false
allowPrivilegeEscalation: false
requiredDropCapabilities:
- ALL
volumes:
- 'secret'
hostNetwork: false
hostIPC: false
hostPID: false
runAsUser:
rule: 'MustRunAsNonRoot'
seLinux:
rule: 'RunAsAny'
supplementalGroups:
rule: 'RunAsAny'
fsGroup:
rule: 'RunAsAny'
allowedHostPaths:
- pathPrefix: '/'
readOnly: false
allowedFlexVolumes: null
allowedUnsafeSysctls: null
readOnlyRootFilesystem: false
allowedProcMountTypes:
- 'None'
```

Apply the Pod Security Policy using the following command:

```shell
kubectl apply -f psp.yaml
```

Step 6: Deploy the Pod
To deploy the pod, run the following command:

```shell
kubectl apply -f pod.yaml
```

Step 7: Test the Pod's Connectivity
Check if the pod is running:

```shell
kubectl get pods -n my-namespace
```

To test the connectivity to the restricted port, execute the following command:

```shell
kubectl run test-pod --namespace=my-namespace -it --rm --image=busybox -- /bin/sh -c "telnet restricted-pod 8080"
```

If the connectivity is successful, it means the container has access to the specific port.

Conclusion:
In this article, we learned how to restrict container access to specific ports in Kubernetes. We discussed the step-by-step process, including creating a Kubernetes cluster, defining a pod, applying pod security policies, and testing the pod's connectivity. By following these steps, you can easily limit container access to specific ports, ensuring better security and control in your Kubernetes environment.