Kubernetes commands in Unix/Linux

Make a note with a list of commands for working with Kubernetes. Supplement will as necessary use of certain teams at work or on my server.

For starters, if you use or plan to use vi/vim, then add the settings. Open:

$ vim ~/.vimrc


"Yaml file handling autocmd FileType yaml using the setlocal ts=2 sts=2 sw=2 expandtab filetype plugin indent on autocmd FileType yaml setl indentkeys-=<:>" Copy paste with ctr+c, ctr+v etc :behave mswin :set clipboard=unnamedplus :smap <Del> <C-g>"_d :smap <C-c> <C-g>y :smap <C-x> <C-g>x :imap <C-v> <Esc>pi :smap <C-v> <C-g>p :smap <Tab> <C-g>1> :smap <S-Tab> <C-g>1<

You can also add auto-completion of commands for kubectl.

If you are using BASH:

$ source <(kubectl bash completion) &&  echo "source <(kubectl bash completion)" >> ~/.bashrc

Restart the shell or you can run:

$ bash ~/.bashrc

If you are using ZSH:

$ source <(kubectl completion zsh) &&  echo "if [ $commands[kubectl] ]; then source <(kubectl completion. zsh); fi" >> ~/.zshrc

Restart the shell or you can run:

$ . ~/.zshrc

And so, let’s get to the teams.

Working with POD-s in Kubernetes

Bring all the pods that have deplores:

$ kubectl get pods NAME STATUS AGE READY RESTARTS mirrormaker-in-kubernetes-chart-mirrormaker-in-kubernetes-mpsb8 0/1 0 Pending 19d

Bring all of the name-space that use pods:

$ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE default mirrormaker-in-kubernetes-chart-mirrormaker-in-kubernetes-mpsb8 0/1 0 Pending 19d docker compose-7b7c5cbbcc-6l5gt 1/1 28d 0 Running docker compose-api-dbbf7c5db-jxmz4 1/1 Running 1 28d 

To display information about zaleplon UNDER-s, use:

$ kubectl get pod mirrormaker-in-kubernetes-chart-mirrormaker-in-kubernetes-mpsb8 -o wide NAME READY STATUS AGE RESTARTS the IP NODE NOMINATED NODE READINESS GATES mirrormaker-in-kubernetes-chart-mirrormaker-in-kubernetes-mpsb8 0/1 0 Pending 19d <none> <none> <none> <none>

Ie the syntax is:

$ kubectl get pod <NAME_of_POD_HERE> -o wide

Or to display the info in a YAML output, use:

$ kubectl get pod <NAME_of_POD_HERE> -o yaml

If you want to get YAML output from UNDER-and without information about the cluster, here is the command:

$ kubectl get pod my-pod-o yaml --export

For outputdiscribe the POD and running:

$ kubectl describe pod <NAME_of_POD_HERE>


$ kubectl describe pods <NAME_of_POD_HERE>

It is possible still so:

$ kubectl describe nodes <NAME_of_POD_HERE>

If there is a need to sort the pods, such as the number of restarts, the command:

$ kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

To all pods on a particular label:

$ kubectl get pods --selector=app=my_app_here -o  jsonpath='{.items[*].metadata.labels.version}'

Also, if necessary, to list all pods for a specific namespace and to UNDER-s was launched:

$ kubectl get pods --field-selector=status.phase=Running-n docker NAME READY STATUS RESTARTS AGE compose-7b7c5cbbcc-6l5gt 1/1 0 Running 28d compose-api-dbbf7c5db-jxmz4 1/1 Running 1 28d

To ExternalIPs all nodes:

$ nodes kubectl get-o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

To UNDER-s with all the brands that they have by running the command:

$ kubectl get pods --show-labels

Delete under, you can:

$ kubectl delete pod <NAME_OF_POD_HERE_1> <NAME_OF_POD_HERE_2>

To remove UNDER file in which the configuration is stored, you can:

