> For the complete documentation index, see [llms.txt](https://debugme.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://debugme.gitbook.io/project/the-kubernetes-book.md).

# The Kubernetes Book

Common tasks with kubernetes

## How to apply a deployment using kubectl

{% tabs %}
{% tab title="Shell" %}

```bash
$ kubectl apply -f posts.yaml
```

{% endtab %}

{% tab title="posts.yaml" %}

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: posts
spec:
  containers:
  - name: posts
    image: debugme/posts:0.0.1
    env:
    - name: PORT
      value: "3001"
    - name: EVENTS_PORT
      value: "3005"
    - name: HOST
      value: "http://localhost"
```

{% endtab %}
{% endtabs %}

## How to see what pods are running in your cluster

```
$ kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
posts   1/1     Running   0          5m24s
$ 
```

## How to see what files are inside your pod

```
$ kubectl exec posts -- ls
index.js
node_modules
package-lock.json
package.json
$ 
```

## How to see the logs for your pod

```
$ kubectl logs posts

> posts@1.0.0 start:docker
> nodemon index.js

[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
[posts] running on port 3001
$ 
```

## How to delete a pod

```
$ kubectl delete pod posts
pod "posts" deleted
$ 
```

## How to apply a deployment file

{% tabs %}
{% tab title="Shell" %}

```
$ kubernetes apply -f posts-depl.yaml
```

{% endtab %}

{% tab title="posts-depl-yaml" %}

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: posts-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: posts
  template:
    metadata:
      labels:
        app: posts
    spec:
      containers:
        - name: posts
          image: debugme/posts:0.0.1
          env:
          - name: PORT
            value: "3001"
          - name: EVENTS_PORT
            value: "3005"
          - name: HOST
            value: "http://localhost"
```

{% endtab %}
{% endtabs %}

## How to delete a deployment

```
$ kubernetes delete deployment posts-depl
```

## How to get a list of deployments

```
$ kubernetes get deployments
```

## How to get a description of a deployment

```
$ kubernetes describe deployment posts-depl
```

## How to update a deployment

{% tabs %}
{% tab title="ToDo" %}

```
(1) Update the code in your microservice
```

{% endtab %}

{% tab title="posts-depl.yaml" %}

```
// (1) replace the hard-coded "version" with "latest" i.e.

BEFORE
image: debugme/posts:x:y:z

AFTER
image: debugme/posts:latest
```

{% endtab %}

{% tab title="rebuild image" %}

```
$ docker build . -t debugme/posts:latest
```

{% endtab %}

{% tab title="push to docker hub" %}

```
$ docker push debugme/posts:latest
```

{% endtab %}

{% tab title="redeploy" %}

```
$ kubectl rollout restart deployment posts-depl 
```

{% endtab %}
{% endtabs %}

## What are the different types of Service objects?

{% hint style="info" %}
A service is used to allow communications between pods within a cluster or from outside a cluster to a specific pod
{% endhint %}

![](https://112629891-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LLv16VNxV5-IgEy69H0%2F-MPUNdc6qFsKDP_Ay5GR%2F-MPUPihfSyT5_H7_oeDt%2Fk8s-service-types.png?alt=media\&token=1f36d68b-2df5-4bb5-9faf-3be05794ddcb)

## How to access a pod from outside of a cluster

{% tabs %}
{% tab title="posts-srv.yaml" %}

```
apiVersion: v1
kind: Service
metadata:
  name: posts-srv
spec:
  type: NodePort
  selector:
    app: posts
  ports:
    - name: posts
      protocol: TCP
      port: 3001
      targetPort: 3001
```

{% endtab %}

{% tab title="create service" %}

```
$ kubectl apply -f ../infra/k8s/posts-srv.yaml
```

{% endtab %}

{% tab title="find NodePort" %}

```
$ kubectl describe service posts-srv
Name:                     posts-srv
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=posts
Type:                     NodePort
IP:                       10.106.229.229
LoadBalancer Ingress:     localhost
Port:                     posts  3001/TCP
TargetPort:               3001/TCP
NodePort:                 posts  30159/TCP  👈👈👈👈👈👈     
Endpoints:                10.1.0.22:3001
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
```

{% endtab %}

{% tab title="access pod" %}

```
// browser
http://localhost:30159/posts

// postman
GET http://localhost:30159/posts
```

{% endtab %}
{% endtabs %}
