🛡️
CTHFM: Kubernetes
  • Welcome
  • Kubernetes Fundamentals
    • Kubernetes Components
      • Kubernetes Master Node
      • Worker Nodes
      • Pods
      • Service
      • ConfigMaps and Secrets
      • Namespaces
      • Deployments
      • ReplicaSets
      • Jobs and CronJobs
      • Horizontal Pod Autoscaler (HPA)
      • Kubernetes Ports and Protocols
    • Kubectl
      • Installation and Setup
      • Basic Kubectl
      • Working With Pods
      • Deployments and ReplicaSets
      • Services and Networking
      • ConfigMaps and Secrets
      • YAML Manifest Management
      • Debugging and Troubleshooting
      • Kubectl Scripting: Security
      • Customizing Kubectl
      • Security Best Practices
      • Common Issues
      • Reading YAML Files
    • MiniKube
      • Intro
      • Prerequisites
      • Installation MiniKube
      • Starting MiniKube
      • Deploy a Sample Application
      • Managing Kubernetes Resources
      • Configuring MiniKube
      • Persistent Storage in Minikube
      • Using Minikube for Local Development
      • Common Pitfalls
      • Best Practices
  • Kubernetes Logging
    • Kubernetes Logging Overview
    • Audit Logs
    • Node Logs
    • Pod Logs
    • Application Logs
    • Importance of Logging
    • Types of Logs
    • Collecting and Aggregating Logs
    • Monitoring and Alerting
    • Log Parsing and Enrichment
    • Security Considerations in Logging
    • Best Practices
    • Kubernetes Logging Architecture
  • Threat Hunting
    • Threat Hunting Introduction
    • What Makes Kubernetes Threat Hunting Unique
    • Threat Hunting Process
      • Hypothesis Generation
      • Investigation
      • Identification
      • Resolution & Follow Up
    • Pyramid of Pain
    • Threat Frameworks
      • MITRE Containers Matrix
        • MITRE Att&ck Concepts
        • MITRE Att&ck Data Sources
        • MITRE ATT&CK Mitigations
        • MITRE Att&ck Containers Matrix
      • Microsoft Threat for Kubernetes
    • Kubernetes Behavioral Analysis and Anomaly Detection
    • Threat Hunting Ideas
    • Threat Hunting Labs
  • Security Tools
    • Falco
      • Falco Overview
      • Falco's Architecture
      • Runtime Security Explained
      • Installation and Setup
      • Falco Rules
      • Tuning Falco Rules
      • Integrating Falco with Kubernetes
      • Detecting Common Threats with Falco
      • Integrating Falco with Other Security Tools
      • Automating Incident Response with Falco
      • Managing Falco Performance and Scalability
      • Updating and Maintaining Falco
      • Real-World Case Studies and Lessons Learned
      • Labs
        • Deploying Falco on a Kubernetes Cluster
        • Writing and Testing Custom Falco Rules
        • Integrating Falco with a SIEM System
        • Automating Responses to Falco Alerts
    • Open Policy Agent (OPA)
      • Introduction to Open Policy Agent (OPA)
      • Getting Started with OPA
      • Rego
      • Advanced Rego Concepts
      • Integrating OPA with Kubernetes
      • OPA Gatekeeper
      • Policy Enforcement in Microservices
      • OPA API Gateways
      • Introduction to CI/CD Pipelines and Policy Enforcement
      • External Data in OPA
      • Introduction to Decision Logging
      • OPA Performance Monitoring
      • OPA Implementation Best Practices
      • OPA Case Studies
      • OPA Ecosystem
    • Kube-Bench
    • Kube-Hunter
    • Trivy
    • Security Best Practices and Documentation
      • RBAC Good Practices
      • Official CVE Feed
      • Kubernetes Security Checklist
      • Securing a Cluster
      • OWASP
  • Open Source Tools
    • Cloud Native Computing Foundation (CNCF)
      • Security Projects
  • Infrastructure as Code
    • Kubernetes and Terraform
      • Key Focus Areas for Threat Hunters
      • Infastructure As Code: Kubernetes
      • Infrastructure as Code (IaC) Basics
      • Infastructure As Code Essential Commands
      • Terraform for Container Orchestration
      • Network and Load Balancing
      • Secrets Management
      • State Management
      • CI/CD
      • Security Considerations
      • Monitoring and Logging
      • Scaling and High Availability
      • Backup and Disaster Recovery
    • Helm
      • What is Helm?
      • Helm Architecture
      • Write Helm Charts
      • Using Helm Charts
      • Customizing Helm Charts
      • Customizing Helm Charts
      • Building Your Own Helm Chart
      • Advanced Helm Chart Customization
      • Helm Repositories
      • Helm Best Practices
      • Helmfile and Continuous Integration
      • Managing Secrets with Helm and Helm Secrets
      • Troubleshooting and Debugging Helm
      • Production Deployments
      • Helm Case Studies
Powered by GitBook
On this page
  • Managing Kubernetes Resources
  • 1. Understanding Kubernetes Resources
  • 2. Creating Kubernetes Resources
  • Example: Creating a Pod
  • Example: Creating a Deployment
  • Creating Resources Declaratively
  • 3. Viewing and Managing Resources
  • Viewing Resources
  • Updating Resources
  • Deleting Resources
  • 4. Labeling and Selecting Resources
  • Adding Labels to Resources
  • Example: Add a Label to a Pod
  • Selecting Resources by Label
  • Conclusion
  1. Kubernetes Fundamentals
  2. MiniKube

Managing Kubernetes Resources

Managing Kubernetes Resources

