Skip to content

Instantly share code, notes, and snippets.

@alicek106
Created August 8, 2019 07:14
Show Gist options
  • Save alicek106/44330544b4b161d1645ac9ec5e9c3e25 to your computer and use it in GitHub Desktop.
Save alicek106/44330544b4b161d1645ac9ec5e9c3e25 to your computer and use it in GitHub Desktop.
Pod CRUD example (쿠버네티스 페이스북 그룹 질문에서)
# Original link : https://github.com/roshanp30/Kubernetes-Api-using-python/blob/master/podcrud.py
import yaml
from os import path
from kubernetes import client, config, utils
from pprint import pprint
DEPLOYMENT_NAME = "alicek106"
IMAGE_NAME = "nginx"
PORT_NO = 80
APP_NAME = "alicek106"
API_VERSION = "v1"
KIND = "Pod"
NAMESPACE = "default"
def create_pod_object():
# Configureate Pod template container
container = client.V1Container(
name=DEPLOYMENT_NAME,
image=IMAGE_NAME,
ports=[client.V1ContainerPort(container_port=PORT_NO)])
# Create and configurate a spec section
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"name": APP_NAME}),
spec=client.V1PodSpec(containers=[container]))
"""
template = client.V1PodTemplateSpec(
spec=client.V1PodSpec(containers=[container]))
"""
# Create the specification of deployment
spec = client.V1PodSpec(containers=[container])
# template=template)
# Instantiate the deployment object
pod_object = client.V1Pod(
api_version=API_VERSION,
kind=KIND,
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
spec=spec)
return pod_object
def create_pod(api_instance,namespace):
pod_body = create_pod_object()
api_response = api_instance.create_namespaced_pod(namespace, pod_body)
pprint(api_response)
def delete_pod(api_instance,namespace,name):
api_response = api_instance.delete_namespaced_pod(name, namespace,body=client.V1DeleteOptions(
propagation_policy='Foreground',
grace_period_seconds=5))
pprint(api_response)
def main():
config.load_incluster_config()
configuration = client.Configuration()
api_instance = client.CoreV1Api(client.ApiClient(configuration))
create_pod(api_instance,"default")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment