k8s - deployment

Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义(declarative)方法,用来替代以前的ReplicationController 来方便的管理应用。典型的应用场景包括:
1. 定义Deployment来创建Pod和ReplicaSet
2. 滚动升级和回滚应用
3. 扩容和缩容
4. 暂停和继续Deployment

RS 与 Deployment 的关联
k8s - deployment_第1张图片
1. 部署一个Nginx应用

[root@k8s-master01 k8s-test]# cat nginx-deployment.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  template:
    metadata: 
      labels: 
        app: nginx
    spec:
      containers:
      - name: nginx
        image: kone.com/library/nginx:latest
        ports:
          - containerPort: 80 

kubectl apply -f nginx-deployment.yaml

[root@k8s-master01 k8s-test]# kubectl get pod 
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-69c69cf68d-7krsq   1/1     Running   0          4m43s
nginx-deployment-69c69cf68d-hzbfv   1/1     Running   0          4m31s
nginx-deployment-69c69cf68d-vvxcc   1/1     Running   0          4m43s

2. 扩容kubectl scale deployment nginx-deployment --replicas=6

[root@k8s-master01 k8s-test]# kubectl scale deployment nginx-deployment --replicas=6
deployment.extensions/nginx-deployment scaled
[root@k8s-master01 k8s-test]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-69c69cf68d-2bfrd   1/1     Running   0          6s
nginx-deployment-69c69cf68d-7krsq   1/1     Running   0          21m
nginx-deployment-69c69cf68d-9nsbk   1/1     Running   0          6s
nginx-deployment-69c69cf68d-hzbfv   1/1     Running   0          21m
nginx-deployment-69c69cf68d-vh9w6   1/1     Running   0          6s
nginx-deployment-69c69cf68d-vvxcc   1/1     Running   0          21m

3. 设置自动扩展
如果集群支持 horizontal pod autoscaling 的话,还可以为Deployment设置自动扩展
kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80

4. 更新镜像,更新nginx版本
获取当前nginx版本

[root@k8s-master01 k8s-test]# kubectl exec nginx-deployment-846fbcbb-gsrbx -- nginx -v
nginx version: nginx/1.9.1

执行命令更新版本kubectl set image deployment/nginx-deployment nginx=kone.com/library/nginx:latest

[root@k8s-master01 k8s-test]# kubectl set image deployment/nginx-deployment nginx=kone.com/library/nginx:latest
deployment.extensions/nginx-deployment image updated

查看当前版本

[root@k8s-master01 k8s-test]# kubectl exec nginx-deployment-69c69cf68d-4qvzn -- nginx -v
nginx version: nginx/1.17.8

5. 回滚kubectl rollout undo deployment/nginx-deployment

[root@k8s-master01 k8s-test]# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment rolled back

查看回滚状态kubectl rollout status deployment/nginx-deployment
[root@k8s-master01 k8s-test]# kubectl rollout status deployment/nginx-deployment
deployment “nginx-deployment” successfully rolled out
查看当前版本

[root@k8s-master01 k8s-test]# kubectl exec nginx-deployment-846fbcbb-645tk -- nginx -v
nginx version: nginx/1.9.1

6. 查看历史版本kubectl rollout history deployment/nginx-deployment

[root@k8s-master01 k8s-test]# kubectl rollout history deployment/nginx-deployment 
deployment.extensions/nginx-deployment 
REVISION  CHANGE-CAUSE
5         
9         
10        

指定回滚到某个历史版本kubectl rollout undo deployment/nginx-deployment --to-revision=2

暂停 deployment 的更新kubectl rollout pause deployment/nginx-deployment

你可能感兴趣的:(K8S)