|
| 1 | +--- |
| 2 | +title: Viewing Pods and Nodes |
| 3 | +weight: 10 |
| 4 | +--- |
| 5 | + |
| 6 | +## {{% heading "objectives" %}} |
| 7 | + |
| 8 | +* Learn about Kubernetes Pods. |
| 9 | +* Learn about Kubernetes Nodes. |
| 10 | +* Troubleshoot deployed applications. |
| 11 | + |
| 12 | +## Kubernetes Pods |
| 13 | + |
| 14 | +When you created a Deployment in [Module 2](/docs/tutorials/kubernetes-basics/deploy-app/deploy-intro/), |
| 15 | +Kubernetes created a **Pod** to host your application instance. A Pod is a Kubernetes |
| 16 | +abstraction that represents a group of one or more application containers (such as Docker), |
| 17 | +and some shared resources for those containers. Those resources include: |
| 18 | + |
| 19 | +* Shared storage, as Volumes |
| 20 | +* Networking, as a unique cluster IP address |
| 21 | +* Information about how to run each container, such as the container image version |
| 22 | +or specific ports to use |
| 23 | + |
| 24 | +A Pod models an application-specific "logical host" and can contain different application |
| 25 | +containers which are relatively tightly coupled. For example, a Pod might include |
| 26 | +both the container with your Node.js app as well as a different container that feeds |
| 27 | +the data to be published by the Node.js webserver. The containers in a Pod share an |
| 28 | +IP Address and port space, are always co-located and co-scheduled, and run in a shared |
| 29 | +context on the same Node. |
| 30 | + |
| 31 | +Pods are the atomic unit on the Kubernetes platform. When we create a Deployment |
| 32 | +on Kubernetes, that Deployment creates Pods with containers inside them (as opposed |
| 33 | +to creating containers directly). Each Pod is tied to the Node where it is scheduled, |
| 34 | +and remains there until termination (according to restart policy) or deletion. In |
| 35 | +case of a Node failure, identical Pods are scheduled on other available Nodes in |
| 36 | +the cluster. |
| 37 | + |
| 38 | +### Pods overview |
| 39 | + |
| 40 | +{{< figure src="../../public/images/module_03_pods.svg" class="diagram-medium" >}} |
| 41 | + |
| 42 | +## Nodes |
| 43 | + |
| 44 | +A Pod always runs on a **Node**. A Node is a worker machine in Kubernetes and may |
| 45 | +be either a virtual or a physical machine, depending on the cluster. Each Node is |
| 46 | +managed by the control plane. A Node can have multiple pods, and the Kubernetes |
| 47 | +control plane automatically handles scheduling the pods across the Nodes in the |
| 48 | +cluster. The control plane's automatic scheduling takes into account the available |
| 49 | +resources on each Node. |
| 50 | + |
| 51 | +Every Kubernetes Node runs at least: |
| 52 | + |
| 53 | +* Kubelet, a process responsible for communication between the Kubernetes control |
| 54 | +plane and the Node; it manages the Pods and the containers running on a machine. |
| 55 | + |
| 56 | +* A container runtime (like Docker) responsible for pulling the container image |
| 57 | +from a registry, unpacking the container, and running the application. |
| 58 | + |
| 59 | +### Nodes overview |
| 60 | + |
| 61 | +{{< figure src="../../public/images/module_03_nodes.svg" class="diagram-medium" >}} |
| 62 | + |
| 63 | +## Troubleshooting with kubectl |
| 64 | + |
| 65 | +In [Module 2](/docs/tutorials/kubernetes-basics/deploy-app/deploy-intro/), you used |
| 66 | +the kubectl command-line interface. You'll continue to use it in Module 3 to get |
| 67 | +information about deployed applications and their environments. The most common |
| 68 | +operations can be done with the following kubectl subcommands: |
| 69 | + |
| 70 | +* `kubectl get` - list resources |
| 71 | +* `kubectl describe` - show detailed information about a resource |
| 72 | +* `kubectl logs` - print the logs from a container in a pod |
| 73 | +* `kubectl exec` - execute a command on a container in a pod |
| 74 | + |
| 75 | +You can use these commands to see when applications were deployed, what their current |
| 76 | +statuses are, where they are running and what their configurations are. |
| 77 | + |
| 78 | +Now that we know more about our cluster components and the command line, let's explore |
| 79 | +our application. |
| 80 | + |
| 81 | +### Check application configuration |
| 82 | + |
| 83 | +Let's verify that the application we deployed in the previous scenario is running. |
| 84 | +We'll use the `kubectl get` command and look for existing Pods: |
| 85 | + |
| 86 | +```shell |
| 87 | +kubectl get pods |
| 88 | +``` |
| 89 | + |
| 90 | +If no pods are running, please wait a couple of seconds and list the Pods again. |
| 91 | +You can continue once you see one Pod running. |
| 92 | + |
| 93 | +Next, to view what containers are inside that Pod and what images are used to build |
| 94 | +those containers we run the `kubectl describe pods` command: |
| 95 | + |
| 96 | +```shell |
| 97 | +kubectl describe pods |
| 98 | +``` |
| 99 | + |
| 100 | +We see here details about the Pod’s container: IP address, the ports used and a |
| 101 | +list of events related to the lifecycle of the Pod. |
| 102 | + |
| 103 | +The output of the `describe` subcommand is extensive and covers some concepts that |
| 104 | +we didn’t explain yet, but don’t worry, they will become familiar by the end of this tutorial. |
| 105 | + |
| 106 | +{{< note >}} |
| 107 | +The `describe` subcommand can be used to get detailed information about most of the |
| 108 | +Kubernetes primitives, including Nodes, Pods, and Deployments. The describe output is |
| 109 | +designed to be human readable, not to be scripted against. |
| 110 | +{{< /note >}} |
| 111 | + |
| 112 | +### Show the app in the terminal |
| 113 | + |
| 114 | +Recall that Pods are running in an isolated, private network - so we need to proxy access |
| 115 | +to them so we can debug and interact with them. To do this, we'll use the `kubectl proxy` |
| 116 | +command to run a proxy in a **second terminal**. Open a new terminal window, and |
| 117 | +in that new terminal, run: |
| 118 | + |
| 119 | +```shell |
| 120 | +kubectl proxy |
| 121 | +``` |
| 122 | + |
| 123 | +Now again, we'll get the Pod name and query that pod directly through the proxy. |
| 124 | +To get the Pod name and store it in the `POD_NAME` environment variable: |
| 125 | + |
| 126 | +```shell |
| 127 | +export POD_NAME="$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')" |
| 128 | +echo Name of the Pod: $POD_NAME |
| 129 | +``` |
| 130 | + |
| 131 | +To see the output of our application, run a `curl` request: |
| 132 | + |
| 133 | +```shell |
| 134 | +curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/ |
| 135 | +``` |
| 136 | + |
| 137 | +The URL is the route to the API of the Pod. |
| 138 | + |
| 139 | +{{< note >}} |
| 140 | +We don't need to specify the container name, because we only have one container inside the pod. |
| 141 | +{{< /note >}} |
| 142 | + |
| 143 | +### Executing commands on the container |
| 144 | + |
| 145 | +We can execute commands directly on the container once the Pod is up and running. |
| 146 | +For this, we use the `exec` subcommand and use the name of the Pod as a parameter. |
| 147 | +Let’s list the environment variables: |
| 148 | + |
| 149 | +```shell |
| 150 | +kubectl exec "$POD_NAME" -- env |
| 151 | +``` |
| 152 | + |
| 153 | +Again, it's worth mentioning that the name of the container itself can be omitted |
| 154 | +since we only have a single container in the Pod. |
| 155 | + |
| 156 | +Next let’s start a bash session in the Pod’s container: |
| 157 | + |
| 158 | +```shell |
| 159 | +kubectl exec -ti $POD_NAME -- bash |
| 160 | +``` |
| 161 | + |
| 162 | +We have now an open console on the container where we run our NodeJS application. |
| 163 | +The source code of the app is in the i`server.js` file: |
| 164 | + |
| 165 | +```shell |
| 166 | +cat server.js |
| 167 | +``` |
| 168 | + |
| 169 | +You can check that the application is up by running a curl command: |
| 170 | + |
| 171 | +```shell |
| 172 | +curl http://localhost:8080 |
| 173 | +``` |
| 174 | + |
| 175 | +{{< note >}} |
| 176 | +Here we used `localhost` because we executed the command inside the NodeJS Pod. |
| 177 | +If you cannot connect to `localhost:8080`, check to make sure you have run the |
| 178 | +`kubectl exec` command and are launching the command from within the Pod. |
| 179 | +{{< /note >}} |
| 180 | + |
| 181 | +To close your container connection, type `exit`. |
| 182 | + |
| 183 | +## {{% heading "whatsnext" %}} |
| 184 | + |
| 185 | +Once you're ready, move on to |
| 186 | +[Using A Service To Expose Your App](/docs/tutorials/kubernetes-basics/expose/expose-intro/). |
| 187 | + |
0 commit comments