{"id":3469,"date":"2022-10-26T19:18:38","date_gmt":"2022-10-26T19:18:38","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=3469"},"modified":"2024-10-19T17:36:34","modified_gmt":"2024-10-19T17:36:34","slug":"python-script","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/python-script","title":{"rendered":"Python script for automating Kubernetes deployment"},"content":{"rendered":"<p>In this blog, we will write a Python script to automate the deployment of Kubernetes resources using the<a href=\"https:\/\/pypi.org\/project\/kubernetes\/\"> <strong>Kubernetes module<\/strong><\/a>. It is a Python client library. Using it we can interact with Kubernetes REST API to manage resources on our Kubernetes cluster.<\/p>\n<p>To install the Kubernetes module use the following command.<\/p>\n<pre>pip3 install Kubernetes\r\n<\/pre>\n<p>Using the following python script we will create a Kubernetes object in a non-default namespace &#8220;dev&#8221;. We have defined the Deployment object using a Python dictionary.\u00a0You can specify deployments to make new ReplicaSets or to delete current deployments and replace them with new deployments that use all of their resources.<\/p>\n<p>Our python script will take a unique ID as an argument so that we can create deployments with unique resource names.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3482\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-web-development-agencies-300x191.png\" alt=\"Deployment\" width=\"762\" height=\"485\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-web-development-agencies-300x191.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-web-development-agencies-768x490.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-web-development-agencies.png 781w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Next, we need to connect to Kubernetes API. We will use <strong>config.load_kube_function()<\/strong>\u00a0 to access the kubeconfig file. By default, it loads the kubeconfig file from $HOME\/.kube\/config. We will manage the deployment object using<strong> <code class=\"fj ur us ut uu b\">client.AppsV1Api<\/code> <\/strong>which allows us to work with all the resources that belong to <strong>apiVersion: apps\/v1<\/strong> <img loading=\"lazy\" class=\"alignnone wp-image-3483\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-database-management-company-300x35.png\" alt=\"\" width=\"729\" height=\"85\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-database-management-company-300x35.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-database-management-company.png 571w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now we will write a function to create a deployment object. It will also verify that the object is successfully deployed.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3485\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-php-development-services-300x91.png\" alt=\"Deployment\" width=\"745\" height=\"226\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-php-development-services-300x91.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-php-development-services-768x233.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-php-development-services.png 997w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3486\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-web-developer-300x84.png\" alt=\"Deployment\" width=\"796\" height=\"223\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-web-developer-300x84.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-web-developer-1024x288.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-web-developer-768x216.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-web-developer-1200x338.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-web-developer.png 1282w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<pre><code>\r\nfrom kubernetes import client, config\r\nimport sys\r\nimport time\r\nfrom logzero import logger\r\nfrom chaoslib.exceptions import ActivityFailed\r\ntry:\r\n  ns = \"dev\"\r\n  unique_id = sys.argv[1]\r\n  deployment_name=\"dev-app-\"+unique_id\r\n  manifest = {\r\n      \"apiVersion\": \"apps\/v1\",\r\n      \"kind\": \"Deployment\",\r\n      \"metadata\": {\"name\": deployment_name},\r\n      \"spec\": {\"replicas\": 1,\r\n               \"selector\": {\r\n                  \"matchLabels\": {\r\n                      \"app\": \"dev\"\r\n                  }},\r\n          \"template\": {\"metadata\": {\"labels\": {\"app\": \"dev\"}},\r\n              \"spec\": {\"containers\": [\r\n                  {\"name\": \"webapp\", \"image\": \"nginx\"}]\r\n              }\r\n          },\r\n      }\r\n  }\r\n\r\n  config.load_kube_config()\r\n  v1 = client.AppsV1Api()\r\n  field_selector = \"metadata.name={name}\".format(name=deployment_name)\r\n  def create_deploy():\r\n   deploy_names=[]\r\n   list = v1.list_namespaced_deployment(namespace = ns)\r\n   for i in list.items:\r\n      deploy_names.append(i.metadata.name)\r\n\r\n   if  deployment_name in deploy_names:\r\n      raise Exception(\"Invalid Argument: Resource already exists\")\r\n   else:\r\n     deploy = v1.create_namespaced_deployment(body = manifest, namespace = ns)\r\n     print(\"Waiting for Deployment to become ready...\")\r\n     time.sleep(90)\r\n\r\n  def deploy_status():\r\n      ret = v1.list_namespaced_deployment(ns, field_selector=field_selector)\r\n      for d in ret.items:\r\n                  logger.debug(\"Deployment has '{s}' available replicas\".format(s=d.status.available_replicas))\r\n\r\n                  if d.status.available_replicas != d.spec.replicas:\r\n                     raise ActivityFailed( \"Deployment '{name}' failed\".format(name=deployment_name))\r\n                  else:\r\n                     print(\"Deployment '{name}' successful\".format(name=deployment_name))\r\n\r\n  create_deploy()\r\n  deploy_status()\r\nexcept Exception as e:\r\n     print(e)\r\n<\/code><\/pre>\n<p>Now we will execute our script to create this deployment object.<\/p>\n<pre>python deploy.py unique_id_001\r\n<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3487\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-python-developer-300x27.png\" alt=\"Deployment\" width=\"733\" height=\"66\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-python-developer-300x27.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-python-developer-768x68.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/hire-python-developer.png 790w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3488\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-data-analytics-services-3-300x64.png\" alt=\"Deployment\" width=\"745\" height=\"159\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-data-analytics-services-3-300x64.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-data-analytics-services-3-768x165.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/10\/Checkmate-data-analytics-services-3.png 1021w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>This blog is written by our DevOps engineer. You can Please contact with our <a href=\"https:\/\/www.checkmateq.com\/virtual-cto-services\">Virtual CTO Consultants<\/a> to discuss <a href=\"https:\/\/www.checkmateq.com\/cloud\">cloud\u00a0 infrastructure services in India<\/a> , <a href=\"https:\/\/www.checkmateq.com\/cloud\">Cloud Support Services<\/a>, <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">IT Staffing Services in India<\/a>, <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">Hire Software Developers in India<\/a>, and product development best practices.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will write a Python script to automate the deployment of Kubernetes resources using the Kubernetes module. It is a Python client library. Using it we can interact with Kubernetes REST API to manage resources on our Kubernetes cluster. To install the Kubernetes module use the following command. pip3 install Kubernetes Using &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/python-script\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python script for automating Kubernetes deployment&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":3479,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,2,70,69,68,6],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/3469"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=3469"}],"version-history":[{"count":20,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/3469\/revisions"}],"predecessor-version":[{"id":4628,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/3469\/revisions\/4628"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/3479"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=3469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=3469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=3469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}