Kubernetes Basics

Essential Kubernetes for container orchestration

Download YAML Files

Kubernetes Commands

Deploy the App

kubectl apply -f myapp.yaml

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

kubectl apply -f myservice.yaml

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:

kubectl get po -o wide

Get Service Endpoint:

kubectl get ep myservice

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

kubectl port-forward service/myservice 8080:80

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

kubectl apply -f myapp.yaml

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

kubectl get ep myservice

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

kubectl port-forward service/myservice 8080:80

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

kubectl delete -f myservice.yaml
kubectl delete -f myapp.yaml

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

myapp.yaml

Download
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

myservice.yaml

Download
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)