https://drive.google.com/file/d/15JhuuRPCbnmudauXmZQHLxtUBCE5RE2w/view?usp=sharing

πŸ” Current YAML Breakdown

# 1. Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: unsecure
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: lovelearnlinux/unsecureapache:v1  # ← Custom Apache image
        ports:
        - containerPort: 8080                 # ← App listens on 8080

---
# 2. ClusterIP Service
apiVersion: v1
kind: Service
metadata:
  name: unsecure-svc
spec:
  selector:
    app: nginx
  ports:
    - port: 80          # ← Service port
      targetPort: 8080  # ← Pod port

---
# 3. NodePort Service
apiVersion: v1
kind: Service
metadata:
  name: unsecure-svc-np
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30007   # ← Fixed NodePort (optional)

πŸ”‘ Key Observations:

⚠️ Missing Security Features:


πŸ› οΈ Enhanced Secure Version (Production-Ready)

Here’s how to harden this Deployment:

# secure-apache-enhanced.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: secure
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      # πŸ”’ SECURITY: Run as non-root user
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000      # ← Must match image user
        fsGroup: 2000
      containers:
      - name: nginx
        image: lovelearnlinux/secureapache:v1  # ← Official NGINX image
        ports:
        - containerPort: 8080
        # πŸ”’ SECURITY: Container-level hardening
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
          capabilities:
            drop:
            - ALL
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "200m"
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 10
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 5

---
# ClusterIP Service (internal)
apiVersion: v1
kind: Service
metadata:
  name: secure-svc
spec:
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 8080

---
# NodePort Service (external)
apiVersion: v1
kind: Service
metadata:
  name: secure-svc-np
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 30007  # Optional: fixed port

βœ… Key Security Enhancements:


πŸ§ͺ k3s Lab: Deploy Secure App + Test Access

πŸ”§ Step 1: Deploy the Enhanced YAML

# Save as secure-apache-enhanced.yaml
kubectl apply -f secure-apache-enhanced.yaml

# Verify Pods
kubectl get pods -l app=nginx

πŸ”§ Step 2: Test Internal Access (ClusterIP)

# Start debug Pod
kubectl run debug --image=curlimages/curl -it --rm -- sh

# Inside shell:
curl <http://secure-svc>
# βœ… Should return Apache welcome page

exit

πŸ”§ Step 3: Test External Access (NodePort)

πŸ’‘ In k3s, NodePort is accessible on any node’s IP.

# Get k3s node IPs
kubectl get nodes -o wide

# From your laptop (or any machine with network access to k3s nodes):
curl http://<NODE-IP>:30007
# Example: curl <http://192.168.1.10:30007>
# βœ… Should return Apache welcome page

πŸ” Note: