The Wayback Machine - https://web.archive.org/web/20240118173411/https://www.geeksforgeeks.org/kubernetes-kubectl-create-and-kubectl-apply/
Open In App
Related Articles

Kubernetes – Kubectl Create and Kubectl Apply

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The kubectl is a command line interface tool used to interact with the Kubernetes cluster. It uses Kubernetes API to interact with the cluster. kubectl can be used to deploy resources on a Kubernetes cluster in 2 ways: 

  • Imperative way
  • Declarative way

The former is used for experimental or interactive purposes i.e development, whereas the latter is used with manifest files for production. 

This article consists of two  Key kubectl commands:

  • kubectl create
  • kubectl apply

Kubectl Create:

The kubectl create command is an imperative way of deploying resources on a Kubernetes cluster. Below is a list of resources that can be created using kubectl create command.

1. Deployment:

To create a deployment with ReplicaSets and Pods using a specific image, the command below is used:

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

Output:

 

2. Namespace:

Namespaces are used to logically isolate resources within a cluster. By default, a namespace is already created in a cluster i.e “Default”. Let’s say we want to isolate a few applications from one another or we want to create different environments within a cluster, we can use the following command:

kubectl create namespace <Namespace-Name>

To use the Namespace, use the below command:

kubectl get ns

Output:

 

3. ServiceAccount:

A specific service account can be used to work on a cluster. To create a service account, we can use the following command:

kubectl create ServiceAccount <ServiceAccount-Name>

Output:

 

4. Secret:

Secrets can be created within a cluster that can be used for Service accounts or for connecting to a repository and many more. To create a secret, we can use the following command:

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

 

Kubectl Apply

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

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” for the same as shown in the image below:

 

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

kubectl apply -f <ManifestFile-Name>

Output:

 

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.

Lost in the complex landscape of DevOps? It's time to find your way! Enroll in our DevOps Engineering Planning to Production Live Course and set out on an exhilarating expedition to conquer DevOps methodologies with precision and timeliness.
What We Offer:
  • Comprehensive DevOps Curriculum
  • Expert Guidance for Streamlined Learning
  • Hands-on Experience with Real-world Scenarios
  • Proven Track Record with 100,000+ Successful DevOps Enthusiasts

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 30 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials