Coder Social home page Coder Social logo

psyhomb / k8s-deployer Goto Github PK

View Code? Open in Web Editor NEW
25.0 4.0 5.0 264 KB

Deploy Kubernetes service and store retrieved information in the Consul K/V store

License: MIT License

Python 98.23% Dockerfile 1.77%
kubernetes k8s-deployer consul consul-template docker supervisord kubectl kubeadm kubernetes-deployer deployer

k8s-deployer's Introduction

k8s-deployer HTTP API

About

Deploy Kubernetes service and store retrieved information in the Consul K/V store

kubernetes-external-load-balancing

Installation

Supervisor

Install pew (python environment wrapper)

pip2 install pew

Create required project dirs

mkdir -p /data/pew/virtualenvs
mkdir -p /etc/k8s-deployer
mkdir -p /var/log/k8s-deployer

Clone repo

git clone https://<fqdn>/<username>/k8s-deployer.git /data/pew/k8s-deployer
cd /data/pew/k8s-deployer

Create a new virtualenv for k8s-deployer project

WORKON_HOME="/data/pew/virtualenvs" pew new -a /data/pew/k8s-deployer -r requirements.txt k8s-deployer

Enter virtualenv (previous commad will also enter virtualenv at the end)

WORKON_HOME=/data/pew/virtualenvs pew workon k8s-deployer

Copy and modify k8s-deployer configuration file

cp /data/pew/k8s-deployer/config.json /etc/k8s-deployer/config.json

Copy and modify supervisor configuration file

cp /data/pew/k8s-deployer/supervisor/k8s-deployer.conf /etc/supervisor/conf.d/k8s-deployer.conf

Add and start service

supervisorctl reread
supervisorctl add k8s-deployer

Docker

Supported environment variables

Note: Environment variables have precedence over configuration file

Env Keys Env (default) Values Value Examples Description
K8S_DEPLOYER_KUBE_SCHEME http Scheme http or https
K8S_DEPLOYER_KUBE_HOST localhost Kubernetes API hostname or IP address
K8S_DEPLOYER_KUBE_PORT 8080 Kubernetes API port
K8S_DEPLOYER_KUBE_API_HEADERS none key1__value1,key2__value2,keyN__valueN HTTP request headers
K8S_DEPLOYER_CONSUL_SCHEME http Scheme http or https
K8S_DEPLOYER_CONSUL_HOST localhost Consul API hostname or IP address
K8S_DEPLOYER_CONSUL_PORT 8500 Consul API port
K8S_DEPLOYER_CONSUL_KEY_PATH kubernetes kubernetes/prod Consul K/V store path where all the data will be stored
K8S_DEPLOYER_CONSUL_SPECS_RETENT 5 How many specifications have to be preserved at any time

Build and run

docker build --no-cache -t k8s-deployer .
docker run -it -d --name k8s-deployer -p 8089:8089 k8s-deployer

Usage

For quick test deploy you can use deploy.sh script located in examples dir, in this dir you will also find echoserver.json specification descriptor that will be used for echoserver service deployment

cd examples
./deploy.sh

Specification descriptor:

{
    "id": null,
    "namespace": null,
    "objects": {
        "deployments": {
            "specification": {
                "### Kubernetes deployment object spec goes here"
            }
        },
        "services": {
            "specification": {
                "### Kubernetes service object spec goes here"
            }
        }
    }
}

If you already have yaml spec files you can use kubectl to convert these locally to json format

Note: Only services of type NodePort will be registered in the Consul service catalog

kubectl convert -f echoserver-deployment.yaml --local -o json > echoserver-deployment.json
kubectl convert -f echoserver-service.yaml --local -o json > echoserver-service.json

After successful yaml => json conversion you can use k8s-specgen.py script to easily generate k8s-deployer specification file

k8s-specgen.py -d echoserver-deployment.json -s echoserver-service.json -o echoserver.json

Kubernetes documentation regarding deployment and service objects

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

https://kubernetes.io/docs/concepts/services-networking/service/

Kubernetes API references

https://kubernetes.io/docs/reference/


Examples

In the following example we're going to explain how we can deploy echoserver service in default namespace

Insert specification for new service into the Consul K/V store

Note: by default max 5 specifications for the same service will be preserved on the Consul K/V store at any time, you can modify this value in the configuration file or through environment variable $K8S_DEPLOYER_CONSUL_SPECS_RETENT

Note: after successful transaction, specification ID will be returned as value of Location header

curl -X POST -isSL -H 'Content-Type: application/json' --data '@echoserver.json' http://localhost:8089/specifications/default/echoserver

List all available specification IDs

curl -isSL http://localhost:8089/specifications/default/echoserver

Show service specification

curl -isSL http://localhost:8089/specifications/default/echoserver/latest

Deploy a new service using specification previously inserted into the Consul K/V store

Note: if we omit specification ID, latest specification will be used

Note: after every successful build deployed spec will be created on this Consul K/V path $K8S_DEPLOYER_CONSUL_KEY_PATH/specifications/<namespace>/<service_name>

curl -X PUT -isSL http://localhost:8089/deployments/default/echoserver

or you can explicitly set specification ID

curl -X PUT -isSL http://localhost:8089/deployments/default/echoserver/1490691025506482_1650b288-e79c-4247-9b3b-95f1051302c4

Undeploy existing service

Note: it's going to delete all the service related objects from Kubernetes and service definition from the Consul K/V store

curl -X DELETE -isSL http://localhost:8089/deployments/default/echoserver

Update existing service definitions that have been manually modified on the Kubernetes side or populate Consul K/V store with new service definitions for services that are not deployed through k8s-deployer (register service on Consul)

Note: Deletion of services that are not fully deployed through k8s-deployer API will not be possible via API itself

curl -X PUT -isSL http://localhost:8089/registration/default/echoserver

Next go to consul-template

k8s-deployer's People

Contributors

dependabot[bot] avatar psyhomb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.