首页 > 其他分享 >k8s——deployment

k8s——deployment

时间:2024-05-21 11:29:51浏览次数:26  
标签:kubectl deploy nginx master deployment k8s root

创建deployment

[root@master deploy]# kubectl create deploy nginx-deploy --image=nginx:1.7.9
deployment.apps/nginx-deploy created
// kubectl create 资源类型   资源名称(deployment的名称)  指定镜像
// 都是在动态的更新pod副本的数量   deployment和replicaSet是嵌套关系,


[root@master deploy]# kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   1/1     1            1           66s
[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   1         1         1       75s
[root@master deploy]# kubectl get replicaset
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   1         1         1       4m5s
[root@master deploy]# kubectl get po
NAME                            READY   STATUS    RESTARTS      AGE
my-pod                          1/1     Running   1 (19m ago)   4d19h
nginx-demo                      1/1     Running   2 (19m ago)   4d23h
nginx-deploy-68c658776b-z9rc6   1/1     Running   0             4m48s
nginx-po                        1/1     Running   2 (19m ago)   4d23h
[root@master deploy]# 

//先是有deployment,然后有replicaSet,然后有pod

//vim :set paste 粘贴模式


[root@master deploy]# vim nginx-deploy.yaml 

piVersion: apps/v1  #deployment api 版本
kind: Deployment # 资源类型为deployment
metadata:      # 元数据
  labels:      # 标签
    app: nginx-deploy   # 具体的 k:y配置形式
  name: nginx-deploy     # deployment的名字
  namespace: default    # 所在的命名空间
spec:
  replicas: 1     # 期望的副本数
  revisionHistoryLimit: 10     # 进行滚动更新后,保留的历史版本数
  selector:    # 选择器,用于找到匹配的rs
    matchLabels:  # 按照标签匹配
      app: nginx-deploy     # 匹配的标签k:y
  strategy:      # 更新策略
    rollingUpdate:      #  滚动更新配置
      maxSurge: 25%       # 进行滚动更新时候,更新的个数最多可以超过预期副本数的个数/比
例
      maxUnavailable: 25%  #进行滚动更新时,最大不可用更新比例,表述在所有副本数中,最>多可以有多少个不更新成功
    type: RollingUpdate   # 更新类型,采用滚动更新
  template:    # pod模版
    metadata:  # pod原信息
      labels:  # pod标签
        app: nginx-deploy    
    spec:      # pod的期望信息
      containers:   # pod 的容器
      - image: nginx:1.7.9   # 镜像
        imagePullPolicy: IfNotPresent   # 拉取策略
        name: nginx   # 容器名称
      restartPolicy: Always  # 重启策略
      terminationGracePeriodSeconds: 30    #删除操作最多宽限多久

滚动更新

[root@master deploy]# kubectl edit deploy nginx-deploy
deployment.apps/nginx-deploy edited


[root@master deploy]# kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   3/3     3            3           28m
[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   3         3         3       29m
[root@master deploy]# kubectl get po
NAME                            READY   STATUS    RESTARTS      AGE
my-pod                          1/1     Running   1 (44m ago)   4d19h
nginx-demo                      1/1     Running   2 (44m ago)   4d23h
nginx-deploy-68c658776b-hgppq   1/1     Running   0             23s
nginx-deploy-68c658776b-vld7l   1/1     Running   0             23s
nginx-deploy-68c658776b-z9rc6   1/1     Running   0             29m
nginx-po                        1/1     Running   2 (44m ago)   4d23h
[root@master deploy]# 

//开两个终端
[root@master deploy]# kubectl set image deployment/nginx-deploy nginx=nginx:1.7.9
deployment.apps/nginx-deploy image updated
[root@master deploy]# 

[root@master deploy]# kubectl rollout status deplloy nginx-deploy
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deploy" successfully rolled out
[root@master deploy]# 
# 滚动更新过程
1.创建一个空的rs2
2.在空的rs2中创建pod1,在rs1中关闭原pod1
3. 在rs2中创建pod2,在rs1中关闭原pod2
//整个过程中保持2个pod的存在

# 查看rs可以看到2个rs
[root@master deploy]# kubectl get rs --show-labels
NAME                      DESIRED   CURRENT   READY   AGE     LABELS
nginx-deploy-68c658776b   3         3         3       40m     app=nginx-deploy,pod-template-hash=68c658776b
nginx-deploy-7c97fc8644   0         0         0       9m21s   app=nginx-deploy,pod-template-hash=7c97fc8644
[root@master deploy]# 

回滚

// 当你的更新存在问题,可以回滚
//可以使用如下命令查看更新的历史版本
[root@master deploy]# kubectl rollout history deployment/nginx-deploy
deployment.apps/nginx-deploy 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

// 查看历史版本
[root@master deploy]# kubectl rollout history deployment/nginx-deploy --revision=2
deployment.apps/nginx-deploy with revision #2
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=7c97fc8644
  Containers:
   nginx:
    Image:	nginx:1.9.1
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

[root@master deploy]# kubectl rollout history deployment/nginx-deploy --revision=3
deployment.apps/nginx-deploy with revision #3
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=68c658776b
  Containers:
   nginx:
    Image:	nginx:1.7.9
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

// 会记录修改的值
// 回退操作
[root@master deploy]# kubectl rollout undo deployment/nginx-deploy --to-revision=2
deployment.apps/nginx-deploy rolled back
[root@master deploy]# kubectl rollout status deployment/nginx-deploy
deployment "nginx-deploy" successfully rolled out
[root@master deploy]# 

// 回退操作 在.spec.revisionHistoryLimit 中指定deploment保留多少revision,如果设置0,则不允许回滚

扩容和缩容

扩容

[root@master deploy]# kubectl scale --replicas=6 deploy nginx-deploy
deployment.apps/nginx-deploy scaled
[root@master deploy]# kubectl get po
NAME                            READY   STATUS    RESTARTS      AGE
my-pod                          1/1     Running   1 (81m ago)   4d20h
nginx-demo                      1/1     Running   2 (81m ago)   5d
nginx-deploy-7c97fc8644-62dpp   1/1     Running   0             13m
nginx-deploy-7c97fc8644-g48zt   1/1     Running   0             10s
nginx-deploy-7c97fc8644-jlhfn   1/1     Running   0             13m
nginx-deploy-7c97fc8644-lqpxj   1/1     Running   0             13m
nginx-deploy-7c97fc8644-v69f4   1/1     Running   0             10s
nginx-deploy-7c97fc8644-zslj4   1/1     Running   0             10s
nginx-po                        1/1     Running   2 (81m ago)   5d
[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   0         0         0       66m
nginx-deploy-7c97fc8644   6         6         6       36m
[root@master deploy]# kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   6/6     6            6           67m
[root@master deploy]# 

缩容

[root@master deploy]# kubectl scale --replicas=3 deploy nginx-deploy
deployment.apps/nginx-deploy scaled
[root@master deploy]# kubectl get po
NAME                            READY   STATUS    RESTARTS      AGE
my-pod                          1/1     Running   1 (83m ago)   4d20h
nginx-demo                      1/1     Running   2 (83m ago)   5d
nginx-deploy-7c97fc8644-62dpp   1/1     Running   0             14m
nginx-deploy-7c97fc8644-jlhfn   1/1     Running   0             14m
nginx-deploy-7c97fc8644-lqpxj   1/1     Running   0             14m
nginx-po                        1/1     Running   2 (83m ago)   5d
[root@master deploy]# kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   3/3     3            3           68m
[root@master deploy]# 

暂停与恢复

暂停

// 由于每次对pod template的修改都会触发更新,所以可以通过暂停更新来避免频繁更新。我们可以先暂停更新,然后修改pod template,最后恢复更新。


[root@master deploy]# kubectl rollout pause deployment nginx-deploy 
deployment.apps/nginx-deploy paused
[root@master deploy]# kubectl edit deploy nginx-deployment
Error from server (NotFound): deployments.apps "nginx-deployment" not found
[root@master deploy]# kubectl edit deploy nginx-deploy
deployment.apps/nginx-deploy edited

[root@master deploy]# kubectl rollout  history deployment  nginx-deploy 
deployment.apps/nginx-deploy 
REVISION  CHANGE-CAUSE
3         <none>
4         <none>

[root@master deploy]# kubectl rollout  history deployment  nginx-deploy  --revision=4
deployment.apps/nginx-deploy with revision #4
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=7c97fc8644
  Containers:
   nginx:
    Image:	nginx:1.9.1
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   0         0         0       80m
nginx-deploy-7c97fc8644   3         3         3       49m
[root@master deploy]# 

恢复

[root@master deploy]# 
[root@master deploy]# kubectl rollout resume deployment nginx-deploy 
deployment.apps/nginx-deploy resumed
[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   0         0         0       82m
nginx-deploy-6cbbdbbf8b   3         3         3       10s
nginx-deploy-7c97fc8644   0         0         0       51m
[root@master deploy]# kubectl rollout  history 
daemonset    deployment   statefulset  
[root@master deploy]# kubectl rollout  history deployment  nginx-deploy 
deployment.apps/nginx-deploy 
REVISION  CHANGE-CAUSE
3         <none>
4         <none>
5         <none>

[root@master deploy]# kubectl rollout  history deployment  nginx-deploy  --revision=5
deployment.apps/nginx-deploy with revision #5
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=6cbbdbbf8b
  Containers:
   nginx:
    Image:	nginx:1.9.1
    Port:	<none>
    Host Port:	<none>
    Limits:
      cpu:	500m
      memory:	512Mi
    Requests:
      cpu:	100m
      memory:	128Mi
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

[root@master deploy]# 

标签:kubectl,deploy,nginx,master,deployment,k8s,root
From: https://www.cnblogs.com/humlogs/p/18203358

相关文章

  • K8S多master节点更换证书
    以下命令master节点均需要执行1.备份cp-a/etc/kubernetes{,.bak}cp-a/var/lib/kubelet{,.bak}cp-a/var/lib/etcd/var/lib/etcd.bak2.生成kubeadm-configkubectl-nkube-systemgetcmkubeadm-config-oyaml>kubeadm-config-20240521.yaml3.刷新证书到期时间再......
  • k8s安全权限配置
     https://blog.csdn.net/qq_51545656/article/details/136627284一、安全机制Kubernetes的安全机制非常关键,因为它确保了集群的稳定性和数据的安全。三个主要安全机制是Kubernetes安全架构的核心组成部分:认证(Authentication):这是安全机制的第一道防线。它负责确认请求者的......
  • k8s常用命令
    aliask=kubectl操作kubeconfig#查看contextkconfigget-contexts#切换contextkconfiguse-contextcontextName#使用context访问k--context=contextNamegetpod操作k8s资源#设置节点标签klabelnodenodeNamekey=value--overwrite#设置节点污点ktaintn......
  • rancher添加k8s节点时显示节点已添加
    由于几台服务器都用相同的外部externalip,而添加k8s节点时有外部ip先使用外部ip,所以会显示节点已添加,无法添加成功,此时需要用--address参数来指定内部ip,这样节点就由内部ip来添加如下添加k8s节点的命令:sudodockerrun-d--privileged--restart=unless-stopped--net=ho......
  • k3s和k8s对比,应该选择哪个
    k3s和k8s都是Kubernetes的发行版,但在一些关键特性上存在差异。以下是两者的对比及选择建议:1.特性与功能:k8s(Kubernetes)是一个功能完善的容器编排系统,具有自动装箱、自我修复、水平扩展、服务发现和负载均衡等特性。它支持复杂的部署、扩展和管理操作,适用于大规模生产环境。k3s......
  • K8S下应用异常卡顿问题的分析与学习
    K8S下应用异常卡顿问题的分析与学习背景周二自己在处理申威服务器的问题时,被同事拉进一个群聊.告知客户现场有一个特殊情况:服务晚上重启,上午速度还可以,但是到了下午就开始变的非常卡顿.因为当时正在车上也看不到具体信息.晚上九点上会进行了一次简单查看.发现......
  • 放弃k8s,拥抱ECI;水很深,一切都是为了钱,没有钱的动力,云服务商会发展k8s生态???
    学习k8s前鄙视eci,学习完后决定放弃k8s,拥抱ECI;从架构图可以看到,以后ECI不会是方向,只是一些压测,弹性伸缩的业务;2.eci不能融入k8s生态,导致企业不愿意转,这也是eci一直想融入k8s生态,向k8s生态靠拢的原因,就是说是eci什么k8sserverless版本退出,底层基于eci,没有k8s生态eci就是一......
  • k8s安装网络组件calico报错Init:ImagePullBackOff
    问题:安装网络组件calico报错Init:ImagePullBackOff[root@k8s-master15:33:08~/kubernets]#kubectlgetpods-nkube-systemNAMEREADYSTATUSRESTARTSAGEcalico-kube-controllers......
  • 记一次k8s etcd报错Unhealthy
    使用命令某个节点ectd不健康#kubectlgetcsNAMESTATUSMESSAGEERRORcontroller-managerHealthy......
  • kubernetes DeploymentController 源码解析
    DeploymentController对象从 NewDeploymentController方法开始创建,我们首先看这个方法1//NewDeploymentControllercreatesanewDeploymentController.2funcNewDeploymentController(ctxcontext.Context,dInformerappsinformers.DeploymentInformer,rsInformer......