kubectl
bit.ly/k8s-kubectl
Try using kubectl
to list resources by type:
kubectl get nodes
Request the same info, but output the results as structured yaml:
kubectl get nodes -o yaml
Fetch an individual resource by type/id
, output as json
:
kubectl get node/minikube -o json
View human-readable API output:
kubectl describe node/minikube
List resources by type:
kubectl get pods
Create a new resource based on a json object specification:
curl https://raw.githubusercontent.com/ryanj/metrics-k8s/master/pod.json
kubectl create -f https://raw.githubusercontent.com/ryanj/metrics-k8s/master/pod.json
List resources by type:
kubectl get pods
Fetch a resource by type and id, output the results as yaml
:
kubectl get pod metrics-k8s -o yaml
Notice any changes?
Expose the pod by creating a new service
(or "loadbalancer"):
kubectl expose pod/metrics-k8s --port 2015 --type=NodePort
Contact your newly-exposed pod using the associated service id:
minikube service metrics-k8s
Schedule a pod to be deleted:
kubectl delete pod metrics-k8s
Contact the related service. What happens?:
minikube service metrics-k8s
Delete the service:
kubectl delete service metrics-k8s
Create a specification for your deployment
:
kubectl run metrics-k8s --image=quay.io/ryanj/metrics-k8s \
--expose --port=2015 --service-overrides='{ "spec": { "type": "NodePort" } }' \
--dry-run -o yaml > deployment.yaml
View the generated deployment spec file:
cat deployment.yaml
Create a new resource based on your yaml specification:
kubectl create -f deployment.yaml
List resources by type:
kubectl get po,svc
Connect to your new deployment via the associated service id:
minikube service metrics-k8s
Scale up the metrics-k8s
deployment to 3 replicas:
kubectl scale deploy/metrics-k8s --replicas=3
List pods:
kubectl get po
Edit deploy/metrics-k8s
, setting spec.replicas
to 5
:
kubectl edit deploy/metrics-k8s -o json
Save and quit. What happens?
kubectl get pods
Watch for changes to pod
resources:
kubectl get pods --watch
In another terminal, delete several pods by id:
kubectl delete pod $(kubectl get pods | grep ^metrics-k8s | cut -f1 -s -d' ' | head -n 3 | tr '\n' ' ')
What happend? How many pods remain?
kubectl get pods
Watch deployments (leave this running until the 'cleanup' section):
kubectl get deploy --watch
View the current state of your deployment:
minikube service metrics-k8s
Update your deployment's image spec to rollout a new release:
kubectl set image deploy/metrics-k8s metrics-k8s=quay.io/ryanj/metrics-k8s:v1
Reload your browser to view the state of your deployment
kubectl get rs,deploy
View the list of previous rollouts:
kubectl rollout history deploy/metrics-k8s
Rollback to the previous state:
kubectl rollout undo deployment metrics-k8s
Reload your browser to view the state of your deployment
Cleanup old resources if you don't plan to use them:
kubectl delete service,deployment metrics-k8s
Close any remaining --watch
listeners
Kubernetes Command-Line Basics with kubectl
bit.ly/k8s-kubectl
Continue learning with other k8s-workshops
: