Kubernetes basics - Pod

Page content

Environment

Please refer to the set up memo .

I suppose you are already familiar to Docker image.

K8s basic concepts

In k8s environment, there are a few concepts we should know before starting.

“INTRODUCTION TO KUBERNETES” from AWS is a conprehensive article.

note. Architecture: https://eksworkshop.com/010_introduction/architecture/architecture_control_and_data_overview/

K8s objects

While creating your own k8s environment, you create and delete a lot of types of objects, such as,

  • Pod
  • ReplicaSet
  • Deployment
  • Service
  • Namespace

and so on.

We descrive each object in code. Once codes are generated, we can create the same object many times based on the code. The code is written in YAML format, and we define the type of object as kind value, like

kind: Pod

Structure - bottom up

  • Each functions (e.g., program codes or applications) is already written in image.
  • A container is an object the correspond image is embodied (running in computers.)
  • An accumulation of containers could be an application. This accumulation is called Pod.
    • Pod is the smallest object we can create in k8s.
    • When k8s scale out automatically, it increases the number of Pods, not containers themselves.
    • In Pod, there is own network, containers in the Pod communicate eashother as localhost.
  • A single k8s environment can have multi Pods.
  • All of those objects have their namespace. The namespace is kind of logical `boundary.
  • A VM or a bare-metal machine is called Node.

https://www.ibm.com/cloud/architecture/content/course/kubernetes-101/deployments-replica-sets-and-pods

Once you delete a Pod, all containers in the Pod will be deleted.

MyNote:

namespace -> context (microk8s
service -> Ingress

In this note, I create every object from YAML file as possible.

First step - Pod

YAML file

Here is the sample

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: myapp
spec:
  containers:
    - image: nginx
      name: nginx
      ports:
        - containerPort: 80

https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/#required-fields

The following 4 fields are required for every object (not only Pod) YAML file.

  • apiVersion: The version of k8s API.
  • kind: The type of an object.
  • metadata: name, UID, or namespace
  • spec: Details of the object.

In the example above, Pod has two medatada, name and labels. name is a name for this object. labels are any key-value pairs. In the example, the key is app and the value is myapp.

image: nginx defines an docker image used in the Pod. When k8s creates the Pod, it download image nginx from Docker Hub automatically.

portst are a list of ports to expose from the container. containerPort is a number of port to expose on the pod’s IP address.

Here is the details.

https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#pod-v1-core

Create the Pod

Now, create a Pod from the file!

Every object is created by kubectl create -f {file} or kubectl apply -f {file}.

Here is the difference between create and apply.

https://intellipaat.com/community/468/difference-between-kubectl-apply-and-kubectl-create

$ kubectl get all --all-namespaces
NAMESPACE   NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
default     service/kubernetes   ClusterIP   10.152.183.1   <none>        443/TCP   129m

$ kubectl apply -f Pod_nginx.yml 
pod/nginx created

$ kubectl get all --all-namespaces
NAMESPACE   NAME        READY   STATUS              RESTARTS   AGE
default     pod/nginx   0/1     ContainerCreating   0          5s

NAMESPACE   NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
default     service/kubernetes   ClusterIP   10.152.183.1   <none>        443/TCP   137m

Status ContainerCreating will be change to Running soon.

At this stage, we can’t access to nginx service directly. In order to do that, we need “Service” object. Instead, we can get a shell of the Pod by kubectl exec -it nginx -- /bin/bash.

You can get more details about the Pod with kubectl describe {pod}.

You can edit the Pod directly from command kubectl edit pod {pod}.

Delete the Pod

kubectl delete pods <pod> --grace-period=0 --force