-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubeApiDeploy.py
67 lines (56 loc) · 2.31 KB
/
kubeApiDeploy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
from kubernetes import client, config
def deploy_app():
kubeconfig_filename = "kubeconfig.yaml"
kubeconfig_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "infrastructure", kubeconfig_filename))
print(f"Using kubeconfig file: {kubeconfig_path}")
os.environ["KUBECONFIG"] = kubeconfig_path
# Loads kubernetes config from default location
config.load_kube_config(config_file=kubeconfig_path)
# Ask the user for input
deployment_name = input("Enter a name for the deployment: ")
replica_count = int(input("Enter the number of replicas: "))
image_name = input("Enter the Docker image name: ")
container_port = int(input("Enter the container port: "))
namespace_name = input("Enter the namespace: ")
# Create a kubernetes client
api_client = client.ApiClient()
print("Created kubernetes client")
# Create a kubernetes api instance
api_instance = client.AppsV1Api(api_client)
print("Created kubernetes api instance")
# Create a kubernetes deployment object
deployment = client.V1Deployment(
api_version="apps/v1",
kind="Deployment",
metadata=client.V1ObjectMeta(name=deployment_name),
spec = client.V1DeploymentSpec(
replicas=replica_count,
selector=client.V1LabelSelector(
match_labels={"app": deployment_name}
),
template=client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"app": deployment_name}),
spec=client.V1PodSpec(
containers=[
client.V1Container(
name=deployment_name,
image=image_name,
ports=[client.V1ContainerPort(container_port=container_port)],
resources=client.V1ResourceRequirements(
requests={"cpu": "0.1", "memory": "256Mi"},
limits={"cpu": "0.5", "memory": "512Mi"}
)
)
]
)
)
)
)
# Create a deployment
api_instance.create_namespaced_deployment(
namespace=namespace_name,
body=deployment
)
if __name__ == "__main__":
deploy_app()