Introduction to DevOps-7-K8S

Posted by We Must Design on Thu, 03 Mar 2022 02:12:57 +0100

Introduction to Lec7-K8S

1. Why do you need K8S?

1.1. What is the container?

  1. Container is the representative of resource refinement.
  2. Container usually refers to the process running in an isolated environment and the environment on which the process depends.
    1. Isolation: with the help of linux namespace mechanism
    2. Resource limitation: cgroup computing based on linux can limit the resources that can be used by the container, mainly referring to cpu and memory resources
    3. Process: linux Process
    4. Dependent environment: the library and other resources on which the process runs
  3. Advantages of containers
    1. Lighter weight
    2. More efficient use of resources
    3. Application centered management

1.2. Why do I need K8S

  1. A system is needed to manage the large number of containers on the cluster.
  2. It is used to complete the arrangement of containers. At present, K8S is the de facto standard in the field of container arrangement.

2. What is K8S

  1. K8S is a portable and extensible open source platform for managing containerized workloads and services, which can promote declarative configuration and automation.
  2. ability
    1. Support different underlying resource platforms
    2. Automatic elastic expansion
    3. high reliability

2.1. K8S logic architecture

  1. Master node
    1. API Server
    2. Schuduler
    3. Controller
    4. etcd
  2. Node node
    1. pod
    2. docker
    3. kubelet
    4. kube-proxy
    5. fluentd

2.2. K8S assembly

  1. Control plane: make global decisions on the cluster and check and respond to the cluster time (for example, start a new pod when the replicas field of the deployment is not satisfied)
    1. kube-apiserver
    2. kube-scheduler
    3. kube-controller-manager
    4. cloud-controller-manager
    5. etcd
  2. Calculation plane
    1. The calculation plane consists of Node nodes
    2. Run component
      1. Kubelet
      2. Kube-proxy
      3. Container runtime
  3. Addons
    1. DNS
    2. Dashboard
    3. Container resource monitoring
    4. journal

2.3. Key concepts of k8s

2.3.1. POD

  1. POD is the smallest resource management unit of K8S, and POD is a little fragile.
  2. POD contains resources
    1. One or more containers
    2. Storage volume
    3. Network address, such as IP address, port, etc

2.3.2. Workload workload

  1. Workload is an application running on K8S, which mainly supports
    1. Deployment
    2. Statefulset
    3. Damonset
    4. Job
    5. CronJob

2.3.3. Service

  1. Service defines how POD is provided to external applications
  2. Why introduce Service?
  3. How is Service associated with Pod?
  4. What types of services are there
    1. ClusterIP
    2. NodePort
    3. LoadBalancer
    4. ExternalName

2.3.4. label

  1. label is a key/value pair, which is used to identify, manage and identify resources.
  2. Tags can be added to all resources such as Node, Pod, workload, key, configmap, service, etc
  3. Tags are usually used to identify resources and are generally used in conjunction with tag selectors.

3. K8S practical operation

  1. The actual operation will cover the complete life cycle of an application
  2. Experimental operation address:

3.1. Create cluster

  1. Minikube was used in this experiment
cat /etc/lsb-release
hostnamectl set-hostname yanwei
minikube start	
kubectl version
kubectl cluster-info
kubectl get nodes
kubectl get pod -n kube-system
systemctl status kubelet    Thinking questions: kubelet Can I put it in a container?

kubectl get pod --no-headers --all-namespaces |wc -l
docker ps -a |grep -v NAMES |wc -l

pause Container i.e infra Container, mainly responsible for POD Prepare network and storage, other containers and Pause Containers share network and storage.
docker inspect 

kubectl get nodes --show-labels
kubectl label nodes <your-node-name> disktype=ssd

3.2. Deploy application

kubectl get nodes //Wait for the node to become ready
kubectl create deployment
kubectl get pod -o wide
kubectl label pod

3.3. expose application

kubectl delete deployment kubernetes-bootcamp
kubectl create deployment
kubectl scale deployments/ --replicas=3
kubectl expose deployment/ --type="NodePort" --port 8080  --name yanwei

visit service´╝îMultiple visits service´╝îIt's different pod Returned in.
curl $(minikube ip):31172

kubectl exec -ti $POD_NAME -- /bin/bash
ps -elf
more server.js

3.4. Capacity expansion and capacity reduction application

kubectl get deployments   
kubectl get rs
kubectl delete deployments kubernetes-bootcamp
kubectl create deployment

kubectl get deployments   
kubectl scale deployments/ --replicas=4
kubectl scale deployments/ --replicas=2

3.5. Rolling upgrade

kubectl delete deployments kubernetes-bootcamp
kubectl delete service kubernetes-bootcamp
kubectl create deployment

kubectl scale deployments/ --replicas=3
kubectl expose deployment/ --type="NodePort" --port 8080  --name yanwei

curl $(minikube ip):31120  //Execute multiple times 
kubectl set image deployments/ kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
curl $(minikube ip):31120  //Check the version before and after multiple upgrades

kubectl rollout status deployments/

Upgrade failed
kubectl set image deployments/

kubectl get pod -o wide  // Failed to download a pod image

kubectl describe pod

kubectl rollout undo deployments/

3.6. K8S learning suggestions

4. K8S advanced

4.1. Design mode - K8S design mode

  1. Controller mode: programming mode oriented to the desired state
    1. One controller manages at least one type of K8S resources.
    2. Each resource object has a spec field that represents the desired state.
    3. The controller of the resource is responsible for ensuring that the current state approaches the desired state
  2. Recommended reading: Kubernetes design pattern

4.2. Design pattern - plug in pattern

4.3. storage

4.3.1. K8S storage architecture

4.3.2. CSI architecture

4.4. network

4.4.1. Communication between pods

4.4.2. kube-proxy & kube-dns

4.4.3. flannel

4.5. dispatch

4.6. Container runtime

Topics: architecture Container DevOps