Exploring Your App
In this scenario you will learn how to troubleshoot Kubernetes applications using the kubectl get, describe, logs and exec commands.
Check application configuration
kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5c69669756-vdwgm 1/1 Running 0 11m
kubectl describe pods
Name: kubernetes-bootcamp-5c69669756-vdwgm
Namespace: default
Node: minikube/10.0.2.15
Start Time: Sun, 10 Jun 2018 15:08:15 +0000
Labels: pod-template-hash=1725225312
run=kubernetes-bootcamp
Annotations: <none>
Status: Running
IP: 172.17.0.4
Controlled By: ReplicaSet/kubernetes-bootcamp-5c69669756
Containers:
kubernetes-bootcamp:
Container ID: docker://b3c882ba24392b8b1417e420ac45c351959098cacc82fe59a64dff9d6e3859d6
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://gcr.io/google-samples/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Sun, 10 Jun 2018 15:08:48 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-x5428 (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-x5428:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-x5428
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 12m default-scheduler Successfully assigned kubernetes-bootcamp-5c69669756-vdwgm to minikube
Normal SuccessfulMountVolume 12m kubelet, minikube MountVolume.SetUp succeeded for volume "default-token-x5428"
Normal Pulling 12m kubelet, minikube pulling image "gcr.io/google-samples/kubernetes-bootcamp:v1"
Normal Pulled 11m kubelet, minikube Successfully pulled image "gcr.io/google-samples/kubernetes-bootcamp:v1"
Normal Created 11m kubelet, minikube Created container
Normal Started 11m kubelet, minikube Started container
Show the app in the terminal
We need to get the Pod name, and we'll store in the environment variable POD_NAME
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-5c69669756-vdwgm
Now we can make an HTTP request to the application running in that pod
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5c69669756-vdwgm | v=1
The url is the route to the API of the Pod.
View the container logs
kubectl logs $POD_NAME
Kubernetes Bootcamp App Started At: 2018-06-10T15:08:48.691Z | Running On: kubernetes-bootcamp-5c69669756-vdwgm
Running On: kubernetes-bootcamp-5c69669756-vdwgm | Total Requests: 1 | App Uptime: 385.809 seconds | Log Time: 2018-06-10T15:15:14.500Z
Running On: kubernetes-bootcamp-5c69669756-vdwgm | Total Requests: 2 | App Uptime: 811.04 seconds | Log Time: 2018-06-10T15:22:19.731Z
Running On: kubernetes-bootcamp-5c69669756-vdwgm | Total Requests: 3 | App Uptime: 846.227 seconds | Log Time: 2018-06-10T15:22:54.918Z
Executing command on the container
List the environment variables
kubectl exec $POD_NAME env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-5c69669756-vdwgm
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
HOME=/root
Start a bash session in the Pod’s container
kubectl exec -ti $POD_NAME bash
root@kubernetes-bootcamp-5c69669756-vdwgm:/#
We have now an open console on the container where we run our NodeJS application. The source code of the app is in the server.js file
cat server.js
var http = require('http');
var requests=0;
var podname= process.env.HOSTNAME;
var startTime;
var host;
var handleRequest = function(request, response) {
response.setHeader('Content-Type', 'text/plain');
response.writeHead(200);
response.write("Hello Kubernetes bootcamp! | Running on: ");
response.write(host);
response.end(" | v=1\n");
console.log("Running On:" ,host, "| Total Requests:", ++requests,"| App Uptime:", (new Date() - startTime)/1000 , "seconds", "| Log Time:",new Date());
}
var www = http.createServer(handleRequest);
www.listen(8080,function () {
startTime = new Date();;
host = process.env.HOSTNAME;
console.log ("Kubernetes Bootcamp App Started At:",startTime, "| Running On: " ,host, "\n" );
});
curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5c69669756-vdwgm | v=1
We used localhost because we executed the command inside the NodeJS container
root@kubernetes-bootcamp-5c69669756-vdwgm:/# exit
exit
hadoop@salt:~$