tags:
categories:
什么是控制器:Kubernetes内建了很多controller (控制器),这些相当于一个状态机,用来控制Pod的具体状态和行为。
控制器类型
ReplicationController和ReplicaSet介绍
Deployment介绍:(如果在RC和Deployment选择,选Deployment有很多功能。比如:滚动更新RC就做不到)
DaemonSet介绍(Node节点的)
Job介绍:(批处理任务) Job负责批处理任务,即仅执行一次的任务, 它保证批处理任务的一个或多个Pod成功结束
CronJob介绍:(批处理任务)CronJob管理基于时间的Job,即:
StatefulSet介绍(有状态服务)
Horizontal Pod Autoscaling(自动拓展,可以理解成控制器的附属品,控制控制器)
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
spec:
replicas: 3
selector: # 选择标签
matchLabels: # 匹配标签
app: frontend # 标签名
template: # 模板
metadata:
labels: # 定义标签
app: frontend
spec:
containers:
- name: myapp
image: hub.qnhyn.com/library/myapp
env: # 注入环境变量
- name: GET_HOST_FROM
value: dns
ports:
- containerPort: 80
# 删除默认空间下所有pod
kubectl delete pod --all
kubectl create -f rs.yaml
# RS创建的标签查看
kubectl get pod
kubectl explain rs
# 查看pod的标签 上面rs.yaml定义的标签
kubectl get pod --show-labels
# 修改一个pod的标签
kubectl label pod frontend-dxkrc app=qnhyn --overwrite=True
# 删除RS
kubectl delete rs --all
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector: # 选择标签
matchLabels: # 匹配标签
app: nginx-deployment # 标签名
template:
metadata:
labels:
app: nginx-deployment
spec:
containers:
- name: nginx
image: hub.qnhyn.com/library/myapp
ports:
- containerPort: 80
## 加上--record参数可以记录命令,我们可以很方便的查看每次 revision 的变化
kubectl apply -f deployment.yaml --record
## 观察创建deployment
kubectl get deployment
# deployment先创建rs
kubectl get rs
# deployment通过rs创建pod
kubectl get pod
kubectl get pod -o wide
# 访问服务
curl 10.244.1.19
kubectl scale deployment nginx-deployment --replicas=10
kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
kubectl rollout undo deployment/nginx-deployment
# 通过这个命令可以看到 更新策略
kubectl describe deployments
# 设置新镜像
kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
# 查看更新的状态
kubectl rollout status deployments nginx-deployment
kubectl get pods
# 查看历史版本记录
kubectl rollout history deployment/nginx-deployment
# 回退到上一版本
kubectl rollout undo deployment/nginx-deployment
# 回退到指定历史版本 使用 --revision参数指定某个历史版本
kubectl rollout undo deployment/nginx-deployment --to-revision=2
## 暂停deployment的更新
kubectl rollout pause deployment/nginx-deployment
# 您可以用kubectl rollout status 命令查看Deployment是否完成
kubect1 rollout status # 将返回一个0值的Exit Code
kubectl rollout status deploy/nginx
# 查看上一条命令的退出码
echo $?
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: deamonset-example
labels:
app: daemonset
spec:
selector:
matchLabels:
name: deamonset-example
template:
metadata:
labels:
name: deamonset-example
spec:
containers:
- name: daemonset-example
image: hub.qnhyn.com/library/myapp
kubectl create -f deamonset-example.yaml
# 默认情况下, Pod不会部署到主节点上 默认副本为1
kubectl get DaemonSet
kubectl get pod -o wide
# 删除一个pod
kubectl delete pod deamonset-example-c24rb
# 自动恢复pod
kubectl get pod -o wide
kubectl delete DaemonSet deamonset-example
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
metadata:
name: pi
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
kubectl create -f job.yaml
# 发现一个pi的pod 状态为完成
kubectl get pod
# 查看job
kubectl get job
# 通过日志查看输出
kubectl logs pi-xvrbg
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
vi cronjob.yaml
kubectl create -f cronjob.yaml
# 查看定时任务
kubectl get cronjob
# 一分钟后查看pod
kubectl get pod
kubectl get job
# 查看输出
kubectl logs hello-1595068560-9lsr5
# 删除定时任务
kubectl delete cronjob hello