Skip to content

Usage of kubectl

homepage-banner

Get started

alias kubectl="kubectl --kubeconfig=$HOME/tmp/kubeconfig.yaml"
## or put config into "$HOME/.kube/config"

kubectl config view
kubectl config current-context
kubectl config get-contexts
kubectl get clusterrolebindings
kubectl cluster-info dump

### get Pod CIDR
kubectl get nodes -o custom-columns=NODE:.metadata.name,POD_CIDR:.spec.podCIDR

### get Service CIDR
kubectl describe cm kubeadm-config -n kube-system |grep Subnet

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 custom-columns=NAME:metadata.name,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 label=io.kubernetes.container.name=hello-kiamol

now delete that container

docker container rm -f $(docker container ls -q --filter label=io.kubernetes.container.name=hello-kiamol)

check the Pod status

kubectl get pod hello-kiamol

and find the container again

docker container ls -q --filter label=io.kubernetes.container.name=hello-kiamol

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 0.0.0.0 pod/hello-kiamol 8980:80
kubectl port-forward --address localhost,10.153.40.102 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 https://raw.githubusercontent.com/sixeyed/kiamol/master/ch02/pod.yaml

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 custom-columns=NAME:metadata.name,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

exit

Part 3

kubectl logs -f xxx -c yyy
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 label=io.kubernetes.container.name=hello-kiamol)

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"'

Reference

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