一、deployment案例
vim nginx-deployment.yaml ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 template: #以下为嵌套pod的字段; metadata: labels: tier: nginx spec: containers: - name: nginx image: hub.atguigu.com/library/nginx:latest ports: - containerPort: 80 #由于k8s的网络为扁平化网络,直接可通过IP访问,因此无需声明端口。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kubectl apply -f nginx-deployment.yaml --record #--record参数可以记录命令,以便查看每次revision的变化。尽量加上该参数,如果不加,则 kubectl rollout history 查询历史版本时CHANGE-CAUSE的值皆为none。 kubectl get deployment;kubectl get rs;kubectl get pod --show-labels #创建deployment会自动创建对应的RS和pod出来。 kubectl get pod -o wide #查看pod运行节点及IP; curl 10.244.2.36:80 #测试nginx服务是否能访问。 kubectl scale deployment nginx-deployment --replicas=5 #对指定nginx-deployment进行扩容,此时,pod的RS不会发生变化; kubectl scale deployment nginx-deployment --replicas=2 #对指定nginx-deployment进行缩容,此时,pod的RS不会发生变化; kubectl get deployment;kubectl get rs;kubectl get pod #查看扩容结果; # kubectl autoscale deployment nginx-deployment --min=2 --max=15 --cpu-percent=80 #如果集群支持HPA的化,还可以为deployment设置为自动扩展。 kubectl exec nginx-deployment-567c69b49f-4vzqg -it -- /usr/sbin/nginx -v #查看nginx版本为1.21.0; kubectl set image deployment/nginx-deployment nginx=nginx:1.19.1 #更新镜像为nginx:1.19.1;也可直接edit其yaml文件中image版本。此时,pod的RS会发生变化; kubectl exec nginx-deployment-5c5c5d7bdc-f9bvz -it -- /usr/sbin/nginx -v #查看nginx更新后的版本为1.19.1; kubectl rollout undo deployment/nginx-deployment #回滚nginx上一个版本 kubectl rollout status deployment/nginx-deployment #查看rollout的状态及更新流程(按照更新策略更新)。如果rollout成功完成,将会返回一个0的exit code。 kubectl rollout history deployment/nginx-deployment #查看rollout的历史。 kubectl rollout pause deployment/nginx-deployment #暂停deployment的更新。 kubectl rollout undo deployment/nginx-deployment --to-revision=2 #回滚nginx指定版本; kubectl exec nginx-deployment-567c69b49f-5mnx7 -it -- /usr/sbin/nginx -v #查看nginx更新后的版本为1.21.0; kubectl get rs #查看rs,包含历史RS; kubectl describe deployments #查看deployment详细信息,包含变更events;