文章目录
控制器
Pod 的分类:
• 自主式 Pod:Pod 退出后不会被创建
• 控制器管理的 Pod:在控制器的生命周期里,始终要维持 Pod 的副本数目
• 控制器类型:
• Replication Controller和ReplicaSet
• Deployment
• DaemonSet
• StatefulSet
• Job
• CronJob
• HPA全称Horizontal Pod Autoscaler
ReplicaSet控制器
设定副本数
[root@k8s1 ~]# vim replicaset-example.yaml
[root@k8s1 ~]# kubectl apply -f replicaset-example.yaml
replicaset.apps/replicaset-example created
[root@k8s1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-example-5dm96 1/1 Running 0 8s
replicaset-example-q5fn2 1/1 Running 0 8s
replicaset-example-tnl9v 1/1 Running 0 8s
replicas:
设定副本数,可修改
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
标签的重要性
修改标签之后,会自动拉取一个新的符合旧标签条件的pod容器
[root@k8s1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-example-5dm96 1/1 Running 0 4m47s app=nginx
replicaset-example-q5fn2 1/1 Running 0 4m47s app=nginx
replicaset-example-tnl9v 1/1 Running 0 4m47s app=nginx
[root@k8s1 ~]# kubectl label pod
poddisruptionbudgets.policy pods podsecuritypolicies.policy podtemplates
[root@k8s1 ~]# kubectl label pod replicaset-example-5dm96 app=myapp --overwrite
pod/replicaset-example-5dm96 labeled
[root@k8s1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-example-4cdxx 1/1 Running 0 4s app=nginx
replicaset-example-5dm96 1/1 Running 0 5m23s app=myapp
replicaset-example-q5fn2 1/1 Running 0 5m23s app=nginx
replicaset-example-tnl9v 1/1 Running 0 5m23s app=nginx
[root@k8s1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicaset-example 3 3 3 5m39s
deployment控制器
deploymnet控制器会去先拉取replicaset控制器,通过replicaset控制器去拉取Pod容器
[root@k8s1 ~]# vim deployment.yaml
[root@k8s1 ~]# kubectl apply -f deployment.yaml
deployment.apps/deployment-example created
[root@k8s1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-example-85b98978db-2d4c4 1/1 Running 0 12s
pod/deployment-example-85b98978db-5jmk6 1/1 Running 0 12s
pod/deployment-example-85b98978db-bgs5g 1/1 Running 0 12s
pod/replicaset-example-5dm96 1/1 Running 0 10m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d
service/liveness-http ClusterIP 10.99.7.186 <none> 80/TCP 21m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment-example 3/3 3 3 12s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-example-85b98978db 3 3 3 12s
[root@k8s1 ~]#
更新控制器版本
ps:myapp镜像一定要先上传到harbor仓库
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 6
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec: