---
# System prepended metadata

title: TOI K8s

---

# TOI K8s
![](https://i.imgur.com/1bQ8kLa.jpg)

<!-----------  ---------- ---------- ---------- ---------- -->

## Deployment
Create custom namespace
```
$ kubectl create namespace <your-namespace>
```
Verify namespaces created
```
$ kubectl get namespace
```
Apply deployment.yaml
```
$ kubectl apply -f deployment.yaml
```

`deployment.yaml`
```yaml=
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: albert-poc
  labels:
    app: nginx
spec:
  replicas: 3 # 同時建立 3 個 nginx 的 pod
  # replicaset 的效果套用在帶有 app=nginx 的 pod 上
  # 必須要與下面的 pod label 有相符合
  selector:
    matchLabels:
      app: nginx
  # .spec.template 其實就是 pod 的定義
  template:
  # pod metadata
    metadata:
    # 設定給 pod 的 label 資訊
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
```
Get deployment pods
```
$ kubectl get pods -n <your-namespace>
$ kubectl get pod -o wide -n <your-namespace> -> detail informations
```
Port forward and verify pods
```
$ kubectl port-forward -n <your-namespace> <pod-name> 8000:80
```
Verify on local
```
$ 127.0.0.1:8080
```
![](https://i.imgur.com/SUZ4Y21.png)

Reference: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

<!-----------  ---------- ---------- ---------- ---------- -->

---
## Service
Apply service.yaml
```
$ kubectl apply -f service.yaml
```
`service.yaml`
```yaml=
apiVersion: v1
kind: Service
metadata:
  name: nginx-deployment
  namespace: albert-poc
spec:
  type: ClusterIP
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

```
Get service 
```
$ kubectl get svc -n <your-namespace>
$ kubectl describe svc -n <your-namespace>
```
Port forward
```
$ kubectl port-forward svc/<service-name> -n <pod-name> 8000:80
```
Verify on local
```
$ 127.0.0.1:8080
```
![](https://i.imgur.com/SUZ4Y21.png)

<!-----------  ---------- ---------- ---------- ---------- -->
---
Service has three type:
![](https://i.imgur.com/mVyM75W.png)
 1. ClusterIP
    - Default. It means we want our pod just be accessed in cluster.
![](https://i.imgur.com/xIQAa8L.png)


2. NodePort
    - Open to outside. Rarely use in our team. You may use it when you want to create k8s with yourself.
![](https://i.imgur.com/oILYuxz.png)


3. LoadBalancer
    - Only can use in cloud like aws eks, we'll use this type to finish our task.
![](https://i.imgur.com/SVyymlA.png)


### Difference
![](https://i.imgur.com/CKEO9F5.png)

Reference:
1. [StackoverFlow](https://stackoverflow.com/questions/41509439/whats-the-difference-between-clusterip-nodeport-and-loadbalancer-service-types#:~:text=A%20ClusterIP%20service%2C%20to%20which,a%20cloud%20provider's%20load%20balancer)
2. [k8s](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/)


---

<!-----------  ---------- ---------- ---------- ---------- -->

## Istio virtual service

Apply istio-virtualservice.yaml
```
$ kubectl apply -f istio-virtualservice.yaml
```

`istio-virtualservice.yaml`
```yaml=
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: albert-poc
  namespace: albert-poc
  labels:
    app: nginx
spec:
#The destination hosts to which traffic is being sent. 
#Could be a DNS name with wildcard prefix or an IP address
  hosts:
  - albert-poc-eu1.test.mgcp.a1q7.net
  gateways:
  # The names of gateways and sidecars that should apply these routes.
  - albert-poc
  http:
  - route:
    - destination:
        # Specify to service
        host: nginx-deployment
        port:
          number: 80
```
Reference: https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPRouteDestination

---
<!-----------  ---------- ---------- ---------- ---------- -->

## Istio Gateway

Apply istio-gateway.yaml
```
$ kubectl apply -f istio-gateway.yaml
```

`istio-gateway.yaml`
```yaml=
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: albert-poc
  namespace: albert-poc
  labels:
    app: nginx
spec:
  selector:
    istio: ingressgateway-private-mgcp
  servers:
  - port:
      number: 443
      name: http
      protocol: HTTP
    hosts:
    - albert-poc-eu1.test.mgcp.a1q7.net
```
Reference: https://istio.io/latest/docs/reference/config/networking/gateway/

---

## CheckPoint
Verify https works
[albert-poc-eu1.test.mgcp.a1q7.net](https://albert-poc-eu1.test.mgcp.a1q7.net/) 

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

DNS record might cache wrong information, if so
wait DNS cach to expire
```
$ Dig albert-poc-eu1.test.mgcp.a1q7.net
```

```
$ nslookup albert-poc-eu1.test.mgcp.a1q7.net
```

## Delete resource
```
$ kubectl delete namespaces albert-poc
```

## PV & PVC 
Pod 分為有狀態(stateful)和無狀態(stateless)兩種，大多數情況下都是使用 stateless，兩者差別在於 stateful 會有一個儲存空間，被稱為 [Persistent Volumes](https://kubernetes.io/docs/concepts/storage/persistent-volumes/)(PV)，可以理解為一個持久存在的 volume，不會因為 Pod 的關閉而跟著消失，而 [Persistent Volume Claim](https://kubernetes.io/docs/concepts/storage/persistent-volumes/#claims-as-volumes)(PVC) 則可以對 PV 發出請求。

# Reference from Louis
https://hackmd.io/@Louis-Su/HJdpuYHqc





