Docker K8S 容器化技术linux

Kubernetes的一些常用的API

2017-06-15  本文已影响2163人  hanxs

Kubernetes API

Based on kubernetes1.5
本文主要摘抄一些kubernetes及相关工具的常用的接口的参数及验证方式。

简介

Kubernetes API,主要提供一些RESTFUL的接口供开发者对集群进行一些管理。对于大多数功能都有对应的增删改查接口,对应参数的描述。部分接口还带有yaml和curl/json的示例。通过这些接口我们可以定制一些web页面来管理我们的集群。

Create Namespaces

POST /api/v1/namespaces
Json
{
    "kind": "Namespace",
    "apiVersion": "v1",
    "metadata": {
        "name": "beta",
        "labels": {
            "app": "beta"
        }
    }
}
Complex Json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "name": "development",
        "labels": {
            "name": "development"
        }
    },
    "spec": {
        "finalizers": [
            "openshift.com/origin",
            "kubernetes"
        ]
    },
    "status": {
        "phase": "Active"
    }
}
验证
kubectl get namespaces

Create Deployment

Yaml ex
$ kubectl proxy
$ curl -X POST -H 'Content-Type: application/yaml' --data '
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: deployment-example
spec:
  replicas: 3
  revisionHistoryLimit: 10
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.10
        ports:
        - containerPort: 80
' http://127.0.0.1:8001/apis/extensions/v1beta1/namespaces/default/deployments
POST /apis/extensions/v1beta1/namespaces/{namespace}/deployments
Json
{
    "kind": "Deployment",
    "apiVersion": "extensions/v1beta1",
    "metadata": {
        "name": "whoami"
    },
    "spec": {
        "replicas": 3,
        "revisionHistoryLimit": 2,
        "template": {
            "metadata": {
                "labels": {
                    "app": "whoami"
                }
            },
            "spec": {
                "containers": [
                    {
                        "name": "whoami",
                        "image": "1.1.1.1:5000/heapster_influxdb:0.6",
                        "resources":{
                            "limits":{
                                "cpu":"600m",
                                "memory":"600Mi"
                            },
                            "requests":{
                                "cpu":"500m",
                                "memory":"500Mi"
                            }
                        },
                        "command":["sh","c","bala bala bala"],
                        "env":[
                            {
                                "name":"SPRING CONFIG URI",
                                "VALUE":"http://ms.microservice123!@yidui"
                            },
                            {
                                "name":"CONFIG_ACTIVE",
                                "VALUE":"test"
                            }   
                        ],
                        "ports": [
                            {
                                "containerPort": 8080,
                                "hostPort": 8000
                            }
                        ]
                    }
                ]
            }
        }
    }
}
revisionHistoryLimit(非必填): The number of old ReplicaSets to retain to allow rollback.This is a pointer to distinguish between explicit zero and not specified.
images: 由于是本地的镜像,所以该参数格式为:docker registry的地址:端口号/镜像名:tag
containers.resources(非必填):配置一下CPU、内存等参数
containers.command(非必填):有些微服务是需要用自己的命令去启动
containers.ports(非必填):将服务的端口通过docker-proxy映射到宿主机的对应端口上。若如此做,则一个节点只能部署一个这样的节点,同时要注意与其他服务的端口冲突

验证
kubectl get deployments

Delete Deployment (暂时只按顺序调用123)

1.delete deployment
Yaml ex
$ kubectl proxy
$ curl -X DELETE -H 'Content-Type: application/yaml' --data '
gracePeriodSeconds: 0
orphanDependents: false
' 'http://127.0.0.1:8001/apis/extensions/v1beta1/namespaces/default/daemonsets/daemonset-example'
DELETE /apis/extensions/v1beta1/namespaces/{namespace}/deployments/{name}
可选Json参数
{
    "gracePeriodSeconds": 0,
    "orphanDependents": false
}
orphanDependents: Should the dependent objects be orphaned. If true/false, the "orphan" finalizer will be
added to/removed from the object's finalizers list
验证
kubectl get deployments --all-namespaces=true
2.delete replicasets
DELETE /apis/extensions/v1beta1/namespaces/{namespace}/replicasets?labelSelector=app in ({lable})
验证
kubectl get rs --all-namespaces=true
3.delete pods
DELETE /apis/extensions/v1beta1/namespaces/{namespace}/pods?labelSelector=app in ({lable})
验证
kubectl get pods --all-namespaces=true
4.list rs
GET /apis/extensions/v1beta1/namespaces/ns1/replicasets?labelSelector=app in (whoami)

根据label里的app字段筛选需要的rs资源

5.delete rs
DELETE /apis/extensions/v1beta1/namespaces/{namespace}/replicasets/{name}

name取自2中的metadata.name

验证
kubectl get rs --all-namespaces=true
6.list pod
GET /api/v1/namespaces/ns3/pods?labelSelector=app in (whoami)

根据label里的app字段筛选需要的pod资源

7.delete pod
DELETE /apis/extensions/v1beta1/namespaces/{namespace}/pods/{name}
验证
kubectl get pods --all-namespaces=true
8.expose service
POST /api/v1/namespaces/{namespace}/services
Json
{
    "kind": "Service",
    "apiVersion": "v1",
    "metadata": {
        "name": "whoami",
        "labels": {
            "app": "whoami"
        },
        "namespace": "ns1"
    },
    "spec": {
        "ports": [
            {
                "name": "http",
                "port": 8000,
                "targetPort": 8000,
                "nodePort": 32417
            }
        ],
        "selector": {
            "app": "whoami"
        },
        "type": "NodePort"
    }
}
验证
docker get services --all-namespaces

注:只能看见服务,还没有研究怎么使用

9.delete service
DELETE /api/v1/namespaces/{namespace}/services/{name}
验证
docker get services --all-namespaces

Docker Registry API

HttpTransform

部署
docker load -i https.tar
docker run -d --name=https-transformer -p 23456:8080  xiaoxiaozyq/https-transformer
curl http://1.1.1.8:23456/getRepo?url=

GET http://{registry ip}:23456/getRepo?url=
url: https://{registry ip}:5000/v2/_catalog 
      或者
      https://{registry ip}:5000/v2/{image name}/tags/list
      经过URL encode之后
1.Listing Repositories
GET /v2/_catalog?n=<integer>

n,结果数,可选
Data
{
    "repositories": [
        "gcr/pause",
        "heapster",
        "heapster_grafana",
        "heapster_influxdb",
        "nginx",
        "resizer"
    ]
}
2.Image Tags

根据上个接口的Repository获取其下image的tags

GET /v2/<name>/tags/list
Data
{
    "name": "heapster_influxdb",
    "tags": [
        "0.6"
    ]
}

Docker Registry Push Image

先打Tag,再Push
docker tag hello-world 127.0.0.1:5000/hello-world:latest

docker images

docker push 127.0.0.1:5000/hello-world

为了指定新的Registry目的地址,需要在镜像名前加上主机和端口前缀。

Eureka

部署

 docker run -d --name=eureka -p 32222:8761 springcloud/eureka

可以外网访问32222端口访问web界面和api

查看app详情
GET /eureka/apps/{app name}
上一篇下一篇

猜你喜欢

热点阅读