In this lesson, we'll dive into managing Kubernetes resources using the kubectl command-line tool. You will learn how to create, update, and delete resources such as Pods, Deployments, and Services. By the end of this lesson, you'll have a solid understanding of how to interact with your Kubernetes cluster and manage its resources effectively.

1. Understanding Kubernetes Resources

Kubernetes resources are the objects that make up your Kubernetes cluster. These include:

  • Pods: The smallest deployable units in Kubernetes, representing a single instance of a running process in your cluster.

  • Deployments: Controllers that manage a set of identical Pods, ensuring the correct number of replicas are running.

  • Services: Abstractions that define a logical set of Pods and a policy to access them, enabling communication between different components of your application.

  • ConfigMaps and Secrets: Used to manage configuration data and sensitive information, respectively, outside of your Pods.

2. Creating Kubernetes Resources

You can create Kubernetes resources in two ways: by using imperative commands with kubectl or by defining resource configurations in YAML files.

Creating Resources Imperatively

Imperative commands allow you to quickly create resources with a single command.

Example: Creating a Pod

  • To create a Pod running a simple nginx container:

    kubectl run nginx-pod --image=nginx --restart=Never
    • Explanation:

      • kubectl run: Command to create a Pod.

      • nginx-pod: The name of the Pod.

      • --image=nginx: Specifies the container image to use.

      • --restart=Never: Ensures that the Pod will not be automatically restarted by a controller like a Deployment.

Example: Creating a Deployment

  • To create a Deployment with three replicas:

    kubectl create deployment nginx-deployment --image=nginx --replicas=3
    • Explanation:

      • kubectl create deployment: Command to create a Deployment.

      • nginx-deployment: The name of the Deployment.

      • --image=nginx: Specifies the container image for the Pods.

      • --replicas=3: Creates three replicas of the Pods.

Creating Resources Declaratively

Declarative commands involve defining resource configurations in YAML files and then applying them to the cluster.

Step 1: Create a YAML File

  • Create a file named nginx-deployment.yaml with the following content:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx
            ports:
            - containerPort: 80

Step 2: Apply the YAML File

  • Apply the configuration to create the Deployment:

    kubectl apply -f nginx-deployment.yaml
    • Explanation:

      • kubectl apply -f: Command to apply the configuration defined in the specified file.

      • nginx-deployment.yaml: The name of the YAML file containing the resource definition.

3. Viewing and Managing Resources

Once your resources are created, you can use kubectl to view and manage them.

Viewing Resources

Example: Get All Pods

  • To list all Pods in the current namespace:

    kubectl get pods
    • Explanation:

      • kubectl get pods: Lists all Pods with their current status.

Example: Get Detailed Information

  • To get detailed information about a specific Pod:

    kubectl describe pod <pod-name>
    • Explanation:

      • kubectl describe pod: Provides detailed information, including events, environment variables, and more.

      • <pod-name>: Replace with the name of the Pod you want to inspect.

Example: Get Services

  • To list all Services:

    kubectl get services

Updating Resources

Example: Scaling a Deployment

  • To scale a Deployment up or down:

    kubectl scale deployment nginx-deployment --replicas=5
    • Explanation:

      • kubectl scale deployment: Command to scale a Deployment.

      • nginx-deployment: The name of the Deployment to scale.

      • --replicas=5: Scales the Deployment to 5 replicas.

Example: Rolling Update

  • If you update the container image in a Deployment, Kubernetes will perform a rolling update to ensure zero downtime.

    Step 1: Update the YAML File

    • Modify the nginx-deployment.yaml file to use a different version of the nginx image:

      containers:
      - name: nginx
        image: nginx:1.19.10

    Step 2: Apply the Update

    • Apply the update:

      kubectl apply -f nginx-deployment.yaml
    • Kubernetes will start a rolling update, gradually replacing old Pods with new ones using the updated image.

Deleting Resources

Example: Deleting a Pod

  • To delete a specific Pod:

    bashCopy codekubectl delete pod <pod-name>

Example: Deleting a Deployment

  • To delete a Deployment and all associated Pods:

    bashCopy codekubectl delete deployment nginx-deployment
    • Explanation:

      • kubectl delete deployment: Deletes the Deployment and its Pods.

      • nginx-deployment: The name of the Deployment to delete.

Example: Deleting All Resources in a YAML File

  • To delete all resources defined in a YAML file:

    bashCopy codekubectl delete -f nginx-deployment.yaml

4. Labeling and Selecting Resources

Labels are key-value pairs attached to Kubernetes resources, allowing you to organize and select resources efficiently.

Adding Labels to Resources

Example: Add a Label to a Pod

  • To add a label to a Pod:

    kubectl label pod <pod-name> env=production
    • Explanation:

      • kubectl label pod: Command to add a label to a Pod.

      • env=production: The label key (env) and value (production).

Selecting Resources by Label

Example: Select Pods by Label

  • To list all Pods with a specific label:

    kubectl get pods -l env=production
    • Explanation:

      • -l env=production: Filters Pods with the label env=production.

Conclusion

In this lesson, you’ve learned how to manage Kubernetes resources using kubectl. From creating and updating resources to labeling and deleting them, you now have the essential skills to manage your Kubernetes cluster effectively. Understanding how to interact with resources in Kubernetes is fundamental to deploying, scaling, and maintaining applications in any Kubernetes environment. In the next module, we'll explore more advanced features of Minikube, including configuring storage and adding custom addons to your cluster.

4o

PreviousDeploy a Sample ApplicationNextConfiguring MiniKube

Last updated 9 months ago