CKAD-Exercises

CKAD Practice: Services and Networking

Overview

The Services and Networking topic in the CKAD (Certified Kubernetes Application Developer) exam focuses on understanding and managing connectivity within Kubernetes clusters. This includes creating and managing network policies, exposing applications using services, and configuring ingress rules for external access.

Topics Covered

Practice Questions

1. Create a Default Deny-All NetworkPolicy

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-all namespace: default spec: podSelector: {} policyTypes: - Ingress - Egress ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f deny-all.yaml ``` 2. Verify the policy: ```bash kubectl describe networkpolicy deny-all ```

2. Allow Ingress Traffic from Specific Pods

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-frontend namespace: default spec: podSelector: matchLabels: app: backend ingress: - from: - podSelector: matchLabels: role: frontend ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f allow-frontend.yaml ``` 2. Test connectivity from a frontend Pod to a backend Pod.

3. Restrict Egress Traffic to Specific IPs

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: restrict-egress namespace: default spec: podSelector: matchLabels: app: web egress: - to: - ipBlock: cidr: 192.168.1.0/24 ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f restrict-egress.yaml ``` 2. Verify egress rules: ```bash kubectl describe networkpolicy restrict-egress ```

4. Allow Ingress Traffic on Specific Ports

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-port namespace: default spec: podSelector: matchLabels: app: database ingress: - from: - podSelector: {} ports: - protocol: TCP port: 3306 ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f allow-port.yaml ``` 2. Test connectivity to the database Pod on port 3306.

5. Combine Ingress and Egress Rules

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: combined-policy namespace: default spec: podSelector: matchLabels: app: backend ingress: - from: - podSelector: matchLabels: role: frontend egress: - to: - ipBlock: cidr: 192.168.2.0/24 ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f combined-policy.yaml ``` 2. Test ingress and egress connectivity for the backend Pods.

6. Test Default Allow Behavior

Scenario:

Details #### Steps to Test 1. Deploy two Pods: ```bash kubectl run pod1 --image=busybox --command -- sleep 3600 kubectl run pod2 --image=busybox --command -- sleep 3600 ``` 2. Test connectivity: ```bash kubectl exec pod1 -- ping pod2 ```

7. Deny All Egress Traffic

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-egress namespace: prod spec: podSelector: {} policyTypes: - Egress egress: [] ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f deny-egress.yaml ``` 2. Test egress connectivity from any Pod in the `prod` namespace.

8. Allow Egress to a Specific Namespace

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: namespace-egress namespace: dev spec: podSelector: matchLabels: app: frontend egress: - to: - namespaceSelector: matchLabels: environment: prod ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f namespace-egress.yaml ``` 2. Test connectivity between `dev` and `prod` namespaces.

9. Isolate a Namespace with a Default Deny Policy

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: isolate-namespace namespace: staging spec: podSelector: {} policyTypes: - Ingress - Egress ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f isolate-namespace.yaml ``` 2. Verify that traffic is denied.

10. Allow DNS Traffic for Specific Pods

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-dns namespace: default spec: podSelector: matchLabels: app: web egress: - to: - ipBlock: cidr: 8.8.8.8/32 ports: - protocol: UDP port: 53 ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f allow-dns.yaml ``` 2. Verify DNS access for the web Pods.

11. Expose a Deployment via a ClusterIP Service

Scenario:

Details #### Declarative YAML Configuration ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: my-app labels: app: my-app spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: app image: busybox command: ["/bin/sh", "-c", "while true; do echo hello; sleep 5; done"] --- apiVersion: v1 kind: Service metadata: name: my-app-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 type: ClusterIP ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f my-app-service.yaml ``` 2. Test the Service: ```bash kubectl exec -it -- curl my-app-service ``` </details> --- ### 12. Expose a Deployment Externally Using a NodePort Service **Scenario**: - Create a Deployment named `external-app`. - Expose it externally using a NodePort Service on port 30007.
Details #### Declarative YAML Configuration ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: external-app labels: app: external-app spec: replicas: 2 selector: matchLabels: app: external-app template: metadata: labels: app: external-app spec: containers: - name: app image: busybox command: ["/bin/sh", "-c", "while true; do echo hello; sleep 5; done"] --- apiVersion: v1 kind: Service metadata: name: external-app-service spec: selector: app: external-app ports: - protocol: TCP port: 80 targetPort: 8080 nodePort: 30007 type: NodePort ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f external-app-service.yaml ``` 2. Test the Service externally: ```bash curl :30007 ``` </details> --- ### 13. Configure a LoadBalancer Service for a Deployment **Scenario**: - Deploy an application named `loadbalanced-app`. - Expose it using a LoadBalancer Service.
Details #### Declarative YAML Configuration ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: loadbalanced-app labels: app: loadbalanced-app spec: replicas: 3 selector: matchLabels: app: loadbalanced-app template: metadata: labels: app: loadbalanced-app spec: containers: - name: app image: nginx --- apiVersion: v1 kind: Service metadata: name: loadbalanced-service spec: selector: app: loadbalanced-app ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer ``` #### Steps to Apply 1. Save the YAML file and apply it: ```bash kubectl apply -f loadbalanced-service.yaml ``` 2. Verify the external IP of the LoadBalancer: ```bash kubectl get svc loadbalanced-service ``` 3. Test access to the application using the external IP.
--- ### 14. Troubleshoot a Service Not Forwarding Traffic **Scenario**: - A Service named `troubleshoot-service` is not forwarding traffic to the backend Pods. - Investigate and resolve the issue.
Details #### Steps to Troubleshoot 1. Verify Service configuration: ```bash kubectl describe service troubleshoot-service ``` 2. Check the endpoint mappings: ```bash kubectl get endpoints troubleshoot-service ``` 3. Ensure the backend Pods are running and labeled correctly: ```bash kubectl get pods -l app=