├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Muhammad Rehan Saeed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kubernetes Cheat Sheet 2 | 3 | A cheat sheet for Kubernetes commands. 4 | 5 | ## Kubectl Alias 6 | 7 | Linux 8 | ``` 9 | alias k=kubectl 10 | ``` 11 | 12 | Windows 13 | ``` 14 | Set-Alias -Name k -Value kubectl 15 | ``` 16 | 17 | ## Cluster Info 18 | 19 | - Get clusters 20 | ``` 21 | kubectl config get-clusters 22 | NAME 23 | docker-for-desktop-cluster 24 | foo 25 | ``` 26 | 27 | - Get cluster info. 28 | ``` 29 | kubectl cluster-info 30 | Kubernetes master is running at https://172.17.0.58:8443 31 | ``` 32 | 33 | ## Contexts 34 | 35 | A context is a cluster, namespace and user. 36 | 37 | - Get a list of contexts. 38 | ``` 39 | kubectl config get-contexts 40 | ``` 41 | ``` 42 | CURRENT NAME CLUSTER AUTHINFO NAMESPACE 43 | docker-desktop docker-desktop docker-desktop 44 | * foo foo foo bar 45 | ``` 46 | 47 | - Get the current context. 48 | ``` 49 | kubectl config current-context 50 | foo 51 | ``` 52 | 53 | - Switch current context. 54 | ``` 55 | kubectl config use-context docker-desktop 56 | ``` 57 | 58 | - Set default namesapce 59 | ``` 60 | kubectl config set-context $(kubectl config current-context) --namespace=my-namespace 61 | ``` 62 | 63 | To switch between contexts, you can also install and use [kubectx](https://github.com/ahmetb/kubectx). 64 | 65 | ## Get Commands 66 | 67 | ``` 68 | kubectl get all 69 | kubectl get namespaces 70 | kubectl get configmaps 71 | kubectl get nodes 72 | kubectl get pods 73 | kubectl get rs 74 | kubectl get svc kuard 75 | kubectl get endpoints kuard 76 | ``` 77 | 78 | Additional switches that can be added to the above commands: 79 | 80 | - `-o wide` - Show more information. 81 | - `--watch` or `-w` - watch for changes. 82 | 83 | ## Namespaces 84 | 85 | - `--namespace` - Get a resource for a specific namespace. 86 | 87 | You can set the default namespace for the current context like so: 88 | 89 | ``` 90 | kubectl config set-context $(kubectl config current-context) --namespace=my-namespace 91 | ``` 92 | 93 | To switch namespaces, you can also install and use [kubens](https://github.com/ahmetb/kubectx/blob/master/kubens). 94 | 95 | ## Labels 96 | 97 | - Get pods showing labels. 98 | ``` 99 | kubectl get pods --show-labels 100 | ``` 101 | 102 | - Get pods by label. 103 | ``` 104 | kubectl get pods -l environment=production,tier!=frontend 105 | kubectl get pods -l 'environment in (production,test),tier notin (frontend,backend)' 106 | ``` 107 | 108 | ## Describe Command 109 | 110 | ``` 111 | kubectl describe nodes [id] 112 | kubectl describe pods [id] 113 | kubectl describe rs [id] 114 | kubectl describe svc kuard [id] 115 | kubectl describe endpoints kuard [id] 116 | ``` 117 | 118 | ## Delete Command 119 | 120 | ``` 121 | kubectl delete nodes [id] 122 | kubectl delete pods [id] 123 | kubectl delete rs [id] 124 | kubectl delete svc kuard [id] 125 | kubectl delete endpoints kuard [id] 126 | ``` 127 | 128 | Force a deletion of a pod without waiting for it to gracefully shut down 129 | ``` 130 | kubectl delete pod-name --grace-period=0 --force 131 | ``` 132 | 133 | ## Create vs Apply 134 | 135 | `kubectl create` can be used to create new resources while `kubectl apply` inserts or updates resources while maintaining any manual changes made like scaling pods. 136 | 137 | - `--record` - Add the current command as an annotation to the resource. 138 | - `--recursive` - Recursively look for yaml in the specified directory. 139 | 140 | ## Create Pod 141 | 142 | ``` 143 | kubectl run kuard --generator=run-pod/v1 --image=gcr.io/kuar-demo/kuard-amd64:1 --output yaml --export --dry-run > kuard-pod.yml 144 | kubectl apply -f kuard-pod.yml 145 | ``` 146 | 147 | ## Create Deployment 148 | 149 | ``` 150 | kubectl run kuard --image=gcr.io/kuar-demo/kuard-amd64:1 --output yaml --export --dry-run > kuard-deployment.yml 151 | kubectl apply -f kuard-deployment.yml 152 | ``` 153 | 154 | ## Create Service 155 | 156 | ``` 157 | kubectl expose deployment kuard --port 8080 --target-port=8080 --output yaml --export --dry-run > kuard-service.yml 158 | kubectl apply -f kuard-service.yml 159 | ``` 160 | 161 | ## Export YAML for New Pod 162 | 163 | ``` 164 | kubectl run my-cool-app —-image=me/my-cool-app:v1 --output yaml --export --dry-run > my-cool-app.yaml 165 | ``` 166 | 167 | ## Export YAML for Existing Object 168 | 169 | ``` 170 | kubectl get deployment my-cool-app --output yaml --export > my-cool-app.yaml 171 | ``` 172 | 173 | ## Logs 174 | 175 | - Get logs. 176 | ``` 177 | kubectl logs -l app=kuard 178 | ``` 179 | 180 | - Get logs for previously terminated container. 181 | ``` 182 | kubectl logs POD_NAME --previous 183 | ``` 184 | 185 | - Watch logs in real time. 186 | ``` 187 | kubectl attach POD_NAME 188 | ``` 189 | 190 | - Copy files out of pod (Requires `tar` binary in container). 191 | ``` 192 | kubectl cp POD_NAME:/var/log . 193 | ``` 194 | 195 | You can also install and use [kail](https://github.com/boz/kail). 196 | 197 | ## Port Forward 198 | 199 | ``` 200 | kubectl port-forward deployment/kuard 8080:8080 201 | ``` 202 | 203 | ## Scaling 204 | 205 | - Update replicas. 206 | ``` 207 | kubectl scale deployment nginx-deployment --replicas=10 208 | ``` 209 | 210 | ## Autoscaling 211 | 212 | - Set autoscaling config. 213 | ``` 214 | kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80 215 | ``` 216 | 217 | ## Rollout 218 | 219 | - Get rollout status. 220 | ``` 221 | kubectl rollout status deployment/nginx-deployment 222 | Waiting for rollout to finish: 2 out of 3 new replicas have been updated... 223 | deployment "nginx-deployment" successfully rolled out 224 | ``` 225 | 226 | - Get rollout history. 227 | ``` 228 | kubectl rollout history deployment/nginx-deployment 229 | kubectl rollout history deployment/nginx-deployment --revision=2 230 | ``` 231 | 232 | - Undo a rollout. 233 | ``` 234 | kubectl rollout undo deployment/nginx-deployment 235 | kubectl rollout undo deployment/nginx-deployment --to-revision=2 236 | ``` 237 | 238 | - Pause/resume a rollout 239 | ``` 240 | kubectl rollout pause deployment/nginx-deployment 241 | kubectl rollout resume deploy/nginx-deployment 242 | ``` 243 | 244 | ## Pod Example 245 | 246 | ``` 247 | apiVersion: v1 248 | kind: Pod 249 | metadata: 250 | name: cuda-test 251 | spec: 252 | containers: 253 | - name: cuda-test 254 | image: "k8s.gcr.io/cuda-vector-add:v0.1" 255 | resources: 256 | limits: 257 | nvidia.com/gpu: 1 258 | nodeSelector: 259 | accelerator: nvidia-tesla-p100 260 | ``` 261 | 262 | ## Deployment Example 263 | 264 | ``` 265 | apiVersion: apps/v1 266 | kind: Deployment 267 | metadata: 268 | name: nginx-deployment 269 | namespace: my-namespace 270 | labels: 271 | - environment: production, 272 | - teir: frontend 273 | annotations: 274 | - key1: value1, 275 | - key2: value2 276 | spec: 277 | replicas: 3 278 | selector: 279 | matchLabels: 280 | app: nginx 281 | template: 282 | metadata: 283 | labels: 284 | app: nginx 285 | spec: 286 | containers: 287 | - name: nginx 288 | image: nginx:1.7.9 289 | ports: 290 | - containerPort: 80 291 | ``` 292 | 293 | ## Dashboard 294 | 295 | - Enable proxy 296 | 297 | ``` 298 | kubectl proxy 299 | ``` 300 | 301 | # Azure Kubernetes Service 302 | 303 | [List of az aks commands](https://docs.microsoft.com/en-us/cli/azure/aks?view=azure-cli-latest) 304 | 305 | ## Get Credentials 306 | ``` 307 | az aks get-credentials --resource-group --name 308 | ``` 309 | 310 | ## Show Dashboard 311 | 312 | Secure the dashboard like [this](http://blog.cowger.us/2018/07/03/a-read-only-kubernetes-dashboard.html). Then run: 313 | ``` 314 | az aks browse --resource-group --name 315 | ``` 316 | 317 | ## Upgrade 318 | 319 | Get updates 320 | ``` 321 | az aks get-upgrades --resource-group --name 322 | ``` 323 | --------------------------------------------------------------------------------