k8s core concepts
Zhengliang Wang edited at Mon Jul 01 2024
Cloud
k8s

Architecture

Master
  • ETCD Cluster: K-V database
  • kube-apiserver: management component in k8s.
  • Controller Manager:
    • Node Controller
    • Replication Controller
  • kube-scheduler
Worker Nodes
  • kubelet: an agent that runs on each node in a cluster to monitor the health and status of the node.
  • Kube-proxy: ensure the necessary rules in place on the worker nodes to allow the containers running on them can reach each other.

ETCD

K-V Store

listen on port 2379

To set K-V

./etcdctl put key1 value1

To retrieve a value

./etcdctl get key1

To set ETCD API version:

ETCDCTL_API=3 ./etcdctl version

Kube-apiserver

Responsible for

  1. Authenticate user
  2. Validate request
  3. retrieve data
  4. update ETCD
  5. scheduler
  6. kubelet

Controller manager

Node controller:

  • watch status
  • remediate situation
  • node monitoring:
    • heartbeat every n seconds
    • on unreachable state, node controller puts grace period to wait til the node comes back up.
    • POD Eviction timeout takes action after grace period. Replication Controller:
  • responsible for monitoring replica sets and ensure the desired number of PODs are available at all time within the set.

Scheduler

responsible for scheduling pods on nodes.

filter nodes -> rank nodes ( resource requirements and limits, taints and tolerations, node selectors/ affinity)

Kubelet

  • Register Node
  • Create PODs
  • Monitor Nodes and PODs

YML in K8S

example:

# pod-definition.yml

apiVersion: v1
kind: Pod
metadata:
  name: nginx

spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

Commands:

Document

Create pods:

kubectl apply -f filename.yml

kubectl run [pod_name] --image=[image_name]
# to do dry run create
kubectl run redis --image=redis --dry-run=client -o yml

Get pods:

kubectl get pods

Delete pods:

  # Delete a pod using the type and name specified in pod.json
  kubectl delete -f ./pod.json
  
  # Delete resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml
  kubectl delete -k dir
  
  # Delete resources from all files that end with '.json'
  kubectl delete -f '*.json'
  
  # Delete a pod based on the type and name in the JSON passed into stdin
  cat pod.json | kubectl delete -f -
  
  # Delete pods and services with same names "baz" and "foo"
  kubectl delete pod,service baz foo
  
  # Delete pods and services with label name=myLabel
  kubectl delete pods,services -l name=myLabel
  
  # Delete a pod with minimal delay
  kubectl delete pod foo --now
  
  # Force delete a pod on a dead node
  kubectl delete pod foo --force
  
  # Delete all pods
  kubectl delete pods --all

Get details of the pod

kubectl describe pod [pod_name]

Create an NGINX Pod

kubectl run nginx --image=nginx

Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)

kubectl run nginx --image=nginx --dry-run=client -o yaml

Create a deployment

kubectl create deployment --image=nginx nginx

Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)

kubectl create deployment --image=nginx nginx --dry-run=client -o yaml

Generate Deployment YAML file (-o yaml). Don’t create it(–dry-run) and save it to a file.

kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml

Make necessary changes to the file (for example, adding more replicas) and then create the deployment.

kubectl create -f nginx-deployment.yaml****