$ kubectl delete-f ./pod.json 

To remove UNDER-s at a specific label:

$ kubectl delete pods -l name=myLabel 

To delete all UNDER-s in a certain namespace:

$ kubectl -n <NAMESPACE_HERE> delete pod --all

Remove all POD-s matching pattern1 or pattern2 awk patterns:

$ kubectl get pods -n <NAMESPACE_HERE> --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete-n <NAMESPACE_HERE> pod

Go ahead.

Working with Deployments to Kubernetes

Create a single deployment:

$ kubectl run <NAME_of_POD_HERE> --image=<NAME_of_IMAGE_HERE> --record

Get a list of deployments:

$ kubectl get deployments

Moving update “www” containers “frontend” of the deployment update the image:

$ kubectl set image deployment/frontend www=image:v2

The history of the deployments can be obtained as follows:

$ kubectl history rollout deployment/<YOUR_DEPLOYMENT_NAME_HERE>

If you want to get specific, then perform:

$ kubectl rollout undo deployment/<YOUR_DEPLOYMENT_NAME_HERE> --to-revision=N

Observe continuous update of the deployment status of the “external interface” to the end

$ kubectl rollout status-w deployment/<YOUR_DEPLOYMENT_NAME_HERE>

To deploy several replicas with the application running:

$ kubectl scale deployment/<NAME_of_POD_HERE> --replicas=<N_COUNT_REPLICAS>

You can use the file:

$ kubectl scale --replicas=3-f scale_replicas.yaml

If the current size deploymen equals 3, scale it to 4:

$ kubectl scale --current-replicas=3 --replicas=4 deployment/<YOUR_DEPLOYMENT_HERE>

You can make a replica for multiple deployments, for example:

$ kubectl scale --replicas=5 rc/<YOUR_DEPLOYMENT_HERE_1> rc/<YOUR_DEPLOYMENT_HERE_2> rc/<YOUR_DEPLOYMENT_HERE_3>

To replace the description of which is contained in the file, you can:

$ cat pod.json | kubectl replace-f -

You can run the command and re-create UNDER the power (there Will be autec!):

$ kubectl replace --force -f ./pod.json

And so on.

Working with Services in Kubernetes

To get a list of services running:

$ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ClusterIP kubernetes <none> 443/TCP 28d

If there is a need to sort the services by name, use the command:

$ kubectl get services --sort-by=.metadata.name

To create a POD as a service (creates endpoints):

$ kubectl expose deployment/<YOUR_DEPLOYMENT_NAME_HERE> --port=8080 --type=NodePort

To remove service for a particular label:

$ kubectl delete service-l name=myLabel 

To delete all service available in a specific namespace:

$ kubectl -n <NAMESPACE_HERE> delete service --all

Marching on!

Working with Volumes in Kubernetes

To get a list of Persistent Volumes:

$ kubectl get pv

To get a list of Persistent Volumes Claims:

$ kubectl get pvc

If you need sorting, you can use:

$ kubectl get pv-n <YOUR_NAMESPACE_HERE> --sort-by=.spec.capacity.storage

You can use other keys if necessary.

Working with Secrets in Kubernetes

For starters, get a list of the secret:

$ kubectl get secrets NAME AGE TYPE DATA default-token-rr6t6 kubernetes.io/service-account-token 3 28d sh.helm.release.v1.mirrormaker-in-kubernetes-chart.helm v1.sh/release.v1 1 19d

If you do not know how to create secret using the CLI, you can use help:

$ kubectl create secret generic --help

For example, create a here’s a secret:

$ kubectl create secret generic mysql --from-literal=password=root

Get information about the generated secret so:

$ kubectl get secrets mysql -o yaml apiVersion: v1 data: password: cm9vdA== kind: Secret metadata: creationTimestamp: "2019-12-23T21:33:24Z" name: mysql namespace: default resourceVersion: "210625" selfLink: /api/v1/namespaces/default/secrets/mysql uid: e0ad6215-cc96-4e03-9ea8-ca5a58a5980c type: Opaque 

Or, you can create a secret in the following way:

$ cat <<EOF | kubectl apply-f - apiVersion: v1 kind: Secret metadata: name: mysecret type: Opaque data: password: $(echo-n "s33msi4" | base64 -w0) username: $(echo-n "jane" | base64 -w0) EOF

Whatever you like, and use.

Bring all the secrets that are being UNDER-AMI:

$ kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

What’s next? Then go….

Working with ConfigMaps in Kubernetes

To create a config mapou, such as JS file:

$ kubectl create configmap <configmap_name_here> --from-file=config.js

Get info on config-Marie:

$ kubectl get configmap <configmap_name_here> -o yaml

Working with DNS in Kubernetes

Get all the DNS’s on all namespaces:

$ kubectl get pods --all-namespaces |grep dns kube-system coredns-5c98db65d4-95dlc 1/1 1 Running 28d kube-system coredns-5c98db65d4-rhvvs 1/1 Running 1 28d

Check DNS for nginx UNDER-and (assuming that POD / container runs), you can:

$ kubectl exec-ti busybox -- nslookup nginx

Note: kube-proxy running in the worker nodes, controls services and installs iptables rules to direct traffic.

Work with Ingress in Kubernetes

Get all ingresy:

$ kubectl get ingress

Or, if you must use a different namespace:

$ kubectl get ingress-n docker No resources found in the docker namespace.

Commands to control the type of service Ingress for ClusterIP:

$ kubectl expose deployment <YOUR_DEPLOYMENT_HERE> --port=2368

Go ahead.

Working with Horizontal Autoscaler Pod in Kubernetes

To get all the pods that were zaleplone in a horizontal masturbirovala, so:

$ kubectl get hpa

Or, if you want to specify a different name for the namespace:

$ kubectl get hpa-n docker No resources found in the docker namespace.

Zaleplon service replica, for example:

$ kubectl autoscale deployment <YOUR_DEPLOYMENT_HERE> --min=6 --max=666 

For help, use:

$ kubectl --help autoscale

Working with DaemonSets in Kubernetes

To get Dimensity is to perform:

$ kubectl get daemonsets


$ kubectl get ds

Will add on if there infa.

Work with Scheduler Kubernetes

The policy on the basis NodeSelector:

$ kubectl label node minikube foo=bar

Linking of nodes through the API server:

$ kubectl proxy: $ curl -H "Content-Type: application/json" -X POST --data @binding.json http://localhost:8001/api/v1/namespaces/default/pods/foobar-sched/binding

Working with Tains and Tolerations in Kubernetes

Command is:

$ kubectl taint master node foo=bar:NoSchedule

Something like that.

Troubleshooting in Kubernetes

And so, here is a list of commands which will help in Troubleshooting K8S:

$ kubectl describe $ kubectl logs $ kubectl exec $ nodes kubectl get --show-labels $ kubectl get events

Example, if you want to get the logs is a:

$ kubectl logs <YOUR_POD_HERE>

The same thing, but using labels:

$ kubectl log -l name=<YOUR_LABEL_HERE> 

To bring the logs from preduduschego instala:

$ kubectl logs <YOUR_POD_HERE> --previous 

The same thing, but MultiCover:


The log dump with the name label = yourLabel (standard output):

$ kubectl log -l name= yourLabel -c <YOUR_CONTAINER_HERE>

You can add the option “-f” to get the logs in real time:

$ kubectl log -f <YOUR_POD_HERE>

To run the pod as an interactive shell:

$ kubectl run-i --tty --image busybox=busybox -- sh

To start nginx in the pod in a specific namespace:

$ kubectl run nginx --image=nginx --restart= "Never" - n your_namespace_here

Start pod with nginx and write its specification in a file with the name pod.yaml can be as follows:

$ kubectl run nginx --image=nginx --restart= "Never" --dry-run -o yaml > pod.yaml

To pratcise to the container, you can:

$ kubectl attach <YOUR_POD_HERE> -i

To make forwarding ports from the local machine (for example 5555) UNDER the (port 6666) as follow:

$ kubectl port-forward <YOUR_POD_HERE> 5555:6666

A command to execute on the UNDER-e (in one container):

$ kubectl exec <YOUR_POD_HERE> -- ls / 

A command to execute on the UNDER-e (several containers):

$ kubectl exec <YOUR_POD_HERE> -c <YOUR_CONTAINER_HERE> -- ls / 

Show the metrics for this UNDER-and containers:

$ kubectl top pod <YOUR_POD_HERE> --containers

Display a list of events sorted by timestamp:

$ kubectl get events --sort-by=.metadata.creationTimestamp

Maybe something else useful I will complement here.

The interaction with the nodes in the cluster

You can mark the node as “unreachable” and that it was not the traffic:

$ kubectl cordon <YOUR_POD_HERE>

Deduce the node to mannens window:

$ kubectl drain <YOUR_POD_HERE>

Return node you can:

$ kubectl uncordon <YOUR_POD_HERE>

To see metrics on the host:

$ kubectl top node <YOUR_POD_HERE>

To display the master address and its services, follow:

$ kubectl cluster-info Kubernetes master is running at https://kubernetes.docker.internal:6443 KubeDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Displays the status of the cluster dump to stdout:

$ kubectl cluster-info dump

You can record the current status of the dump cluster in the file, for example:

$ kubectl cluster-info dump --output-directory=./k8s_state.json 

Go ahead.

Working with Role Based Access Control in Kubernetes

List all the supported resource types along with their short names, the group API, whether they are namespace and Kind:

$ kubectl api resources

Taks to bring all the resources namespace:

$ kubectl api-resources --types are namespaced=true

To display all resources not related to the namespace:

$ kubectl api-resources --types are namespaced=false

To get only the list of names you can use:

$ kubectl api-resources -o name

Or, use this approach (show all resources with advanced (aka “wide”) output):

$ kubectl api. resources o wide 

To get all the resources that support the “list” and “get”:

$ kubectl api-resources --verbs=list,get

To all resources in the group API “Extensions”:

$ kubectl api-resources --api-group=extensions NAME SHORTNAMES APIGROUP types are namespaced KIND daemonsets ds extensions true DaemonSet deployments deploy Deployment ingresses true extensions ing extensions true extensions Ingress networkpolicies netpol true NetworkPolicy podsecuritypolicies psp extensions false PodSecurityPolicy replicasets rs extensions true ReplicaSet

Almost came to the end…

Working with Role Based Access Control in Kubernetes

Create the role:

$ kubectl create role YOUR_ROLE_HERE --verb=get --verb=list --verb=watch --resource=pods

Get info on Rolet:

$ kubectl get rolebinding <YOUR_ROLE_HERE> -o yaml

Working with Security Contexts in Kubernetes

To deploy the context which is described in the file, do:

$ kubectl apply-f https://k8s.io/examples/pods/security/security-context.yaml

Or 2nd example:

$ kubectl apply-f https://k8s.io/examples/pods/security/security-context-2.yaml

Will complete later, when you are faced with the need….

Working with Security Policies the Pod in Kubernetes

Will complete later, when you are faced with the need….

Work with Network Policies in Kubernetes

Create annotation:

$ kubectl annotate ns <namespace> "net.beta.kubernetes.io/network-policy={"ingress": {"isolation": "DefaultDeny"}}"

To obtain a complete or updated information, we suggest you contact the official documentation!

That’s all, “the Kubernetes Commands in Unix/Linux” is completed.

Source: linux-notes.org

(Visited 2 times, 1 visits today)