> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ahmadraza.in/llms.txt
> Use this file to discover all available pages before exploring further.

# 2 General k8 cmd

# General Kubernetes Commands Documentation

## Creating and Managing Pods

### Create an NGINX Pod

```sh theme={null}
kubectl run nginx --image=nginx
```

### Generate POD Manifest YAML file (-o yaml). Don't create it (--dry-run)

```sh theme={null}
kubectl run nginx --image=nginx --dry-run=client -o yaml
```

## Creating and Managing Deployments

### Create a Deployment

```sh theme={null}
kubectl create deployment --image=nginx nginx
```

### Generate Deployment YAML file (-o yaml). Don't create it (--dry-run)

```sh theme={null}
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml
```

### Generate Deployment YAML file (-o yaml). Don’t create it (--dry-run) and save it to a file.

```sh theme={null}
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml
```

### Create a Deployment from a YAML file

```sh theme={null}
kubectl create -f nginx-deployment.yaml
```

### Create a Deployment with 4 replicas directly using command

```sh theme={null}
kubectl create deployment nginx --image=nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml
```

### Scale a Deployment using the kubectl scale command

```sh theme={null}
kubectl scale deployment nginx --replicas=4
```

## Creating and Managing Services

### Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379

```sh theme={null}
kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml
```

Or

```sh theme={null}
kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
```

### Create a Service named nginx of type NodePort to expose pod nginx's port 80 on port 30080 on the nodes

```sh theme={null}
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml
```

Or

```sh theme={null}
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml
```

## Practise Test Commands

### Create a Redis Pod with specific labels and no restart

```sh theme={null}
kubectl run redis-pod --image=redis:alpine --labels="app=redis,tier=db" --restart=Never
```

### Expose a Deployment named redis-deployment as a ClusterIP service

```sh theme={null}
kubectl expose deployment redis-deployment --name=redis-service --port=6379 --target-port=6379 --selector=app=redis,tier=db --type=ClusterIP
```

### Expose a Pod named custom-nginx as a NodePort service

```sh theme={null}
kubectl expose pod custom-nginx --name=custom-nginx-service --port=8080 --target-port=8080 --type=NodePort
```

### Create a new namespace

```sh theme={null}
kubectl create namespace dev-ns
```

### Edit a Deployment in a specific namespace

```sh theme={null}
kubectl edit deployment <deployment-name> -n <namespace>
```

### Expose a Pod named httpd as a ClusterIP service

```sh theme={null}
kubectl expose pod httpd --name=httpd --port=80 --target-port=80 --type=ClusterIP
```

### Replace a resource defined in a YAML file

```sh theme={null}
kubectl replace --force -f nginx.yml
```

### Continuously watch Pod status updates

```sh theme={null}
kubectl get pods --watch
```

### Describe a Node and grep for Taints

```sh theme={null}
kubectl describe node kubemaster | grep Taint
```

### Add a Taint to a Node

```sh theme={null}
kubectl taint node node01 spray=mortein:NoExecute
```

### Remove a Taint from a Node

```sh theme={null}
kubectl taint node controlplane spray=mortein:NoExecute-
```

### Add a label to a Node

```sh theme={null}
kubectl label node node01 size=large
```

## Metrics Server Commands

### Enable the Metrics Server add-on in Minikube

```sh theme={null}
minikube addons enable metric-server
```

### Clone the Metrics Server repository and deploy

```sh theme={null}
git clone https://github.com/kubernetes-sigs/metrics-server.git
kubectl create -f deploy/1.8+/
```

### Check Node resource usage

```sh theme={null}
kubectl top node
```

### Check Pod resource usage

```sh theme={null}
kubectl top pod
```

## Logs and Debugging

### View logs of a specific Pod

```sh theme={null}
kubectl logs <pod-name>
```

### View logs of a specific container within a Pod

```sh theme={null}
kubectl logs <podname> <cont-name>
```

***

> By using these commands, you can effectively manage your Kubernetes resources, from creating and scaling deployments to exposing services and monitoring resource usage. Each command provides a way to interact with the Kubernetes API, enabling you to automate and streamline your workflows.
