以下介绍如何使用kubernetes的deployment对象运行应用程序。
目标
– 创建nginx deployment。
– 使用kubectl列出有关部署的信息。
– 更新部署
创建和探索nginx部署
可以通过创建kubernetes部署对象来运行应用程序,还可以在yaml文件中描述部署。例如,这个yaml文件描述了运行nginx:1.7.9 docker映像的部署:
application/deployment.yaml
apiVersion: apps/v1 # for versions before 1.9. use apps/v1beta2
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: # tells deployment to run pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.
ports:
- containerPort:
注意:
selector字段定义Deployment如何查找要管理的pod。
在本例中,只需选择Pod模板中定义的标签(app:nginx)。然而,只要Pod模板本身满足规则,就可能有更复杂的选择规则。
1. 基于YAML文件创建deployment
kubectl apply -f application/deployment.yaml
2. 显示有关部署的信息:
kubectl describe deployment nginx-deployment
输出类似于:
user@computer:~/website$ kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Tue, Aug :: -
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision=
Selector: app=nginx
Replicas: desired | updated | total | available | unavailable
StrategyType: RollingUpdate
MinReadySeconds:
RollingUpdateStrategy: max unavailable, max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.7.
Port: /TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment- (/ replicas created)
No events.
3. 列出demployment创建的Pods:
kubectl get pods -l app=nginx
输出类似于:
NAME READY STATUS RESTARTS AGE
nginx-deployment--7o5ns / Running 16h
nginx-deployment--r18az / Running 16h
4. 显示Pod信息:
kubectl describe pod <pod-name>
<pod-name>是具体Pod的名称
更新Deployment
你可以通过应用新的yaml文件来更新部署。以下YAML文件将nginx更新到nginx 1.8
application/deployment-update.yaml
apiVersion: apps/v1 # for versions before 1.9. use apps/v1beta2
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas:
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.8 # Update the version of nginx from 1.7. to 1.8
ports:
- containerPort:
1. 应用新的yaml文件:
kubectl apply -f application/deployment-update.yaml
2. 查看使用新名称创建的Pods并删除Pods:
kubectl get pods -l app=nginx
通过增加副本计数来扩展应用程序
你可以通过应用新的yaml文件来增加部署中的Pod数量。此YAML文件将replicas设置为4,指定部署应具有四个pod:
application/deployment-scale.yaml
apiVersion: apps/v1 # for versions before 1.9. use apps/v1beta2
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: # Update the replicas from to
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.8
ports:
- containerPort:
1. 应用新的yaml文件:
kubectl apply -f application/deployment-scale.yaml
2. 验证部署是否有四个Pod:
kubectl get pods -l app=nginx
输出类似于:
NAME READY STATUS RESTARTS AGE
nginx-deployment--4zdqq / Running 25s
nginx-deployment--6zgi1 / Running 25s
nginx-deployment--fxcez / Running 2m
nginx-deployment--rwovn / Running 2m
删除Deployment
通过Deployment的名称删除Deployment
kubectl delete deployment nginx-deployment