{"id":35565,"date":"2023-05-23T06:19:55","date_gmt":"2023-05-23T06:19:55","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=35565"},"modified":"2025-01-23T12:45:17","modified_gmt":"2025-01-23T12:45:17","slug":"how-to-get-port-of-a-pod-in-kubernetes","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-get-port-of-a-pod-in-kubernetes\/","title":{"rendered":"How to get port of a pod in Kubernetes?"},"content":{"rendered":"\n<p>To get the port of a pod in Kubernetes, you can use the <code>kubectl<\/code> command along with the <code>get<\/code> and <code>describe<\/code> options. <\/p>\n\n\n\n<p>Here are the steps:<\/p>\n\n\n\n<p>1. List all the pods in the cluster to find the specific pod you&#8217;re interested in:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">kubectl <span class=\"hljs-keyword\">get<\/span> pods\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>2. Identify the name of the pod from the list of pods.<\/p>\n\n\n\n<p>3. Retrieve detailed information about the pod, including the container ports:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">kubectl describe pod <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">pod-name<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><\/p>\n\n\n\n<p>4. Look for the &#8220;Containers&#8221; section in the output. Under each container, you will find the &#8220;Ports&#8221; field, which lists the container ports.<\/p>\n\n\n\n<p>Alternatively, you can use the <code>kubectl get<\/code> command with the <code>-o<\/code> or <code>--output<\/code> flag to format the output and display only the port information for the pod:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">kubectl <span class=\"hljs-keyword\">get<\/span> pod &lt;pod-name&gt; -o jsonpath='{.spec.containers&#91;*].ports&#91;*].containerPort}<span class=\"hljs-string\">'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><\/p>\n\n\n\n<p>This command uses <code>jsonpath<\/code> to extract the container port(s) from the pod&#8217;s specification.<\/p>\n\n\n\n<p>Replace <code>&lt;pod-name&gt;<\/code> with the name of the pod you want to get the port for.<\/p>\n\n\n\n<p>By following these steps, you can retrieve the port(s) associated with a specific pod in Kubernetes.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To get the port of a pod in Kubernetes, you can use the kubectl command along with the get and describe options. Here are the steps: 1. List all the&#8230; <\/p>\n","protected":false},"author":48,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4859],"tags":[],"class_list":["post-35565","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/35565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=35565"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/35565\/revisions"}],"predecessor-version":[{"id":35568,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/35565\/revisions\/35568"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=35565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=35565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=35565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}