How to Deploy Containers with Kubectl Run in Kubernetes

KubernetesKubernetesBeginner
Practice Now

Introduction

This tutorial will guide you through the process of deploying containers in a Kubernetes cluster using the kubectl run command. You'll start by understanding the fundamental concepts of Kubernetes, then dive into the practical steps of launching containers with kubectl run. Finally, you'll explore techniques to optimize your Kubernetes deployments for better performance and reliability.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ClusterInformationGroup(["`Cluster Information`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/CoreConceptsGroup(["`Core Concepts`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/run("`Run`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/version("`Version`") kubernetes/ClusterInformationGroup -.-> kubernetes/cluster_info("`Cluster Info`") kubernetes/CoreConceptsGroup -.-> kubernetes/architecture("`Architecture`") subgraph Lab Skills kubernetes/describe -.-> lab-400127{{"`How to Deploy Containers with Kubectl Run in Kubernetes`"}} kubernetes/create -.-> lab-400127{{"`How to Deploy Containers with Kubectl Run in Kubernetes`"}} kubernetes/run -.-> lab-400127{{"`How to Deploy Containers with Kubectl Run in Kubernetes`"}} kubernetes/apply -.-> lab-400127{{"`How to Deploy Containers with Kubectl Run in Kubernetes`"}} kubernetes/version -.-> lab-400127{{"`How to Deploy Containers with Kubectl Run in Kubernetes`"}} kubernetes/cluster_info -.-> lab-400127{{"`How to Deploy Containers with Kubectl Run in Kubernetes`"}} kubernetes/architecture -.-> lab-400127{{"`How to Deploy Containers with Kubectl Run in Kubernetes`"}} end

Understanding Kubernetes Fundamentals

What is Kubernetes?

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF).

Key Kubernetes Concepts

  • Pods: The smallest deployable units in Kubernetes, representing one or more containers running together.
  • Nodes: The physical or virtual machines that run the Kubernetes pods.
  • Deployments: Declarative way to manage the lifecycle of a set of pods.
  • Services: Provide a stable network endpoint for accessing pods.
  • Volumes: Provide persistent storage for pods.

Kubernetes Architecture

graph TD A[Master Node] --> B[Worker Node] B --> C[Pod] C --> D[Container] A --> E[API Server] A --> F[Scheduler] A --> G[Controller Manager] A --> H[etcd]

Kubectl: The Kubernetes Command-Line Tool

kubectl is the primary command-line tool for interacting with a Kubernetes cluster. It allows you to create, manage, and monitor Kubernetes resources.

## List all pods in the default namespace
kubectl get pods

## Create a new deployment
kubectl create deployment nginx --image=nginx

Deploying Applications on Kubernetes

Kubernetes supports various ways to deploy applications, including:

  • Declarative YAML manifests
  • Imperative kubectl run commands
  • Helm charts

The choice depends on the complexity of the application and the level of control required.

Deploying Containers with Kubectl Run

Understanding kubectl run

The kubectl run command is a quick and easy way to deploy containers on a Kubernetes cluster. It allows you to create a deployment, service, or other Kubernetes resources with a single command.

Syntax for kubectl run

kubectl run <name> --image=<image> [options]

Common options include:

  • --port: Specify the port the container listens on
  • --env: Set environment variables
  • --replicas: Specify the number of replicas to create
  • --labels: Add labels to the resources

Deploying a Simple Nginx Container

## Deploy an Nginx container
kubectl run nginx --image=nginx --port=80

## Verify the deployment
kubectl get pods
kubectl describe deployment nginx

Exposing the Container as a Service

## Create a service to expose the Nginx container
kubectl expose deployment nginx --type=LoadBalancer --port=80

## Verify the service
kubectl get services

Scaling the Deployment

## Scale the deployment to 3 replicas
kubectl scale deployment nginx --replicas=3

## Verify the scaled deployment
kubectl get pods

Updating the Container Image

## Update the Nginx container to a newer version
kubectl set image deployment/nginx nginx=nginx:1.19

## Verify the updated deployment
kubectl rollout status deployment nginx

Optimizing Kubernetes Deployments

Resource Requests and Limits

Defining resource requests and limits for your containers is crucial for optimizing Kubernetes deployments. This ensures that your pods have the necessary resources to run and prevents them from consuming too many resources.

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
    - name: my-container
      image: my-image
      resources:
        requests:
          cpu: 100m
          memory: 128Mi
        limits:
          cpu: 500m
          memory: 256Mi

Liveness and Readiness Probes

Liveness and readiness probes help Kubernetes monitor the health of your containers and ensure they are ready to receive traffic.

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
    - name: my-container
      image: my-image
      livenessProbe:
        httpGet:
          path: /healthz
          port: 8080
      readinessProbe:
        httpGet:
          path: /ready
          port: 8080

Deployment Strategies

Kubernetes supports different deployment strategies to ensure a smooth rollout of your application updates:

Strategy Description
Recreate Shuts down the existing version before deploying the new version
RollingUpdate Gradually replaces old pods with new ones
Blue/Green Deploys a new version alongside the old version, then switches traffic

Horizontal Pod Autoscaling (HPA)

HPA automatically scales the number of pod replicas based on observed CPU utilization or other metrics.

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: my-app
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: 50

LabEx Kubernetes Expertise

LabEx provides expert-level Kubernetes consulting and training to help organizations optimize their deployments and get the most out of the platform.

Summary

By the end of this tutorial, you will have a solid understanding of how to use the kubectl run command to deploy containers in a Kubernetes environment. You'll be able to leverage Kubernetes' powerful features to manage your containerized applications effectively, ensuring they are running efficiently and reliably.

Other Kubernetes Tutorials you may like