文档:https://v1-25.docs.kubernetes.io/zh-cn/docs/concepts/workloads/controllers/
vim rs-example.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-example
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
kubectl apply -f rs-example.yml
kubectl get pod --show-labels
replicaset是通过标签匹配pod
kubectl label pod replicaset-example-w9z28 app=myapp --overwrite
kubectl get pod --show-labels
kubectl label pod replicaset-example-w9z28 app=nginx --overwrite
kubectl get pod --show-labels
replicaset自动控制副本数量,pod可以自愈
kubectl delete pod replicaset-example-q2sq9
kubectl get pod --show-labels
回收资源
kubectl delete -f rs-example.yml
vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
kubectl apply -f deployment-example.yaml
kubectl get all
vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v2
kubectl apply -f deployment-example.yaml
kubectl get all
replicaset就是deployment的一个版本
vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
kubectl apply -f deployment-example.yaml
kubectl get all
vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
minReadySeconds: 5
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
kubectl apply -f deployment-example.yaml
kubectl describe deployments.apps deployment-example
暂停,避免触发不必要的线上更新
kubectl rollout pause deployment deployment-example
vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
minReadySeconds: 5
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
replicas: 6
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: nginx
resources:
limits:
cpu: 0.5
memory: 200Mi
requests:
cpu: 0.5
memory: 200Mi
kubectl apply -f deployment-example.yaml
kubectl get all
调整副本数,不受影响
但是更新镜像和修改资源并没有触发更新
kubectl rollout history deployment deployment-example
恢复后开始触发更新
kubectl rollout resume deployment deployment-example
kubectl rollout history deployment deployment-example
kubectl get pod
回收
kubectl delete -f deployment-example.yaml
vim daemonset-example.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
tolerations:
- effect: NoSchedule
operator: Exists
containers:
- name: nginx
image: nginx
kubectl apply -f daemonset-example.yml
kubectl get pod -o wide
kubectl delete -f daemonset-example.yml
vim job.yml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
completions: 6
parallelism: 2
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
kubectl apply -f job.yml
kubectl get pod
kubectl delete -f job.yml
vim cronjob.yml
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
kubectl apply -f cronjob.yml
kubectl get all
kubectl delete -f cronjob.yml