{"id":850,"date":"2022-04-08T12:02:39","date_gmt":"2022-04-08T12:02:39","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=850"},"modified":"2022-04-08T12:02:39","modified_gmt":"2022-04-08T12:02:39","slug":"create-kubernetes-yaml","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=850","title":{"rendered":"How To Create Kubernetes YAML Manifests Quickly"},"content":{"rendered":"<p>In this blog, you will learn <strong>easy methods to create Kubernetes YAML<\/strong> manifest quickly for testing and deploying applications on Kubernetes. You can also these use methods in the <a href=\"https:\/\/devopscube.com\/best-kubernetes-certifications\/\">Kubernetes certification<\/a> exam.<\/p>\n<p>One common thing while working with kubernetes is that we tend to search for Kubernetes YAML files whenever we want to deploy a test pod, deployment, or any other object. Who wants to write every line of a YAML file, right?<\/p>\n<p>Let&#8217;s look at some of the Kubernetes tips to make the YAML creation process easy.<\/p>\n<h2 id=\"yaml-autogeneration-using-kubernetes-extention\">YAML Autogeneration using Kubernetes Extention<\/h2>\n<p>One of the easiest ways to create Kubernetes YAML is using the visual studio kubernetes extension.<\/p>\n<p>Install the <a href=\"https:\/\/code.visualstudio.com\/docs\/azure\/kubernetes?ref=devopscube.com\" rel=\"noreferrer noopener\">Kubernetes VS code extension<\/a>, and it will help develop k8s manifests for most kubernetes objects. It also supports deploying apps to local and remote k8s clusters.<\/p>\n<p>All you have to do is, start typing the Object name and it will automatically populate the options for you. Then, based on your selection, it will autogenerate the basic YAML structure for you as shown n the following image.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/ks-extension-1.gif\" class=\"kg-image\" alt=\"Create Kubernetes YAML \" loading=\"lazy\" width=\"528\" height=\"691\"><\/figure>\n<p>This extension supports YAML generation of Pods, Deployment, Statefulset, Replicationset, Persistent Volumes (PV), Persistent Volume Claims (PVC), etc.<\/p>\n<h2 id=\"create-yaml-manifest-using-kubectl-dry-run\">Create YAML Manifest Using Kubectl Dry Run<\/h2>\n<p>You can create the manifests using the <code>kubectl<\/code> imperative commands. There is a flag called <code>--dry-run<\/code> that helps you create the entire manifest template.<\/p>\n<p>Also, you cannot create all the Kubernetes resource YAML using dry-run. For example, you cannot create a Statefulset or a persistent volume using dry-run.<\/p>\n<blockquote><p>Note: If you are preparing for Kubernetes certifications like <strong>CKA, CKAD, or CKS<\/strong>, imperative commands come in handy during the exam.<\/p><\/blockquote>\n<h2 id=\"kubectl-yaml-dry-run-examples\">Kubectl YAML Dry Run Examples<\/h2>\n<p>Let&#8217;s look at the examples to generate YAML using a dry run and write it to an output file.<\/p>\n<h3 id=\"create-pod-yaml\">Create Pod YAML<\/h3>\n<p>Create a pod YAML named <code>myapp<\/code> which uses image <code>nginx:latest<\/code>.<\/p>\n<pre><code>kubectl run mypod --image=nginx:latest \\\n            --labels type=web \\\n            --dry-run=client -o yaml &gt; mypod.yaml<\/code><\/pre>\n<h3 id=\"create-a-pod-service-yaml\">Create a Pod service YAML<\/h3>\n<p>Generate YAML for a Pod Service that exposes a NodePort. This will only work if you have a running pod.<\/p>\n<pre><code>kubectl expose pod mypod \\\n    --port=80 \\\n    --name mypod-service \\\n    --type=NodePort \\\n    --dry-run=client -o yaml &gt; mypod-service.yaml<\/code><\/pre>\n<h3 id=\"create-nodeport-service-yaml\">Create NodePort Service YAML<\/h3>\n<p>Create  a service type <code>nodeport<\/code> with port <code>30001<\/code> with service to pod TCP port mapping on port <code>80<\/code>.<\/p>\n<pre><code>kubectl create service nodeport mypod \\\n    --tcp=80:80 \\\n    --node-port=30001 \\\n    --dry-run=client -o yaml &gt; mypod-service.yaml<\/code><\/pre>\n<h3 id=\"create-deployment-yaml\">Create Deployment YAML<\/h3>\n<p>Create a deployment named <code>mydeployment<\/code> with image <code>Nginx<\/code><\/p>\n<pre><code>kubectl create deployment mydeployment \\\n    --image=nginx:latest \\\n    --dry-run=client -o yaml &gt; mydeployment.yaml<\/code><\/pre>\n<h3 id=\"create-deployment-service-yaml\">Create Deployment Service YAML<\/h3>\n<p>Create a NodePort service YAML for deployment <code>mydeployment<\/code> with service port <code>8080<\/code><\/p>\n<pre><code>kubectl expose deployment mydeployment \\\n    --type=NodePort \\\n    --port=8080 \\\n    --name=mydeployment-service \\\n    --dry-run=client -o yaml &gt; mydeployment-service.yaml<\/code><\/pre>\n<h3 id=\"create-job-yaml\">Create Job YAML<\/h3>\n<p>Crate job named <code>myjob<\/code> with <code>nginx<\/code> image.<\/p>\n<pre><code>kubectl create job myjob \\\n    --image=nginx:latest \\\n    --dry-run=client -o yaml<\/code><\/pre>\n<h3 id=\"create-cronjob-yaml\">Create Cronjob YAML<\/h3>\n<p>Create a cronjob named <code>mycronjob<\/code> with <code>nginx<\/code> image and a corn schedule.<\/p>\n<pre><code>kubectl create cj mycronjob \\\n    --image=nginx:latest \\\n    --schedule=\"* * * * *\" \\\n    --dry-run=client -o yaml<\/code><\/pre>\n<p>I have given generic YAML examples. You can further change parameters and use them as per your requirements.<\/p>\n<h3 id=\"kubectl-dry-run-alias\">Kubectl &amp; Dry Run Alias<\/h3>\n<p>To make things fast, you can set up an alias in <code>~\/.bashrc<\/code> or <code>~\/.zshrc<\/code> for <code>kubectl<\/code> command as follows. So that you don&#8217;t have to type <code>kubectl<\/code> every time.<\/p>\n<pre><code>alias k=kubectl<\/code><\/pre>\n<p>You can also set up an alias for a kubectl dry run parameters as follows.<\/p>\n<pre><code>alias kdr='kubectl --dry-run=client -o yaml'<\/code><\/pre>\n<p>You can execute the command as follows.<\/p>\n<pre><code>kdr run web --image=nginx:latest &gt; nginx.yaml<\/code><\/pre>\n<p>Also, you can check out my video where I explain I explain the whole YAML creation process with a demo.<\/p>\n<figure class=\"kg-card kg-embed-card\"><iframe loading=\"lazy\" width=\"160\" height=\"90\" src=\"https:\/\/www.youtube.com\/embed\/1I4xqhN6t1A?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen=\"\"><\/iframe><\/figure>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this blog, we have seen <strong>methods to create Kubernetes YAML quickly<\/strong>. Moreover, the imperative commands are very helpful in the kubernetes Certification exams.<\/p>\n<p>If you are preparing for Kubernetes certification, you can check out the <a href=\"https:\/\/devopscube.com\/kubernetes-certification-coupon\/\" rel=\"noreferrer noopener\">Kubernetes certification coupons<\/a> to save money on exam registration.<\/p>\n<p>Also, if you are learning kubernetes, you can check out my <a href=\"https:\/\/devopscube.com\/kubernetes-tutorials-beginners\/\" rel=\"noreferrer noopener\">30+ Kubernetes tutorials for beginners.<\/a><\/p>\n<p>If you have any tips, share it with the DevOps community by dropping a comment below.<\/p>\n<p>I have also created a demo video on this tutorial.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/create-kubernetes-yaml\/\" target=\"_blank\" rel=\"noopener noreferrer\">How To Create Kubernetes YAML Manifests Quickly \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/create-kubernetes-yaml\/<\/p>\n","protected":false},"author":1,"featured_media":851,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-850","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=850"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/850\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/851"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}