{"id":102049,"date":"2024-05-24T20:39:33","date_gmt":"2024-05-24T20:39:33","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=102049"},"modified":"2025-06-27T15:35:21","modified_gmt":"2025-06-27T15:35:21","slug":"how-to-create-kubernetes-deployments-and-services-using-yaml-files-and-kubectl","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/devops\/containers-and-virtualization\/how-to-create-kubernetes-deployments-and-services-using-yaml-files-and-kubectl\/","title":{"rendered":"How to Create Kubernetes Deployments and Services using YAML files and Kubectl"},"content":{"rendered":"<p>This article is about how to create Kubernetes Deployments and Services using YAML files and Kubectl. You will also learn how to containerize and deploy any containerized application to Kubernetes. We will create a basic React.js application, containerize it using Docker, and then deploy it to Kubernetes using the Kubernetes Deployment YAML file. We will also create a Kubernetes Service YAML file to expose the application pods via an external IP address. You can then input the external IP into your browser to access the deployed Kubernetes application.<\/p>\n<p>Additionally, we will use Kubectl to execute the YAML files. The Kubectl commands will deploy the Kubernetes Deployment and Service to the Kubernetes Cluster.<\/p>\n<h2>Prerequisites<\/h2>\n<p>To follow along easily with this article, you need to:<\/p>\n<ul>\n<li>Install <a href=\"https:\/\/docs.docker.com\/engine\/install\/\">Docker Engine<\/a>: We will use Docker Engine to build the Docker Image and container for the basic React.js application in this article.<\/li>\n<li>Know how to create Docker containers: You can go through the official <a href=\"https:\/\/docs.docker.com\/\">Docker documentation<\/a> for a refresher<\/li>\n<li>Install <a href=\"https:\/\/minikube.sigs.k8s.io\/docs\/start\/\">Minikube<\/a> locally: Minikube will be our local Kubernetes Cluster.<\/li>\n<li>Install <a href=\"https:\/\/kubernetes.io\/docs\/tasks\/tools\/install-kubectl\/\">Kubectl<\/a>: Command line interface tool for Kubernetes.<\/li>\n<\/ul>\n<h2>What are Kubernetes Deployments and Services?<\/h2>\n<p>Kubernetes Deployments and Services are key Kubernetes Objects that are created within a Kubernetes Cluster. They are utilized for deploying containerized applications. The most common way of defining Kubernetes Deployments and Services files is in YAML format. A YAML file encapsulating all necessary configurations for the application.<\/p>\n<p>Kubernetes interprets these YAML files, executes the specifications in the files, and deploys the Kubernetes application onto the cluster. In the process, it ensures the Kubernetes application meets the desired state. During the deployment process, you will also need Kubectl.<\/p>\n<h2>What is Kubectl?<\/h2>\n<p>Kubectl is a command-line interface tool specifically designed to manage Kubernetes clusters. In this article, we will use Kubectl commands to execute the YAML files. In doing so, we will effectively deploy the Kubernetes Objects onto the Kubernetes Cluster.<\/p>\n<p>You also need to know how to use the Kubectl command line interface tool for Kubernetes. We will use the Kubectl tool to execute the YAML files and deploy the Kubernetes Deployment and Service to Kubernetes Cluster to Kubernetes Cluster.<\/p>\n<p>Now that you understand our article&#8217;s theoretical part let&#8217;s start implementing it practically. Before we create the YAML files for both the Kubernetes Deployment and Kubernetes Service, the first step is to create a basic React.js application.<\/p>\n<h2>Setting up a Basic React.js Application<\/h2>\n<p>To set up a basic React.js application, run the following <code>npx<\/code> command below:<\/p>\n<pre class=\"lang:none theme:none\">npx create-react-app basic-app<\/pre>\n<p>This command will create a directory named <code>basic-app<\/code>, generating all necessary files for a functional React.js application. To launch the application, navigate into the <code>basic-app<\/code> directory and execute the following <code>npm<\/code> command:<\/p>\n<pre class=\"lang:none theme:none\">npm start<\/pre>\n<p>Executing this<code> npm<\/code> command will initiate the application, making it accessible at <code>http:\/\/localhost:3000\/:<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"297\" class=\"wp-image-102050\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/04\/word-image-102049-1.png\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/04\/word-image-102049-1.png 710w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/04\/word-image-102049-1-300x125.png 300w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/p>\n<p>You can enter this URL into your browser to view the React.js application.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1380\" height=\"731\" class=\"wp-image-102051\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/04\/word-image-102049-2.png\" \/><\/p>\n<p>The React.js application is now up and running. Now that we have our React.js application operational, the next step is to containerize it using Docker.<\/p>\n<h2>Containerizing the Basic React.js Application using Docker<\/h2>\n<p>To containerize the basic React.js application using Docker, we will first build a Docker image using a Dockerfile. In the <code>basic-app<\/code> directory, create a <code>Dockerfile<\/code>. After creating the file, add the following instructions:<\/p>\n<pre class=\"lang:none theme:none\">FROM node:21-alpine\nWORKDIR \/basic-app\nCOPY package.json .\nCOPY package-lock.json .\nRUN npm install\nCOPY . .\nEXPOSE 3000\nCMD [\"npm\", \"start\"]<\/pre>\n<p>Docker will use these instructions within the Dockerfile to build a Docker image for our basic React.js application. To now build the Docker image, run the following Docker command:<\/p>\n<pre class=\"lang:none theme:none\">docker build -t &lt;image-name&gt;\/&lt;tag-name&gt; .<\/pre>\n<p><em>NOTE: When naming the Docker image, use the same name as your Docker hub account username. It will make it easier to push your Docker image to your Docker hub repository and subsequently deploy the containerized application to the Kubernetes cluster.<\/em><\/p>\n<h3>Initiating a Docker Container<\/h3>\n<p>To launch a Docker container for the React.js application, run the Docker image with the following <code>docker<\/code> command:<\/p>\n<pre class=\"lang:none theme:none\">docker run -p 3000:3000 &lt;image-name&gt;\/&lt;tag-name&gt;<\/pre>\n<p>This command will initiate and launch the React.js application within a Docker container. You can access the containerized React.js application by entering http:\/\/localhost:3000\/ into your browser.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1378\" height=\"735\" class=\"wp-image-102052\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/04\/word-image-102049-3.png\" \/><\/p>\n<h3>Pushing the Docker Image to Docker Hub<\/h3>\n<p>To push the Docker image to Docker Hub, ensure you&#8217;ve set up a Docker Hub account and then create a Docker Hub repository. When creating the Docker hub repository, use this naming convention: <code>&lt;image-name&gt;\/&lt;tag-name&gt;<\/code><\/p>\n<p>After completing the process, run the following commands one after the other in your terminal:<\/p>\n<pre class=\"lang:none theme:none\">docker login\n\ndocker push &lt;image-name&gt;\/&lt;tag-name&gt;<\/pre>\n<p>This process of pushing your Docker image to Docker Hub holds significant importance since we will specify the Docker Hub repository for our image in the Kubernetes Deployment YAML file. Kubernetes will then pull the specified Docker image from this Docker Hub repository and create a containerized application inside the Kubernetes Cluster.<\/p>\n<p>The next step involves launching the Minikube Kubernetes Cluster. Inside Minikube, we will deploy the Kubernetes Deployments and Services.<\/p>\n<h2>Launching the Minikube Kubernetes Cluster<\/h2>\n<p>To launch the Minikube Kubernetes Cluster, execute the following commands sequentially:<\/p>\n<pre class=\"lang:none theme:none \">minikube config set driver docker\n\nminikube start<\/pre>\n<p>Executing these commands will activate and launch a Minikube cluster on your local machine, as depicted in the image below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1308\" height=\"381\" class=\"wp-image-102053\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/04\/word-image-102049-4.png\" \/><\/p>\n<p>Next, ensure Kubectl is operational by executing the following command:<\/p>\n<pre class=\"lang:none theme:none\">kubectl --version<\/pre>\n<p><em>NOTE: If Kubectl is not operational, go through the official Kubectl documentation <\/em><a href=\"https:\/\/kubernetes.io\/docs\/tasks\/tools\/\"><em>here<\/em><\/a><em> and install it on your specific operating system.\u00a0<\/em><\/p>\n<p>After successful execution, everything should be configured and operational. The next step involves creating the YAML file for the Kubernetes Deployment and Service. Let&#8217;s proceed by creating the YAML file for the Kubernetes Deployment.<\/p>\n<h2>Creating the YAML file for the Kubernetes Deployment<\/h2>\n<p>In the <code>basic-app <\/code>directory, create a <code>&lt;file-name&gt;.yaml<\/code> file and add the following YAML code:<\/p>\n<pre class=\"lang:none theme:none\">apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n\u00a0\u00a0name: &lt;deployment-name&gt;\nspec:\n\u00a0\u00a0replicas: 2\n\u00a0\u00a0selector:\n\u00a0 \u00a0\u00a0matchLabels:\n\u00a0 \u00a0 \u00a0\u00a0app: &lt;app-name&gt;\n\u00a0\u00a0template:\n\u00a0 \u00a0\u00a0metadata:\n\u00a0 \u00a0 \u00a0\u00a0labels:\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0app: &lt;app-name&gt;\n\u00a0 \u00a0\u00a0spec:\n\u00a0 \u00a0 \u00a0\u00a0containers:\n\u00a0 \u00a0 \u00a0\u00a0- name: &lt;container-name&gt;\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0image: &lt;image-name&gt;\/&lt;tag-name&gt;\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0resources:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0limits:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0memory: \"256Mi\"\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0cpu: \"500m\"\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0ports:\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0- containerPort: 3000<\/pre>\n<p>This YAML file is designed to create a Kubernetes Deployment named <code>&lt;deployment-name&gt;<\/code>. It will generate two replicas or pods within the Kubernetes cluster, with the application running on both pods. The file will pull <code>the &lt;image-name&gt;\/&lt;tag-name&gt;<\/code> image from the Docker Hub repository and create a containerized application named <code>&lt;container-name&gt;<\/code> in the Kubernetes Cluster. Additionally, it configures resource limits for the running pods, with the container running on port <code>3000.<\/code><\/p>\n<p>To create this Kubernetes Deployment, execute this subsequent <code>kubectl<\/code> command:<\/p>\n<pre class=\"lang:none theme:none\">kubectl apply -f deployment.yaml<\/pre>\n<p>After creating the Kubernetes Deployment, the next step is to create the YAML file for the Kubernetes Service.<\/p>\n<h2>Creating the YAML file for the Kubernetes Service<\/h2>\n<p>In the <code>basic-app<\/code> directory, create a <code>&lt;file-name&gt;.yaml<\/code> file and add the following YAML code:<\/p>\n<pre class=\"lang:none theme:none\">apiVersion: v1\nkind: Service\nmetadata:\n\u00a0\u00a0name: &lt;service-name&gt;\nspec:\n\u00a0\u00a0type: LoadBalancer\n\u00a0\u00a0selector:\n\u00a0 \u00a0\u00a0app: &lt;app-name&gt;\n\u00a0\u00a0ports:\n\u00a0\u00a0- port: 4000\n\u00a0 \u00a0\u00a0targetPort: 4000\n\u00a0 \u00a0\u00a0protocol: TCP<\/pre>\n<p>This YAML file is structured to create a Kubernetes Service named <code>&lt;service-name&gt;.<\/code> This Kubernetes Service will function as a <code>LoadBalancer<\/code>for the operational pods. Kubernetes will allocate an external IP address to the application pods. You can then input the external IP into your browser to access the deployed React.js Kubernetes application.<\/p>\n<p>The next step is to get information about the Kubernetes Deployment,t, and Service<\/p>\n<h2>Getting Information about the Kubernetes Deployment and Service<\/h2>\n<p>To get the information about the Kubernetes Deployment, execute the following command:<\/p>\n<pre class=\"lang:none theme:none\">kubectl get deployment<\/pre>\n<p>To get the information about the Kubernetes Service, execute the following command:<\/p>\n<pre class=\"lang:none theme:none\">kubectl get service<\/pre>\n<p>The next step is to access the deployed React.js Kubernetes application.<\/p>\n<h2>Accessing the Deployed React.js Kubernetes Application<\/h2>\n<p>To access the deployed React.js Kubernetes application, run the following command in your terminal:<\/p>\n<pre class=\"lang:none theme:none\">minikube service &lt;service-name&gt;<\/pre>\n<p>Minikube will establish a tunnel, assigning a unique external IP address to the pods, as illustrated below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1130\" height=\"362\" class=\"wp-image-102054\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/04\/word-image-102049-5.png\" \/><\/p>\n<p>Enter the unique external IP address into your browser&#8217;s address bar to access the application:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1378\" height=\"674\" class=\"wp-image-102055\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/04\/word-image-102049-6.png\" \/><\/p>\n<p>Our React.js Kubernetes application is now operational inside the Minikube Kubernetes Cluster. We can also access the Kubernetes Deployment through the `&lt;service-name&gt;` Kubernetes Service. We have now successfully created Kubernetes Deployments and Services using YAML files and Kubectl.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, you have learned how to create Kubernetes Deployments and Services using YAML files and Kubectl. These YAML files are crucial blueprints for creating all Kubernetes objects and application resources in a Kubernetes Cluster.<\/p>\n<p>Regardless of the complexity of your application, you will always require YAML files to create Kubernetes Deployments and Services. I hope this article helps you understand the process of creating Kubernetes Deployments and Services using YAML files and Kubectl. That&#8217;s it for the article. Happy Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is about how to create Kubernetes Deployments and Services using YAML files and Kubectl. You will also learn how to containerize and deploy any containerized application to Kubernetes. We will create a basic React.js application, containerize it using Docker, and then deploy it to Kubernetes using the Kubernetes Deployment YAML file. We will&#8230;&hellip;<\/p>\n","protected":false},"author":342511,"featured_media":102050,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143513,53],"tags":[159074],"coauthors":[159023],"class_list":["post-102049","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-containers-and-virtualization","category-featured","tag-kubernetes"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/102049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/342511"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=102049"}],"version-history":[{"count":4,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/102049\/revisions"}],"predecessor-version":[{"id":107317,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/102049\/revisions\/107317"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media\/102050"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=102049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=102049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=102049"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=102049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}