传统虚拟化和Docker
k8s集群结构
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位于其他主机。
Pod
Pod 是在 Kubernetes 中构建和管理的最小可部署计算单元。由一个或多个容器组成。
具备以下组件
- 一个专有 IP 地址,使Pod之间能够相互通信
- 基于应用程序需求的持久存储卷
- 确定容器应如何运行的配置信息
Deployments
Deployment允许对 pod 和 ReplicaSet 进行声明性更改并提供所需的状态,Deployment控制器将逐步将当前状态更改为所需的状态。
Deployment 包含Pod
下面是创建一个deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-sample-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1:21
ports:
- containerPort: 80
ReplicaSet 重复集
# 创建并运行
kubectl apply -f FILENAME
# 查看deployment状态
kubectl get deployments
# NAME 命名空间中deployment的名称。
# READY 显示有多少个应用程序副本可用。
# UP-TO-DATE 显示已更新以达到所需状态的副本数。
# AVAILABLE 显示可用副本的数量。
# AGE 表示应用程序运行的时间长度。
# 显示deployment的详细信息
kubectl describe deployments
# 删除部署
kubectl delete -f FILENAME
StatefulSet 和 DaemonSet
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www_volume
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www_volume
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "my-storage-class"
resources:
requests:
storage: 10Gi
StatefulSet API 对象用于处理有状态的应用程序。StatefulSet 与deployment类似,处理具有相同容器规范的 pod。与deployment不同,StatefulSet 为其每个 pod 使用持久身份。
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluent-bit
namespace: kube-system
labels:
k8s-app: fluent-bit
spec:
selector:
matchLabels:
name: fluent-bit
template:
metadata:
labels:
name: fluent-bit
spec:
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
containers:
- name: fluent-bit
image: fluent/fluent-bit:latest
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
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配置
apiVersion: batch/v1
kind: Job
metadata:
name: example-job
spec:
template:
spec:
containers:
- name: example-job
image: busybox
command: ['echo', 'echo example Job Pod is Running']
restartPolicy: OnFailure
backoffLimit: 4
CronJob 是一份定期创建的job;
通过Cron( Chronos 时间)格式编写Job何时工作(实现自动化)类似 crontab(cron table)文件中的一行。
apiVersion: batch/v1
kind: CronJob
metadata:
name: example-cronjob
spec:
schedule: "*/1 * * * *" // linux crontab 语法
jobTemplate:
spec:
template:
spec:
containers:
- name: example-cronjob
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo example-cronjob Pod is Running ; sleep 5
restartPolicy: OnFailure
Services
服务是定义一组逻辑 pod 的抽象以及访问它们的策略。
该服务将TCP9876端口路由到所有带有app=exampleApp标签的pods 80端口(类似端口映射)
Kubernetes 服务将一组 pods 连接到一个抽象的服务名称(example-service)和 IP 地址,Pod 之间的发现和路由由服务提供。
apiVersion: v1
kind: Service
metadata:
name: example-service
spec:
selector:
app: exampleApp
ports:
- protocol: TCP
port: 80
targetPort: 9876
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: webserver-nginx-multiport-example
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080
- name: https
protocol: TCP
port: 443
targetPort: 8090