{"id":6279,"date":"2019-07-26T18:32:14","date_gmt":"2019-07-26T18:32:14","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=6279"},"modified":"2021-11-16T11:12:43","modified_gmt":"2021-11-16T11:12:43","slug":"working-with-kubernetes-cluster-using-kubectl-part-5-anatomy-api-request","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/working-with-kubernetes-cluster-using-kubectl-part-5-anatomy-api-request\/","title":{"rendered":"Working with Kubernetes Pods &#038; Deployment"},"content":{"rendered":"\n<p><strong>deployment.yaml<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: hello-world\n  labels:\n    app: hello-world\nspec:\n  replicas: 4\n  selector:\n    matchLabels:\n      app: hello-world\n  template:\n    metadata:\n      labels:\n        app: hello-world\n    spec:\n      containers:\n      - name: hello-world\n        image: gcr.io\/google-samples\/hello-app:1.0\n        ports:\n        - containerPort: 8080\n\n\n<\/code><\/span><\/pre>\n\n\n<p><strong>pod.yaml<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: v1\nkind: Pod\nmetadata:\n  name: hello-world\nspec:\n  containers:\n  - name: hello-world\n    image: gcr.io\/google-samples\/hello-app:1.0<\/code><\/span><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#Anatomy of an API Request<\/span>\n<span class=\"hljs-comment\">#Creating a pod with YAML<\/span>\nkubectl apply -f pod.yaml\n\n<span class=\"hljs-comment\">#Get a list of our currently running Pods<\/span>\nkubectl get pod hello-world\n\n<span class=\"hljs-comment\">#We can use the -v option to increase the verbosity of our request.<\/span>\n<span class=\"hljs-comment\">#Display requested resource URL. Focus on VERB, API Path and Response code<\/span>\nkubectl get pod hello-world -v <span class=\"hljs-number\">6<\/span> \n\n<span class=\"hljs-comment\">#Same output as 6, add HTTP Request Headers. Focus on application type, and User-Agent<\/span>\nkubectl get pod hello-world -v <span class=\"hljs-number\">7<\/span> \n\n<span class=\"hljs-comment\">#Same output as 7, adds Response Headers and truncated Response Body.<\/span>\nkubectl get pod hello-world -v <span class=\"hljs-number\">8<\/span> \n\n<span class=\"hljs-comment\">#Same output as 8, add full Response. Focus on the bottom, look for metadata<\/span>\nkubectl get pod hello-world -v <span class=\"hljs-number\">9<\/span> \n\n<span class=\"hljs-comment\">#Start up a kubectl proxy session, this will authenticate use to the API Server<\/span>\n<span class=\"hljs-comment\">#Using our local kubeconfig for authentication and settings<\/span>\nkubectl proxy &amp;\ncurl http:<span class=\"hljs-comment\">\/\/localhost:8001\/api\/v1\/namespaces\/default\/pods\/hello-world | head -n 20<\/span>\n\nfg\nctrl+c\n\n<span class=\"hljs-comment\">#Watch, Exec and Log Requests<\/span>\n<span class=\"hljs-comment\">#A watch on Pods will watch on the resourceVersion on api\/v1\/namespaces\/default\/Pods<\/span>\nkubectl get pods --watch -v <span class=\"hljs-number\">6<\/span> &amp;\n\n<span class=\"hljs-comment\">#We can see kubectl keeps the TCP session open with the server...waiting for data.<\/span>\nnetstat -plant | grep kubectl\n\n<span class=\"hljs-comment\">#Delete the pod and we see the updates are written to our stdout. Watch stays, sicne we're watching All Pods in the default namespace.<\/span>\nkubectl delete pods hello-world\n\n<span class=\"hljs-comment\">#But let's bring our Pod back...because we have more demos.<\/span>\nkubectl apply -f pod.yaml\n\n<span class=\"hljs-comment\">#And kill off our watch<\/span>\nfg\nctrl+c\n\n<span class=\"hljs-comment\">#Accessing logs<\/span>\nkubectl logs hello-world\nkubectl logs hello-world -v <span class=\"hljs-number\">6<\/span>\n\n<span class=\"hljs-comment\">#Start kubectl proxy, we can access the resource URL directly.<\/span>\nkubectl proxy &amp;\ncurl http:<span class=\"hljs-comment\">\/\/localhost:8001\/api\/v1\/namespaces\/default\/pods\/hello-world\/log <\/span>\n\n<span class=\"hljs-comment\">#Kill our kubectl proxy, fg then ctrl+c<\/span>\nfg\nctrl+c\n\n<span class=\"hljs-comment\">#Authentication failure Demo<\/span>\ncp ~\/.kube\/config  ~\/.kube\/config.ORIG\n\n<span class=\"hljs-comment\">#Make an edit to our username changing user: kubernetes-admin to user: kubernetes-admin1<\/span>\nvi ~\/.kube\/config\n\n<span class=\"hljs-comment\">#Try to access our cluster, and we see GET on .\/api\/v1\/namespaces\/default\/pods?limit=500 returns a 403. Status, failure.<\/span>\nkubectl get pods -v <span class=\"hljs-number\">6<\/span>\n\n<span class=\"hljs-comment\">#Let's put our backup kubeconfig back<\/span>\ncp ~\/.kube\/config.ORIG ~\/.kube\/config\n\n<span class=\"hljs-comment\">#Test out access to the API Server<\/span>\nkubectl get pods \n\n<span class=\"hljs-comment\">#Missing resources, we can see the response code for this resources is 404...it's Not Found.<\/span>\nkubectl get pods nginx-pod -v <span class=\"hljs-number\">6<\/span>\n\n<span class=\"hljs-comment\">#Let's look at creating and deleting a deployment. We see a query for the existence of the deployment, then a 201 OK on the POST to create the deployment<\/span>\nkubectl apply -f deployment.yaml -v <span class=\"hljs-number\">6<\/span>\n\n<span class=\"hljs-comment\">#Get a list of the Deployments<\/span>\nkubectl get deployment \n\n<span class=\"hljs-comment\">#Clean up when we're finished. we see a DELETE 200OK and a GET 404.<\/span>\nkubectl delete deployment hello-world -v <span class=\"hljs-number\">6<\/span>\nkubectl delete pod hello-world\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-devops-devsecops-sre-dataops-aiops wp-block-embed-devops-devsecops-sre-dataops-aiops\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"v6wsvKLm6f\"><a href=\"https:\/\/www.bestdevops.com\/what-is-kubernetes-pods-with-example-commands\/\" target=\"_blank\" rel=\"noopener\">What is kubernetes pods with example commands<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;What is kubernetes pods with example commands&#8221; &#8212; DevOps - DevSecOps - SRE - DataOps - AIOps\" src=\"https:\/\/www.bestdevops.com\/what-is-kubernetes-pods-with-example-commands\/embed\/#?secret=v6wsvKLm6f\" data-secret=\"v6wsvKLm6f\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-devops-devsecops-sre-dataops-aiops wp-block-embed-devops-devsecops-sre-dataops-aiops\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"8QGxkS8B5N\"><a href=\"https:\/\/www.bestdevops.com\/how-to-use-troubleshoot-kubernetes-pods\/\" target=\"_blank\" rel=\"noopener\">How to use &#038; troubleshoot Kubernetes pods?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to use &#038; troubleshoot Kubernetes pods?&#8221; &#8212; DevOps - DevSecOps - SRE - DataOps - AIOps\" src=\"https:\/\/www.bestdevops.com\/how-to-use-troubleshoot-kubernetes-pods\/embed\/#?secret=8QGxkS8B5N\" data-secret=\"8QGxkS8B5N\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_69373\"><iframe loading=\"lazy\"  id=\"_ytid_44601\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_69373\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>deployment.yaml pod.yaml<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4859],"tags":[],"class_list":["post-6279","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6279","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=6279"}],"version-history":[{"count":5,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6279\/revisions"}],"predecessor-version":[{"id":25479,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6279\/revisions\/25479"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=6279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=6279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=6279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}