3-1、Pod控制器-deployment

Pod控制器:
ReplicationController(简称RC,已经淘汰)
ReplicaSet
Deployment
DaemonSet
Job
Cronjob
StatefulSet

k8s名词解释:https://www.kubernetes.org.cn/deployment

自主式POD:delete pod后不会被重新拉起的,不是由pod控制器控制和管理的pod
deployment: 只能管控无状态应用

1. Deployment概述

Deployment对象,顾名思义,是用于部署应用的对象。它使Kubernetes中最常用的一个对象,它为ReplicaSet和Pod的创建提供了一种声明式的定义方法,从而无需像前两篇文章中那样手动创建ReplicaSet和Pod对象(使用Deployment而不直接创建ReplicaSet是因为Deployment对象拥有许多ReplicaSet没有的特性,例如滚动升级和回滚)

deployment

vi deployment-demo.yml
apiVersion: apps/v1
kind: Deployment
metadata:
    name: myapp-deploy
    namespace: default
spec:
    replicas: 2
    selector:
        matchLabels:
            app: myapp
            release: canary
    template:
        metadata:
           labels:
            app: myapp
            release: canary
        spec:
            containers:
            - name: myapp
              image: ikubernetes/myapp:v1
              ports:
              - name: http
                containerPort: 80

# kubectl apply -f deployment-demo.yml 
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
myapp-deploy   2/2     2            2           2m4s

# kubectl get pods
NAME                            READY   STATUS             RESTARTS   AGE
myapp-deploy-67b6dfcd8-wbflb    1/1     Running            0          2m27s
myapp-deploy-67b6dfcd8-wdkxv    1/1     Running            0          2m27s

# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
myapp-deploy-67b6dfcd8    2         2         2       3m49s

若修改了deployment-demo.yml,直接再次apply就生效:
# kubectl apply -f deployment.yaml

修改pod的数量(扩容/缩容),patch打补丁的方法:
# kubectl patch deployment myqpp-deploy -p '{"spec":{"replicas":3}}'

版本升级:由vi升级到v2,直接修改yml文件或打补丁的方法
# kubectl set image deployment myapp-deploy  myapp=ikubernetes/myapp:v2
# kubectl rollout pause deploument myapp-deploy #暂停更新
# kubectl rollout resume  deploument myapp-deploy #继续更新


回滚:
查看回滚的历史记录:
# kubectl rollout history deployment myapp-deploy
deployment.apps/myapp-deploy 

REVISION  CHANGE-CAUSE

1         <none>

2         <none>
3         <none>
# kubectl rollout undo deployment myapp-deploy  #默认是回滚到上一版本
# kubectl rollout undo deployment myapp-deploy --to-revision=3  #回滚到指定版本


定义多个资源:

apiVersion: apps/v1
kind: Deployment
metadata:
    name: redis
    namespace: default
spec:
    replicas: 1
    selector:
        matchLabels:
            app: redis
            role: logstor
    template:
        metadata:
           labels:
            app: redis
            release: logstor
        spec:
            containers:
            - name: redis
              image: redis:4.0-alpine
              ports:
              - name: redis
                containerPort: 6379


################
# kubectl describe deployment myapp-deploy
Name:                   myapp-deploy
Namespace:              default
CreationTimestamp:      Sat, 06 Apr 2019 20:23:40 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
                        kubectl.kubernetes.io/last-applied-configuration:
                          {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"myapp-deploy","namespace":"default"},"spec":{"replicas":2...
Selector:               app=myapp,release=canary
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=myapp
           release=canary
  Containers:
   myapp:
    Image:        ikubernetes/myapp:v1
    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:   myapp-deploy-67b6dfcd8 (2/2 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  6m57s  deployment-controller  Scaled up replica set myapp-deploy-67b6dfcd8 to 2

k8s探测:
  liveness 存活性探测
  readiness 服务就绪与否的探测
  lifecycle 

# liveness 存活性探测
# exec探针
vi  pod-liveness-exec.yml
apiVersion: v1

kind: Pod

metadata:

  name: liveness-exec-pod

  namespace: default

spec:

  containers:

  - name: liveness-exec-contaienr

    image: busybox:latest

    imagePullPolicy: IfNotPresent

    command: ["/bin/sh","-c","touch /tmp/healthy","sleep 30","rm -f /tmp/healthy","sleep 3600"]

    livenessProbe:

      exec:

        command: ["test","e","/tmp/healthy"]

      initialDelaySeconds: 1

      periodSeconds: 3
# kubectl create -f  pod-liveness-exec.yml

# httpGet探针
vi  pod-liveness-httpGet.yml
apiVersion: v1

kind: Pod

metadata:

  name: liveness-httpget-pod

  namespace: default

spec:

  containers:

  - name: liveness-httpget-container

    image: ikubernetes/myapp:v1

    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80



    livenessProbe:

      httpGet:

        port: http
        path: /index.html

      initialDelaySeconds: 1

      periodSeconds: 3
# kubectl create -f  pod-liveness-httpget.yml


readiness 服务就绪与否的探测
vi  pod-readiness.yml
apiVersion: v1

kind: Pod

metadata:

  name: readiness-httpget-pod

  namespace: default

spec:

  containers:

  - name: readiness-httpget-container

    image: ikubernetes/myapp:v1

    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80



    readinessProbe:

      httpGet:

        port: http
        path: /index.html

      initialDelaySeconds: 1

      periodSeconds: 3
# kubectl create -f pod-readiness.yml

 

posted @ 2022-11-14 23:41  Sky-wings  阅读(40)  评论(0编辑  收藏  举报