Kubernetes_getting_started
Kubernetes Getting Started Guide
This guide provides an introduction to common kubectl commands for interacting with a Kubernetes cluster. It also covers creating deployments and introduces some popular management tools.
A very good official reference is the kubectl Cheat Sheet.
Note on Examples: Many examples use specific names like app-routable-demo for namespaces or echoserver-1-deployment-7f49577554-9x8xq for pods. If you are working in your own environment, you'll need to replace these with names relevant to your setup, or deploy sample applications to have these resources available.
Exploring Your Cluster
Namespaces
Namespaces are a way to divide cluster resources between multiple users or teams. A good way to start exploring is to list all namespaces:
kubectl get nsPods
Pods are the smallest deployable units in Kubernetes. Here are some commands to inspect pods:(Assuming a namespace app-routable-demo and a pod with label app=echoserver-1 and name containing echoserver-1-deployment exist for these examples.)
List pods in a specific namespace:
kubectl get po -n app-routable-demoGet more details about pods, including the node they are running on:
kubectl get po -n app-routable-demo -o wideShow pod labels:
View a pod's full YAML definition:
List pods matching a specific selector:
Logs
View logs from a specific pod (and optionally a container within it):
Exec and Attach (Interacting with Containers)
Execute a command inside a container or attach to a running process.
Execute a shell (sh) inside a pod:
Attach to a running process in a pod (less common for general interaction than exec):
Deployments
Deployments manage stateless applications.
List deployments in a namespace:
Describe a specific deployment to see its details, status, and events:
Scaling a Deployment
Change the number of replicas (pods) managed by a deployment.
First, check the current pods for a deployment:
Scale the deployment (e.g., to 4 replicas):
Verify the new number of pods:
Creating a New Deployment
This section walks through creating a new deployment, highlighting good practices.
Unadvisable Direct Apply (Example Only)
Applying YAML directly from a URL can be risky as you might not know its exact contents. While convenient for trusted sources, it's generally not recommended for production or sensitive environments.
The original document notes: "What you should not do, but happens all of the time ..."
Corrected Approach: Local Manifest and Cleanup
Let's assume the above command was run for demonstration. Here's how you might find and clean it up:
List all deployments across all namespaces to find it:
List deployments in the default namespace (if it was deployed there):
Delete the example deployment (assuming it was named radarhack-deployment and in the default namespace):
Modifying the Manifest Locally
Obtain the deployment manifest: Download the manifest to your local machine for inspection and modification.
Edit
radarhack-deploy.yaml: Use a text editor (e.g.,vi,nano, VS Code) to modify theradarhack-deploy.yamlfile.Make the following changes as an example:
Under the main
metadatasection of the Deployment, add a label likedeployment: brucon.Under
spec.template.metadata(this is metadata for the Pods created by the Deployment), add a label likeversion: v1.Change the
spec.replicascount (e.g., to 2 or 3).In the main
metadatasection of the Deployment, specify a namespace, for example,namespace: my-radarhack.
Applying the Modified Manifest
Create the new namespace: If you added a namespace to your manifest, create it first.
Apply the local manifest:
Verify the deployment: Check the pods in your new namespace.
Example Solution YAML
After the modifications described above, your radarhack-deploy.yaml (combined with a Namespace object for completeness if creating the namespace via YAML too) might look like this:
Verifying the Deployment Details
See your deployment and pods with their labels:
You can continue to edit ./radarhack-deploy.yaml and re-apply the changes with kubectl apply -f ./radarhack-deploy.yaml.
Cleanup
An easy way to clean up everything related to this deployment (if you used a dedicated namespace):
Managing Your Kubernetes Cluster in Style
Here are a couple of popular command-line tools that offer enhanced UIs for managing Kubernetes.
K9s
K9s provides a terminal-based UI to navigate, observe, and manage your deployed applications in K8s. Project Link: https://github.com/derailed/k9s
Installation (Example for Linux x86_64, version v0.20.5):Note: Always check the K9s GitHub releases page for the latest version and installation instructions.
Octant
Octant is a tool for developers to understand the complexity of Kubernetes clusters through a visual interface. It runs as a web application on your local machine. Project Link: https://github.com/vmware-tanzu/octant
Installation (Example for Linux x86_64, version v0.13.1):Note: Always check the Octant GitHub releases page for the latest version and installation instructions.
Running Octant:
The original document mentions: "You could do it like this ... but this is not very smart" referring to:
Binding to 0.0.0.0 makes Octant accessible on all network interfaces, which might be a security risk if your machine is exposed.
A common way to run Octant is to let it bind to 127.0.0.1 (localhost) by default, and if you are running it on a remote machine (e.g., studentx-1.training.dockerhack.me), use SSH local port forwarding to access its web interface:
On your local machine, run:
(This forwards local port 8900 to port 7777 on the remote host where Octant will run.)
On the remote host (e.g.,
studentx-1.training.dockerhack.mevia the SSH session or another terminal): Start Octant. It will typically listen on127.0.0.1:7777by default.Access Octant: Open your local web browser and navigate to
http://127.0.0.1:8900.
Last updated