{"id":207,"date":"2023-10-18T06:58:44","date_gmt":"2023-10-18T05:58:44","guid":{"rendered":"https:\/\/technology.corporatesstore.com\/?page_id=207"},"modified":"2024-09-02T13:31:22","modified_gmt":"2024-09-02T12:31:22","slug":"kubernetes","status":"publish","type":"page","link":"https:\/\/technology.corporatesstore.com\/index.php\/kubernetes\/","title":{"rendered":"Kubernetes"},"content":{"rendered":"\n<p class=\"has-text-align-center has-cyan-bluish-gray-background-color has-background has-medium-font-size\">Overview<\/p>\n\n\n\n<p>Kubernetes is an orchestration framework for Docker containers which helps expose containers as services to the outside world. For example, you can have two services \u2212 One service would contain&nbsp;<strong>nginx<\/strong>&nbsp;and&nbsp;<strong>mongoDB<\/strong>, and another service would contain&nbsp;<strong>nginx<\/strong>&nbsp;and&nbsp;<strong>redis<\/strong>. Each service can have an IP or service point which can be connected by other applications. Kubernetes is then used to manage these services.<\/p>\n\n\n\n<p class=\"has-text-align-center has-cyan-bluish-gray-background-color has-background has-medium-font-size\">Architecture<\/p>\n\n\n\n<p>The following diagram shows in a simplistic format how Kubernetes works from an architecture point of view.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/docker\/images\/kubernetes_architecture.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>The&nbsp;<strong>minion<\/strong>&nbsp;is the node on which all the services run. You can have many minions running at one point in time. Each minion will host one or more POD. Each&nbsp;<strong>POD<\/strong>&nbsp;is like hosting a service. Each POD then contains the Docker containers. Each POD can host a different set of Docker containers. The proxy is then used to control the exposing of these services to the outside world.<\/p>\n\n\n\n<p>Kubernetes has several components in its architecture. The role of each component is explained below:<\/p>\n\n\n\n<p><strong>etcd <\/strong>\u2212 This component is a highly available key-value store that is used for storing shared configuration and service discovery. Here the various applications will be able to connect to the services via the discovery service.<br><strong>Flannel <\/strong>\u2212 This is a backend network which is required for the containers.<br><strong>kube-apiserver<\/strong> \u2212 This is an API which can be used to orchestrate the Docker containers.<br><strong>kube-controller-manager<\/strong> \u2212 This is used to control the Kubernetes services.<br><strong>kube-scheduler<\/strong> \u2212 This is used to schedule the containers on hosts.<br><strong>Kubelet <\/strong>\u2212 This is used to control the launching of containers via manifest files.<br><strong>kube-proxy<\/strong> \u2212 This is used to provide network proxy services to the outside world.<\/p>\n\n\n\n<p class=\"has-text-align-center has-cyan-bluish-gray-background-color has-background has-medium-font-size\">Commands<\/p>\n\n\n\n<p class=\"has-text-align-center has-pale-cyan-blue-background-color has-background\" style=\"font-size:15px\">PODS<\/p>\n\n\n\n<p><strong>List all Pods<\/strong><\/p>\n\n\n\n<ul>\n<li>kubectl get pods<\/li>\n\n\n\n<li>kubectl get po<\/li>\n\n\n\n<li>kubectl get pods -o wide<\/li>\n<\/ul>\n\n\n\n<p><strong>Create\/Run a Pod<\/strong><\/p>\n\n\n\n<ul>\n<li>kubectl run nginx &#8211;image=nginx<\/li>\n<\/ul>\n\n\n\n<p><strong>Describe a Pod<\/strong><\/p>\n\n\n\n<ul>\n<li>kubectl describe pod webapp<\/li>\n<\/ul>\n\n\n\n<p><strong>Delete a Pod<\/strong><\/p>\n\n\n\n<ul>\n<li>kubectl delete pod webapp<\/li>\n\n\n\n<li>kubectl delete po webapp<\/li>\n<\/ul>\n\n\n\n<p><strong>Create\/Run YAML File<\/strong><\/p>\n\n\n\n<ul>\n<li>kubectl run redis &#8211;image=redis123 &#8211;dry-run=client -o yaml &gt; redis-definition.yaml<\/li>\n<\/ul>\n\n\n\n<p>This command along with Linux output redirection generates a resource definition file quickly, that you can then modify and create resources as required, instead of creating the files from scratch.<\/p>\n\n\n\n<p>We use kubectl run command with &#8211;dry-run=client -o yaml option to create a manifest file.<\/p>\n\n\n\n<p>If you simply want to test your command, use the &#8211;dry-run=client option. This will not create the resource. Instead, tell you whether the resource can be created and if your command is right.<\/p>\n\n\n\n<p>-o yaml will output the resource definition in YAML format on the screen.<\/p>\n\n\n\n<p><strong>Generate POD Manifest YAML File<\/strong><\/p>\n\n\n\n<ul>\n<li>kubectl run nginx &#8211;image=nginx &#8211;dry-run=client -o yaml<\/li>\n<\/ul>\n\n\n\n<p>It generates POD Manifest YAML file (-o yaml), and does not create it (&#8211;dry-run).<\/p>\n\n\n\n<p><strong>kubectl run redis -l tier=db &#8211;image=redis:alpine<\/strong><\/p>\n\n\n\n<p><strong>kubectl create -f redis-definition.yaml<\/strong><\/p>\n\n\n\n<ul>\n<li>Use kubectl create -f command to create a resource from the manifest file.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl edit pod redis<\/strong><\/p>\n\n\n\n<ul>\n<li>Use the kubectl edit command to update the image of the pod.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl apply -f redis-definition.yaml<\/strong><\/p>\n\n\n\n<ul>\n<li>If you used a pod definition file then update the image in the definition file via Vi or Nano editor and then run kubectl apply command to update the image.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center has-pale-cyan-blue-background-color has-background\">REPLICASET<\/p>\n\n\n\n<p><strong>kubectl get replicaset<\/strong><\/p>\n\n\n\n<p><strong>kubectl get rs<\/strong><\/p>\n\n\n\n<p><strong>kubectl api-resources | grep -i replicaset<\/strong><\/p>\n\n\n\n<ul>\n<li>You can check for apiVersion of replicaset by the above command.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl explain replicaset | grep VERSION<\/strong><\/p>\n\n\n\n<p><strong>kubectl apply -f replicaset-definition.yaml<\/strong><\/p>\n\n\n\n<p><strong>kubectl delete replicaset new-replica-set<\/strong><\/p>\n\n\n\n<p><strong>kubectl delete -f replicaset-definition.yaml<\/strong><\/p>\n\n\n\n<p><strong>kubectl edit replicaset new-replica-set<\/strong><\/p>\n\n\n\n<ul>\n<li>Modify the attributes and then save the file.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl scale rs new-replica-set &#8211;replicas=5<\/strong><\/p>\n\n\n\n<ul>\n<li>Modify the replicas to scale up to 5 PODs.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center has-pale-cyan-blue-background-color has-background\" style=\"font-size:15px\">DEPLOYMENT<\/p>\n\n\n\n<p><strong>kubectl get deployment<\/strong><\/p>\n\n\n\n<p><strong>kubectl get deploy<\/strong><\/p>\n\n\n\n<ul>\n<li>Count the number of Deployments.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl get deployment -n dev-ns<\/strong><\/p>\n\n\n\n<p><strong>kubectl describe deployment new-deploy<\/strong><\/p>\n\n\n\n<p><strong>kubectl explain deployment<\/strong><\/p>\n\n\n\n<p><strong>kubectl create deployment &#8211;image=nginx nginx<\/strong><\/p>\n\n\n\n<p><strong>kubectl create deployment &#8211;image=nginx nginx &#8211;dry-run=client -o yaml<\/strong><\/p>\n\n\n\n<ul>\n<li>It generates POD Manifest YAML file (-o yaml), and does not create it (&#8211;dry-run).<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl create deployment nginx &#8211;image=nginx &#8211;replicas=4<\/strong><\/p>\n\n\n\n<ul>\n<li>It generates Deployment with 4 Replicas.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl scale deployment nginx &#8211;replicas=4<\/strong><\/p>\n\n\n\n<ul>\n<li>You can scale deployment using the above command.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl create deployment nginx &#8211;image=nginx &#8211;dry-run=client -o yaml &gt; nginx-deployment.yaml<\/strong><\/p>\n\n\n\n<ul>\n<li>It creates POD Manifest YAML file.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size\">NAMESPACE<\/p>\n\n\n\n<p><strong>kubectl get namespace<\/strong><\/p>\n\n\n\n<p><strong>kubectl get ns &#8211;no-headers | wc -l<\/strong><\/p>\n\n\n\n<ul>\n<li>Get the exact number of namespaces.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl get pods &#8211;namespace=research<\/strong><\/p>\n\n\n\n<p><strong>kubectl -n research get pods &#8211;no-headers | wc -l<\/strong><\/p>\n\n\n\n<p><strong>kubectl run redis &#8211;image=redis -n finance<\/strong><\/p>\n\n\n\n<p><strong>kubectl get pods &#8211;all-namespaces<\/strong><\/p>\n\n\n\n<p><strong>kubectl create namespace dev-ns<\/strong><\/p>\n\n\n\n<p><strong>kubectl create ns dev-ns<\/strong><\/p>\n\n\n\n<p><strong>kubectl create deployment redis-deploy &#8211;image=redis &#8211;replicas=2 -n dev-ns<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size\">SERVICE<\/p>\n\n\n\n<p><strong>kubectl get svc -n=marketing<\/strong><\/p>\n\n\n\n<p><strong>kubectl get svc redis<\/strong><\/p>\n\n\n\n<p><strong>kubectl get svc<\/strong><\/p>\n\n\n\n<p><strong>kubectl describe svc redis-service<\/strong><\/p>\n\n\n\n<p><strong>kubectl expose pod redis &#8211;port=6379 &#8211;name=redis-service &#8211;dry-run=client -o yaml<\/strong><\/p>\n\n\n\n<ul>\n<li>Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379.<\/li>\n\n\n\n<li>This will automatically use the pod&#8217;s labels as selectors.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl create service clusterip redis &#8211;tcp=6379:6379 &#8211;dry-run=client -o yaml<\/strong><\/p>\n\n\n\n<ul>\n<li>This will not use the pods&#8217; labels as selectors; instead it will assume selectors as app=redis. You cannot pass in selectors as an option. So it does not work well if your pod has a different label set. So generate the file and modify the selectors before creating the service.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl expose pod nginx &#8211;port=80 &#8211;name nginx-service &#8211;type=NodePort &#8211;dry-run=client -o yaml<\/strong><\/p>\n\n\n\n<ul>\n<li>Create a Service named nginx of type NodePort to expose pod nginx&#8217;s port 80.<\/li>\n\n\n\n<li>This will automatically use the pod&#8217;s labels as selectors, but you cannot specify the node port. You have to generate a definition file and then add the node port in manually before creating the service with the pod.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl create service nodeport nginx &#8211;tcp=80:80 &#8211;node-port=30080 &#8211;dry-run=client -o yaml<\/strong><\/p>\n\n\n\n<ul>\n<li>This will not use the pods&#8217; labels as selectors.<\/li>\n<\/ul>\n\n\n\n<p><strong>kubectl run httpd &#8211;image:httpd:alpine &#8211;port=80 &#8211;expose<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-center has-cyan-bluish-gray-background-color has-background has-medium-font-size\">Docker Images<\/p>\n\n\n\n<p class=\"has-text-align-center has-pale-cyan-blue-background-color has-background\">Practice Test<\/p>\n\n\n\n<p><strong>How many images are available on this host?<\/strong><\/p>\n\n\n\n<ul>\n<li>docker images<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"352\" src=\"https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/image-1024x352.png\" alt=\"\" class=\"wp-image-766\" style=\"aspect-ratio:2.909090909090909;width:618px;height:auto\" srcset=\"https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/image-1024x352.png 1024w, https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/image-600x206.png 600w, https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/image-300x103.png 300w, https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/image-768x264.png 768w, https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/image.png 1030w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>We just downloaded the code of an application. What is the base image used in the Dockerfile?<\/strong><\/p>\n\n\n\n<ul>\n<li>cat \/root\/webapp-color\/Dockerfile<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" width=\"462\" height=\"374\" src=\"https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/image-2.png\" alt=\"\" class=\"wp-image-768\" style=\"aspect-ratio:1.2352941176470589;width:262px;height:auto\" srcset=\"https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/image-2.png 462w, https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/image-2-300x243.png 300w\" sizes=\"(max-width: 462px) 100vw, 462px\" \/><\/figure>\n\n\n\n<p><strong>Build a docker image using the Dockerfile and name it\u00a0<code>webapp-color<\/code>. No tag to be specified.<\/strong><\/p>\n\n\n\n<ul>\n<li>cd \/root\/webapp-color<\/li>\n\n\n\n<li>docker build -t webapp-color .<\/li>\n<\/ul>\n\n\n\n<p>At the end of the command, we used the &#8220;.&#8221; (dot) symbol which indicates for the current directory, so you need to run this command from within the directory that has the\u00a0<code>Dockerfile<\/code>.<\/p>\n\n\n\n<p><strong>Run an instance of the image\u00a0<code>webapp-color<\/code>\u00a0and publish port\u00a0<code>8080<\/code>\u00a0on the container to\u00a0<code>8282<\/code>\u00a0on the host.<\/strong><\/p>\n\n\n\n<ul>\n<li>docker run -p 8282:8080 webapp-color<\/li>\n<\/ul>\n\n\n\n<p><strong>What is the base Operating System used by the\u00a0<code>python:3.6<\/code>\u00a0image?<\/strong><\/p>\n\n\n\n<ul>\n<li>docker run python:3.6 cat \/etc\/*release*<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" width=\"581\" height=\"321\" src=\"https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/Image_OS.jpg\" alt=\"\" class=\"wp-image-769\" style=\"aspect-ratio:1.809968847352025;width:333px;height:auto\" srcset=\"https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/Image_OS.jpg 581w, https:\/\/technology.corporatesstore.com\/wp-content\/uploads\/2024\/09\/Image_OS-300x166.jpg 300w\" sizes=\"(max-width: 581px) 100vw, 581px\" \/><\/figure>\n\n\n\n<p><strong>Build a new smaller docker image by modifying the same Dockerfile and name it\u00a0<code>webapp-color<\/code>\u00a0and tag it\u00a0<code>lite<\/code>.<\/strong><\/p>\n\n\n\n<ul>\n<li>docker build -t webapp-color:lite .<\/li>\n<\/ul>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Install Kubenetes<\/strong><\/p>\n\n\n\n<p>In this section, we will see how to install&nbsp;<strong>Kubenetes<\/strong>&nbsp;via&nbsp;<strong>kubeadm<\/strong>. This is a tool which helps in the installation of Kubernetes. Let\u2019s go step by step and learn how to install Kubernetes.<\/p>\n\n\n\n<p><strong>Step 1<\/strong>&nbsp;\u2212 Ensure that the&nbsp;<strong>Ubuntu server version<\/strong>&nbsp;you are working on is&nbsp;<strong>16.04<\/strong>.<\/p>\n\n\n\n<p><strong>Step 2<\/strong>&nbsp;\u2212 Ensure that you generate a&nbsp;<strong>ssh<\/strong>&nbsp;key which can be used for&nbsp;<strong>ssh<\/strong>&nbsp;login. You can do this using the following command.<\/p>\n\n\n\n<p><strong>ssh-keygen<\/strong><\/p>\n\n\n\n<p>This will generate a key in your&nbsp;<strong>home folder<\/strong>&nbsp;as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/docker\/images\/home_folder.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Step 3<\/strong>&nbsp;\u2212 Next, depending on the version of Ubuntu you have, you will need to add the relevant site to the&nbsp;<strong>docker.list<\/strong>&nbsp;for the&nbsp;<strong>apt package manager<\/strong>, so that it will be able to detect the&nbsp;<strong>Kubernetes packages<\/strong>&nbsp;from the&nbsp;<strong>kubernetes<\/strong>&nbsp;site and download them accordingly.<\/p>\n\n\n\n<p>We can do it using the following commands.<\/p>\n\n\n\n<p><strong>curl -s https:\/\/packages.cloud.google.com\/apt\/doc\/apt-key.gpg | apt-key add &#8211; echo &#8220;deb http:\/\/apt.kubernetes.io\/ kubernetes-xenial main\u201d | sudo tee \/etc\/apt\/sources.list.d\/docker.list<\/strong><\/p>\n\n\n\n<p><strong>Step 4<\/strong>&nbsp;\u2212 We then issue an apt-get update to ensure all packages are downloaded on the Ubuntu server.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/docker\/images\/issue_apt_get_update.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Step 5<\/strong>&nbsp;\u2212 Install the Docker package as detailed in the earlier sections.<\/p>\n\n\n\n<p><strong>Step 6<\/strong>&nbsp;\u2212 Now it\u2019s time to install&nbsp;<strong>kubernetes<\/strong>&nbsp;by installing the following packages:<\/p>\n\n\n\n<p><strong>apt-get install \u2013y kubelet kubeadm kubectl kubernetes-cni<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/docker\/images\/install_kubernetes.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/docker\/images\/docker_package.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Step 7<\/strong>&nbsp;\u2212 Once all&nbsp;<strong>kubernetes<\/strong>&nbsp;packages are downloaded, it\u2019s time to start the kubernetes controller using the following command:<\/p>\n\n\n\n<p><strong>kubeadm init<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/docker\/images\/kubernetes_packages.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Once done, you will get a successful message that the master is up and running and nodes can now join the cluster.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Kubernetes is an orchestration framework for Docker containers which helps expose containers as services to the outside world. For example, you can have two services \u2212 One service would contain&nbsp;nginx&nbsp;and&nbsp;mongoDB, and another service would contain&nbsp;nginx&nbsp;and&nbsp;redis. Each service can have an IP or service point which can be connected by other applications. Kubernetes is then [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/technology.corporatesstore.com\/index.php\/wp-json\/wp\/v2\/pages\/207"}],"collection":[{"href":"https:\/\/technology.corporatesstore.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/technology.corporatesstore.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/technology.corporatesstore.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/technology.corporatesstore.com\/index.php\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":8,"href":"https:\/\/technology.corporatesstore.com\/index.php\/wp-json\/wp\/v2\/pages\/207\/revisions"}],"predecessor-version":[{"id":770,"href":"https:\/\/technology.corporatesstore.com\/index.php\/wp-json\/wp\/v2\/pages\/207\/revisions\/770"}],"wp:attachment":[{"href":"https:\/\/technology.corporatesstore.com\/index.php\/wp-json\/wp\/v2\/media?parent=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}