Using kubectl apply
allows Kubernetes configurations to be applied or updated based on the YAML or JSON files provided. This command is essential for deploying new resources, updating existing configurations, or even reverting to a previous state if needed.
Here are several examples showcasing the versatility of kubectl apply
:
Example 1: Deploying a new Pod from a YAML file:
kubectl apply -f pod.yaml
This command applies the Pod configuration defined in pod.yaml
. To verify, use kubectl get pods
to see if the Pod is running.
Example 2: Updating a Deployment with a new image:
kubectl apply -f deployment.yaml
If deployment.yaml
specifies a new Docker image, this command updates the Deployment accordingly. Check the Deployment status with kubectl get deployment
.
Example 3: Applying a Service definition:
kubectl apply -f service.yaml
Creates or updates a Kubernetes Service based on the configuration in service.yaml
. Confirm the Service’s creation with kubectl get services
.
Example 4: Adding a ConfigMap to the cluster:
kubectl apply -f configmap.yaml
This applies the ConfigMap defined in configmap.yaml
. Verify its existence with kubectl get configmap
.
Example 5: Applying a PersistentVolumeClaim:
kubectl apply -f pvc.yaml
Creates or updates a PersistentVolumeClaim as defined in pvc.yaml
. Check its status using kubectl get pvc
.
Example 6: Deploying a CronJob:
kubectl apply -f cronjob.yaml
This command schedules a new CronJob based on the configuration provided in cronjob.yaml
. Use kubectl get cronjob
to see its execution status.
Example 7: Applying a HorizontalPodAutoscaler:
kubectl apply -f hpa.yaml
Creates or updates a HorizontalPodAutoscaler defined in hpa.yaml
. Verify its creation using kubectl get hpa
.
Example 8: Deploying a StatefulSet:
kubectl apply -f statefulset.yaml
Applies the StatefulSet configuration from statefulset.yaml
. Check its deployment status with kubectl get statefulset
.
Example 9: Updating a Namespace:
kubectl apply -f namespace.yaml
If namespace.yaml
modifies Namespace attributes, this command updates it. Ensure changes with kubectl get namespace
.
Example 10: Applying a Role and RoleBinding:
kubectl apply -f role.yaml -f rolebinding.yaml
Applies both Role and RoleBinding configurations defined in respective files. Verify creation with kubectl get roles
and kubectl get rolebindings
.
Also check similar articles.
Diffing Kubernetes Configurations with kubectl diff
Listing Kubernetes Events with kubectl events
Troubleshooting Kubernetes with kubectl debug
Managing Kubernetes Authorization with kubectl auth
Copy Files to and from Kubernetes Containers with kubectl cp
Discussion about this post