---
# System prepended metadata

title: Kubernetes 101

---

# Kubernetes 101

## Requirements

- [minikube](https://minikube.sigs.k8s.io/docs/start/)
- [kubectl](https://kubernetes.io/docs/tasks/tools/)

## Workshop

Welcome Kubernetes beginner! in this workshop we will try to deploy & expose an `nginx` container. The next diagram shows what we expect to deploy. :rocket: 

![](https://i.imgur.com/5VswjIr.png)

Basic stuff:
* [Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/): A Deployment provides declarative updates for Pods and ReplicaSets.
* [Service](https://kubernetes.io/docs/concepts/services-networking/service/): An abstract way to expose an application running on a set of Pods as a network service.

---

With all that said, now you're ready to start. :+1: 

1. Create a folder to work

```bash=
mkdir k8s-101-nginx
cd k8s-101-nginx/
```

2. Create a `k8s-101-nginx` namespace

```bash=
kubectl create namespace k8s-101-nginx
kubectl label namespace k8s-101-nginx istio-injection=enabled --overwrite
```

3. Create a `deployment.yaml` file

```yaml=
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: k8s-101-nginx
  labels:
    app: nginx 
spec:
  replicas: 3
  selector:
    matchLabels: # spec.selector.matchLabels must match with the labels at spec.template.metadata.labels
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
```

Check if your `Deployment` create the pods

```bash=
kubectl -n k8s-101-nginx get pods
```

Expected output

```bash=
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66b6c48dd5-gplp7   2/2     Running   0          58s
nginx-deployment-66b6c48dd5-m6lq9   2/2     Running   0          58s
nginx-deployment-66b6c48dd5-wk6vx   2/2     Running   0          58s
```


3. Create a `service.yaml` file

```yaml=
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: k8s-101-nginx
spec:
  selector: # This labels must match deployment.yaml spec.template.metadata.labels to work
    app: nginx 
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 80
```

Check if your `service` was created

```
kubectl -n k8s-101-nginx get svc
```

Expected output

```bash=
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
nginx-service   ClusterIP   10.0.128.147   <none>        8080/TCP   5m14s
```

Next we will expose the service in our own computer with `port-forward`

```
kubectl -n k8s-101-nginx port-forward svc/nginx-service 9090:8080
```

Now go to [http://localhost:9090](http://localhost:9090)

Expected output

![](https://i.imgur.com/tcjYVMW.png)

---

## Exposing your `nginx` to the world!! :world_map: 

![](https://i.imgur.com/K0j5eQV.png)

virtual-service.yaml

:::danger
Please replace ${slug} and ${environment} to match your cluster config!!!!
:::

```yaml=
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: nginx-vts
  namespace: k8s-101-nginx
spec:
  gateways:
  - default/global-gateway
  hosts:
  - ${slug}-${environment}-priv.fif.tech
  http:
  - name: "nginx"
    match:
    - uri:
        prefix: "/nginx-qa"
    rewrite:
      uri: "/"
    route:
    - destination:
        host: nginx-service.${slug}-${environment}.svc.cluster.local
        port:
          number: 8080
```

Gateway Example:

```
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: global-gateway
  namespace: default
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - '*.fif.tech'
    port:
      name: http
      number: 80
      protocol: HTTP
    tls:
      httpsRedirect: true
  - hosts:
    - '*.fif.tech'
    port:
      name: https
      number: 443
      protocol: HTTPS
    tls:
      credentialName: istio-ingressgateway-certs
      mode: SIMPLE
```