Kubernetes Commands
Deploy the App
Deploys the application using the YAML configuration file.
Explanation:
- kubectl apply -f → Creates or updates resources from a configuration file
- myapp.yaml → The YAML file containing pod configuration
Deploy the Service
Deploys the service using the YAML configuration file.
Explanation:
- kubectl apply -f → Creates or updates resources from a configuration file
- myservice.yaml → The YAML file containing service configuration
Check Service-Pod Connection
Get Pod IP Address:
Get Service Endpoint:
Check if the service endpoint IP matches the pod IP to verify connection.
Explanation:
- kubectl get po -o wide → Shows detailed pod information including IP
- kubectl get ep → Shows service endpoints
- If connected, the IP addresses should match
Port Forward to Service
Forwards local port 8080 to the service's port 80. Access via http://localhost:8080
Explanation:
- port-forward → Creates a tunnel between local machine and Kubernetes service
- service/myservice → The service to forward
- 8080:80 → Local port : Service port
- Stop with Ctrl-C
Edit and Redeploy App
Edit myapp.yaml:
Change the app label to myapp2
Redeploys the application with the updated configuration.
Explanation:
- Changing the app label breaks the service-pod connection
- Service selects pods based on labels
Check Endpoint Again
After changing the app label, check if the service endpoint still points to the pod.
Expected Result:
The endpoint should be empty since the service selector no longer matches the pod's labels.
Port Forward Again
Try to access the service again after changing the pod labels.
Expected Result:
The port forward might succeed, but accessing http://localhost:8080 will fail because the service has no endpoints.
Cleanup
Deletes all resources created from the YAML files.
Explanation:
- kubectl delete -f → Deletes resources defined in a file
- Clean up resources after completing the exercise
YAML Configuration Files
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 250m
memory: 256Mi
ports:
- containerPort: 80
Explanation:
- apiVersion: v1 → Kubernetes API version for Pod resources
- kind: Pod → Defines a Pod resource
- metadata.name: myapp-pod → Unique name for the pod
- metadata.labels.app: myapp → Primary label used by service for pod selection
- metadata.labels.type: front-end → Additional label for categorization
- spec.containers[0].name: nginx-container → Name of the container within the pod
- image: nginx → Uses the official Nginx image from Docker Hub
- resources.requests.cpu: 100m → Minimum CPU required (100 milliCPU = 0.1 core)
- resources.requests.memory: 128Mi → Minimum memory required (128 Mebibytes)
- resources.limits.cpu: 250m → Maximum CPU allowed (250 milliCPU = 0.25 core)
- resources.limits.memory: 256Mi → Maximum memory allowed (256 Mebibytes)
- containerPort: 80 → Exposes port 80 from the container for incoming traffic
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- port: 80
targetPort: 80
selector:
app: myapp
type: front-end
Explanation:
- apiVersion: v1 → Kubernetes API version for Service resources
- kind: Service → Defines a Service resource for network access
- metadata.name: myservice → Unique name for the service
- spec.ports[0].port: 80 → Service exposes port 80 to external clients
- spec.ports[0].targetPort: 80 → Forwards traffic to port 80 on the selected pods
- spec.selector.app: myapp → Selects pods with label "app: myapp"
- spec.selector.type: front-end → Additional selector for pods with "type: front-end" label
- Both selectors combined → Service will route traffic only to pods matching BOTH labels
- Service Type → Defaults to ClusterIP (internal cluster access only)