---
# System prepended metadata

title: 老船長共筆
tags: [Kubernetes]

---

---
tags: Kubernetes
---


# 老船長共筆

1. 演進史
    * OS 上架各種 Apps
    * VM
    * Containers
    * 但 container 越來越多該怎麼管理呢？


> 各服務倚靠 IP or DNS，現在有了 k8s，不需要了！ - By Wonder Paul

2. Kubernetes 提供的服務
    * Service Discovery and Load Balancing - 工程師不需要再知道哪些 container 放在哪台機器上，K8S 幫忙處理了
    * Secret & Configuration Management - 不再需要擔心 Secret 或 config 該怎麼處理，K8S 提供了 best practice

> 一個環境就是一個 k8s cluster。
> e.g. 只需要知道這個 cluster 就是 preparing 環境，不需要知道實際部署在哪一台機器上



## 名詞解釋
1. Master node (Master)
2. Worker node (Node)
3. Pods (Container)
4. Deployment: Pods 的藍圖

## 預先準備
### aws-cli
install guide: https://docs.aws.amazon.com/zh_tw/cli/latest/userguide/install-cliv2-mac.html

``` bash=
$ aws --version # 確認已安裝完成
```
### kubernetes-cli
``` bash=
$ brew install kubernetes-cli
$ kubectl version  # 確認已安裝完成
```

### 下載 kubeconfig 到本地
```bash=
$ aws eks --region us-east-1 update-kubeconfig --name paul-demo-1
```

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


### 測試是否運作成功
```bash=
$ kubectl get nodes
```



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

## 題目：

### 建立一個 `deployment.yml` , 產生 pods, pods 有 postgres, postgres 有 preparing Db import

ssh deployer@174.129.100.103

0. 拿到 member_Center_preparing.sql
    1. ssh 進去 ec2
    2. docker exec 
    3. pg_dump ....
        - `pg_dump sso_member_center_preparing -Fc -U postgres > member_center.sql`
    5. docker cp
        - `docker cp  postgres:member_center.sql /home/deployer`
    7. scp
        - `scp  deployer@174.129.100.103:~/member_center.sql ~/`
2. 建立一個 Dockerfile
    1. From postgres (postgres:9.6)
    2. POSTGRES_PASSWORD
    3. copy sql
    4. sh 跑 pg_restore
    5. copy sh /docker-entrypoint-initdb.d
3. 確認 run 時是我們要的結果
    1. docker build -t james-demo:1.0
    2. docker run -d ... -e POSTGRES_PASSWORD=testtest
    3. docker exec -it 
7. upload to ECR
    1. k8s-sandbox:<name>
8. configMap.yml 
9. deployment.yml
    1. 我要用那個 configMap
    2. 指定這個 image
6. kubectl apply -f deployment.yml
7. 確認結果 kubectl exec ....


* pg_dump member_center_preparing.sql
* sh(pg_restore) where ?
* 找一個 deploment.yml

> 把問題拆解成小問題，比較好 google


## 提示區
[pg_restore指令](https://www.postgresql.org/docs/9.2/app-pgrestore.html)

[pg_dump指令](https://www.postgresql.org/docs/9.3/app-pgdump.html)

[Postgres dockerHub](https://hub.docker.com/_/postgres)

* /docker-entrypoint-initdb.d
* POSTGRES_USER=deployer
* POSTGRES_DB=store_center_preparing
* POSTGRES_PASSWORD=testtest

> configMap Example
```
apiVersion: v1
kind: ConfigMap
metadata:
  name: sam-config
  labels:
    app: sam-app
data:
  POSTGRES_DB: store_center_preparing
  POSTGRES_USER: deployer
  POSTGRES_PASSWORD: testtest
```

> deployment.yaml
```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sam-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sam-postgres
  template:
    metadata:
      labels:
        app: sam-postgres
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

```

### References
1. [configMap](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/)

---

```bash=
$ kubectl get pods
$ kubectl exec -it [POD] -- [COMMAND]
```

---

## Q&A
1. 有 ingress controller 還需要 Load balancer 嗎？
  -> ingress controller inside each node
2. 