Skip to content

kubectl commands

Get started

alias kubectl="kubectl --kubeconfig=$HOME/tmp/kubeconfig.yaml"

Part 1

run a Pod with a single container

the restart flag tells Kubernetes to create just the Pod and no other resources:

kubectl run hello-kiamol --image=kiamol/ch02-hello-kiamol --restart=Never

wait for the Pod to be ready

kubectl wait --for=condition=Ready pod hello-kiamol

list all the Pods in the cluster

kubectl get pods show detailed information about the Pod

kubectl describe pod hello-kiamol

get the basic information about the Pod

kubectl get pod hello-kiamol

specify custom columns in the output

selecting network details

kubectl get pod hello-kiamol --output,NODE_IP:status.hostIP,POD_IP:status.podIP

specify a JSONPath query in the output

selecting the ID of the first container in the Pod

kubectl get pod hello-kiamol -o jsonpath='{.status.containerStatuses[0].containerID}'

find the Pod’s container

docker container ls -q --filter

now delete that container

docker container rm -f $(docker container ls -q --filter

check the Pod status

kubectl get pod hello-kiamol

and find the container again

docker container ls -q --filter

listen on port 8080 on your machine

and send traffic to the Pod on port 80

kubectl port-forward pod/hello-kiamol 8980:80
kubectl port-forward --address pod/hello-kiamol 8980:80
kubectl port-forward --address localhost, pod/hello-kiamol 8980:80

create a Deployment

called “hello-kiamol-2”, running the same web app

kubectl create deployment hello-kiamol-2 --image=kiamol/ch02-hello-kiamol

list all the Pods

kubectl get pods
kubectl get deploy hello-kiamol-2 -o jsonpath='{.spec.template.metadata.labels}'

list the Pods that have that matching label

kubectl get pods -l app=hello-kiamol-2

deploy the application from the manifest file

kubectl apply -f

list running Pods

kubectl get pods

Part 2

check the internal IP address of the first Pod we ran

kubectl get pod hello-kiamol -o,POD_IP:status.podIP

run an interactive shell command in the Pod

kubectl exec -it hello-kiamol -- sh

inside the Pod, check the IP address

hostname -i

test the web app

wget -O - http://localhost | head -n 4

leave the shell


Part 3

kubectl logs --tail=2 hello-kiamol

and compare the actual container logs if you’re using Docker

docker container logs --tail=2 $(docker container ls -q --filter

delete all Pods

kubectl delete pods --all

check again

kubectl get pods

view Deployments

kubectl get deploy

delete all Deployments

kubectl delete deploy --all

view Pods

kubectl get pods

check all resources

kubectl get all

Part 4

create two Deployments, which each run one Pod

kubectl apply -f kiamol/ch03/sleep/sleep1.yaml -f kiamol/ch03/sleep/sleep2.yaml

wait for the Pod to be ready again

kubectl wait --for=condition=Ready pod -l app=sleep-2

check the IP address of the second Pod

kubectl get pod -l app=sleep-2 --output jsonpath='{.items[0].status.podIP}'

use that address to ping the second Pod from the first

kubectl exec deploy/sleep-1 -- ping -c 2 $(kubectl get pod -l app=sleep-2 --output jsonpath='{.items[0].status.podIP}')

Part 5

deploy the Service

kubectl apply -f sleep/sleep2-service.yaml

show the basic details of the Service

kubectl get svc sleep-2

run a ping command to check connectivity—this will fail

kubectl exec deploy/sleep-1 -- ping -c 1 sleep-2

Part 6

remove the existing Service

kubectl delete svc numbers-api

deploy the headless Service

kubectl apply -f numbers-services/api-service-headless.yaml

check the Service

kubectl get svc numbers-api

check the endpoint

kubectl get endpoints numbers-api

verify the DNS lookup

kubectl exec deploy/sleep-1 -- sh -c 'nslookup numbers-api | grep "^[^*]"'

Part 7

check the Services in the default namespace

kubectl get svc --namespace default
kubectl get svc -o wide

check Services in the system namespace

kubectl get svc -n kube-system

try a DNS lookup to a fully qualified Service name

kubectl exec deploy/sleep-1 -- sh -c 'nslookup numbers-api.default.svc.cluster.local | grep "^[^*]"'

and for a Service in the system namespace

kubectl exec deploy/sleep-1 -- sh -c 'nslookup kube-dns.kube-system.svc.cluster.local | grep "^[^*]"'

Part 8

create a ConfigMap with data from the command line

kubectl create configmap sleep-config-literal --from-literal=kiamol.section='4.1'

check the ConfigMap details

kubectl get cm sleep-config-literal

show the friendly description of the ConfigMap

kubectl describe cm sleep-config-literal

deploy the updated Pod spec

kubectl apply -f sleep/sleep-with-configMap-env.yaml

check the Kiamol environment variables

kubectl exec deploy/sleep -- sh -c 'printenv | grep "^KIAMOL"'


  • Learn Kubernetes in a Month of Lunches (ELTON STONEMAN)

  1. License under CC BY-NC 4.0
  2. Copyright issue feedback, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try iOS App