作用:
- 在集群上管理和运行容器的对象
- POD是通过Controller实现应用的运维,比如伸缩、滚动升级等;
- POD和Controller之间通过label标签建立关系(selector);
deployment的应用场景:
- 部署无状态应用;
- 管理POD和ReplicaSet;
- 部署、滚动升级;
- 部署如:web服务、微服务等;
部署应用
使用deployment部署应用(yml)
- 生成yml文件
kubectl create deployment web --image=nginx --dry-run=client -o yaml > web.yaml
执行之后,会在当前文件夹生成一个web.yaml
的文件,文件中的当前需要关注的节点信息有spec的selector和labels:
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
- 使用yml部署应用
kubectl apply -f web.yaml
- 对外发布(暴露对外端口)
kubectl expose deployment web --port=80 --type=NodePort --target-port=80 --name=web -o yaml > webInfo.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2021-01-28T08:05:49Z"
labels:
app: web
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:labels:
.: {}
f:app: {}
f:spec:
f:externalTrafficPolicy: {}
f:ports:
.: {}
k:{"port":80,"protocol":"TCP"}:
.: {}
f:port: {}
f:protocol: {}
f:targetPort: {}
f:selector:
.: {}
f:app: {}
f:sessionAffinity: {}
f:type: {}
manager: kubectl-expose
operation: Update
time: "2021-01-28T08:05:49Z"
name: web
namespace: default
resourceVersion: "79217"
uid: 8b885b95-544f-4a9a-b910-be052ae724f4
spec:
clusterIP: 10.109.39.36
clusterIPs:
- 10.109.39.36
externalTrafficPolicy: Cluster
ports:
- nodePort: 30582
port: 80
protocol: TCP
targetPort: 80
selector:
app: web
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
kubectl apply -f webInfo.yaml
- 验证
$ kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-6799fc88d8-k6sjg 1/1 Running 0 9d
pod/web-96d5df5c8-5bhv8 1/1 Running 0 10m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 13d
service/nginx NodePort 10.106.2.129 <none> 80:32024/TCP 9d
service/web NodePort 10.109.39.36 <none> 80:30582/TCP 4m33s
最后一行的port 30582即为访问端口,访问方式为任意Node的IP+port。
应用升级和回滚
- 升级
$ kubectl set image deployment web nginx=nginx:1.15
> deployment apps/web image updated
- 查看升级状态
$ kubctl rollout status deployment web
> deployment "web" successfully rolled out
- 查看升级版本
$ kubectl rollout history deployment web
> deployment apps/web
> REVERSION CHANGE-CAUSE
> 1 <none>
> 2 <none>
上面显示有两个版本,最后一条为最新正在使用的版本。
- 回滚到上一个版本
$ kubectl rollout undo deployment web
- 回滚到制定版本
$ kubectl rollout undo deployment web --to-version=2
--to-reversion
指定要回滚的版本号。
- 改变副本数量
$ kubectl scale deployment web --reolicas=10
新增副本数量到10个。
网友评论