Giter VIP home page Giter VIP logo

node-k8s-client's Introduction

Nodejs Kubernetes client

Node.js client library for Google's Kubernetes Kubectl And API

Install:

    npm install k8s

Usage

Create client

var K8s = require('k8s')

// use kubectl

var kubectl = K8s.kubectl({
    endpoint:  'http://192.168.10.10:8080'
    , binary: '/usr/local/bin/kubectl'
})

//use restful api
var kubeapi = K8s.api({
	endpoint: 'http://192.168.10.10:8080'
	, version: 'v1'
})

// Configure using kubeconfig
var kube = k.kubectl({
	binary: '/bin/kubectl'
	,kubeconfig: '/etc/cluster1.yaml'
	,version: 'v1'
});

Options

endpoint : URL for API

version : API Version

binary : Path to binary file

kubeconfig : Path to kubeconfig

:auth Authentication to REST API. Currently supported authentication method type is password

{
  "auth": {
    "type" : "password",
    "username": "admin",
    "password": "123123"
  }
}

:strictSSL If set to false, use of the API will not validate SSL certificate. Defualt is true.

kubeAPI

using callback

// method GET
kubeapi.get('namespaces/default/replicationcontrollers', function(err, data){})

// method POST
kubeapi.post('namespaces/default/replicationcontrollers', require('./rc/nginx-rc.json'), function(err, data){})
// method PUT
kubeapi.put('namespaces/default/replicationcontrollers/nginx', require('./rc/nginx-rc.json'), function(err, data){})
// method PATCH
kubeapi.patch('namespaces/default/replicationcontrollers/nginx', [{ op: 'replace', path: '/spec/replicas', value: 2 }], function(err, data){})
// method DELETE
kubeapi.delete('namespaces/default/replicationcontrollers/nginx', function(err, data){})

using promise

// method GET
kubeapi.get('namespaces/default/replicationcontrollers').then(function(data){}).catch(function(err){})
// method POST
kubeapi.post('namespaces/default/replicationcontrollers', require('./rc/nginx-rc.json')).then(function(data){}).catch(function(err){})
// method PUT
kubeapi.put('namespaces/default/replicationcontrollers/nginx', require('./rc/nginx-rc.json')).then(function(data){}).catch(function(err){})
// method PATCH
kubeapi.patch('namespaces/default/replicationcontrollers/nginx', [{ op: 'replace', path: '/spec/replicas', value: 2 }]).then(function(data){}).catch(function(err){})
// method DELETE
kubeapi.delete('namespaces/default/replicationcontrollers/nginx').then(function(data){}).catch(function(err){})

using async/await

!async function()
{
    try
    {
        // method GET
        const data1 = await kubeapi.get('namespaces/default/replicationcontrollers')
        // method POST
        const data2 = await kubeapi.post('namespaces/default/replicationcontrollers', require('./rc/nginx-rc.json'))
        // method PUT
        const data3 = await kubeapi.put('namespaces/default/replicationcontrollers/nginx', require('./rc/nginx-rc.json'))
        // method PATCH
        const data4 = await kubeapi.patch('namespaces/default/replicationcontrollers/nginx', [{ op: 'replace', path: '/spec/replicas', value: 2 }])
        // method DELETE
        const data5 = await kubeapi.delete('namespaces/default/replicationcontrollers/nginx')
    }
    catch(err){
        console.log(err)
    }
}()

method GET -> watch

using callback
kubeapi.watch('watch/namespaces/default/pods', function(data){
	// message
}, function(err){
	// exit
}, [timeout])
using rxjs
kubeapi.watch('watch/namespaces/default/pods', [timeout]).subscribe(data=>{
    // message
}, err=>{
    // exit
})

kubectl (callback, promise, async/await)

example

    //kubectl['type']['action]([arguments], [flags], [callback]): Promise

    //callback
    kubect.pod.delete('pod_name', function(err, data){})
    kubect.pod.delete('pod_name', ['--grace-period=0'], function(err, data){})
    //promise
    kubect.pod.delete('pod_name').then()
    kubect.pod.delete('pod_name', ['--grace-period=0']).then()
    //async/await
    const data = kubect.pod.delete('pod_name')
    const data = kubect.pod.delete('pod_name',['--grace-period=0'])

excute command

    kubectl.command('get pod pod_name --output=json', function(err, data){})
    kubectl.command('get pod pod_name --output=json').then()
    const data = await kubectl.command('get pod pod_name --output=json')

Pods

get pod list

kubectl.pod.list(function(err, pods){})

//selector
var label = { name: nginx }
kubectl.pod.list(label, function(err, pods){})

get pod

kubectl.pod.get('nginx', function(err, pod){})

// label selector
kubectl.pod.list({ app: 'nginx' }, function(err, pods){}) 

create a pod

kubectl.pod.create('/:path/pods/nginx.yaml'), function(err, data){})

delete a pod

kubectl.pod.delete('nginx', function(err, data){})

log

kubectl.pod.log('pod_id1 pod_id2 pod_id3', function(err, log){})

ReplicationController

get rc list

kubectl.rc.list(function(err, pods){})

get a rc

kubectl.rc.get('nginx', function(err, pod){})

create a rc

kubectl.rc.create('/:path/pods/nginx.yaml'), function(err, data){})

delete a rc

kubectl.rc.delete('nginx', function(err, data){})

rolling-update by image name

kubectl.rc.rollingUpdate('nginx', 'nginx:vserion', function(err, data){})

rolling-update by file

kubectl.rc.rollingUpdateByFile('nginx', '/:path/rc/nginx-v2.yaml', function(err, data){})

change replicas

kubectl.rc.scale('nginx', 3, function(err, data){})

Service

get service list

kubectl.service.list(function(err, pods){})

get a service

kubectl.service.get('nginx', function(err, pod){})

create a service

kubectl.service.create('/:path/service/nginx.yaml'), function(err, data){})

delete a service

kubectl.service.delete('nginx', function(err, data){})

Node

get node list

kubectl.node.list(function(err, pods){})

get a node

kubectl.node.get('nginx', function(err, pod){})

create a node

kubectl.node.create('/:path/nodes/node1.yaml'), function(err, data){})

delete a node

kubectl.node.delete('nginx', function(err, data){})

node-k8s-client's People

Contributors

mmoonn2 avatar kundralaci avatar andreassolberg avatar timcrider avatar tslater avatar ladlavj avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.