k8s restful api 访问

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
简介: restful api访问k8s集群,增删改查信息,做界面二次开发。需要预先创建访问权限的配置。 官网api文档https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.

restful api访问k8s集群,增删改 查信息,做界面二次开发。
需要预先创建访问权限的配置。

官网api文档
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/

下面罗列部分api

curl -u admin:admin "https://localhost:6443/api/v1" -k
curl -u admin:admin "https://localhost:6443/api/v1/pods" -k

curl -u admin:admin "https://localhost:6443/api/v1/namespaces/{namespace}/pods" -k
curl -u admin:admin "https://localhost:6443/api/v1/namespaces/default/pods" -k

获取节点信息

curl -u admin:admin "https://localhost:6443/api/v1/nodes/{nodename}" -k
curl -u admin:admin "https://localhost:6443/api/v1/nodes/tensorflow1" -k
 
...
  "status": {
    "capacity": {
      "cpu": "4",
      "memory": "7970316Ki",
      "pods": "110"
    },
    "allocatable": {
      "cpu": "4",
      "memory": "7867916Ki",
      "pods": "110"
    },
...

获取namespace信息

curl -u admin:admin "https://localhost:6443/api/v1/namespaces/{namespace}" -k
curl -u admin:admin "https://localhost:6443/api/v1/namespaces/default" -k

获得quota信息

curl -u admin:admin "https://localhost:6443/api/v1/namespaces/{namespace}/resourcequotas/" -k
curl -u admin:admin "https://localhost:6443/api/v1/namespaces/default/resourcequotas/" -k

实践

k8s_master_ip:192.168.1.138
username 不同用户不同
password 不同用户不同
namespace 不同用户不同

api地址
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/
版本更新到v1.10以后 上面这个链接就找不到了 要把v1.9改成v1.10才能访问。

查看容器

curl -u {username}:{password} "https://{k8s_master_ip}:6443/api/v1/namespaces/{namespace}/pods/" -k
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/default/pods/" -k

看起来像是把所有的pod都拿出来了,包括活的和死的。
看了一下信息很多不过没有资源使用信息。

"phase": "Running"

这个是正在运行的pod

"phase": "Failed"
"reason":"Evicted"

这种是删除了的,状态是failed 原因是被驱逐

增加continue参数取出正在运行的容器

curl -u {username}:{password} "https://{k8s_master_ip}:6443/api/v1/namespaces/{namespace}/pods?continue" -k
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/default/pods?continue" -k
查看replicationcontroller
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/user1/replicationcontrollers/" -k

查看service

curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/user1/services/" -k

查看资源总览resourcequotas

curl -u {username}:{password} "https://{k8s_master_ip}:6443/api/v1/namespaces/{namespace}/resourcequotas/" -k
[root@tensorflow1 info]# curl -u admin:admin "https://localhost:6443/api/v1/namespaces/default/resourcequotas/" -k
 
...
"status": {
  "hard": {
    "limits.cpu": "2",
    "limits.memory": "6Gi",
    "pods": "20",
    "requests.cpu": "1",
    "requests.memory": "1Gi"
  },
  "used": {
    "limits.cpu": "400m",
    "limits.memory": "1Gi",
    "pods": "2",
    "requests.cpu": "200m",
    "requests.memory": "512Mi"
  }
}
...

hard是限额   used是当前申请的限额
limits 和 requests 的区别是 limits是上限,不能突破,但不保证能给。 requests是下限,保证能给。 举例说明:一个容器 requests.memory 512Mi,limits.memory 1Gi。宿主机内存使用量高时,一定会留512Mi内存给这个容器,不一定能拿到1Gi内存。宿主机内存使用量低时,容器不能突破1Gi内存。
Gi 和 G 的区别是 Gi是1024进制,G是1000进制,M Mi也是同理。就像一个U盘8G但实际能使用的是7.45G(其实这里单位就是Gi)
pods是指容器,单位个
cpu单位 m指千分之一,200m即0.2个cpu。这是绝对值,不是相对值。比如0.1CPU不管是在单核或者多核机器上都是一样的,都严格等于0.1CPU core

实时数据 

官方文档
https://kubernetes.io/docs/tasks/debug-application-cluster/core-metrics-pipeline/ 
https://github.com/kubernetes/metrics 
https://github.com/kubernetes-incubator/metrics-server

下载 metrics-server 压缩包文件
下载 googlecontainer/metrics-server-amd64:v0.2.0 
cd metrics-server-0.2.1/deploy
修改 metrics-server-deployment.yaml 文件 image 和 imagePullPolicy: IfNotPresent
kubectl create -f .

获取节点信息

curl -u {username}:{password} "https://{k8s_master_ip}:6443/apis/metrics.k8s.io/v1beta1/nodes" -k
curl -u admin:admin "https://192.168.1.138:6443/apis/metrics.k8s.io/v1beta1/nodes" -k
 
{
  "kind": "NodeMetricsList",
  "apiVersion": "metrics.k8s.io/v1beta1",
  "metadata": {
    "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes"
  },
  "items": [
...
    {
      "metadata": {
        "name": "tensorflow1",
        "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/tensorflow1",
        "creationTimestamp": "2018-04-09T08:44:17Z"
      },
      "timestamp": "2018-04-09T08:44:00Z",
      "window": "1m0s",
      "usage": {
        "cpu": "265m",
        "memory": "3448228Ki"
      }
    }
...
  ]
}

获取pod信息

curl -u {username}:{password} "https://{k8s_master_ip}:6443/apis/metrics.k8s.io/v1beta1/namespaces/{namespace}/pods" -k
curl -u admin:admin "https://192.168.1.138:6443/apis/metrics.k8s.io/v1beta1/namespaces/default/pods" -k
 
{
  "kind": "PodMetricsList",
  "apiVersion": "metrics.k8s.io/v1beta1",
  "metadata": {
    "selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods"
  },
  "items": [
...
    {
      "metadata": {
        "name": "tensorflow-worker-rc-998wf",
        "namespace": "default",
        "selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/tensorflow-worker-rc-998wf",
        "creationTimestamp": "2018-04-09T08:52:38Z"
      },
      "timestamp": "2018-04-09T08:52:00Z",
      "window": "1m0s",
      "containers": [
        {
          "name": "worker",
          "usage": {
            "cpu": "0",
            "memory": "39964Ki"
          }
        }
      ]
    }
...
  ]
}

获取namespace信息
没找到url,就把上面获取pod的使用量全加起来就是namespace的使用量了

Metrics API 文档
网上找不到文档 只能从 kubectl top 命令帮助里找

[root@tensorflow1 ~]# kubectl top
Display Resource (CPU/Memory/Storage) usage.
 
The top command allows you to see the resource consumption for nodes or pods.
 
This command requires Heapster to be correctly configured and working on the server.
 
Available Commands:
  node        Display Resource (CPU/Memory/Storage) usage of nodes
  pod         Display Resource (CPU/Memory/Storage) usage of pods
 
Usage:
  kubectl top [options]
 
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

[root@tensorflow1 ~]# kubectl top pod --help
Display Resource (CPU/Memory/Storage) usage of pods.
 
The 'top pod' command allows you to see the resource consumption of pods.
 
Due to the metrics pipeline delay, they may be unavailable for a few minutes since pod creation.
 
Aliases:
pod, pods, po
 
Examples:
  # Show metrics for all pods in the default namespace
  kubectl top pod
  
  # Show metrics for all pods in the given namespace
  kubectl top pod --namespace=NAMESPACE
  
  # Show metrics for a given pod and its containers
  kubectl top pod POD_NAME --containers
  
  # Show metrics for the pods defined by label name=myLabel
  kubectl top pod -l name=myLabel
 
Options:
      --all-namespaces=false: If present, list the requested object(s) across all namespaces. Namespace in current
context is ignored even if specified with --namespace.
      --containers=false: If present, print usage of containers within a pod.
      --heapster-namespace='kube-system': Namespace Heapster service is located in
      --heapster-port='': Port name in service to use
      --heapster-scheme='http': Scheme (http or https) to connect to Heapster as
      --heapster-service='heapster': Name of Heapster service
  -l, --selector='': Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
 
Usage:
  kubectl top pod [NAME | -l label] [options]
 
Use "kubectl options" for a list of global command-line options (applies to all commands).

弃用的数据获取
参考 https://jimmysong.io/posts/using-heapster-to-get-object-metrics/
官方api文档 https://github.com/kubernetes/heapster/blob/master/docs/model.md 弃用了 
弃用的api取值 https://blog.csdn.net/mofiu/article/details/77126848

获取heapster url
[root@tensorflow1 influxdb]kubectl cluster-info
Kubernetes master is running at https://192.168.1.138:6443
Heapster is running at https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
monitoring-grafana is running at https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
monitoring-influxdb is running at https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy
 
 
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/heapster/proxy/api/v1/model/namespaces/" -k
 
 
curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/heapster/proxy/api/v1/model/namespaces/default/metrics" -k
[
  "memory/request",
  "memory/limit",
  "cpu/usage_rate",
  "memory/usage",
  "cpu/request",
  "cpu/limit"
]
[root@tensorflow1 influxdb]# curl -u admin:admin "https://192.168.1.138:6443/api/v1/namespaces/kube-system/services/heapster/proxy/api/v1/model/namespaces/default/metrics/memory/usage" -k
{
  "metrics": [
  ...
   {
    "timestamp": "2018-04-09T07:45:00Z",
    "value": 81121280
   },
   {
    "timestamp": "2018-04-09T07:46:00Z",
    "value": 81121280
   }
  ...
  ],
  "latestTimestamp": "2018-04-09T07:46:00Z"
}

本文转自CSDN-k8s restful api 访问

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
7天前
|
缓存 监控 API
构建高效RESTful API的实战指南
在数字时代的浪潮中,API成为了连接不同软件和服务的桥梁。一个设计良好的RESTful API不仅能够提升开发效率,还能带来更好的用户体验。本文将深入探讨如何构建高效的RESTful API,从理论到实践,带你一步步打造稳定、可靠且易于维护的后端服务。无论你是初学者还是有经验的开发者,这篇文章都将为你提供宝贵的指导和启示。
|
2天前
|
开发框架 .NET API
RESTful API 设计与实现:C# 开发者的一分钟入门
【10月更文挑战第5天】本文从零开始,介绍了如何使用 C# 和 ASP.NET Core 设计并实现一个简单的 RESTful API。首先解释了 RESTful API 的概念及其核心原则,然后详细说明了设计 RESTful API 的关键步骤,包括资源识别、URI 设计、HTTP 方法选择、状态码使用和错误处理。最后,通过一个用户管理 API 的示例,演示了如何创建项目、定义模型、实现控制器及运行测试,帮助读者掌握 RESTful API 的开发技巧。
19 7
|
6天前
|
存储 缓存 API
构建高效后端:RESTful API 设计的最佳实践
【10月更文挑战第2天】在数字化时代,后端开发是连接用户与数据的桥梁。本文将深入探讨如何设计一个高效、易于维护的后端系统,特别是围绕RESTful API的设计原则和最佳实践。我们将从基础概念出发,逐步深入到实际案例分析,最终通过代码示例具体展示如何实现这些设计原则。无论你是初学者还是有经验的开发者,这篇文章都将为你提供价值,帮助你构建更优秀的后端服务。
42 10
|
2天前
|
前端开发 API 网络架构
掌握RESTful API设计的艺术
【10月更文挑战第6天】本文将带你深入理解RESTful API的设计原则,通过实际的代码示例,展示如何创建符合REST架构风格的API。我们将探索资源的定义、HTTP方法的应用以及状态码的正确使用,让你能够构建出既优雅又高效的后端服务接口。
|
4天前
|
JSON JavaScript 前端开发
使用 Node.js 和 Express 构建 RESTful API 服务器
【10月更文挑战第3天】使用 Node.js 和 Express 构建 RESTful API 服务器
|
9天前
|
存储 API 数据库
深入浅出后端开发:从零到一搭建RESTful API
在数字化的浪潮中,后端开发如同一座桥梁,连接着用户界面与数据存储。本文将引导你理解后端开发的核心概念,并通过实践案例,展示如何从零开始构建一个RESTful API。我们将探索设计原则、选择合适的编程语言和框架、数据库交互以及API测试等方面。无论你是初学者还是希望巩固知识的开发者,这篇文章都将为你提供一条清晰的学习路径。
|
7天前
|
存储 缓存 中间件
构建高效RESTful API:最佳实践与技巧
构建高效RESTful API:最佳实践与技巧
|
11天前
|
存储 JSON JavaScript
探索后端开发:从零构建简易RESTful API
【9月更文挑战第35天】在数字时代的浪潮中,了解如何搭建一个后端服务变得至关重要。本文将通过构建一个简易的RESTful API来揭开后端开发的神秘面纱。我们将使用Node.js和Express框架,逐步引导你理解并实践API的设计、实现与测试过程。无论你是编程新手还是希望扩展技能边界的开发者,这篇文章都将为你提供一次深入浅出的学习旅程。
|
8天前
|
存储 Kubernetes 负载均衡
基于Ubuntu-22.04安装K8s-v1.28.2实验(四)使用域名访问网站应用
基于Ubuntu-22.04安装K8s-v1.28.2实验(四)使用域名访问网站应用
14 1
|
8天前
|
负载均衡 应用服务中间件 nginx
基于Ubuntu-22.04安装K8s-v1.28.2实验(二)使用kube-vip实现集群VIP访问
基于Ubuntu-22.04安装K8s-v1.28.2实验(二)使用kube-vip实现集群VIP访问
25 1