本文最后更新于 321 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com
有-无状态控制器
Pod与controllers的关系
- controllers:在集群上管理和运行容器的对象
- 通过label-selector相关联
- Pod通过控制器实现应用的运维,如伸缩,滚动升级等
一、Deployment
部署无状态应用
应用场景:Web服务、微服务
- 管理Pod和ReplicaSet
- 具有上线部署、副本设定、滚动升级、回滚等功能
- 提供声明式更新,例如只更新一个新的Image
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
二、DaemonSet
-
在每一个Node上运行一个Pod
-
新加入的Node也同样会自动运行一个Pod
-
应用场景:Agent
官方文档:https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/
三、Job
-
Job分为普通任务(Job)和定时任务(CronJob)
-
一次性执行
-
-
应用场景:离线数据处理,视频解码等业务
官方文档:https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
四、CronJob
-
定时任务,像Linux的Crontab一样。
-
定时任务
-
-
应用场景:通知,备份
官方文档:https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/
apiVersion: batch/v1beta1 kind: CronJob metadata: name: hello spec: schedule: "*/1 * * * *" jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: - /bin/sh - -c - date; echo Hello from the Kubernetes cluster restartPolicy: OnFailure
五、有状态控制器
官方文档:https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
1.StatefulSet
- 部署有状态应用
- 解决Pod独立生命周期,保持Pod启动顺序和唯一性
- 稳定,唯一的网络标识符,持久存储
- 有序,优雅的部署和扩展、删除和终止
- 有序,滚动更新
- 应用场景:数据库
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 # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 3 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "my-storage-class"
resources:
requests:
storage: 1Gi
StatefulSet与Deployment区别:有身份的!
身份三要素:
-
域名
-
主机名
-
存储(PVC)
-
ClusterIP A记录格式:
. .svc.cluster.local ClusterIP=None A记录格式: . .svc.cluster.local 示例:web-0.nginx.default.svc.cluster.local
2.Headless
无头Service
clusterIP: None
主要应用于有状态服务:
域名
主机名
存储(PVC)