It's hard to remember all the kubectl commands. Even kubectl itself is hard to input. So I make an alias for it.

 alias k=kubectl

Get the culster info:

 $ k cluster-info
Kubernetes control plane is running at https://35.233.204.76
GLBCDefaultBackend is running at https://35.233.204.76/api/v1/namespaces/kube-system/services/default-http-backend:http/proxy
KubeDNS is running at https://35.233.204.76/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
KubeDNSUpstream is running at https://35.233.204.76/api/v1/namespaces/kube-system/services/kube-dns-upstream:dns/proxy
Metrics-server is running at https://35.233.204.76/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy

Get services info:

 $ k get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-app-service LoadBalancer 10.104.2.106 34.82.180.103 80:32221/TCP 47h
kubernetes ClusterIP 10.104.0.1 <none> 443/TCP 47h

Get nodes info:

 $ k get nodes
NAME STATUS ROLES AGE VERSION
gk3-hello-cluster-default-pool-c4c5959a-4pbd Ready <none> 47h v1.22.12-gke.2300
gk3-hello-cluster-default-pool-d3b8c13d-9q98 Ready <none> 47h v1.22.12-gke.2300
gk3-hello-cluster-default-pool-d3b8c13d-n7df Ready <none> 47h v1.22.12-gke.2300

Or with the wide option for any "get" command:

 $ k get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
gk3-hello-cluster-default-pool-c4c5959a-4pbd Ready <none> 47h v1.22.12-gke.2300 10.138.0.6 34.168.27.195 Container-Optimized OS from Google 5.10.133+ containerd://1.5.13
gk3-hello-cluster-default-pool-d3b8c13d-9q98 Ready <none> 47h v1.22.12-gke.2300 10.138.0.7 34.83.245.222 Container-Optimized OS from Google 5.10.133+ containerd://1.5.13
gk3-hello-cluster-default-pool-d3b8c13d-n7df Ready <none> 47h v1.22.12-gke.2300 10.138.0.5 34.168.140.33 Container-Optimized OS from Google 5.10.133+ containerd://1.5.13

Get pods info:

 $ k get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-app-7d989d7dcc-l5jsh 1/1 Running 0 47h 10.103.128.130 gk3-hello-cluster-default-pool-d3b8c13d-9q98 <none> <none>

Or output the content to JSON which is long enough:

 $ k get pods -o json
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"annotations": {
"seccomp.security.alpha.kubernetes.io/pod": "runtime/default"
},
...

Get namepsace info:

 $ k get ns
NAME STATUS AGE
default Active 47h
kube-node-lease Active 47h
kube-public Active 47h
kube-system Active 47h

To explain what's namespace:

 $ k explain ns
KIND: Namespace
VERSION: v1
DESCRIPTION:
Namespace provides a scope for Names. Use of multiple namespaces is
optional.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object>
Spec defines the behavior of the Namespace. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
status <Object>
Status describes the current status of a Namespace. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

To describe a specified resource:

 $ k describe pods
Name: hello-app-7d989d7dcc-l5jsh
Namespace: default
Priority: 0
Service Account: default
Node: gk3-hello-cluster-default-pool-d3b8c13d-9q98/10.138.0.7
Start Time: Sat, 08 Oct 2022 10:05:52 +0800
Labels: app=hello-app
pod-template-hash=7d989d7dcc
Annotations: seccomp.security.alpha.kubernetes.io/pod: runtime/default
Status: Running
IP: 10.103.128.130
IPs:
IP: 10.103.128.130
Controlled By: ReplicaSet/hello-app-7d989d7dcc
...

This command can be particularly useful for troubleshooting containers that aren’t working properly, as it records each stage of the container’s life cycle, along with any errors that occurred.

We can always use help command here:

 $ k get -h
Display one or many resources.
Prints a table of the most important information about the specified resources. You can filter the list using a label
selector and the --selector flag. If the desired resource type is namespaced you will only see results in your current
namespace unless you pass --all-namespaces.
By specifying the output as 'template' and providing a Go template as the value of the --template flag, you can filter
the attributes of the fetched resources.
Use "kubectl api-resources" for a complete list of supported resources.
...

Return to home | Generated on 10/10/22