brain/src/ops/k8s/kubectl.md

60 lines
1.3 KiB
Markdown

# kubectl
kubectl is the main command line tool for k8s. See [reference](https://kubernetes.io/docs/reference/kubectl/conventions/)
## Raw commands
Sometimes it's handy to just run a container without all the yaml mess.
### Running a container
```sh
kubectl run --image=IMAGE name
```
### apply is not always the answer
Even if you see `kubectl apply` everywhere sometimes it's handy to use dedicated commands
For example, `kubectl create` is used to create a ressource and will throw an error if a ressource already exists.
#### Imperative commands quick reference
Do not forget that `--dry-run=client` can be usefull
##### Create a simple pod
```sh
kubectl run nginx --image=nginx
```
##### Create a deployement
```sh
kubectl create deployment --image=nginx nginx
```
**Gotcha**, deployement do not have a `--replicas` option, you need to use `kubctl scale` after deployement creation
### Generating a manifests, blazzing fast way
```sh
kubectl run app --image=app --dry-run=client -o yaml
```
```sh
kubectl create deployment --image=redis redis --dry-run=client -o yaml
```
Of course, if you need to write it into a file
```sh
kubectl create deployment --image=redis redis --dry-run=client -o yaml > deploy-manifest.yml
```
### Switching namespaces contexts
```sh
kubectl config set-config $(kubectl config current-context) --namespace=target
```