K8s cheatsheet ========= #### Show stuff kubectl get pods kubectl get pods node1 kubectl get pods -o wide kubectl get pods bee -o wide kubectl get all kubectl get deployments #### Create an NGINX Pod - `kubectl run nginx --image=nginx` - `kubectl run nginx --image=nginx --dry-run=client -o yaml` - `kubectl run nginx --image=nginx --labels="tier=web"` ###### Deployment - `kubectl create deployment --image=nginx nginx` - `kubectl create deployment --image=nginx nginx --dry-run=client -o yaml` - `kubectl create deployment nginx --image=nginx --replicas=4` - `kubectl scale deployment nginx --replicas=4` - `kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml` - `kubectl create deployment --image=nginx nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml` - `kubectl create deployment --image=mojojo/secret secret` #this creates a deployment based on that image and names the deployment `secret` ###### Service ClusterIP - `kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml` - `kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml ` - `kubectl create service clusterip secret --tcp=8080:8080` #this creates a service named secret. It will automatically connect to the deployment named `secret`. NodePort - `kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml` - `kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml` Loadbalancer - `kubectl create service loadbalancer foo --tcp=8765:9376 --dry-run=client -o yaml` - `kubectl expose deployment example --port=8765 --target-port=9376 --name=example-service --type=LoadBalancer` ###### Labels - `kubectl get deployments.apps --show-labels` ###### Apply stuff from a file - kubectl create -f nginx-deployment.yaml ##### Imperative commands - `--dry-run=client allows you to preview the object that would be sent to your cluster without really submitting it.` - `-o yaml: This will output the resource definition in YAML format on screen.` ###### Namespace - `kubectl create namespace foonamespace` - Can do most of the above commands in a different namespace by appending `--namespace=newnsname` to them. i.e. `kubectl get deployments --namespace=dev`. - `kubectl get all -A` gets all resources in all namespaces