Introduction to Lec7-K8S
1. Why do you need K8S?
1.1. What is the container?
- Container is the representative of resource refinement.
- Container usually refers to the process running in an isolated environment and the environment on which the process depends.
- Isolation: with the help of linux namespace mechanism
- 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
- Process: linux Process
- Dependent environment: the library and other resources on which the process runs
- Advantages of containers
- Lighter weight
- More efficient use of resources
- Application centered management
1.2. Why do I need K8S
- A system is needed to manage the large number of containers on the cluster.
- 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
- K8S is a portable and extensible open source platform for managing containerized workloads and services, which can promote declarative configuration and automation.
- ability
- Support different underlying resource platforms
- Automatic elastic expansion
- high reliability
2.1. K8S logic architecture
- Master node
- API Server
- Schuduler
- Controller
- etcd
- Node node
- pod
- docker
- kubelet
- kube-proxy
- fluentd
2.2. K8S assembly
- 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)
- kube-apiserver
- kube-scheduler
- kube-controller-manager
- cloud-controller-manager
- etcd
- Calculation plane
- The calculation plane consists of Node nodes
- Run component
- Kubelet
- Kube-proxy
- Container runtime
- Addons
- DNS
- Dashboard
- Container resource monitoring
- journal
2.3. Key concepts of k8s
2.3.1. POD
- POD is the smallest resource management unit of K8S, and POD is a little fragile.
- POD contains resources
- One or more containers
- Storage volume
- Network address, such as IP address, port, etc
2.3.2. Workload workload
- Workload is an application running on K8S, which mainly supports
- Deployment
- Statefulset
- Damonset
- Job
- CronJob
2.3.3. Service
- Service defines how POD is provided to external applications
- Why introduce Service?
- How is Service associated with Pod?
- What types of services are there
- ClusterIP
- NodePort
- LoadBalancer
- ExternalName
2.3.4. label
- label is a key/value pair, which is used to identify, manage and identify resources.
- Tags can be added to all resources such as Node, Pod, workload, key, configmap, service, etc
- Tags are usually used to identify resources and are generally used in conjunction with tag selectors.
3. K8S practical operation
- The actual operation will cover the complete life cycle of an application
- Experimental operation address: https://kubernetes.io/zh/docs/tutorials/kubernetes-basics/create-cluster/cluster-interactive/
3.1. Create cluster
- Minikube was used in this experiment
cat /etc/lsb-release
hostname
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 yanwei.app --image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl get pod -o wide
kubectl label pod yanwei.app-65ddbc7454-8fm8g transwarp.io/name=yanwei
3.3. expose application
kubectl delete deployment kubernetes-bootcamp
kubectl create deployment yanwei.app --image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl scale deployments/yanwei.app --replicas=3
kubectl expose deployment/yanwei.app --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 yanwei.app --image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl get deployments
kubectl scale deployments/yanwei.app --replicas=4
kubectl scale deployments/yanwei.app --replicas=2
3.5. Rolling upgrade
kubectl delete deployments kubernetes-bootcamp
kubectl delete service kubernetes-bootcamp
kubectl create deployment yanwei.app --image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl scale deployments/yanwei.app --replicas=3
kubectl expose deployment/yanwei.app --type="NodePort" --port 8080 --name yanwei
curl $(minikube ip):31120 //Execute multiple times
kubectl set image deployments/yanwei.app kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
curl $(minikube ip):31120 //Check the version before and after multiple upgrades
kubectl rollout status deployments/yanwei.app
Upgrade failed
kubectl set image deployments/yanwei.app kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
kubectl get pod -o wide // Failed to download a pod image
kubectl describe pod
kubectl rollout undo deployments/yanwei.app
3.6. K8S learning suggestions
4. K8S advanced
4.1. Design mode - K8S design mode
- Controller mode: programming mode oriented to the desired state
- One controller manages at least one type of K8S resources.
- Each resource object has a spec field that represents the desired state.
- The controller of the resource is responsible for ensuring that the current state approaches the desired state
- 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