计算机系统应用教程网站

网站首页 > 技术文章 正文

Linux 理解K8S Deployment配置的详细解释输出

btikc 2024-09-27 01:09:20 技术文章 3 ℃ 0 评论

第一个是HAP我这个是HPA组合deployment

第一HPA


apiVersion: autoscaling/v2 # 指定使用的Kubernetes Autoscaling API版本为autoscaling/v2
kind: HorizontalPodAutoscaler # 表示这是一个HorizontalPodAutoscaler对象,用于自动调整Pod的副本数
metadata:
  name: sprint-lex-hap-memroy-cpu-hpa # 定义HPA的名称为sprint-lex-hap-memroy-cpu-hpa
  namespace: teamsnamespace # 指定HPA所属的命名空间为teamsnamespace
spec:
  minReplicas: 1 # HPA允许的最小副本数为1
  maxReplicas: 4 # HPA允许的最大副本数为4
  metrics:
  - type: Resource # 指定指标类型为资源指标
    resource:
      name: cpu # 资源指标的名称为CPU
      target:
        type: Utilization # 指定目标类型为利用率
        averageUtilization: 750 # 目标平均CPU利用率为750%
  - type: Resource # 指定指标类型为资源指标
    resource:
      name: memory # 资源指标的名称为内存
      target:
        type: Utilization # 指定目标类型为利用率
        averageUtilization: 50 # 目标平均内存利用率为50%
  scaleTargetRef:
    apiVersion: apps/v1 # 目标对象的API版本为apps/v1
    kind: Deployment # 目标对象的类型为Deployment
    name: sprint-lex-hap-memroy-cpu # 目标对象的名称为sprint-lex-hap-memroy-cpu

第二deployment


kind: Deployment # 表示这是一个Deployment对象,用于定义应用程序的部署
metadata:
  namespace: teamsnamespace # 指定部署所在的命名空间为teamsnamespace
  name: sprint-lex-hap-memroy-cpu # 指定部署的名称为sprint-lex-hap-memroy-cpu
  labels:
    194f867a2: enabled # 定义部署的标签,标签名为194f867a2,值为enabled
    app: nginx # 定义部署的应用标签,标签名为app,值为nginx
    type: pa # 定义部署的类型标签,标签名为type,值为pa
spec:
  replicas: 2 # 定义部署的副本数为2,即同时运行的Pod实例数量为2
  selector: # 定义标签选择器,用于选择要管理的Pod
    matchLabels:
      194f867a2: enabled # 标签选择器的条件,要求Pod的标签中必须包含194f867a2=enabled
      team: lex # 标签选择器的条件,要求Pod的标签中必须包含team=lex
  template: # 定义创建Pod的模板
    metadata:
      labels:
        194f867a2: enabled # Pod的标签,标签名为194f867a2,值为enabled
        app: nginx # Pod的标签,标签名为app,值为nginx
        type: pa # Pod的标签,标签名为type,值为pa
        team: lex # Pod的标签,标签名为team,值为lex
    spec: # 定义Pod的规范
      serviceAccountName: lex-teamsnamespace-sa # 指定Pod使用的Service Account名称为lex-teamsnamespace-sa
      automountServiceAccountToken: false # 禁用自动挂载Service Account Token
      imagePullSecrets: # 定义从私有容器镜像仓库拉取镜像所需的认证信息
      - name: sprint # 私有镜像仓库的认证信息名称为sprint
      containers: # 定义Pod中的容器
      - image: eu.gcr.io/company/lex-api-podsdata-collector:1.0.1 # 容器的镜像地址
        env: # 定义容器的环境变量
        #- name: https_proxy
        #  value: "192.168.192.51:443"
        imagePullPolicy: Always # 定义容器镜像拉取策略为始终拉取
        name: docker-container-hello-node # 容器的名称为docker-container-hello-node
        resources: # 定义容器的资源限制和请求
          requests: # 定义容器的资源请求
            memory: "900Mi" # 定义内存请求为900Mi
            cpu: "200m" # 定义CPU请求为200m
            ephemeral-storage: "2Gi" # 定义临时存

# summary

