1.Deployment概念
用于部署无状态的服务,这个最常用的控制器。一般用于管理维护企业内部无状态的微服务,比如configserver、zuul、springboot。他可以管理多个副本的Pod实现无缝迁移、自动扩容缩容、自动灾难恢复、一键回滚等功能。
2.创建一个Deployment
1.命令行创建
[root@k8s-master01 ~]# kubectl create deployment nginx --image=nginx:1.15.2
2.导出yaml文件
[root@k8s-master01 ~]# kubectl get deployment nginx -o yaml > nginx-deploy.yaml、
3.查看yaml文件
[root@k8s-master01 ~]# cat -n nginx-deploy.yaml #### 这是删减修改后的
1 apiVersion: apps/v1
2 kind: Deployment
3 metadata:
4 annotations:
5 deployment.kubernetes.io/revision: "1"
6 creationTimestamp: "2020-09-19T02:41:11Z"
7 generation: 1
8 labels:
9 app: nginx
10 name: nginx
11 namespace: default
12 spec:
13 progressDeadlineSeconds: 600
14 replicas: 2 #副本数,默认是1
15 revisionHistoryLimit: 10 # 历史记录保留的个数
16 selector:
17 matchLabels:
18 app: nginx
19 strategy:
20 rollingUpdate:
21 maxSurge: 25%
22 maxUnavailable: 25%
23 type: RollingUpdate
24 template:
25 metadata:
26 creationTimestamp: null
27 labels:
28 app: nginx
29 spec:
30 containers:
31 - image: nginx:1.15.2
32 imagePullPolicy: IfNotPresent
33 name: nginx
34 resources: {}
35 terminationMessagePath: /dev/termination-log
36 terminationMessagePolicy: File
37 dnsPolicy: ClusterFirst
38 restartPolicy: Always
39 schedulerName: default-scheduler
40 securityContext: {}
41 terminationGracePeriodSeconds: 30
3.replace下
[root@k8s-master01 ~]# kubectl replace -f nginx-deploy.yaml # 通过文件管理我们的deployment
4.查看
[root@k8s-master01 ~]# kubectl get deployment nginx
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 2/2 2 2 11m
[root@k8s-master01 ~]# kubectl get deploy -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 2/2 2 2 12m nginx nginx:1.15.2 app=nginx
NAME: Deployment名称
READY:Pod的状态,已经Ready的个数
UP-TO-DATE:已经达到期望状态的被更新的副本数
AVAILABLE:已经可以用的副本数
AGE:显示应用程序运行的时间
CONTAINERS:容器名称
IMAGES:容器的镜像
SELECTOR:管理的Pod的标签
[root@k8s-master01 ~]# kubectl get po -owide 查看部署在哪个节点
5.edit管理
[root@k8s-master01 ~]# kubectl edit deploy nginx ### 副本数修改为1
deployment.apps/nginx edited
[root@k8s-master01 ~]# kubectl get po # 会先有个pod会进入Terminating
NAME READY STATUS RESTARTS AGE
nginx-66bbc9fdc5-jmgwq 1/1 Running 0 15m
nginx-66bbc9fdc5-l2lfn 0/1 Terminating 0 10m
[root@k8s-master01 ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-66bbc9fdc5-jmgwq 1/1 Running 0 16m