Kubernetes python client

Kubernetes python client hanya membutuhkan kubeconfig yang berada di .kube/config. Client ini dapat digunakan baik didalam cluster kubernetes ataupun akses dari luar cluster dengan memanfaatkan kubeconfig.

  • Instalasi
pip install kubernetes
  • Melihat pod pada namespace wordpress
from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret =v1.list_namespaced_pod("wordpress")
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))


btech@zu-haproxy:~$ python list_pod.py 
Listing pods with their IPs:
10.244.2.4      wordpress       nginx-deployment-76bf4969df-fj6kd
10.244.3.2      wordpress       nginx-deployment-76bf4969df-m2bjv
10.244.1.3      wordpress       nginx-deployment-76bf4969df-x225q
  • melihat service pada namespace wordpress
from kubernetes import client, config, watch

def main():    
    config.load_kube_config()
    api = client.CoreV1Api()
    service = api.list_namespaced_service(namespace="wordpress",watch=False)
    
    for i in service.items:
        print("%s\t%s\t%s\t%s:%s" %(i.metadata.name, i.spec.type, i.spec.cluster_ip, i.spec.ports[0].port, i.spec.ports[0].node_port))

if __name__ == '__main__':
    main()

btech@zu-haproxy:~$ python list_service.py 
nginx-service   NodePort        10.104.119.199  80:30000
  • membuat deployment dari yaml pada namespace wordpress
from os import path
import yaml
from kubernetes import client, config

def main():
    config.load_kube_config()

    with open(path.join(path.dirname(__file__), "nginx-deployment.yaml")) as f:
        dep = yaml.safe_load(f)
        k8s_beta = client.ExtensionsV1beta1Api()
        resp = k8s_beta.create_namespaced_deployment(
            body=dep, namespace="wordpress")
        print("Deployment created. status='%s'" % str(resp.status))

if __name__ == '__main__':
    main()


apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
  • membuat service dari deployment pada namespace wordpress
from os import path
import yaml
from kubernetes import client, config, watch


def main():
    config.load_kube_config()

    with open(path.join(path.dirname(__file__), "nginx-service.yaml")) as f:
        dep = yaml.safe_load(f)

        api = client.CoreV1Api()
        svc = api.create_namespaced_service(namespace="wordpress",body=dep)
        print("Service created. status='%s'" % str(svc.spec.type))

if __name__ == '__main__':
    main()

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30000
      name: http
  selector:
    app: nginx

silahkan dapat diexplore lebih dalam pada GitHubnya secara langsung.

Comments are closed.