Skip to content

Instantly share code, notes, and snippets.

@pnijem
Last active August 1, 2022 09:37
Show Gist options
  • Save pnijem/150f368436ed4126538ff31ee6ee6fc4 to your computer and use it in GitHub Desktop.
Save pnijem/150f368436ed4126538ff31ee6ee6fc4 to your computer and use it in GitHub Desktop.
Kubernetes Useful Commands
Adding K8s (EKS) clusters

aws eks update-kubeconfig --name fieldin-qa --region eu-west-1 aws eks update-kubeconfig --name fieldin-dev --region eu-west-1

Creates an object i.e. deployment, configmap

kubectl apply -f <file.yaml>

Get resource

kubectl get <objecttype> e.g. kubectl get pods e.g. kubectl get deployments

 

Get resource with additional information

kubectl get <objecttype> -o wide e.g. kubectl get pods -o wide

 ##### Describe resource

kubectl describe <object type> [objectname] e.g. kubectl describe pod client-pod

 

Removes a deployment via the deployment file

kubectl delete -f <configfile.yaml>

Removes a deployment

kubectl delete <objecttype> <objectname>

e.g. kubectl delete deployment client-deployment

Removes a pod

kubectl delete pod <pod_name_id>

 

View the logs of the pod

kubectl logs <podname>

 

Connect via sh to pod

kuebctl exec -it <podname> sh

List contexts

kubectl config get-contexts

Switch context

kubectl config set current-context <context_name>

Switch context using a namespace

kubectl config set-context --current --namespace=<insert-namespace-name-here>

Expose port for specific deployment

kubectl expose deployment trackings-preprocess --type=NodePort --port=9090

Get information about a service

kubectl get service <service_name>

Mount external directory

kubectl cp <local_file> <pod_name>:<path_in_pod>

e.g. kubectl cp buffer.sql  db-buffer-6544788fb-bn24m:/var/tmp

 

Restart deployment

kubectl rollout restart deployment/<deployment>

 

Enable connections outside of the pod

kubectl port-forward <pod> <external_port>:<internal_port>

Generate token for K8s dashboard

kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"

 

View the last restart logs of a container

kubectl logs podname -c containername --previous e.g.  kubectl logs is-spraying-67cd646694-p9r5t -c is-spraying --previous

Change the number of pods running

kubectl scale deployment <DEPLOYMENT NAME> --replicas=<#PODS>

"System" related commands

kubectl -n kube-system get deployment kubectl -n kube-system get svc kubectl -n kube-system rollout restart deployment/coredns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment