Kubernetes API 介绍

作者: skywalker | 来源:发表于2017-09-18 22:59 被阅读166次

一. API服务在架构中的整体位置

Kubernetes的API服务是整个架构中最重要的服务,kubelet在其上实现了node管理,pods管理;default-scheduler在其上实现了调度;ControllerManager在其上实现了副本管理等功能。本文将会从几个角度分析说明k8s API的功能和设计细节

kubernetes整体架构

二. 设计原则

  1. API逻辑简单化,服务性功能在其他模块实现,如:kubelet,scheduler
  2. REST 模式

GET /<resourceNamePlural> - Retrieve a list of type <resourceName>, e.g. GET /pods returns a list of Pods.
POST /<resourceNamePlural> - Create a new resource from the JSON object provided by the client.
GET /<resourceNamePlural>/<name> - Retrieves a single resource with the given name, e.g. GET /pods/first returns a Pod named 'first'. Should be constant time, and the resource should be bounded in size.
DELETE /<resourceNamePlural>/<name> - Delete the single resource with the given name. DeleteOptions may specify gracePeriodSeconds, the optional duration in seconds before the object should be deleted. Individual kinds may declare fields which provide a default grace period, and different kinds may have differing kind-wide default grace periods. A user provided grace period overrides a default grace period, including the zero grace period ("now").
PUT /<resourceNamePlural>/<name> - Update or create the resource with the given name with the JSON object provided by the client.
PATCH /<resourceNamePlural>/<name> - Selectively modify the specified fields of the resource. See more information below

  1. 不支持多个资源的锁
    只通过resource version 支持一个node节点上资源的分配的原子性。支持多个资源的锁,容易造成死锁

三. API的历史版本和兼容性

  1. Alpha level(如:v1alpha1)

Availability: committed to main kubernetes repo; appears in an official release; feature is disabled by default, but may be enabled by flag

  1. Beta level(如: v2beta3)

Availability: in official Kubernetes releases, and enabled by default

  1. Stable level(如:V1)

Availability: in official Kubernetes releases, and enabled by default
后续版本会严格兼容

可以通过设置 --runtime-config==extensions/v1beta1/deployments=false, 开启/关闭某些功能
在生产环境,尽量使用Beta Level之后的API功能

四. 访问控制

  1. Authentication
    通过证书访问,成功后获取username
  2. Authorization
    RBAC,例如:每一个team一个namespace一个访问证书
    框架有一个bot的admin账号
  3. Admission Control
    更灵活的自定义,比如:强制pod拉取image,不能复用本地的
  4. 服务默认的端口
    http:8080,https:6443

五. 访问约定/协议

  1. 并发性支持
    通过resourceVersion实现。具体流程:1)先获取最新状态;2)操作;3)失败后重试
  2. 通信协议
    json,后续可能支持pb
  3. 返回值
    遵循http协议:200,300,400,500
    如果没有成功,会返回额外的status字段

A Status kind will be returned by the API in two cases:
When an operation is not successful (i.e. when the server would return a non 2xx HTTP status code).
When a HTTP DELETE call is successful.

  1. streaming接口
    支持从http升级至 SPDY protocols or WebSockets,典型应用:exec, log, attach, and portforward

六. Workloads:要怎样使用pods干活?

1. pods

pods作为工作的最小单元,一般不直接部署提供服务, 详见后续workload类型
init container:可以做初始化工作,串行执行,不成功重新调度, use case:在某些服务启动好了之后再启动本服务(服务启动顺序依赖)
container 可以设置Hook(lifecycle),在运行job之前和运行完job之后。类似我们现在的初始化脚本和结束清理脚本
资源申请的维度
request:scheduler按照此调度
limit:资源上限,超配并设置 oom分数 可以提升集群资源利用率20% by google经验
推荐1pod1container的设计模式
restart policy:默认:always

2. ReplicaSets和Replicaton Controller

紧耦合管理pods
保证副本数为N

3. StatefulSet

区别是pods之间是有序的
创建从0-N;销毁从N-0
网络上:pod 到 pod上是不连通的,不适合做交互比较强的负载

4. Deployment

目标:支持变更,适用于在线服务,持续在线,持续发版变更(rolling-update)

更新镜像命令:kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1

回滚所需的历史版本也会保留
包含的子资源 不应被其他deployment控制

Q:资源不够了,会怎么样?
A: 会报错,后续有资源了自动调度上

5. DeamonSets

use case: 如:glusterfs存储服务,收集日志:gmond服务
支持node selector:选择有某些特性的服务器部署
服务器添加到集群,自动启动相关的pods
访问:可通过暴露hostPort提供对集群的整体服务

6. Job

use case:
batch job:视频处理, 1pod 处理1个mp4,并且使用gpu
最重要的2个参数
completions:总共要完成的pods(任务)个数
Parallelism:工作中最大的并发执行数
cleanup策略
为了能够提供日志查看等功能,pods停止了也不会删除,需要手动删除job
整体的deadline
通过配置spec.activeDeadlineSeconds

7. CronJob

支持定期的执行Job
alpha,默认不开启

8. pods资源GC策略

Object之间的dependency:ownerReference
级联删除
orphan: pod变成孤儿
foreground:deployment一定设置此方式,否则pods变成孤儿资源
background

9. Service

每个pod的ip通过网络插件自动分配,但是不能提供访问点
Service通过配置从port负载均衡到后端targetPort实现

  • 流量分发到Endpoint
  • 可以导流到k8s之外的服务:ip+port

功能原理作用于kube-proxy
默认, Proxy-mode: iptables

Q:为什么使用vip做负载均衡而不是轮询的DNS
A:DNS cache 刷新慢的问题

访问service的2种方式

  • 通过环境变量:运行的pod会得到环境变量{SVCNAME}_SERVICE_HOST
  • 推荐:通过DNS,直接通过服务名字访问

七. 访问客户端和SDK

客户端kubectl
通过读取yaml,并转换为json发送请求
cheetsheet

官方支持SDK
原生:go client
python client

八. API详细参考1.7版本

核心是3元组
ResourceSpec:目标状态
ResourceStatus:现状
Resource ObjectMeta:元信息(分配方式等)

K8s 支持Swagger 和 OpenApi
通过 /swaggerapi 可以查看Swagger API, /swagger.json 查看OpenAPI。
开启 --enable-swagger-ui=true 后还可以通过 /swagger-ui 访问Swagger UI。

k8s swagger-ui

相关文章

网友评论

    本文标题:Kubernetes API 介绍

    本文链接:https://www.haomeiwen.com/subject/aofcsxtx.html