{"id":6272,"date":"2019-07-26T18:22:34","date_gmt":"2019-07-26T18:22:34","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=6272"},"modified":"2021-11-16T11:13:52","modified_gmt":"2021-11-16T11:13:52","slug":"working-with-kubernetes-cluster-using-kubectl-part-2","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/working-with-kubernetes-cluster-using-kubectl-part-2\/","title":{"rendered":"Kubernetes Commands: kubectl run &#8211; Tutorials and Examples"},"content":{"rendered":"<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\">#Imperatively working with your cluster. Run will \"generate\" a Deployment by default.<\/span>\n<span class=\"hljs-comment\">#This is pulling a specified image from Google's container registry.<\/span>\n<span class=\"hljs-comment\">#kubectl run, will convert a pod creation into a \"Deployment generation\"<\/span>\n<span class=\"hljs-comment\">#http:\/\/kubernetes.io\/docs\/user-guide\/kubectl-conventions\/#generators <\/span>\nkubectl run hello-world --image=gcr.io\/google-samples\/hello-app:<span class=\"hljs-number\">1.0<\/span>\n\n<span class=\"hljs-comment\">#But let's deploy a single pod too...<\/span>\nkubectl run hello-world-pod --image=gcr.io\/google-samples\/hello-app:<span class=\"hljs-number\">1.0<\/span> --generator=run-pod\/v1\n\n<span class=\"hljs-comment\">#Let's follow our pod and deployment status<\/span>\nkubectl get pods\nkubectl get deployment\nkubectl get pods -o wide\n\n<span class=\"hljs-comment\">#Remember, k8s is a container orchestrator and it's starting up containers on Nodes.<\/span>\n<span class=\"hljs-comment\">#Open a second terminal and ssh into the node that hello-world pod is running on.<\/span>\nssh aen@c1-node1\nsudo docker ps\n<span class=\"hljs-keyword\">exit<\/span>\n\n<span class=\"hljs-comment\">#Back on the Master, we can pull the logs from the container. Which is going to be anything written to stdout. <\/span>\n<span class=\"hljs-comment\">#Maybe something went wrong inside our app, and our pod won't start. This is useful for troubleshooting.<\/span>\nkubectl logs hello-world-pod\n\n<span class=\"hljs-comment\">#Starting a process inside a container inside a pod.<\/span>\n<span class=\"hljs-comment\">#We can use this to launch any process as long as the executable\/binary is in the container.<\/span>\n<span class=\"hljs-comment\">#Launch a shell into the container. Callout that this is on the *pod* network.<\/span>\nkubectl exec -it hello-world-pod  -- \/bin\/sh\nhostname\nip addr\n<span class=\"hljs-keyword\">exit<\/span>\n\n<span class=\"hljs-comment\">#Remember that first kubectl run we executed, it created a Deployment for us.<\/span>\n<span class=\"hljs-comment\">#Let's look more closely at the deployment<\/span>\n<span class=\"hljs-comment\">#Deployments are made of ReplicaSets!<\/span>\nkubectl get deployment hello-world\nkubectl get replicaset\nkubectl get pods\n\n<span class=\"hljs-comment\">#Let's take a closer look at our pod.<\/span>\n<span class=\"hljs-comment\">#Walk through the pods Events...<\/span>\n<span class=\"hljs-comment\">#Name, Containers, Ports, Conditions, and Events. <\/span>\n<span class=\"hljs-comment\">#Deployments are made of ReplicaSets!<\/span>\nkubectl describe deployment hello-world | more\n\n<span class=\"hljs-comment\">#Let's see what describe can tell us about a deployed Pod.<\/span>\n<span class=\"hljs-comment\">#Check out the Name, Node, Status, Containers, and events.<\/span>\nkubectl get pods\nkubectl describe pods $PASTEPODNAMEHERE | more\n\n<span class=\"hljs-comment\">#Expose the Deployment as a Serivce.<\/span>\n<span class=\"hljs-comment\">#This will create a Service for the ReplicaSet behind the Deployment<\/span>\n<span class=\"hljs-comment\">#We are exposing our serivce on port 80, connecting to an application running on 8080 in our pod.<\/span>\n<span class=\"hljs-comment\">#Port: Interal Cluster Port, the Service's port. You will point cluster resources here.<\/span>\n<span class=\"hljs-comment\">#TargetPort: The Pod's Serivce Port, your application. That one we defined when we started the pods.<\/span>\nkubectl expose deployment hello-world --port=<span class=\"hljs-number\">80<\/span> --target-port=<span class=\"hljs-number\">8080<\/span>\n\n<span class=\"hljs-comment\">#Check out the IP: and Port:, that's where we'll access this service.<\/span>\nkubectl get service hello-world\n\n<span class=\"hljs-comment\">#We can also get that information from using get<\/span>\nkubectl describe service hello-world\n\n<span class=\"hljs-comment\">#Access the service inside the cluster<\/span>\ncurl http:<span class=\"hljs-comment\">\/\/$SERVCIEIP:$PORT<\/span>\n\n<span class=\"hljs-comment\">#Access the pod's application directly, useful for troubleshooting.<\/span>\nkubectl get endpoints hello-world\ncurl http:<span class=\"hljs-comment\">\/\/$ENDPOINT:$TARGETORT<\/span>\n\n<span class=\"hljs-comment\">#Using kubectl to generate yaml or json files of our imperitive configuration.<\/span>\nkubectl get service hello-world -o yaml\nkubectl get service hello-world -o json\n\n<span class=\"hljs-comment\">#Exported resources are stripped of cluster-specific information.<\/span>\nkubectl get service hello-world -o yaml --export &gt; service-hello-world.yaml\nkubectl get deployment hello-world -o yaml --export &gt; deployment-hello-world.yaml\nls *.yaml\n\nmore service-hello-world.yaml\n\n<span class=\"hljs-comment\">#We can ask the API for more information about an object<\/span>\nkubectl explain service | more\n\n<span class=\"hljs-comment\">#And drill down further if needed, includes very good explanation of what's available for that resource<\/span>\nkubectl explain service.spec | more\nkubectl explain service.spec.ports\nkubectl explain service.spec.ports.targetPort\n\n<span class=\"hljs-comment\">#Let's remove everything we created imperitively and start over using a declarative model<\/span>\nkubectl delete service hello-world\nkubectl delete deployment hello-world\nkubectl delete pod hello-world-pod\nkubectl get all\n\n<span class=\"hljs-comment\">#Deploying applications declaratively<\/span>\n<span class=\"hljs-comment\">#we can use apply to create our resources from yaml.<\/span>\nkubectl apply -f deployment-hello-world.yaml\nkubectl apply -f service-hello-world.yaml \n\n<span class=\"hljs-comment\">#This re-creates everything we created, but in yaml<\/span>\nkubectl get all\n\n<span class=\"hljs-comment\">#scale up our deployment<\/span>\nvi deployment-hello-world.yaml\nChange replicas from <span class=\"hljs-number\">1<\/span> to <span class=\"hljs-number\">2<\/span>\n     replicas: <span class=\"hljs-number\">2<\/span>\n\n<span class=\"hljs-comment\">#update our configuration with apply<\/span>\nkubectl apply -f deployment-hello-world.yaml\n\n<span class=\"hljs-comment\">#And check the current configuration of our deployment<\/span>\nkubectl get deployment hello-world\n\n<span class=\"hljs-comment\">#Repeat the curl access to see the load balancing of the HTTP request<\/span>\nkubectl get service hello-world\ncurl http:<span class=\"hljs-comment\">\/\/$SERVICEIP:PORT<\/span>\n\n<span class=\"hljs-comment\">#We can edit the resources \"on the fly\" with kubectl edit. But this isn't reflected in our yaml. But is<\/span>\n<span class=\"hljs-comment\">#persisted in the etcd database...cluster store. Change 2 to 3.<\/span>\nkubectl edit deployment hello-world\n\n<span class=\"hljs-comment\">#Let's clean up our deployment and remove everything<\/span>\nkubectl delete deployment hello-world\nkubectl delete service hello-world\n\nkubectl get all\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<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_76864\"><iframe loading=\"lazy\"  id=\"_ytid_72679\"  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_76864\"  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":"","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-6272","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6272","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=6272"}],"version-history":[{"count":5,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6272\/revisions"}],"predecessor-version":[{"id":25483,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6272\/revisions\/25483"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=6272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=6272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=6272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}