Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DevOps Lifecycle
  • DevOps Roadmap
  • Docker Tutorial
  • Kubernetes Tutorials
  • Amazon Web Services [AWS] Tutorial
  • AZURE Tutorials
  • GCP Tutorials
  • Docker Cheat sheet
  • Kubernetes cheat sheet
  • AWS interview questions
  • Docker Interview Questions
  • Ansible Interview Questions
  • Jenkins Interview Questions
Open In App
Next Article:
Kubernetes - Kubectl Create and Kubectl Apply
Next article icon

Kubernetes - Kubectl Create and Kubectl Apply

Last Updated : 23 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Kubernetes is an open-source Container Orchestrating software platform that is widely used in modern application deployment and management. It comes up with many internal resources to manage the other working nodes Organizing as a Cluster facilitates seamless automatic scaling, and deployment updates making applications highly available and fault tolerant. The `kubectl create` and `kubectl apply` commands are essential for managing containerized applications effectively. This Article involves discussing these two kubectl commands with their method of approaches in deploying the resources.

What is Kubectl?

The Kubectl is a command line interface software that allows you to run commands in the Kubernetes cluster. It acts as a communication bridge between users/clients and the Kubernetes cluster. It uses the Kubernetes API Server to authenticate with the Kubernetes Master Node to interact within the Kubernetes Cluster. The communication of API requests in the Kubernetes Cluster using kubectl to deploy the resources will be done with 2 approaches. The following are the 2 approaches used in Kubernetes deployment :

1. Imperative way ( Manual approach)

2. Declarative way ( Dynamic Approach )

Imperative Way of Deploying Resources in Kubernetes

In this Imperative approach, We have to specify the detailed instructions on the management of resources. What resources have to be used such as Kubernetes Pod, Kubernetes ReplicaSet, Deployments, and Daemon Sets... How to Use resources like Creating, Deleting, and Replacing to reach the desired final state. For example, `kubectl create`, `kubectl scale`, `kubectl delete`... This Approach provides full control over the deployment process allowing the operations to be defined manually as a sequence of operations to reach the desired state.

Declarative Way of Deploying Resources in Kubernetes

The declarative approach is an effective way in kubernetes, The desired specification of resources are specified through Yaml configuration files. While execution it will check the current and desired state of resources with specified resource names and then perform the updates that are required. This declarative way provides the optimization and better utilization of resources. The popular command for this example is `kubectl apply` .

Kubectl Usage Approaches

In earlier times, kubectl was mostly used for interactive or experimental reasons to make the development work easier. After once kubernetes came into usage the functionality purpose of kubectl usage is changed. It comes to serve as a key component in configuring the production environment deployment enhancing the agility through application manifest files. kubectl create in the imperative approach and kubectl apply in the declarative approach are two essential kubectl commands in resource deployment of Kubernetes.

This article focuses on two pivotal kubernetes commands that streamline resource deployment. Those two commands are as follows:

  1. kubectl create ( Imperative way ): This command follows imperative approach provide users specify their desired state directly as a command. It is useful for customizing single resources providing more control to the user. In this resources can configurable without need of manifest files. This will effective for experimenting and rapid prototyping during the development phases.
  2. kubectl apply ( Declarative way ): This command comes with declarative approach, where users define their desired state of the resources through manifest files. It is preferred for the resource deployment in production environment offering consistency and idempotent nature. This command facilitates the declarative configurations ensuring the desired state of the cluster meeting the specification of manifest file configurations.

Usage of Kubectl Create in Deploying K8S Resources

The kubectl create command is an imperative way of deploying resources on a Kubernetes cluster . The following are a few popular resources that can be created using kubectl create command.

Kubectl create - Creating Deployment Resource

Creating a deployment resource imperatively with kubectl create command by default replicas count will be 1.

Syntax:

kubectl create deployment <Deployment-Name> --image <Container-Image>

Example

kubectl create deployment demo-deployment --image nginx:latest

The following screenshot shows the practical usage of creating imperative Deployment with kubectl create :

Imperative Deployment

kubectl create - Creating Namespace Resource

Kubernetes Namespaces are used for logically isolate the resources within a cluster. By default, a namespace is already created in a cluster i.e "Default". It is very useful to isolated the resources and its management when you are working with different projects or different teams working in same same cluster.

The following command is used to list all namespaces that are created in the cluster.

kubectl get ns

Syntax

 kubectl create namespace <namespace_name>

( OR )

  kubectl create -n <namespace_name>

Example

kubectl  create namespace "production"

The following screenshot shows the practically creating a namespace imperatively with kubectl create command.

Imperatively creating Namespace

Kubectl create - Creating ServiceAccount Resource

A Kubernetes ServiceAccount is used as an identity for assigning to pods or applications. It enables the resources to interact with the Kubernetes API and access the resources within the cluster, while managing permissions through Role-Based Access Control (RBAC).

