Kubernetes Cheatsheet Af
Kubernetes Cheatsheet Af
Kubernetes Cheatsheet
TABLE OF CONTENTS
Preface 1
Introduction 1
Kubernetes Basics 1
Quick Reference Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Architecture Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Essential Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Pods and Containers 2
Cheat sheet table: Common commands for creating, managing, and troubleshooting pods . . . . . . . . . . . . 2
Best practices for effective containerization within the Kubernetes environment . . . . . . . . . . . . . . . . . . . . 2
Code snippets for creating multi-container pods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Deployments and ReplicaSets 3
Visual guide: Lifecycle of Deployments and how ReplicaSets ensure high availability. . . . . . . . . . . . . . . . . 4
Cheat sheet for scaling applications with Deployments and managing rolling updates . . . . . . . . . . . . . . . . 4
Code examples for creating and updating ReplicaSets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Services and Networking 5
Graphical representation: Kubernetes networking components and their interactions. . . . . . . . . . . . . . . . 5
Cheat sheet for creating and managing Kubernetes services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
YAML examples for defining Services and exposing applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Configuration and Secrets 6
Key configurations in Kubernetes and practical use cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Secure practices for managing and consuming secrets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Code snippets for using ConfigMaps and Secrets in Kubernetes pods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Monitoring and Troubleshooting 7
Cheat sheet for troubleshooting common issues in Kubernetes clusters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Visual representation of Kubernetes dashboard components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Advanced Topics 8
Cheat sheet for Helm charts and efficient package management in Kubernetes . . . . . . . . . . . . . . . . . . . . . . 8
Code examples for setting up Persistent Volumes and Persistent Volume Claims . . . . . . . . . . . . . . . . . . . . . 8
Additional Commands 9
For Node Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
For Namespace Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Service Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Deployment Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Resources 10
PREFACE
PREFACE Term Meaning
KUBERNETESBASICS
BASICS ESSENTIAL COMMANDS
KUBERNETES
With a foundational understanding of terms and
QUICK REFERENCE TABLE architecture, let’s equip ourselves with essential
commands for managing Kubernetes clusters.
The quick reference table below serves as a
compass for navigating the terminology crucial to These commands are crucial for understanding the
mastering Kubernetes. status, configuration, and available resources
within a Kubernetes cluster. They provide a quick
Command Description kubectl get pods --all List all pods in all
-namespaces namespaces.
kubectl cluster-info Display endpoint
information about the kubectl describe pod Display detailed
master and services in <pod_name> information about a
the cluster. specific pod.
kubectl version Display the Kubernetes kubectl logs <pod_name> View the logs of a
version running on the specific pod.
client and server. kubectl exec -it Open an interactive
kubectl config view <pod_name> — /bin/bash
Get the configuration of shell inside a running
the cluster. container.
kubectl api-resources List the API resources kubectl delete pod Delete a specific pod.
that are available. <pod_name>
kubectl apply -f Apply the configuration
kubectl api-versions List the API versions <pod_configuration> specified in a YAML file
that are available.
to create or update a
kubectl get all --all List everything in all pod.
-namespaces namespaces.
kubectl port-forward Forward ports from a
<pod_name> pod to your local
PODSAND
PODS ANDCONTAINERS
CONTAINERS <local_port>:<pod_port>
machine.
CHEAT SHEET TABLE: COMMON • Minimize the size of your container images by
COMMANDS FOR CREATING, MANAGING, reducing unnecessary layers and dependencies.
AND TROUBLESHOOTING PODS Smaller images improve deployment speed and
reduce resource consumption.
Updating a ReplicaSet
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: example-replicaset
spec:
replicas: 5 # Update the number
of replicas
selector:
matchLabels: Figure 2. Kubernetes Networking Components
app: example
template:
CHEAT SHEET FOR CREATING AND
metadata: MANAGING KUBERNETES SERVICES
labels:
app: example Creating Services
spec:
containers: Create a Service from a YAML file: kubectl apply -f
service-definition.yaml
- name: app-container
image: updated-example- Example* service-definition.yaml:*
image:latest # Update the container
image
apiVersion: v1
kind: Service
• This YAML configuration updates the existing
metadata:
ReplicaSet named example-replicaset.
name: example-service
• It modifies the desired number of replicas to 5. spec:
• The pod template is updated to use the selector:
updated-example-image:latest container image. app: example
ports:
SERVICESAND
SERVICES ANDNETWORKING
NETWORKING - protocol: TCP
port: 80
Services define a stable endpoint for accessing a set targetPort: 8080
of pods, while Networking encompasses the type: ClusterIP
mechanisms that enable effective communication
between these pods.
Managing Services
GRAPHICAL REPRESENTATION:
List all Services in the default namespace: kubectl
KUBERNETES NETWORKING
get services
COMPONENTS AND THEIR
INTERACTIONS Delete a Service: kubectl delete service
<service_name>
Service Discovery
Retrieve the ClusterIP of a Service: kubectl get • The service exposes port 80 on each node,
service <service_name> -o forwarding traffic to the selected pods on port
jsonpath='{.spec.clusterIP}' 8080.
apiVersion: v1
SECURE PRACTICES FOR MANAGING AND kind: Pod
CONSUMING SECRETS metadata:
name: secret-pod
• Use Kubernetes Secrets: Leverage the built-in
spec:
Kubernetes Secrets API to store sensitive
information securely.
containers:
- name: app-container
• Encrypt Data in Transit: Ensure that data image: app-image:latest
exchanged between components is encrypted,
envFrom:
especially when dealing with sensitive
- secretRef:
information.
name: example-secret
• Role-Based Access Control (RBAC): Implement
RBAC to control access to Secrets, allowing only
authorized entities to retrieve sensitive data. • This YAML configuration creates a pod named
secret-pod.
• Avoid Hardcoding Secrets: Refrain from
hardcoding secrets directly in application code • The envFrom field references a Secret named
or configuration files. example-secret.
• Regularly Rotate Secrets: Periodically rotate • The values from the Secret are injected as
passwords and cryptographic keys to minimize environment variables into the pod.
the impact of a potential security breach.
Creating ConfigMap
CODE SNIPPETS FOR USING CONFIGMAPS kubectl create configmap example-configmap --from
AND SECRETS IN KUBERNETES PODS -literal=key1=value1 --from-literal=key2=value2
• Pod Issues:
Figure 3. Kubernetes Dashboard
◦ Check pod logs: kubectl logs <pod_name>
• Node Issues:
CHEAT SHEET FOR HELM CHARTS AND
◦ View nodes: kubectl get nodes EFFICIENT PACKAGE MANAGEMENT IN
KUBERNETES
◦ Check node events: kubectl describe node
<node_name>
Install Helm: curl
• Networking Issues: https://raw.githubusercontent.com/helm/helm/
master/scripts/get-helm-3 | bash
◦ Inspect network policies: kubectl get
networkpolicies Add Helm Repository: helm repo add stable
https://charts.helm.sh/stable
• Service Issues:
Install a Helm Chart: helm install my-release
◦ Verify service status: kubectl get services
stable/<chart_name>
• Configuration Issues:
Upgrade a Helm Release: helm upgrade my-release
◦ Check ConfigMaps: kubectl get configmaps stable/<chart_name>
• Resource Issues:
Uninstall a Helm Release: helm uninstall my-
◦ View resource usage: kubectl top nodes or release
kubectl top pods
• Cluster Information:
CODE EXAMPLES FOR SETTING UP
PERSISTENT VOLUMES AND PERSISTENT
◦ Get cluster information: kubectl cluster- VOLUME CLAIMS
info
• Kubernetes Podcast
JCG delivers over 1 million pages each month to more than 700K software
developers, architects and decision makers. JCG offers something for everyone,
including news, tutorials, cheat sheets, research guides, feature articles, source code
and more.
CHEATSHEET FEEDBACK
WELCOME
support@javacodegeeks.com
Copyright © 2014 Exelixis Media P.C. All rights reserved. No part of this publication may be SPONSORSHIP
reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, OPPORTUNITIES
mechanical, photocopying, or otherwise, without prior written permission of the publisher. sales@javacodegeeks.com