阿里k8s自动伸缩

yaml#

apiVersion: autoscaling.alibabacloud.com/v1beta1 kind: AdvancedHorizontalPodAutoscaler metadata: name: demoname-name-requestor-aliyun-ahpa namespace: requestor spec: scaleStrategy: auto metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 95 scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: demoname-name-requestor-aliyun-deployment maxReplicas: 128 minReplicas: 32 stabilizationWindowSeconds: 1800 prediction: quantile: 95 scaleUpForward: 60 instanceBounds: - startTime: "2021-12-16 00:00:00" endTime: "2031-12-16 00:00:00" bounds: - cron: "* 0-8 * * *" maxReplicas: 128 minReplicas: 32 - cron: "* 9-15 * * *" maxReplicas: 128 minReplicas: 32 - cron: "* 16-23 * * *" maxReplicas: 128 minReplicas: 32

说明#

这是一份阿里云 ACK(Alibaba Cloud Container Service for Kubernetes)中使用的 AdvancedHorizontalPodAutoscaler(AHPA) 的 YAML 配置文件,用于自动伸缩 Pod 副本数。下面是该配置的逐项解释:

elk在k8s上的部署使用示例

示例Yaml#

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-config
  namespace: ops
  labels:
    k8s-app: filebeat
data:
  filebeat.yml: |-
    filebeat.config:
      inputs:
        # Mounted `filebeat-inputs` configmap:
        path: ${path.config}/inputs.d/*.yml
        # Reload inputs configs as they change:
        reload.enabled: false
      modules:
        path: ${path.config}/modules.d/*.yml
        # Reload module configs as they change:
        reload.enabled: false

    output.elasticsearch:
      hosts: ['49.65.125.91:9200']
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-inputs
  namespace: ops
  labels:
    k8s-app: filebeat
data:
  kubernetes.yml: |-
    - type: docker
      containers.ids:
      - "*"
      processors:
        - add_kubernetes_metadata:
            in_cluster: true
---
apiVersion: apps/v1 
kind: DaemonSet
metadata:
  name: filebeat
  namespace: ops
  labels:
    k8s-app: filebeat
spec:
  selector:
    matchLabels:
      k8s-app: filebeat
  template:
    metadata:
      labels:
        k8s-app: filebeat
    spec:
      serviceAccountName: filebeat
      terminationGracePeriodSeconds: 30
      containers:
      - name: filebeat
        image: elastic/filebeat:7.9.2
        args: [
          "-c", "/etc/filebeat.yml",
          "-e",
        ]
        securityContext:
          runAsUser: 0
          # If using Red Hat OpenShift uncomment this:
          #privileged: true
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - name: config
          mountPath: /etc/filebeat.yml
          readOnly: true
          subPath: filebeat.yml
        - name: inputs
          mountPath: /usr/share/filebeat/inputs.d
          readOnly: true
        - name: data
          mountPath: /usr/share/filebeat/data
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
      volumes:
      - name: config
        configMap:
          defaultMode: 0600
          name: filebeat-config
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers
      - name: inputs
        configMap:
          defaultMode: 0600
          name: filebeat-inputs
      # data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart
      - name: data
        hostPath:
          path: /var/lib/filebeat-data
          type: DirectoryOrCreate
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: filebeat
subjects:
- kind: ServiceAccount
  name: filebeat
  namespace: ops
roleRef:
  kind: ClusterRole
  name: filebeat
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: filebeat
  labels:
    k8s-app: filebeat
rules:
- apiGroups: [""] # "" indicates the core API group
  resources:
  - namespaces
  - pods
  verbs:
  - get
  - watch
  - list
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: filebeat
  namespace: ops
  labels:
    k8s-app: filebeat

将以上内容存储到 elk.yaml

k8s 配套说明

ctr#

如果定义了 -n 应该是namesplace 那么每个命令都得跟上

load镜像#

sudo ctr -n=k8s.io images import ${imageTarFile}

保存文件#

sudo ctr -n k8s.io images export –platform=linux/amd64 hi.tar 101.32.1.4:30002/web-docker/worker:2121

切换tag#

sudo ctr -n k8s.io images tag –force repos.x.com/web-docker/worker:2121 101.32.1.4:30002/web-docker/worker:2121

推送镜像#

sudo ctr -n k8s.io images push -k -u admin:fat213asfdFS2W –plain-http 101.32.1.4:30002/web-docker/worker:2121

拉取#

sudo ctr -n k8s.io images pull -k -u admin:fat1239ASD124W –plain-http 101.32.1.4:30002/web-docker/worker:2121

Docker#

[[Note of Docker]] 好像并没有整理docker命令… 后续补吧

加载镜像#

docker load -i .\hi.tar

保存为本地文件#

docker save  repos.x.com/web-docker/worker:2121 -o hi.tar

k8s技术分享

k8s技术分享#

工作负载资源#

一、pod#

1. 概要#

  1. 可创建管理、最小的可部署计算单元,是可以在主机上运行的容器的集合
  2. 我们的服务都在其中运行。如我们的服务是nginx,则最内层是我们的服务 nginx,运行在 container 容器当中。container (容器) 的本质是进程,而 pod 是管理这一组进程的资源
  3. 所以pod可视为一个极为轻量化、没插网线的电脑,如果所需任务无需交互,那么用pod就很合适。例如给它挂载一个文件来训练模型、生成报表,可以根据场景使用 Job 或者 CronJob 或者其它

图示关系如下#

![[k8s-pod-insert.png]]

2. pod网络#

  1. 当然,pod 可以管理多个 container,又因为container (容器) 的本质是进程,如果有本地网络通信需求(使用 localhost 或者 Socket 文件进行本地通信),在这些场景中使用 pod 管理多个 container 就非常的推荐。

  2. 如下图展示了Pod网络所依赖的3个网络设备

    1. eth0是节点主机上的网卡,支持该节点流量出入的设备、也是支持集群节点间IP寻址和互通的设备;

    2. docker0是一个虚拟网桥,可以简单理解为一个虚拟交换机,支持该节点上的Pod之间进行IP寻址和互通的设备;

    3. veth0则是Pod1的虚拟网卡,支持该Pod内容器互通和对外访问的虚拟设备;

    4. docker0网桥和veth0网卡,都是linux支持和创建的虚拟网络设备;

    5. pause属于特殊容器,其运行的唯一目的是为Pod建立共享的veth0网络接口

![[k8s-nginx-pod.png]]

二、deploymentStatefulSet#

1. 概要#

  1. Deployment 使得 Pod 和 ReplicaSet 能够进行声明式更新

  2. StatefulSet 表示一组具有一致身份的 Pod:

    1. 身份定义为:

        - 网络:一个稳定的 DNS 和主机名。

        - 存储:根据要求提供尽可能多的 VolumeClaim。

        StatefulSet 保证给定的网络身份将始终映射到相同的存储身份。

        虽然pod完全具备在生产环境中部署独立的单体服务的能力,但在生产环境中,我们基本上不会直接管理 pod,我们会使用deployment 代为控管。延续上面的比喻就是将 deployment 视为一个好用的机房管理员:帮助我们进货,对pod进行开关机,对pod做系统升级和重装系统以实现功能升级和回滚。 StatefulSet 也相同,只不过每次它会保证一致性,而不像deployment 每次重启都是随机分配