k8s控制器之deployment(1)
deployment用于部署无状态应用所有pod无差别
·
deployment用于部署无状态应用
什么是无状态应用
1、所有pod无差别
2、所有pod中容器运行同一个镜像
3、所有pod可以运行在集群中任意node上
4、所有pod无启动顺序先后之分
5、随意pod数量扩容或缩容
创建一个deployment的控制器
查看deployment的apiversion信息
[root@master ~]# kubectl explain deployment
KIND: Deployment
VERSION: apps/v1
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-test #deployment名字
labels:
app: deployment #匹配的pod标签,表示deploymen控制器控制带有此标签的pod
spec:
replicas: 2 #副本集,deploment里面使用了replicaset控制器
selector:
matchLabels:
app: deployment #匹配的pod标签,表示rs控制器控制带有此标签的pod
template:
metadata:
labels:
app: deployment #pod的标签
spec:
containers: # 以下为pod里的容器定义
- name: nginx
image: nginx:1.15-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
kubectl apply -f nginx1-deployment.yml
[root@master k8s]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-test 2/2 2 2 4m27s
可以看到这里创建了一个名字叫deployment-test的控制器,管理的pod数量是2个
[root@master k8s]# kubectl describe deployments deployment-test
Name: deployment-test
Namespace: default #运行再default命名空间下
CreationTimestamp: Fri, 19 Mar 2021 17:08:20 +0800
Labels: app=deployment #控制该命名空间下标签为app=deployment的pod
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=deployment
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=deployment
Containers:
nginx:
Image: nginx:1.15-alpine
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: deployment-test-59c79bc456 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 6m37s deployment-controller Scaled up replica set deployment-test-59c79bc456 to 2
deployment的删除
被deployment控制的pod是没办法直接删除的,因为deployment控制会保证pod副本运行的数量,如果不够会自动启动一个新的pod,我们需要先删除控制器,那么里面的pod也会被自动删除
[root@master k8s]# kubectl delete deployments deployment-test
deployment.apps "deployment-test" deleted
[root@master k8s]# kubectl get pods
No resources found in default namespace.
pod版本升级
[root@master k8s]# kubectl describe pod deployment-test-59c79bc456-9vznb | grep Image:
Image: nginx:1.15-alpine
[root@master k8s]# kubectl set image deployment deployment-test nginx=nginx:1.16-alpine --record
deployment.apps/deployment-test image updated
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deployment-test-85f4f97dc5-g7szg 1/1 Running 0 24s
deployment-test-85f4f97dc5-vngqv 1/1 Running 0 21s
[root@master k8s]# kubectl describe pod deployment-test-85f4f97dc5-g7szg |grep Image:
Image: nginx:1.16-alpine
这里可以看到之前的版本是1.15 通过指定deployment升级成1.16版本
-
deployment deployment-test
代表名为deployment-test
的deployment -
nginx=nginx:1.16-alpine
前面的nginx为容器名 -
--record 表示会记录
更多推荐
已为社区贡献7条内容
所有评论(0)