传统虚拟化和Docker

Kubernetes - 图1

k8s集群结构

Kubernetes - 图2

k8s 组件

  • Nodes, which are worker machines that run containerized work units, make up a Kubernetes cluster. Every cluster has at least one worker node.
  • There is an API layer (Kubernetes API) that can communicate with Kubernetes clusters, which may be accessed via a command-line interface called kubectl.
  • The control plane, which controls and manages the cluster
  • The nodes, which are the workers’ nodes that run applications
  • Kubelet: This handles all the communication with the Kubernetes MasterControl plane.
  • kube-proxy: This handles all the networking proxy services on each node.
  • The container runtime, such as Docker.

控制平面组件负责制定全局集群决策(例如应用程序调度),以及监控和响应集群事件。

控制面板位于一台主机,其余node位于其他主机。

Kubernetes - 图3

Pod

Pod 是在 Kubernetes 中构建和管理的最小可部署计算单元。由一个或多个容器组成。

具备以下组件

  1. 一个专有 IP 地址,使Pod之间能够相互通信
  2. 基于应用程序需求的持久存储卷
  3. 确定容器应如何运行的配置信息

Kubernetes - 图4

Deployments

Deployment允许对 pod 和 ReplicaSet 进行声明性更改并提供所需的状态,Deployment控制器将逐步将当前状态更改为所需的状态。

Deployment 包含Pod

Kubernetes - 图5

下面是创建一个deployment

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-sample-deployment
  5. labels:
  6. app: nginx
  7. spec:
  8. replicas: 3
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - name: nginx
  19. image: nginx:1:21
  20. ports:
  21. - containerPort: 80

ReplicaSet 重复集

  1. # 创建并运行
  2. kubectl apply -f FILENAME
  3. # 查看deployment状态
  4. kubectl get deployments
  5. # NAME 命名空间中deployment的名称。
  6. # READY 显示有多少个应用程序副本可用。
  7. # UP-TO-DATE 显示已更新以达到所需状态的副本数。
  8. # AVAILABLE 显示可用副本的数量。
  9. # AGE 表示应用程序运行的时间长度。
  10. # 显示deployment的详细信息
  11. kubectl describe deployments
  12. # 删除部署
  13. kubectl delete -f FILENAME

StatefulSet 和 DaemonSet

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: nginx
  5. labels:
  6. app: nginx
  7. spec:
  8. ports:
  9. - port: 80
  10. name: web
  11. clusterIP: None
  12. selector:
  13. app: nginx
  14. ---
  15. apiVersion: apps/v1
  16. kind: StatefulSet
  17. metadata:
  18. name: web
  19. spec:
  20. selector:
  21. matchLabels:
  22. app: nginx
  23. serviceName: "nginx"
  24. replicas: 3
  25. template:
  26. metadata:
  27. labels:
  28. app: nginx
  29. spec:
  30. containers:
  31. - name: nginx
  32. image: nginx:latest
  33. ports:
  34. - containerPort: 80
  35. name: web
  36. volumeMounts:
  37. - name: www_volume
  38. mountPath: /usr/share/nginx/html
  39. volumeClaimTemplates:
  40. - metadata:
  41. name: www_volume
  42. spec:
  43. accessModes: [ "ReadWriteOnce" ]
  44. storageClassName: "my-storage-class"
  45. resources:
  46. requests:
  47. storage: 10Gi

StatefulSet API 对象用于处理有状态的应用程序。StatefulSet 与deployment类似,处理具有相同容器规范的 pod。与deployment不同,StatefulSet 为其每个 pod 使用持久身份。

  1. apiVersion: apps/v1
  2. kind: DaemonSet
  3. metadata:
  4. name: fluent-bit
  5. namespace: kube-system
  6. labels:
  7. k8s-app: fluent-bit
  8. spec:
  9. selector:
  10. matchLabels:
  11. name: fluent-bit
  12. template:
  13. metadata:
  14. labels:
  15. name: fluent-bit
  16. spec:
  17. tolerations:
  18. - key: node-role.kubernetes.io/master
  19. operator: Exists
  20. effect: NoSchedule
  21. containers:
  22. - name: fluent-bit
  23. image: fluent/fluent-bit:latest
  24. resources:
  25. limits:
  26. memory: 200Mi
  27. requests:
  28. cpu: 100m
  29. memory: 200Mi

一个DaemonSet 保证所有(或部分)节点都有一个运行的 pod 副本

随着节点被添加到集群中,Pod 也被添加到它们中;当从集群中删除节点时,pod也会被回收;当您删除 DaemonSet 时,它生成的 pod 也会被删除。

Jobs 和 CronJobs

一个Job执行一个或多个 pod 并继续尝试执行它们,直到达到指定数量,该Job跟踪已成功完成的 pod 数量;当达到指定数量的成功完成时,任务(即Job)就完成了。

当你删除一个job时,它也会删除它创建的所有 Pod;暂停job时会导致所有当前 pod 被删除,直到job恢复。

以下代码显示了每分钟运行一次 “example Job Pod is Running” 打印的job配置

  1. apiVersion: batch/v1
  2. kind: Job
  3. metadata:
  4. name: example-job
  5. spec:
  6. template:
  7. spec:
  8. containers:
  9. - name: example-job
  10. image: busybox
  11. command: ['echo', 'echo example Job Pod is Running']
  12. restartPolicy: OnFailure
  13. backoffLimit: 4

CronJob 是一份定期创建的job;

通过Cron( Chronos 时间)格式编写Job何时工作(实现自动化)类似 crontab(cron table)文件中的一行。

  1. apiVersion: batch/v1
  2. kind: CronJob
  3. metadata:
  4. name: example-cronjob
  5. spec:
  6. schedule: "*/1 * * * *" // linux crontab 语法
  7. jobTemplate:
  8. spec:
  9. template:
  10. spec:
  11. containers:
  12. - name: example-cronjob
  13. image: busybox
  14. imagePullPolicy: IfNotPresent
  15. command:
  16. - /bin/sh
  17. - -c
  18. - date; echo example-cronjob Pod is Running ; sleep 5
  19. restartPolicy: OnFailure

Services

服务是定义一组逻辑 pod 的抽象以及访问它们的策略。

该服务将TCP9876端口路由到所有带有app=exampleApp标签的pods 80端口(类似端口映射)

Kubernetes 服务将一组 pods 连接到一个抽象的服务名称(example-service)和 IP 地址,Pod 之间的发现和路由由服务提供。

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: example-service
  5. spec:
  6. selector:
  7. app: exampleApp
  8. ports:
  9. - protocol: TCP
  10. port: 80
  11. targetPort: 9876
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: nginx-service
  5. spec:
  6. selector:
  7. app: webserver-nginx-multiport-example
  8. ports:
  9. - name: http
  10. protocol: TCP
  11. port: 80
  12. targetPort: 8080
  13. - name: https
  14. protocol: TCP
  15. port: 443
  16. targetPort: 8090