Skip to content

Latest commit

 

History

History
225 lines (163 loc) · 4.09 KB

getting-started-with-kubernetes.md

File metadata and controls

225 lines (163 loc) · 4.09 KB

Gettting Started with Kubernetes

Namespace Setup

  1. Create a new Kubernetes Namespace:

Command:

kubectl create namespace my-apps

YAML Version: namespace.yaml

# Define the API version and the kind of resource
apiVersion: v1
kind: Namespace
metadata:
  # The name of the Namespace
  name: my-apps

Apply with:

kubectl apply -f namespace.yaml

Basic Deployment

  1. Deploy a Simple App:

Command:

kubectl create deployment hello-world --image=nginx --namespace=my-apps

YAML Version: deployment.yaml

# Define the API version and the kind of resource
apiVersion: apps/v1
kind: Deployment
metadata:
  # The name of the Deployment
  name: hello-world
  # Namespace to deploy into
  namespace: my-apps
spec:
  # Number of replica Pods to maintain
  replicas: 1
  selector:
    # Labels to match against when selecting Pods for this Deployment
    matchLabels:
      app: hello-world
  template:
    metadata:
      # Labels to assign to the Pods spawned by this Deployment
      labels:
        app: hello-world
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            # Container port that needs to be exposed
            - containerPort: 80

Apply with:

kubectl apply -f deployment.yaml

Service Exposure

  1. Expose the Deployment:

Command:

kubectl expose deployment hello-world --type=ClusterIP --port=80 --namespace=my-apps

YAML Version: service.yaml

# Define the API version and the kind of resource
apiVersion: v1
kind: Service
metadata:
  # Name of the Service
  name: hello-world
   # Namespace to create the service in
  namespace: my-apps
spec:
  # Select Pods with this label to expose via the Service
  selector:
    app: hello-world
  ports:
    - protocol: TCP
      # Expose the Service on this port
      port: 80
      # Map the Service port to the target Port on the Pod
      targetPort: 80
  # The type of Service; ClusterIP makes it reachable only within the cluster
  type: ClusterIP

Apply with:

kubectl apply -f service.yaml

Verify Deployment

  1. Verify Using Port-Forward:
# This is only needed if service type is ClusterIP
kubectl port-forward deployment/hello-world 8081:80 --namespace=my-apps

Cleanup: Wiping Everything and Starting Over

Remove All Resources:

kubectl delete namespace my-apps

Or remove individual resources with:

kubectl delete -f <filename>.yaml

Warning: Deleting the namespace will remove all resources in that namespace. Ensure you're okay with that before running the command.


Exercises

Exercise 1: Create and Examine a Pod

  1. Create a simple Pod running Nginx.
kubectl run nginx-pod --image=nginx --restart=Never
  1. Examine the Pod.
kubectl describe pod nginx-pod
  1. Delete the Pod.
kubectl delete pod nginx-pod

Objective: Familiarize yourself with the Pod lifecycle.


Exercise 2: Create a Deployment

  1. Create a Deployment for a simple Node.js app (You can use a Docker image like node:20).
kubectl create deployment node-app --image=node:20
  1. Scale the Deployment.
kubectl scale deployment node-app --replicas=3
  1. Rollback the Deployment.
kubectl rollout undo deployment node-app

Objective: Learn how to manage application instances declaratively using Deployments.


Exercise 3: Expose the Deployment as a Service

  1. Expose the Deployment as a ClusterIP service.
kubectl expose deployment node-app --type=ClusterIP --port=80
  1. Access the service within the cluster.
kubectl get svc

Use kubectl port-forward to test the service.

kubectl port-forward svc/node-app 8080:80

Objective: Learn how Services allow you to abstract and access your Pods.


Exercise 4: Cleanup

  1. Remove the service and deployment.
kubectl delete svc node-app
kubectl delete deployment node-app

Objective: Understand cleanup and resource management.