Kubernetes – Kubectl Commands
As one of the key components of Kubernetes, Kubectl runs on the workstation on any machine. It can manage the nodes in the cluster.
Kubectl commands can be used to interact with Kubernetes objects and the cluster. In this chapter, we’ll cover a few commands.
kubectl annotate − It updates a resource’s annotation.
$kubectl annotate [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N = VAL_N [--resource-version = version]
example,
Tomcat description = ‘my frontend’; kubectl annotate pods
kubectl api-versions − The API versions supported by the cluster are listed.
$ kubectl api-versionkubectl apply − A resource can be configured by file or stdin.
$ kubectl apply –f <filename>kubectl attach − Things are attached to the running container with this command.
$ kubectl attach <pod> –c <container> $ kubectl attach 123456-7890 -c tomcat-conatiner
kubectl autoscale − This auto scales pods that are defined, like deployments, replica sets, and replication controllers.
$ kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min = MINPODS] -- max = MAXPODS [--cpu-percent = CPU] [flags] $ kubectl autoscale deployment foo --min = 2 --max = 10
kubectl cluster-info − Cluster information is displayed.
$ kubectl cluster-infokubectl cluster-info dump − For debugging and diagnosing, it dumps relevant information about the cluster.
$ kubectl cluster-info dump $ kubectl cluster-info dump --output-directory = /path/to/cluster-state
kubectl config − Change the kubeconfig file.
$ kubectl config <SUBCOMMAD> $ kubectl config –-kubeconfig <String of File name>
kubectl config current-context − The current context is displayed.
$ kubectl config current-context #deploys the current context
kubectl config delete-cluster − Removes the specified cluster from kubeconfig.
$ kubectl config delete-cluster <Cluster Name>kubectl config delete-context − Removes a specified context from Kubeconfig.
$ kubectl config delete-context <Context Name>kubectl config get-clusters − Displays the cluster defined in kubeconfig.
$ kubectl config get-cluster $ kubectl config get-cluster <Cluser Name>
kubectl config get-contexts − Describes one or more contexts.
$ kubectl config get-context <Context Name>kubectl config set-cluster − In Kubernetes, sets the cluster entry.
$ kubectl config set-cluster NAME [--server = server] [--certificateauthority = path/to/certificate/authority] [--insecure-skip-tls-verify = true]
kubectl config set-context − Creates a context entry in the Kubernetes entrypoint.
$ kubectl config set-context NAME [--cluster = cluster_nickname] [-- user = user_nickname] [--namespace = namespace] $ kubectl config set-context prod –user = vipin-mishra
kubectl config set-credentials − Creates a user entry in Kubeconfig.
$ kubectl config set-credentials cluster-admin --username = vipin -- password = uXFGweU9l35qcif
kubectl config set − An individual value is set in the kubeconfig file.
$ kubectl config set PROPERTY_NAME PROPERTY_VALUEkubectl config unset − In kubectl, it unsets a specific component.
$ kubectl config unset PROPERTY_NAME PROPERTY_VALUEkubectl config use-context − This command sets the current context in the kubectl file.
$ kubectl config use-context <Context Name>kubectl config view
$ kubectl config view $ kubectl config view –o jsonpath='{.users[?(@.name == "e2e")].user.password}'
kubectl cp − Transfer files between containers.
$ kubectl cp <Files from source> <Files to Destination> $ kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>
kubectl create − JSON or YAML formats are accepted for creating resources by filename or stdin.
$ kubectl create –f <File Name> $ cat <file name> | kubectl create –f -
With kubectl and the create command, we can create multiple things.
- deployment
- namespace
- quota
- secret docker-registry
- secret
- secret generic
- secret tls
- serviceaccount
- service cluster-ip
- service loadbalancer
- service
- nodeport
kubectl delete − Removes resources by file name, stdin, name, and resource.
$ kubectl delete –f ([-f FILENAME] | TYPE [(NAME | -l label | --all)])kubectl describe − Describes any resource in Kubernetes. Shows details of a resource or group of resources.
$ kubectl describe <type> <type name> $ kubectl describe pod tomcat
kubectl drain − The purpose of this is to drain a node so that it can be prepared for maintenance. It marks the node unavailable so that it will not be assigned with a new container.
$ kubectl drain tomcat –forcekubectl edit − In the command line tool, it’s used to end resources on the server. This allows you to edit resources directly.
$ kubectl edit <Resource/Name | File Name)Ex.
$ kubectl edit rc/tomcatkubectl exec − You can use this to run a command in the container.
$ kubectl exec POD <-c CONTAINER > -- COMMAND < args...> $ kubectl exec tomcat 123-5-456 date
kubectl expose − Pods, replication controllers, and services can be exposed as Kubernetes services via a running container or a YAML file.
$ kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol = TCP|UDP] [--target-port = number-or-name] [--name = name] [--external-ip = external-ip-of service] [--type = type] $ kubectl expose rc tomcat –-port=80 –target-port = 30000 $ kubectl expose –f tomcat.yaml –port = 80 –target-port =
kubectl get − Data about Kubernetes resources can be fetched using this command.
$ kubectl get [(-o|--output=)json|yaml|wide|custom-columns=...|custom-columns file=...| go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...] (TYPE [NAME | -l label] | TYPE/NAME ...) [flags]
For example,
$ kubectl get pod <pod name> $ kubectl get service <Service name>
kubectl logs − Printing the logs can be done by defining the container name in the pod. If the POD only has one container, no name needs to be defined.
$ kubectl logs [-f] [-p] POD [-c CONTAINER]Example
$ kubectl logs tomcat. $ kubectl logs –p –c tomcat.8
kubectl port-forward − Pods use them to forward one or more local ports.
$ kubectl port-forward POD [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N] $ kubectl port-forward tomcat 3000 4000 $ kubectl port-forward tomcat 3000:5000
kubectl replace − Replaces a resource with a file name or stdin.
$ kubectl replace -f FILENAME $ kubectl replace –f tomcat.yml $ cat tomcat.yml | kubectl replace –f -
kubectl rolling-update − Replaces the specified replication controller with a new replication controller by updating one POD at a time.
$ kubectl rolling-update OLD_CONTROLLER_NAME ([NEW_CONTROLLER_NAME] -- image = NEW_CONTAINER_IMAGE | -f NEW_CONTROLLER_SPEC) $ kubectl rolling-update frontend-v1 –f freontend-v2.yaml
kubectl rollout − Deployment can be managed by it.
$ Kubectl rollout <Sub Command> $ kubectl rollout undo deployment/tomcat
Other than that, we can do a lot with the rollout, like
- rollout history
- rollout pause
- rollout resume
- rollout status
- rollout undo
kubectl run − The Run command can be used to run an image on a Kubernetes cluster.
$ kubectl run NAME --image = image [--env = "key = value"] [--port = port] [-- replicas = replicas] [--dry-run = bool] [--overrides = inline-json] [--command] -- [COMMAND] [args...] $ kubectl run tomcat --image = tomcat:7.0 $ kubectl run tomcat –-image = tomcat:7.0 –port = 5000
kubectl scale − Scales Kubernetes deployments, replica sets, replication controllers, and jobs.
$ kubectl scale [--resource-version = version] [--current-replicas = count] -- replicas = COUNT (-f FILENAME | TYPE NAME ) $ kubectl scale –-replica = 3 rs/tomcat $ kubectl scale –replica = 3 tomcat.yaml
kubectl set image − A pod template’s image is updated.
$ kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1 = CONTAINER_IMAGE_1 ... CONTAINER_NAME_N = CONTAINER_IMAGE_N $ kubectl set image deployment/tomcat busybox = busybox nginx = nginx:1.9.1 $ kubectl set image deployments, rc tomcat = tomcat 6.0 --all
kubectl set resources − It updates resource/limits on objects with pod templates.
$ kubectl set resources (-f FILENAME | TYPE NAME) ([--limits = LIMITS & -- requests = REQUESTS] $ kubectl set resources deployment tomcat -c = tomcat -- limits = cpu = 200m,memory = 512Mi
kubectl top node − The top command shows the CPU/Memory/Storage usage for each node.
$ kubectl top node [node Name]A pod can also be used with the same command.