Note - Kubernetes in Action

I took notes during reading “Kubernetes in Action”.

It can be found on Amazon.

Part 1: Overview

Part 2: Core concepts

Chapter 3:

  • Po/pods/pod is similar. For example:
    1
    2
    3
    kubectl get po
    kubectl get pod
    kubectl get pods
    It’s the same with service/services.

Introducing pods

A pod is a Kubernetes abstraction that represents a group of one or more application containers (such as Docker or rkt), and some shared resources for those containers. You can have several related docker containers run on a pod.

A Pod always runs on a Node. A Node is a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster.

Create pod with yaml/json

  • Create pod from yaml file
    1
    kubectl create -f [yaml/json file]
  • Get a list of pods
    1
    kubectl get po
  • Get describes of pods
    1
    kubectl get po [pod_name] -o yaml
  • Get log of container (-c is optional, when you want to view the log of a specific container)
    1
    kubectl logs [pod_name] -c [container_name]
  • Forward port 80 from pod to port 8888 local (effective way to test pod)
    1
    kubectl port-forward [pod_name] 8888:80

Organizing resources with labels

  • Label is an arbitrary key-value attached to a resources. It can be defined under metadata.labels tag.
  • Each pod is recommended to be labeled with 2 labels: app (which microservice it belong to), env (which environment: staging, dev,..)
  • Get the list of pods with label
    1
    kubectl get po --show-labels
  • Get the pods with specific label by column (in this case is app & rel)
    1
    kubectl get po -L app,rel
  • Get the pods with label selector
    1
    2
    3
    4
    ///Get the pods have env label
    kubectl get po -l env
    ///Get the pods have specific env label
    kubectl get po -l env=staging
  • Change label of pods
    1
    2
    kubectl label po [pod_name] app=frontend
    kubectl label po [pod_name] --overwrite app=backend

Constrain pod scheduling with labels and selectors

All the pods is scheduled randomly across the worker nodes by default.

  • Add the label to specific nodes
    1
    2
    3
    4
    //get list of nodes
    kubectl get nodes --show-labels
    //add the label
    kubectl label node [node_name] gpu=true
  • Schedule pods to specific nodes which have gpu label is true by editing in yaml/json file. Should do like this instead of scheduling to a specific node because the node can be offline.
    1
    2
    3
    4
    5
    6
    ...
    spec:
    nodeSelector:
    gpu: "true"
    ...

Annotation

Mostly the same with label. But annotation is used for large blobs of data (256KB in total); mostly use of annotations is adding descriptions for each pod.

1
kubectl annotate pod [pod_name] mycompany.com/someannotation="this pos is used for..."

Namespaces

  • Copyrights © 2017-2024 Bach Nguyen

请我喝杯咖啡吧~

支付宝
微信