Skip to the content.

Kubernetes

alt text

A Cluster is an instance of Kubernetes. Each cluster has a Control Plane, and at least one Worker node.

The Control Plane contains the components that manage a cluster, and enable the resiliency and automation that make Kubernetes such a popular container orchestrator.

It’s a collection of the following components:

Kubernetes objects

Worker Nodes

The Worker Nodes are where pods are scheduled and run, and each worker node has three components.

alt text

Ways to manage Kubernetes pods

minikube

minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes.

All you need is Docker (or similarly compatible) container or a Virtual Machine environment, and Kubernetes is a single command away: minikube start

Cheat Sheet

minikube management

cluster management

nodes management

namespaces management

pods management

Creating Namespaces

Namespaces isolate and organize workloads.

  1. Create a namespace.yaml file with two different namespaces.
---
apiVersion: v1
kind: Namespace
metadata:
  name: development
---
apiVersion: v1
kind: Namespace
metadata:
  name: production
  1. Execute “kubectl apply -f namespace.yaml” to create the namespaces.

  2. Execute “kubectl get namespaces” to list the namespaces.

  3. Execute “kubectl delete -f namespaces.yaml” to delete the namespaces.

Deploying an application

  1. Create a deployment.yaml file with the following configuration.
--- 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-info-deployment
  namespace: development
  labels:
    app: pod-info
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-info
  template:
    metadata:
      labels:
        app: pod-info
    spec:
      containers:
      - name: pod-info-container
        image: kimschles/pod-info-app:latest
        ports:
        - containerPort: 3000
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
  1. Execute the command “kubectl apply -f deployment.yaml” to deploy the containers.

  2. Execute the command “kubectl get deployments -n development” to check the containers status.

  3. Execute the command “kubectl get pods -n development” to check the pods that the deployment created.

  4. Execute the command “kubectl delete pod <pod name> -n development” to delete the pod from the development namespace. You’ll see that it will be replaced automatically by a new pod.

Troubleshooting an application

Pod’s Events Log

It’s possible to check the health of a pod by looking at the event logs. For that, execute the command “kubectl describe pod <pod name> -n development” and find the events log of the pod.

BusyBox

BusyBox is a tool to check the application working.

  1. Create the busybox.yaml with the following configuration.
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  namespace: default
  labels:
    app: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox-container
        image: busybox:latest
        # Keep the container running
        command: [ "/bin/sh", "-c", "--" ]
        args: [ "while true; do sleep 30; done;" ]
        resources:
          requests:
            cpu: 30m
            memory: 64Mi
          limits:
            cpu: 100m
            memory: 128Mi
  1. Execute the command “kubectl apply -f busybox.yaml” to deploy the pod in the default namespace.

  2. Execute the command “kubectl get pods” to check the BusyBox deployment.

  3. Execute the command “kubectl exec -it <busybox pod name> – /bin/sh” to enter in busybox pod.

  4. Execute the command “kubectl get pods -n development -o wide”.

  5. Get one the IP address from the list of the previous command, and in the BusyBox shell, execute the command “wget <ip address>:PORT”.

  6. Execute the command “cat index.html” to view the contents of the file generated by the previous command. If the application is OK, the file index.html should contain the information about the pod especified in the development.yaml.

  7. Execute the command “exit” to get out of the BusyBox.

Application Logs

  1. Execute the command “kubectl get pods -n development” and pic a name of one the pods.

  2. Execute the command “kubectl logs <pod name> -n development” to display the pod’s log.

Expose an application to the internet with a LoadBalancer

  1. Execute the command “minikube tunnel” to allow simulation of exposing the services via load balancer to external world.

  2. Create the file service.yaml with the following content.

---
apiVersion: v1
kind: Service
metadata:
  name: demo-service
  namespace: development
spec:
  selector:
    app: pod-info
  ports:
    - port: 80
      targetPort: 3000
  type: LoadBalancer
  1. Execute the command “kubectl apply -f service.yaml”.

  2. Execute the command “kubectl get services -n development” to obtain the EXTERNAL-IP (public ip).

  3. Get the EXTERNAL-IP addres, open an web browser, and put the ip address in the address bar. The result may look like the following.

{
    "pod_name": "pod-info-deployment-757cb75bbb-rth4x",
    "pod_namespace": "development",
    "pod_ip": "10.244.0.6"
}

Add resource requests and limits to a pod

  1. Update the file deployment.yaml with this new content. It includes the resources specification.
--- 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-info-deployment
  namespace: development
  labels:
    app: pod-info
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pod-info
  template:
    metadata:
      labels:
        app: pod-info
    spec:
      containers:
      - name: pod-info-container
        image: kimschles/pod-info-app:latest
        resources:
          requests:
            memory: "64Mi"
            cpu: "250m"
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 3000
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
  1. Execute the command “kubectl apply -f deployment.yaml.” to replace the containers with the new configuration.

Deleting Kubernetes objects and tear down the cluster

Execute the following commands: