Pods are the smallest deployable units in Kubernetes, housing one or more containers with shared storage and network.
# Show a list of podskubectl get po
# Show a detailed list of podskubectl get po -o wide
# Show detailed pod statekubectl describe pod [pod_name]
# Show pods running on a nodekubectl get pods -o wide | grep [node_name]
# Show pod cpu/memory usagekubectl top pod
# Delete a podkubectl delete pod [pod_name]
# Get a shell on a single-container podkubectl exec -it [pod_name] /bin/sh
Kubernetes runs workloads by placing containers into Pods than run on Nodes, which can be virtual or physical machines.
# Show a list of nodeskubectl get no
# Show a detailed list of nodeskubectl get no -o -wide
# Show verbose node detailskubectl describe node [node_name]
# Filter nodes for a specific labelkubectl get node --selector=[label_name]
# Show node cpu/memory usagekubectl top node [node_name]
# Delete a nodekubectl delete node [node_name]
# Add or update labels of a nodekubectl label node [node_name]
Services are an abstract way to expose an application running on a set of Pods as a network service. Kubernetes provide Pods with their own IP addresses, a single DNS name for a set of Pods, and can load-balance across them.
# Show services informationkubectl get svc
# Show detailed state of serviceskubectl describe svc
# Show details about all serviceskubectl get svc -o wide
# Edit the definition of a servicekubectl edit svc
# Expose a deployment as a new servicekubectl expose deployment [deployment_name]
Deployments provide declarative updates for Pods and ReplicaSets. By describing a desired state in a Deployment, the Deployment Controller changes the actual state to the desired state at a controlled rate.
# Show deployments informationkubectl get deploy
# Show a detailed list of deploymentskubectl get deploy -o -wide
# Show verbose deployment detailskubectl describe deploy [deployment_name]
# Update definition of deploy on serverkubectl edit deployment [deployment_name]
# Create a new deploymentkubectl create deployment [deployment_name]
# Delete a deploymentkubectl delete deployment [deployment_name]
# See rollout status of a deploymentkubectl rollout status deployment [deployment_name]
Namespaces provide a way of isolating groups of resources in a Kubernetes cluster, and can be used for restricting networking and RBAC. Resource names must be unique within a namespace.
# List namespaceskubectl get namespace
# Show detailed state of a namespacekubectl describe namespace [namespace_name]
# Create a namespacekubectl create namespace [namespace_name]
# Edit the definition of a namespacekubectl edit namespace [namespace_name]
# Delete a namespacekubectl delete namespace [namespace_name]
# Show resource usage for a namespacekubectl top namespace [namespace_name]
Daemonsets ensure that Nodes run a replica of a Pod. As Nodes are added to a Custer, Pods are added to Daemonsets. If Nodes are removed from a Cluster, Pods are garbage collected. Deleting a DaemonSet cleans up the Pods it created.
# Show all daemonsets (ds) informationkubectl get ds
# Show ds details in all namespaceskubectl describe ds --all-namespaces
# Show ds details for specific namespacekubrctl describe ds [daemonset_name] -n [namespace_name]
# Edit the definition of a daemonsetkubectl edit daemonset [daemonset_name]
# Manage the rollout of a daemonsetkubectl rollout daemonset
ReplicaSets maintain a stable set of replica Pods, which are often used to guarantee availability of a specified number of identical Pods.
# Show all ReplicaSetskubectl get rs
# Show detailed state of a ReplicaSetkubectl describe rs [replicaset_name]
# Show detailed info for ReplicaSetskubectl get rs -o wide
# Scale a ReplicaSetkubectl scale --replicas=[x]
Event objects are created best-effort, with limited retention time, when a state change happens in the system, providing visibility into your cluster.
# Show recent events for all resourceskubectl get events
# Show warnings onlykubectl get events --field-selector type=Warning
# Show events sorted by timestampkubectl get events --sort-by=.metadata.creationTimestamp
# Show events excluding Pod eventskubectl get events --field-selector involvedObject.kind!=Pod
# Show events excluding Normal eventskubectl get events --field-selector type!=Normal
Cluster events are recorded as logs, which are often useful for debugging. Log verbosity is configurable depending on the level of detail required.
# Show logs for a podkubectl logs [pod_name]
# Show logs for a pod and follow new logskubectl logs -f [pod_name]
# Show the last 6h of logs for a podkubectl logs --since=6h [pod_name]
# Show the 50 most recent logs for a podkubectl logs --tail=50 [pod_name]
# Output logs in a file 'pod.log'kubectl logs [pod_name] pod.log
Service Accounts provide an identity for processes that run in a Pod. When processes contact the apiserver, they are authenticated as a particular Service Account.
# List Service Accountskubectl get sa
# Show detailed state of Service Accountskubectl describe sa
# Replace a Service Accountkubectl replace sa
# Delete a Service Accountkubectl delete sa [service_account_name]
Secrets are objects containing a sensitive data such as as passwords or tokens. Using a Secrets means you avoid including sensitive data in your application code.
# Show secretskubectl get secrets
# Show detailed info for secretskubectl describe secrets
# Show secrets for all namespaceskubectl get secrets --all-namespaces
# Create a secretkubectl create secret
# Delete a secretkubectl delete secret [secret_name]
StatefulSet is used to manage stateful applications, managing the deployment and scaling of a set of Pods. It provides guarantees for ordering and uniqueness of these Pods.
# List StatefulSetskubectl get statefulset
# Delete a StatefulSet only, not podskubectl delete statefulset/[stateful_set_name] --cascade=false
If you're not using a platform that automates Kubernetes deployments, here are useful commands for managing Kubernetes objects and making changes from manifest files.
# Apply a configuration to a resource from file kubectl apply -f [manifest].yaml
# Create objects from filekubectl create -f [manifest].yaml
# Delete objects from filekubectl delete -f [manifest].yaml
# Update the taints on one or more nodeskubectl taint node [node_name]
# Mark node as unschedulablekubectl cordon [node_name]
# Mark node as schedulablekubectl uncordon [node_name]
# Drain node to prepare for maintenancekubrctl drain [node_name]
Creating Kubernetes objects from manifest files often involves thousands of lines of YAML.
It needs to follow a specific format so that Kubernetes can understands it. This is the basic format:
/api/v1
.As a very simple example, here's the manifest for how you would create the pod my-pod
in the namespace my-namespace
and deploy the image my-container-image
to the container my-pod-container
, with the imagePullPolicy
set to pull the image every time the pod is created: