|
| 1 | +# Copyright 2019 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from kubernetes import client, config |
| 16 | + |
| 17 | + |
| 18 | +def create_deployment(apps_v1_api): |
| 19 | + container = client.V1Container( |
| 20 | + name="deployment", |
| 21 | + image="gcr.io/google-appengine/fluentd-logger", |
| 22 | + image_pull_policy="Never", |
| 23 | + ports=[client.V1ContainerPort(container_port=5678)], |
| 24 | + ) |
| 25 | + # Template |
| 26 | + template = client.V1PodTemplateSpec( |
| 27 | + metadata=client.V1ObjectMeta(labels={"app": "deployment"}), |
| 28 | + spec=client.V1PodSpec(containers=[container])) |
| 29 | + # Spec |
| 30 | + spec = client.V1DeploymentSpec( |
| 31 | + replicas=1, |
| 32 | + template=template) |
| 33 | + # Deployment |
| 34 | + deployment = client.V1Deployment( |
| 35 | + api_version="apps/v1", |
| 36 | + kind="Deployment", |
| 37 | + metadata=client.V1ObjectMeta(name="deployment"), |
| 38 | + spec=spec) |
| 39 | + # Creation of the Deployment in specified namespace |
| 40 | + # (Can replace "default" with a namespace you may have created) |
| 41 | + apps_v1_api.create_namespaced_deployment( |
| 42 | + namespace="default", body=deployment |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def create_service(): |
| 47 | + core_v1_api = client.CoreV1Api() |
| 48 | + body = client.V1Service( |
| 49 | + api_version="v1", |
| 50 | + kind="Service", |
| 51 | + metadata=client.V1ObjectMeta( |
| 52 | + name="service-example" |
| 53 | + ), |
| 54 | + spec=client.V1ServiceSpec( |
| 55 | + selector={"app": "deployment"}, |
| 56 | + ports=[client.V1ServicePort( |
| 57 | + port=5678, |
| 58 | + target_port=5678 |
| 59 | + )] |
| 60 | + ) |
| 61 | + ) |
| 62 | + # Creation of the Deployment in specified namespace |
| 63 | + # (Can replace "default" with a namespace you may have created) |
| 64 | + core_v1_api.create_namespaced_service(namespace="default", body=body) |
| 65 | + |
| 66 | + |
| 67 | +def create_ingress(networking_v1_beta1_api): |
| 68 | + body = client.NetworkingV1beta1Ingress( |
| 69 | + api_version="networking.k8s.io/v1beta1", |
| 70 | + kind="Ingress", |
| 71 | + metadata=client.V1ObjectMeta(name="ingress-example", annotations={ |
| 72 | + "nginx.ingress.kubernetes.io/rewrite-target": "/" |
| 73 | + }), |
| 74 | + spec=client.NetworkingV1beta1IngressSpec( |
| 75 | + rules=[client.NetworkingV1beta1IngressRule( |
| 76 | + host="boddulabs.com", |
| 77 | + http=client.NetworkingV1beta1HTTPIngressRuleValue( |
| 78 | + paths=[client.NetworkingV1beta1HTTPIngressPath( |
| 79 | + path="/", |
| 80 | + backend=client.NetworkingV1beta1IngressBackend( |
| 81 | + service_port=5678, |
| 82 | + service_name="service-example") |
| 83 | + |
| 84 | + )] |
| 85 | + ) |
| 86 | + ) |
| 87 | + ] |
| 88 | + ) |
| 89 | + ) |
| 90 | + # Creation of the Deployment in specified namespace |
| 91 | + # (Can replace "default" with a namespace you may have created) |
| 92 | + networking_v1_beta1_api.create_namespaced_ingress( |
| 93 | + namespace="default", |
| 94 | + body=body |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def main(): |
| 99 | + # Fetching and loading local Kubernetes Information |
| 100 | + config.load_kube_config() |
| 101 | + apps_v1_api = client.AppsV1Api() |
| 102 | + networking_v1_beta1_api = client.NetworkingV1beta1Api() |
| 103 | + |
| 104 | + create_deployment(apps_v1_api) |
| 105 | + create_service() |
| 106 | + create_ingress(networking_v1_beta1_api) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments