Last active
March 23, 2021 20:21
-
-
Save matheus-goncalves/fedc114e2f2cef339a5525863b1baceb to your computer and use it in GitHub Desktop.
Kubectl commands storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DEBUG | |
kubectl get pods -n “namespace” | |
kubectl get services | |
kubectl get pods -n “namespace ”-o wide | |
kubectl logs {pod-name} | |
kubectl exec -it {pod-name} -- bin/bash | |
get all pods from all namespaces | |
kubectl get all -n namespace | |
kubectl describe endpoins | |
#Get information about our current context, ensure we're logged into the correct cluster. | |
kubectl config get-contexts | |
#Change our context if needed. | |
kubectl config use-context kubernetes-admin@kubernetes | |
#Get information about the API Server for our current context, which should be kubernetes-admin@kubernetes | |
kubectl cluster-info | |
#Get a list of API Resources available in the cluster | |
kubectl api-resources | more | |
#Using kubectl explain | |
kubectl explain pods | more | |
#Creating a pod with YAML | |
kubectl apply -f pod.yaml | |
#Let's look more closely at what we need in pod.spec and pod.spec.containers | |
kubectl explain pod.spec | more | |
kubectl explain pod.spec.containers | more | |
#We can use the -v option to increase the verbosity of our request. | |
#Display requested resource URL. Focus on VERB, API Path and Response code | |
kubectl get pod hello-world -v 6 | |
#Same output as 6, add HTTP Request Headers. Focus on application type, and User-Agent | |
kubectl get pod hello-world -v 7 | |
#Same output as 7, adds Response Headers and truncated Response Body. | |
kubectl get pod hello-world -v 8 | |
#Same output as 8, add full Response. Focus on the bottom, look for metadata | |
kubectl get pod hello-world -v 9 | |
#Watch, Exec and Log Requests | |
#A watch on Pods will watch on the resourceVersion on api/v1/namespaces/default/Pods | |
kubectl get pods --watch -v 6 & | |
#We can see kubectl keeps the TCP session open with the server...waiting for data. | |
netstat -plant | grep kubectl | |
#Accessing logs | |
kubectl logs hello-world | |
kubectl logs hello-world -v 6 | |
#Get a list of all the namespaces in our cluster | |
kubectl get namespaces | |
#get a list of all the API resources and if they can be in a namespace | |
kubectl api-resources --namespaced=true | head | |
kubectl api-resources --namespaced=false | head | |
#Namespaces have state, Active and Terminating (when it's deleting) | |
kubectl describe namespaces | |
#Describe the details of an indivdual namespace | |
kubectl describe namespaces kube-system | |
#Get all the pods in our cluster across all namespaces. Right now, only system pods, no user workload. | |
#You can shorten --all-namespaces to -A | |
kubectl get pods --all-namespaces | |
kubectl get pods -A | |
#Get all the resource across all of our namespaces | |
kubectl get all --all-namespaces | |
kubectl get all -A | |
#Get a list of the pods in the kube-system namespace | |
kubectl get pods --namespace kube-system | |
#Imperatively create a namespace | |
kubectl create namespace playground1 | |
#Create a collection of pods with labels assinged to each | |
more CreatePodsWithLabels.yaml | |
kubectl apply -f CreatePodsWithLabels.yaml | |
#Look at all the Pod labels in our cluster | |
kubectl get pods --show-labels | |
#Look at one Pod's labels in our cluster | |
kubectl describe pod nginx-pod-1 | head | |
#Query labels and selectors | |
kubectl get pods --selector tier=prod | |
kubectl get pods --selector tier=qa | |
kubectl get pods -l tier=prod | |
kubectl get pods -l tier=prod --show-labels | |
#Selector for multiple labels and adding on show-labels to see those labels in the output | |
kubectl get pods -l 'tier=prod,app=MyWebApp' --show-labels | |
kubectl get pods -l 'tier=prod,app!=MyWebApp' --show-labels | |
kubectl get pods -l 'tier in (prod,qa)' | |
kubectl get pods -l 'tier notin (prod,qa)' | |
#Output a particluar label in column format | |
kubectl get pods -L tier | |
kubectl get pods -L tier,app | |
#Edit an existing label | |
kubectl label pod nginx-pod-1 tier=non-prod --overwrite | |
kubectl get pod nginx-pod-1 --show-labels | |
#Adding a new label | |
kubectl label pod nginx-pod-1 another=Label | |
kubectl get pod nginx-pod-1 --show-labels | |
#Removing an existing label | |
kubectl label pod nginx-pod-1 another- | |
kubectl get pod nginx-pod-1 --show-labels | |
#Performing an operation on a collection of pods based on a label query | |
kubectl label pod --all tier=non-prod --overwrite | |
kubectl get pod --show-labels | |
#Delete all pods matching our non-prod label | |
kubectl delete pod -l tier=non-prod | |
#And we're left with nothing. | |
kubectl get pods --show-labels | |
#Kubernetes Resource Management | |
#Start a Deployment with 3 replicas, open deployment-label.yaml | |
kubectl apply -f deployment-label.yaml | |
#Expose our Deployment as Service, open service.yaml | |
kubectl apply -f service.yaml | |
#Look at the Labels and Selectors on each resource, the Deployment, ReplicaSet and Pod | |
#The deployment has a selector for app=hello-world | |
kubectl describe deployment hello-world | |
#The ReplicaSet has labels and selectors for app and the current pod-template-hash | |
#Look at the Pod Template and the labels on the Pods created | |
kubectl describe replicaset hello-world | |
#The Pods have labels for app=hello-world and for the pod-temlpate-hash of the current ReplicaSet | |
kubectl get pods --show-labels | |
#Edit the label on one of the Pods in the ReplicaSet, change the pod-template-hash | |
kubectl label pod PASTE_POD_NAME_HERE pod-template-hash=DEBUG --overwrite | |
#The ReplicaSet will deploy a new Pod to satisfy the number of replicas. Our relabeled Pod still exists. | |
kubectl get pods --show-labels | |
#Let's look at how Services use labels and selectors, check out services.yaml | |
kubectl get service | |
#The selector for this serivce is app=hello-world, that pod is still being load balanced to! | |
kubectl describe service hello-world | |
#Get a list of all IPs in the service, there's 5...why? | |
kubectl describe endpoints hello-world | |
#Get a list of pods and their IPs | |
kubectl get pod -o wide | |
#To remove a pod from load balancing, change the label used by the service's selector. | |
#The ReplicaSet will respond by placing another pod in the ReplicaSet | |
kubectl get pods --show-labels | |
kubectl label pod PASTE_POD_NAME_HERE app=DEBUG --overwrite | |
#Check out all the labels in our pods | |
kubectl get pods --show-labels | |
#Look at the registered endpoint addresses. Now there's 4 | |
kubectl describe endpoints hello-world | |
#To clean up, delete the deployment, service and the Pod removed from the replicaset | |
kubectl delete deployment hello-world | |
kubectl delete service hello-world | |
kubectl delete pod PASTE_POD_NAME_HERE | |
--cascade=false to leave the pods alive | |
#Scheduling a pod to a node | |
#Scheduling is a much deeper topic, we're focusing on how labels can be used to influence it here. | |
kubectl get nodes --show-labels | |
#Label our nodes with something descriptive | |
kubectl label node c1-node2 disk=local_ssd | |
kubectl label node c1-node3 hardware=local_gpu | |
#Query our labels to confirm. | |
kubectl get node -L disk,hardware | |
#Create three Pods, two using nodeSelector, one without. | |
more PodsToNodes.yaml | |
kubectl apply -f PodsToNodes.yaml | |
#View the scheduling of the pods in the cluster. | |
kubectl get node -L disk,hardware | |
kubectl get pods -o wide | |
#restart a deployment | |
kubectl rollout restart -n "namespace" deployment/"name of deployment" | |
#Delete all evitec pods from a namespace | |
kubectl get pod -n "namespace" | grep Evicted | awk '{print $1}' | xargs kubectl delete pod -n "namespace" | |
kubectl delete pod -n namespace $(kubectl get pod -n namespace | awk '/Evicted/ {print $1}') | |
#Enter interactive mode on Ruby pod | |
kubectl exec -it -n "namespace" "podname" -- bundle exec rails c | |
#Enter interactive mode on mongoDB pod | |
kubectl exec -it nome-do-pod -n mongodb mongo | |
Logging | |
#Let's get the logs from the multicontainer pod...this will throw an error and ask us to define which container | |
kubectl logs $PODNAME | |
#But we need to specify which container inside the pods | |
kubectl logs $PODNAME -c container1 | |
kubectl logs $PODNAME -c container2 | |
#We can access all container logs which will dump each containers in sequence | |
kubectl logs $PODNAME --all-containers | |
#If we need to follow a log, we can do that...helpful in debugging real time issues | |
#This works for both single and multi-container pods | |
kubectl logs $PODNAME --all-containers --follow | |
ctrl+c | |
#For all pods matching the selector, get all the container logs and write it to stdout and then file | |
kubectl get pods --selector app=loggingdemo | |
kubectl logs --selector app=loggingdemo --all-containers | |
kubectl logs --selector app=loggingdemo --all-containers > allpods.txt | |
#Also helpful is tailing the bottom of a log... | |
#Here we're getting the last 5 log entries across all pods matching the selector | |
#You can do this for a single container or using a selector | |
kubectl logs --selector app=loggingdemo --all-containers --tail 5 | |
#2 - Nodes | |
#Get key information and status about the kubelet, ensure that it's active/running and check out the log. | |
#Also key information about it's configuration is available. | |
systemctl status kubelet.service | |
#If we want to examine it's log further, we use journalctl to access it's log from journald | |
# -u for which systemd unit. If using a pager, use f and b to for forward and back. | |
journalctl -u kubelet.service | |
#journalctl has search capabilities, but grep is likely easier | |
journalctl -u kubelet.service | grep -i ERROR | |
#Time bounding your searches can be helpful in finding issues add --no-pager for line wrapping | |
journalctl -u kubelet.service --since today --no-pager | |
#Events | |
#Show events for all objects in the cluster in the default namespace | |
#Look for the deployment creation and scaling operations from above... | |
#If you don't have any events since they are only around for an hour create a deployment to generate some | |
kubectl get events | |
#It can be easier if the data is actually sorted... | |
#sort by isn't for just events, it can be used in most output | |
kubectl get events --sort-by='.metadata.creationTimestamp' | |
#Create a flawed deployment | |
kubectl create deployment nginx --image ngins | |
#We can filter the list of events using field selector | |
kubectl get events --field-selector type=Warning | |
kubectl get events --field-selector type=Warning,reason=Failed | |
#We can also monitor the events as they happen with watch | |
kubectl get events --watch & | |
kubectl scale deployment loggingdemo --replicas=5 | |
#break out of the watch | |
fg | |
ctrl+c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Top, irmão!
Dalhe!