k8s练习-创建一个Deployment

创建Deployment

  1. 创建一个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的labels
replicas: 2 # 声明2个副本
template:
metadata:
labels:
app: nginx # 副本的labels
spec:
containers:

  • name: nginx
    image: nginx
    ports:
    • containerPort: 80
复制代码
    [root@k8s-master home]# kubectl create -f nginx-deployment.yaml 

![在这里插入图片描述](https://file.jishuzhan.net/article/1774998191939260417/24151eff40bacedb9d3d09118c304ef5.webp)

2. 查看deploy

   \[root@k8s-master home\]# kubectl get deploy  
   ![在这里插入图片描述](https://file.jishuzhan.net/article/1774998191939260417/450da6356c5a15b6f1dabdb68e093f1c.webp)
3. 显示nginx Deployment的相关信息

   \[root@k8s-master home\]# kubectl describe deploy nginx-deployment

```powershell
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=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    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
  1. 列出该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
  2. 展示某个Pod的相关信息

    复制代码
     kubectl describe pod <pod-name>

更新Deployment

  1. 修改nginx 副本数,将2 个增加到 4 个
    直接修改yaml文件
powershell 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
spec:
 selector:
  matchLabels:
   app: nginx
 replicas: 4		# 增加副本个数
 template:
  metadata:
   labels:
    app: nginx
  spec:
   containers:
   - name: nginx
     image: nginx
     ports:
     - 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

扩展:

  1. 查看所有api资源,简称以及资源类型对应的api

    复制代码
     kubectl api-resources
powershell 复制代码
[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
powershell 复制代码
[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
相关推荐
努力进修19 小时前
跨设备文件共享零烦恼!PicoShare+cpolar让跨设备传输更简单
云原生·eureka·cpolar
tianyuanwo20 小时前
多平台容器化RPM构建流水线全指南:Fedora、CentOS与Anolis OS
linux·运维·容器·centos·rpm
敲上瘾1 天前
【探索实战】:Kurator分布式统一应用分发平台的全面解析与实践指南
分布式·容器·kubernetes·serverless
cui_win1 天前
Docker Compose 部署一个完整的Prometheus监控告警系统
docker·容器·prometheus
roman_日积跬步-终至千里1 天前
【Docker】Docker Stop 后到底发生了什么?——从信号机制到优雅停机
运维·docker·容器
喵手1 天前
云端智变:基于 DevUI 与 MateChat 打造下一代云原生智能运维中台实战教学!
运维·云原生·devui·matechat
z***6261 天前
Docker:基于自制openjdk8镜像 or 官方openjdk8镜像,制作tomcat镜像
docker·容器·tomcat
Connie14511 天前
记一次K8s故障告警排查(Grafna告警排查)
云原生·容器·kubernetes·grafana
❀͜͡傀儡师2 天前
Docker部署视频下载器
docker·容器·音视频
热爱学习的小怪兽2 天前
docker的一些常用指令
运维·docker·容器