{"id":901,"date":"2023-08-18T00:17:41","date_gmt":"2023-08-17T18:47:41","guid":{"rendered":"https:\/\/www.openwriteup.com\/?page_id=901"},"modified":"2023-08-21T20:37:38","modified_gmt":"2023-08-21T15:07:38","slug":"lab-podsinitcontainers","status":"publish","type":"page","link":"https:\/\/www.openwriteup.com\/?page_id=901","title":{"rendered":"Lab: Pods,InitContainers"},"content":{"rendered":"<p>In case you haven&#8217;t cloned the repo<\/p>\n<pre> git clone https:\/\/github.com\/amitopenwriteup\/k8s.git<\/pre>\n<p>Lab1: Create a simple pod<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/expod.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-903\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/expod-300x166.jpg\" alt=\"\" width=\"300\" height=\"166\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/expod-300x166.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/expod.jpg 505w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<ul>\n<li><code>apiVersion<\/code>: Specifies the API version being used (in this case, v1).<\/li>\n<li><code>kind<\/code>: Specifies the kind of resource, which is a Pod in this case.<\/li>\n<li><code>metadata<\/code>: Contains metadata about the Pod, such as its name and labels.<\/li>\n<li><code>spec<\/code>: Defines the specification for the Pod.\n<ul>\n<li><code>containers<\/code>: A list of containers within the Pod.\n<ul>\n<li><code>name<\/code>: Name of the container.<\/li>\n<li><code>image<\/code>: The Docker image to use for the container.<\/li>\n<li><code>ports<\/code>: Specifies the ports that the container exposes.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>cd k8s\r\nkubectl create -f expod.yaml\r\nkubectl get pod\r\nkubectl describe pod example-pod\r\nkubectl delete pod example-pod<\/pre>\n<p>Lab2: Create pod in dev namespace<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/devpod.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-907\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/devpod-300x72.jpg\" alt=\"\" width=\"300\" height=\"72\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/devpod-300x72.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/devpod-700x169.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/devpod-768x185.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/devpod.jpg 1001w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<ul>\n<li><code>namespace<\/code>: Specifies the namespace where the Pod will be created (in this case, &#8220;dev&#8221;).<\/li>\n<li><code>spec<\/code>: Defines the specification for the Pod<code><\/code>\n<ul>\n<li><code>command<\/code>: Specifies the command to run in the container. In this example, it&#8217;s set to [&#8220;sleep&#8221;, &#8220;3600&#8221;] to sleep for 3600 seconds<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>cd k8s\r\ncat devpod.yaml\r\nkubectl create ns dev\r\nkubectl create -f devpod.yaml\r\nkubectl get pods --namespace=dev\r\nkubectl describe pod busybox-pod --namespace=dev\r\nkubectl delete pod busybox-pod --namespace=dev<\/pre>\n<p>Lab3: Multicontainer pods<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/multi.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-910\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/multi-300x76.jpg\" alt=\"\" width=\"426\" height=\"108\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/multi-300x76.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/multi-700x177.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/multi-768x194.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/multi.jpg 1172w\" sizes=\"auto, (max-width: 426px) 100vw, 426px\" \/><\/a><\/p>\n<p>Pod named &#8220;multi-container-pod&#8221; contains two containers: &#8220;main-container&#8221; running the Nginx image and &#8220;sidecar-container&#8221; running the BusyBox image. The main container exposes port 80, and the sidecar container runs a simple loop to continuously print a message.<\/p>\n<pre>cd k8s\r\nkubectl create -f multicont.yaml\r\nkubectl get pods\r\nkubectl describe pod multi-container-pod\r\nkubectl exec multi-container-pod -c main-container -- date\r\nkubectl exec multi-container-pod -c sidecar-container -- ls\r\nkubectl delete pod multi-container-pod<\/pre>\n<p>Lab 4: Init Containers<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/init.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-912\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/init-300x114.jpg\" alt=\"\" width=\"300\" height=\"114\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/init-300x114.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/init-700x265.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/init-768x291.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/init.jpg 1168w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>A list of init containers that run before the main containers.<\/p>\n<pre>cd k8s\r\nkubectl create -f initpod.yaml\r\nkubectl describe pod pod-with-init\r\nkubectl delete pod pod-with-init<\/pre>\n<p><strong>Try following Lab5:<\/strong><\/p>\n<pre><span style=\"color: #ff0000;\">Change the init container image name\u00a0 to busbox:lates, in above yaml.\u00a0<\/span>\r\n\r\n<span style=\"color: #ff0000;\">Create pod, and check whether main container starts or not<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In case you haven&#8217;t cloned the repo git clone https:\/\/github.com\/amitopenwriteup\/k8s.git Lab1: Create a simple pod apiVersion: Specifies the API version being used (in this case, v1). kind: Specifies the kind of resource, which is a Pod in this case. metadata: Contains metadata about the Pod, such as its name and labels. spec: Defines the specification [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-901","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=901"}],"version-history":[{"count":8,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/901\/revisions"}],"predecessor-version":[{"id":974,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/901\/revisions\/974"}],"wp:attachment":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}