创建Deployment
-
创建一个nginx deployment
[root@k8s-master home]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deployment
spec:selector:matchLabels:app: nginx # 配置pod的labelsreplicas: 2 # 声明2个副本template:metadata:labels:app: nginx # 副本的labelsspec: containers:- name: nginximage: nginxports:- containerPort: 80
[root@k8s-master home]# kubectl create -f nginx-deployment.yaml
-
查看deploy
[root@k8s-master home]# kubectl get deploy
-
显示nginx Deployment的相关信息
[root@k8s-master home]# kubectl describe deploy nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Mon, 01 Apr 2024 22:15:47 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:Labels: app=nginxContainers:nginx:Image: nginxPort: 80/TCPHost Port: 0/TCPEnvironment: <none>Mounts: <none>Volumes: <none>
Conditions:Type Status Reason---- ------ ------Available True MinimumReplicasAvailableProgressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-7c5ddbdf54 (2/2 replicas created)
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal ScalingReplicaSet 4m40s deployment-controller Scaled up replica set nginx-deployment-7c5ddbdf54 to 2
-
列出该Deployment创建的Pod
查看nginx-deployment 匹配的Pod标签[root@k8s-master home]# kubectl get deployment nginx-deployment -o wide
通过标签查看该Department创建的所有的Pod[root@k8s-master home]# kubectl get pods -l app=nginx
-
展示某个Pod的相关信息
kubectl describe pod <pod-name>
更新Deployment
- 修改nginx 副本数,将2 个增加到 4 个
直接修改yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deployment
spec:selector:matchLabels:app: nginxreplicas: 4 # 增加副本个数template:metadata:labels:app: nginxspec:containers:- name: nginximage: nginxports:- containerPort: 80
更新nginx-deployment
[root@k8s-master home]# kubectl apply -f nginx-deployment.yaml
查看Pod
[root@k8s-master home]# kubectl get pod
使用edit更新一个Deployment
[root@k8s-master home]# kubectl edit deployment nginx-deployment
将副本数修改为10个并保存
查看pod
删除Deployment
格式
kubectl delete <资源类型> <资源名称>
[root@k8s-master home]# kubectl delete deploy nginx-deployment
扩展:
-
查看所有api资源,简称以及资源类型对应的api
kubectl api-resources
[root@k8s-master home]# kubectl api-resources | grep apps
controllerrevisions apps/v1 true ControllerRevision
daemonsets ds apps/v1 true DaemonSet
deployments deploy apps/v1 true Deployment
replicasets rs apps/v1 true ReplicaSet
statefulsets sts apps/v1 true StatefulSet
[root@k8s-master home]# kubectl api-resources | grep pod
pods po v1 true Pod
podtemplates v1 true PodTemplate
horizontalpodautoscalers hpa autoscaling/v2 true HorizontalPodAutoscaler
poddisruptionbudgets pdb policy/v1 true PodDisruptionBudget