如何理解deployment的一些字段

- deployment name

```
[lex-mac]$ kubectl get deployment -n lex-namespace|grep lex
mac-lex-hap-memroy-cpu 1/1 1 1 38d
[lex-mac]$ kubectl rollout restart deployment/mac-lex-hap-memroy-cpu -n lex-namespace
deployment.apps/mac-lex-hap-memroy-cpu restarted
```

- get rs replicaSets

```
[lex-mac]$ kubectl get rs -n lex-namespace|grep lex
mac-lex-hap-memroy-cpu-5bbdd6dd55 0 0 0 12m
mac-lex-hap-memroy-cpu-6f89c6b56c 0 0 0 38d
mac-lex-hap-memroy-cpu-7d4b675585 0 0 0 27d
mac-lex-hap-memroy-cpu-7f4f64f6d6 1 1 1 12s
[lex-mac]$ kubectl get pod -n lex-namespace|grep lex
mac-lex-hap-memroy-cpu-6f89c6b56c-wqdhm 1/1 Terminating 0 7m56s
mac-lex-hap-memroy-cpu-7f4f64f6d6-znjwd 1/1 Running 0 23s
```

- get replicaSets uid

$ kubectl get replicaSets mac-lex-hap-memroy-cpu-7f4f64f6d6 -n lex-namespace -o jsonpath='{.metadata.uid}'
093d3479-8f24-4131-abbb-e0f6a3131b5a

- kubectl patch 尝试

```
[lex-mac]$ kubectl patch deployment sprint-lex-hap-memroy-cpu --patch '{"spec": {"selector": {"matchLabels": {"194f867a2": "enabled"}}}}' -n teamsnamespace
deployment.apps/sprint-lex-hap-memroy-cpu patched (no change)
[lex-mac]$ kubectl patch deployment sprint-lex-hap-memroy-cpu --patch '{"spec": {"selector": {"matchLabels": {"team": "lex"}}}}' -n teamsnamespace
deployment.apps/sprint-lex-hap-memroy-cpu patched (no change)
[lex-mac]$ kubectl patch deployment sprint-lex-hap-memroy-cpu --patch '{"spec": {"selector": {"matchLabels": {"team": "lex2"}}}}' -n teamsnamespace
The Deployment "sprint-lex-hap-memroy-cpu" is invalid:
* spec.template.metadata.labels: Invalid value: map[string]string{"194f867a2":"enabled", "app":"nginx", "team":"lex", "type":"pa"}: `selector` does not match template `labels`
* spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"194f867a2":"enabled", "team":"lex2"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable
这个报错 是 selector` does not match template `labels` 表面的命令是是针对的selector 看样子是针对的spec.template.metadata.labels ?
```

- 对应关系能找到 也是OK的 但是好像更新都不行 随意改值没什么用 ?对于有些字段来说确实如此

```
kubectl patch deployment sprint-lex-hap-memroy-cpu --patch '{"spec": {"selector": {"matchLabels": {"team": "lex"}}}}' -n teamsnamespace OK
kubectl patch deployment sprint-lex-hap-memroy-cpu --patch '{"spec": {"template": {"metadata": {"labels": {"team": "lex"}}}}}' -n teamsnamespace OK
deployment.apps/sprint-lex-hap-memroy-cpu patched (no change)
kubectl patch deployment patch-demo --patch '{"spec": {"template": {"spec": {"containers": [{"name": "patch-demo-ctr-2","image": "redis"}]}}}}'
kubectl patch deployment sprint-lex-hap-memroy-cpu --patch '{"spec": {"template": {"metadata": {"team": "lex2"}}}}' -n teamsnamespace
kubectl patch deployment sprint-lex-hap-memroy-cpu --patch '{"spec": {"template": {"matchLabels": {"team": "lex2"}}}}' -n teamsnamespace
kubectl get hpa sprint-lex-hap-memroy-cpu-hpa -n teamsnamespace
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
sprint-lex-hap-memroy-cpu-hpa Deployment/sprint-lex-hap-memroy-cpu 5%/50%, 1%/750% 1 4 1 39d
```
```

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表