Syntax

kubectl create serviceaccount  <ServiceAccount_name>

Example

kubectl create serviceaccount "demo-user"

The following screenshot help you in creating the serviceaccount imperatively with kubectl create command.

Imperatively creating serviceaccount

Kubectl create - Creating Secret Resource

A Kubernetes Secret is a resource used to securely store the sensitive data such as passwords or API keys of the applications. It provides a proper way of management and distribution of confidential data to resources within the Kubernetes cluster.

Syntax

kubectl create secret  <Secret-Type>  <Secret-Name>

Example

kubectl create secret genric  "demo-user-secret"

The following screenshot help you in creating a secret resource of generic type imperatively with kubectl create command.

Imperatively creating Secret Resource

Usage of Kubectl Apply in Deploying K8S Resources

The kubectl apply command is a declarative way of deploying resources on a cluster using YAML manifest files.

Syntax

kubectl apply  (-f FILENAME | -k DIRECTORY)   [Options]

Example

We want to create a deployment with a ReplicaSet and 2 pods in it and let's say we use the manifest file "deployment.yml" as shown in the below

apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment-yaml
labels:
type: deployment
spec:
replicas: 3
selector:
matchLabels:
type: pods
template:
metadata:
name: demo-pod
labels:
type: pods
spec:
containers:
- name: nginx-pod
image: nginx
Deployment Yaml file

The following is the command that is used to create the resources in these manifest files:

kubectl apply -f <ManifestFile-Name>

The following screenshot shows the creation of deployment resource with kubectl apply command.

declarative deployment

Upon making any changes to the manifest file, you can re-run the command and it will make the required changes to the resources in the Kubernetes Cluster through checking the current state of the resources and desired state of the resources.

The following Screeenshot shows the updating the deployment resource replicas from 3 to 5 with kubectl apply command.

Updating Deployment configuration

Difference Between Kubernetes Create and Kubernetes Apply

The following are the main difference between kubernetes create and kubernetes apply

Features

`kubernetes create`

`kubernetes apply`

Usage

It follows imperative approach in deploying the k8s resources.

i.e., executing commands explictly

It follows declarative approach in deploying and configuring k8s resources.

i.e., Running through configuration files interpretation.

Updating Resources

It generates error if trying to execute already exist resources.

It facilitates idempotent nature, updating configuration of resources and creating new resources.

Workflow

It is used ideally for one time of resource creation in kubernetes cluster.

It can be used for existing resource management, configuring updates through yaml file.

Idempotence

Not idempotent Nature.
It raises error on trying to create existing resources.

It has idempotent nature. It only do changes new resources, updates. You can execute resource definition multiple times safely

Changes Handling

It does not automatically handle the changes of resources.

It automatically handles the updates and resource changes.

Conclusion

The selection of command usage either `kubectl create` or `kubectl apply` in Kubernetes management of resources depends on the your decision of following imperative and declarative approaches. The Imperative approach with `kubectl create` facilitates sequential control step by step that will be suitable for manual resource manipulation. Conversely, the declarative approach with `kubectl apply` provides the efficiency and the automation of the desired state of resources in Yaml files. The declarative approach in k8s automatically determines and executes the necessary actions required for the desired state. The selection of approach for using these commands depends on the desired level of control while deploying and managing the resources within a Kubernetes cluster.



Next Article
Kubernetes - Kubectl Create and Kubectl Apply

D

devanshpopli
Improve
Article Tags :
  • Kubernetes
  • DevOps
  • Kubernetes-Basics

Similar Reads

    Kubernetes - Kubectl Delete
    Kubernetes is an open-source Container Management tool that automates container deployment, container scaling, descaling, and container load balancing (also called a container orchestration tool). It is written in Golang and has a huge community because it was first developed by Google and later don
    8 min read
    Kubernetes - Kubectl Commands
    Pre-requisites: Kubernetes The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. Some basic Kubectl commands in a Kubernetes cluster are as follows:kubectl Co
    5 min read
    Kubernetes - Kubectl
    Kubectl is a command-line software tool that is used to run commands in command-line mode against the Kubernetes Cluster. Kubectl runs the commands in the command line, Behind the scenes it authenticates with the master node of Kubernetes Cluster with API calls and performs the operations on Kuberne
    12 min read
    What is Kubelet in Kubernetes?
    A Kubelet in Kubernetes is a crucial component of the primary node agent that runs on each node. It assists with container management and orchestration within a Kubernetes cluster. Kubelet supports communication between the Kubernetes control plane and individual nodes and it also enables the effici
    4 min read
    Kubernetes - Architecture
    Kubernetes Cluster mainly consists of Worker Machines called Nodes and a Control Plane. In a cluster, there is at least one worker node. The Kubectl CLI communicates with the Control Plane and the Control Plane manages the Worker Nodes. In this article, we are going to discuss in detail the architec
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences