k8s restful api 访问

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
简介: 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 访问

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
相关文章
|
1月前
|
JSON API 数据库
解释如何在 Python 中实现 Web 服务(RESTful API)。
解释如何在 Python 中实现 Web 服务(RESTful API)。
26 0
|
6天前
|
安全 Java API
java借助代理ip,解决访问api频繁导致ip被禁的问题
java借助代理ip,解决访问api频繁导致ip被禁的问题
|
9天前
|
安全 Java API
RESTful API设计与实现:Java后台开发指南
【4月更文挑战第15天】本文介绍了如何使用Java开发RESTful API,重点是Spring Boot框架和Spring MVC。遵循无状态、统一接口、资源标识和JSON数据格式的设计原则,通过创建控制器处理HTTP请求,如示例中的用户管理操作。此外,文章还提及数据绑定、验证、异常处理和跨域支持。最后,提出了版本控制、安全性、文档测试以及限流和缓存的最佳实践,以确保API的稳定、安全和高效。
|
12天前
|
小程序 前端开发 API
小程序全栈开发中的RESTful API设计
【4月更文挑战第12天】本文探讨了小程序全栈开发中的RESTful API设计,旨在帮助开发者理解和掌握相关技术。RESTful API基于REST架构风格,利用HTTP协议进行数据交互,遵循URI、客户端-服务器架构、无状态通信、标准HTTP方法和资源表述等原则。在小程序开发中,通过资源建模、设计API接口、定义资源表述及实现接口,实现前后端高效分离,提升开发效率和代码质量。小程序前端利用微信API与后端交互,确保数据流通。掌握这些实践将优化小程序全栈开发。
|
22天前
|
前端开发 Java API
构建RESTful API:Java中的RESTful服务开发
【4月更文挑战第3天】本文介绍了在Java环境中构建RESTful API的重要性及方法。遵循REST原则,利用HTTP方法处理资源,实现CRUD操作。在Java中,常用框架如Spring MVC简化了RESTful服务开发,包括定义资源、设计表示层、实现CRUD、考虑安全性、文档和测试。通过Spring MVC示例展示了创建RESTful服务的步骤,强调了其在现代Web服务开发中的关键角色,有助于提升互操作性和用户体验。
构建RESTful API:Java中的RESTful服务开发
|
25天前
|
XML JSON 安全
谈谈你对RESTful API设计的理解和实践。
RESTful API是基于HTTP协议的接口设计,通过URI标识资源,利用GET、POST、PUT、DELETE等方法操作资源。设计注重无状态、一致性、分层、错误处理、版本控制、文档、安全和测试,确保易用、可扩展和安全。例如,`/users/{id}`用于用户管理,使用JSON或XML交换数据,提升系统互操作性和可维护性。
18 4
|
27天前
|
安全 API 开发者
构建高效可扩展的RESTful API服务
在数字化转型的浪潮中,构建一个高效、可扩展且易于维护的后端API服务是企业竞争力的关键。本文将深入探讨如何利用现代后端技术栈实现RESTful API服务的优化,包括代码结构设计、性能调优、安全性强化以及微服务架构的应用。我们将通过实践案例分析,揭示后端开发的最佳实践,帮助开发者提升系统的响应速度和处理能力,同时确保服务的高可用性和安全。
27 3
|
1月前
|
缓存 前端开发 API
构建高效可扩展的RESTful API:后端开发的最佳实践
【2月更文挑战第30天】 在现代Web应用和服务端架构中,RESTful API已成为连接前端与后端、实现服务间通信的重要接口。本文将探讨构建一个高效且可扩展的RESTful API的关键步骤和最佳实践,包括设计原则、性能优化、安全性考虑以及错误处理机制。通过这些实践,开发者可以确保API的健壮性、易用性和未来的可维护性。
|
1月前
|
API 开发者 UED
深入探讨RESTful API设计原则及最佳实践
在当今互联网时代,RESTful API已成为各种软件系统之间进行通信的重要方式。本文将从资源定义、URI设计、HTTP方法选择、状态码规范等方面深入探讨RESTful API设计的原则与最佳实践,帮助开发者更好地构建高效、健壮的API。
|
1月前
|
JSON Java API
Springboot项目中如何设计一个规范的统一的Restful API 响应框架?
Springboot项目中如何设计一个规范的统一的Restful API 响应框架?
23 1

推荐镜像

更多