Kubernetes provides powerful management capabilities through its command-line tool kubectl
. One of the key functionalities is set
, which allows users to modify features on Kubernetes objects directly from the command line. This capability is particularly useful for adjusting configurations or settings dynamically without needing to edit YAML files manually.
Here are several examples demonstrating the use of kubectl set
with different Kubernetes objects:
Example 1: Setting environment variables on a Deployment
kubectl set env deployment/my-deployment ENV_VAR=value
This command updates the environment variable ENV_VAR
with the value value
on the Deployment named my-deployment
.
Verification: Verify the environment variable is set correctly by describing the Deployment and checking its environment section.
Example 2: Scaling replicas of a Deployment
kubectl scale deployment/my-deployment --replicas=3
This command adjusts the number of replicas of the Deployment named my-deployment
to 3.
Verification: List the pods to ensure three instances of the Deployment are running.
Example 3: Adding a label to a Pod
kubectl label pod/my-pod new-label=example
This command adds a label new-label
with the value example
to the Pod named my-pod
.
Verification: Describe the Pod and confirm the label has been applied.
Example 4: Setting a resource request on a Pod
kubectl set resources pod/my-pod --requests=cpu=200m,memory=512Mi
This command sets CPU request to 200 milliCPU and memory request to 512 MiB for the Pod named my-pod
.
Verification: Get the Pod’s YAML definition to see the updated resource requests.
Example 5: Updating an annotation on a Service
kubectl annotate service/my-service description='New description'
This command updates the annotation description
with the value New description
on the Service named my-service
.
Verification: Describe the Service to confirm the annotation has been updated.
Example 6: Setting a service account on a Pod
kubectl set serviceaccount pod/my-pod my-service-account
This command sets the service account my-service-account
on the Pod named my-pod
.
Verification: Describe the Pod and check its service account setting.
Example 7: Setting a command and arguments on a Pod
kubectl set command pod/my-pod -- ls -l
This command sets the command to ls
and arguments to -l
on the Pod named my-pod
.
Verification: Get the Pod’s YAML definition to see the updated command and arguments.
Example 8: Setting a node selector on a Deployment
kubectl set selector deployment/my-deployment app=example-app
This command sets the node selector to match nodes labeled with app=example-app
for the Deployment named my-deployment
.
Verification: Describe the Deployment and verify the node selector configuration.
Example 9: Setting a priority class on a Pod
kubectl set priority pod/my-pod high-priority
This command sets the priority class to high-priority
on the Pod named my-pod
.
Verification: Describe the Pod and ensure the priority class is applied correctly.
Example 10: Setting a toleration on a Pod
kubectl set tolerations pod/my-pod key=value:NoSchedule
This command sets a toleration with key key
, value value
, and effect NoSchedule
on the Pod named my-pod
.
Verification: Describe the Pod and check the tolerations section to confirm the toleration is set.
Also check similar articles.
Running Docker Images on Kubernetes with kubectl run
Expose Kubernetes Services Easily with kubectl expose
How to Create Kubernetes Resources from Files or Stdin
Overriding –databases Option in mysqldump
Creating Tab-Separated Output Files with mysqldump
Discussion about this post