-
Notifications
You must be signed in to change notification settings - Fork 1
Kubernetes
Andres Olarte edited this page Jan 11, 2022
·
26 revisions
kubectl config get-contexts
kubectl config current-context
Set default Namespace
kubectl config set-context my-context --namespace=demo
Remove default Namespace
kubectl config set-context my-context --namespace=
Delete all contexts
kubectl config get-contexts -o=name | xargs -n 1 kubectl config delete-context
Are there endpoints matching the service:
kubectl get endpoints hostnames
To get credentials for kubectl
to work
gcloud container clusters get-credentials example-cluster
To show the running config:
kubectl exec -it `kubectl get pods -o custom-columns=name:metadata.name | grep nginx-ingress-controller` -- cat /etc/nginx/nginx.conf
kubectl label pods my-pod new-label=awesome
Create a simple load balancer
cat > rc.yaml <<- EOM
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
EOM
kubectl create -f ./rc.yaml
kubectl expose rc nginx --port=80 --target-port=80 --name=nginx-lb --type=LoadBalancer
To delete it:
kubectl delete service nginx-lb
Display any HostPort
for all containers in all pods.
kubectl get pods --all-namespaces \
-o=jsonpath='{range .items[*]}Pod {.metadata.namespace}.{.metadata.name}:{range .spec.containers[*]} Container {.name}:{range .ports[*]} HostPort {.hostPort}:{end}:{end}:{end}:' \
| tr ":" "\n"
This uses tr
to convert :
to \n
.
kubectl -n NAMESPACE port-forward POD_NAME 9090:9090 &
or
kubectl -n NAMESPACE port-forward svc/SERVICE_NAME 9090:9090 &
kubectl describe pods
kubectl exec -it POD_NAME -- /bin/bash
kubectl exec -it deploy/DEPLOYMENT_NAME -- /bin/bash
kubectl get pods
kubectl exec -it POD_NAME -c CONTAINER_NAME -- /bin/bash
With busybox
:
kubectl run -i --tty --rm debug --image=busybox --restart=Never -- sh
With an Ubuntu image:
kubectl run -i --tty --rm debug --image=ubuntu --restart=Never -- bash
How I make curl work directly ?
kubectl run my-shell --rm -i --tty --image byrnedo/alpine-curl -- http://web1-service.gw.svc.cluster.local/
In this case we will ignore the image's entry point, and use /bin/sh
as the entry point.
kubectl run my-shell --rm -i --tty --command --image byrnedo/alpine-curl -- /bin/sh
With ssh
client:
kubectl run -i --tty --rm debug --image=kroniak/ssh-client --restart=Never -- bash
kubectl run --namespace ns my-shell --rm -i --tty --image amouat/network-utils -- bash
kubectl api-resources -o wide
The output will look like:
NAME SHORTNAMES APIGROUP NAMESPACED KIND VERBS
bindings true Binding [create]
componentstatuses cs false ComponentStatus [get list]
serviceentries networking.istio.io true ServiceEntry [delete deletecollection get list patch create update watch]
virtualservices networking.istio.io true VirtualService [delete deletecollection get list patch create update watch]
To use the resources:
kubectl get <NAME or SHORTNAME>.<APIGROUP>
kubectl get serviceentries.networking.istio.io
kubectl create sa viewer
kubectl create clusterrolebinding viewer --clusterrole=view --serviceaccount=default:viewer
kubectl run -i --tty --rm debug --image=bitnami/kubectl --restart=Never --serviceaccount='viewer' -- get pods --all-namespaces