kubectl
workskubectl get RESOURCE
get
kubectl get namespaces
NAME STATUS AGE
default Active 39m
kube-node-lease Active 39m
kube-public Active 39m
kube-system Active 39m
kubernetes-dashboard Active 34m
Let us try to deploy our first pod
kubectl run NAME --image=CONTAINER_IMAGE
run
We deploy a simple pod of one container
kubectl run nginx --image=nginx
pod/nginx created
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 10s
If a pod fails then no particular operation will be undertaken by the cluster to rescue it
We need to introduce some other resources to guarantee high availability (HA) of the deployed services (stand by)
Let us introduce another useful command
to get all the information about a specific resource
kubectl describe RESOURCE_TYPE RESOURCE_NAME
describe
kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: sailor_uranus/172.17.0.2
[...]
Status: Running
IP: 172.18.0.3
Containers:
nginx:
Image: nginx:latest
Ready: True
Restart Count: 0
[...]
One of the most useful pieces of information provided
by the command describe can be found at the bottom
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 25s default-scheduler Successfully assigned default/nginx to sailor_uranus
Normal Pulling 22s kubelet Pulling image "nginx:latest"
Normal Pulled 21s kubelet Successfully pulled image "nginx:latest" in 1.519s
Normal Created 20s kubelet Created container nginx
Normal Started 20s kubelet Started container nginx
This list reports all the events about the resource;
it is really useful for debug
An important concept under K8s,
that you may have noticed:
Any resource can be defined in a declarative way
We can get the declarative definition of a resource
exploiting the command get
kubectl get RESOURCE_TYPE RESOURCE_NAME -o=yaml
get RESOURCE_TYPE RESOURCE_NAME
kubectl get pods nginx -o=yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
[...]
spec:
containers:
- image: nginx:latest
imagePullPolicy: Always
name: nginx
resources: {}
[...]
If we have a declarative yaml (or json) file
of a resource we can deploy it
kubectl apply (-f FILENAME | -k DIRECTORY)
apply