资源调度 Deployment:扩缩容
扩容和缩容,常用的功能
scale
[root@kubeadm-master1 ~]# kubectl scale --help
Set a new size for a Deployment, ReplicaSet, Replication Controller, or StatefulSet.
Scale also allows users to specify one or more preconditions for the scale action.
If --current-replicas or --resource-version is specified, it is validated before the scale is attempted, and it is
guaranteed that the precondition holds true when the scale is sent to the server.
Examples:
# Scale a replicaset named 'foo' to 3.
kubectl scale --replicas=3 rs/foo
# Scale a resource identified by type and name specified in "foo.yaml" to 3.
kubectl scale --replicas=3 -f foo.yaml
# If the deployment named mysql's current size is 2, scale mysql to 3.
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql
# Scale multiple replication controllers.
kubectl scale --replicas=5 rc/foo rc/bar rc/baz
# Scale statefulset named 'web' to 3.
kubectl scale --replicas=3 statefulset/web
实操部分
[root@kubeadm-master1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deploy-845964f5bf 4 4 4 61m
nginx-deploy-968b78ccf 0 0 0 41m
nginx-deployment-67dfd6c8f9 1 1 1 56d
tomcat-deployment-6c44f58b47 1 1 1 56d
[root@kubeadm-master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 50d
my-pod 1/1 Running 0 9d
my-pod1 0/1 Completed 6 20h
net-test1 1/1 Running 138 57d
net-test2 1/1 Running 14 57d
nginx-deploy-845964f5bf-dqdzt 1/1 Running 0 6m33s
nginx-deploy-845964f5bf-k8vbj 1/1 Running 0 6m35s
nginx-deploy-845964f5bf-pxs78 1/1 Running 0 10s
nginx-deploy-845964f5bf-xk49f 1/1 Running 0 6m34s
nginx-deployment-67dfd6c8f9-5s6nz 1/1 Running 1 56d
tomcat-deployment-6c44f58b47-4pz6d 1/1 Running 1 56d
[root@kubeadm-master1 ~]# kubectl scale --replicas=2 deploy nginx-deploy
deployment.apps/nginx-deploy scaled
[root@kubeadm-master1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deploy-845964f5bf 2 2 2 61m
nginx-deploy-968b78ccf 0 0 0 41m
nginx-deployment-67dfd6c8f9 1 1 1 56d
tomcat-deployment-6c44f58b47 1 1 1 56d
[root@kubeadm-master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 50d
my-pod 1/1 Running 0 9d
my-pod1 0/1 Completed 6 20h
net-test1 1/1 Running 138 57d
net-test2 1/1 Running 14 57d
nginx-deploy-845964f5bf-k8vbj 1/1 Running 0 7m1s
nginx-deploy-845964f5bf-xk49f 1/1 Running 0 7m
nginx-deployment-67dfd6c8f9-5s6nz 1/1 Running 1 56d
tomcat-deployment-6c44f58b47-4pz6d 1/1 Running 1 56d
资源调度 Deployment:更新的暂停与恢复
kubectl rollout pause 和 kubectl rollout resume 的详细示例。
假设你有一个名为 my-deployment 的 Deployment,它运行的是 my-app:v1 的镜像。你想要把镜像更新为 my-app:v2,但是你希望在更新之前先暂停 Deployment,以便进行一些配置的修改。
首先,用以下命令暂停 Deployment:
kubectl rollout pause deployment/my-deployment
然后,你可以修改 Deployment 的配置。比如,你可以用以下命令更换镜像:
kubectl set image deployment/my-deployment my-app=my-app:v2
此时,即使你修改了 Deployment 的配置,也不会触发新的滚动更新,因为 Deployment 正处于暂停状态。
当你准备好进行更新时,你可以用以下命令恢复 Deployment:
kubectl rollout resume deployment/my-deployment
恢复后,Deployment 控制器会开始新的滚动更新,使用你新设置的 my-app:v2 镜像。
资源调度 StatefulSet:定义一个有状态服务
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
annotations:
volume.alpha.kubernetes.io/storage-class: anything
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi