---
# System prepended metadata

title: Navi kubectl 操作

---

## 安裝以下套件
- kubectl
- kubectx

## 加入 AWS EKS 
```
$ aws eks --region us-east-1 update-kubeconfig --name GAIEKS (production)
$ aws eks --region us-east-1 update-kubeconfig --name aws-rd-develop
$ aws eks update-kubeconfig --name GAIEKS --region aws-region
$ aws eks update-kubeconfig --name eks-cluster-name --region aws-region
$ aws eks update-kubeconfig --name aws-rd-develop --region ap-southeast-1
$ kubectl config use-context arn:aws:eks:us-east-1:185271018684:cluster/aws-rd-develop
$ kubectl config use-context arn:aws:eks:us-east-1:185271018684:cluster/GAIEKS (production)
```
$ mac
aws eks update-kubeconfig --name aws-rd-develop --region us-east-1

## 上傳k8s到AWS

> 1. ```aws ecr get-login-password --region ap-southeast-1 | docker login --username AWS --password-stdin 185271018684.dkr.ecr.ap-southeast-1.amazonaws.com```

>  2. ```cd media-transcriber-worker```

>  3. ```docker buildx build . -t 185271018684.dkr.ecr.ap-southeast-1.amazonaws.com/consumer-app:latest```

>  4. ```docker push 185271018684.dkr.ecr.ap-southeast-1.amazonaws.com/consumer-app:latest```

## 觀看pod log，並且建立新的pod，在k8s上做程式改動追蹤。
> 1. ```kubectl get pods -n ai-package-202-eks-develop```：可以看那些worker運行

> 2. ```kubectl get pod -n job-queue```：可看pods狀態
> ![image](https://hackmd.io/_uploads/B1k06AuZC.png)
> 一般都是時間離現在最短的那個如:embedding-consumer-app-job-20240426072846-2hnv5

> 3. ```kubectl logs embedding-consumer-app-job-20240426072846-2hnv5 -n job-queue```
> ![image](https://hackmd.io/_uploads/SJY1LyF-A.png)
> 就會看出裡面的log有什麼，可以從檔名'Updating Redis for 台灣高速鐵路列車運行策略_論文.docx'，看出是不是自己上傳的檔案，是的話就把這個pod名稱輸出yaml

> 4. ```kubectl get po embedding-consumer-app-job-20240426072846-2hnv5 -n job-queue -o yaml```：-o意思是output，輸出yaml語法。
> ![image](https://hackmd.io/_uploads/r1VbPJYWC.png)
> 複製yaml輸出：
```
apiVersion: v1
kind: Pod
metadata:
    annotations:
    .
    .
    .
============這裡以下到status不用===============
status:
```

> 5. 複製到該路徑底下的檔案如test.yaml
> 
```
spec:
    -env
        -name
        .
        .
        .
        image: 185271018684.dkr.ecr.ap-southeast-1.amazonaws.com/consumer-app:latest
        ==============這裡下面開始新增================
        command:
        - /bin/sh
        - -c
        - sleep 360000
```
> 6. 其中metadata/name可以更換成自己的名稱例如：ian-embedding-consumer-app-job，方便知道自己的pod名稱。
```
metadata:
  annotations:
    kubernetes.io/psp: eks.privileged
  creationTimestamp: "2024-04-26T07:28:46Z"
  generateName: embedding-consumer-app-job-20240426072846-
  labels:
    controller-uid: c5d839e7-5370-4e94-a13b-f7f9ff0c3aef
    job-name: embedding-consumer-app-job-20240426072846
  name: embedding-consumer-app-job-20240426072846-2hnv5
  改成=>
  name: ian-embedding-consumer-app-job
```
> 7. 執行```kubectl apply -f test.yaml```
> ![image](https://hackmd.io/_uploads/BkXMTyt-C.png)
> 就會建立一個pod依照yaml設定的名稱創建。
> 再觀看一次pod執行```kubectl get po -n job-queue```就會看到剛建立的pod。。
> ![image](https://hackmd.io/_uploads/ryg7Cp1tWR.png)

> 8. 執行```kubectl exec -it ian-embedding-consumer-app-job -n job-queue bash```
> ![image](https://hackmd.io/_uploads/ryxqxetWC.png)
> 直接用VScode K8S右鍵點選pod Attach Visual Studio Code更方便。 
> ![截圖 2025-02-24 上午11.17.02](https://hackmd.io/_uploads/SyhR4vtq1x.png)

> 9. ```python main.py```
> 可直接執行該程式，且可以在這包檔案中做程式修改。
> ![image](https://hackmd.io/_uploads/SyDXMeY-0.png)

> 10. ```kubectl logs kendra-consumer-app-job-20240612075743-b7br5 -n job-queue -f```
> 可以看kendra 操作的embedding，且"-f"可以一直顯示他連續log

> 11. ```kubectl get cronjob -n job-queue```
> 可以觀看有多少cronjob
> ```kubectl create job --from=cronjob/[cronjob_name] [自行定義名稱] -n [namespace]```
> 手動觸發cronjob
> ```kubectl create job --from=cronjob/aws-cli-cronjob-scaling-up test2-cronjob-scaling-up -n job-queue```

## 上傳檔案到K8S POD 路徑上
> kubectl cp filename [POD]:/route/filename -n namespace
> kubectl cp loader.py ian-test:/app/lib/s3/loader.py -n job-queue


## 上傳檔案到AWS S3上面
> ```aws s3 cp {file local path} s3://{bucket-name}/{conversation_ID}/```

> ```aws s3 cp /Users/webberhounienlin/Downloads/maxx-WFRBQ94Xhhc-unsplash.jpg s3://navi-image-staging/01HWA3YSX0A4M6DB91WVJ88EQH/```

## GRAFANA

> ```kubectl get service -n monitor```
> 下cmd查看External IP
> 複製IP
> ![image](https://hackmd.io/_uploads/ByXkK2mOC.png)
> 帳密：
> admin
> prom-operator

## 到API POD中執行
>  kubectl exec apiserver-deployment-c6f44cfcb-9xh9x -it -n  ai-package-202-eks-develop -- bash
>  pip install ipython
>  ipython
> redis連接
```
import redis
redis_url = "naviredis-fdzy8f.serverless.use1.cache.amazonaws.com"
# production redis_url = "gairedis-fdzy8f.serverless.use1.cache.amazonaws.com"
redis_pool = redis.ConnectionPool(host=redis_url,port=6379, db=0, connection_class=redis.SSLConnection, decode_responses=True)
redis_client = redis.StrictRedis(connection_pool=redis_pool)
redis_client.get("kendra_or_chroma")
system_prompt=""
redis_client.set("kendra_or_chroma",system_prompt)
```

## 簡單切換cluster
> kubectl config use-context arn:aws:eks:us-east-1:185271018684:cluster/aws-rd-develop 切到Staging Cluster
> kubectl config use-context arn:aws:eks:us-east-1:185271018684:cluster/GAIEKS 切到Production Cluster

## 觀看namespace 變量
> kubectl describe deploy -n job-queue


## kube GUI
[Kubectl GUI](https://k8sdashboard.netron.tech/)
    - K8S GUI dev token：
    ``` eyJhbGciOiJSUzI1NiIsImtpZCI6Ik1TVWNsUm0zdTlSSHJRZ3hER1JKSFZCSWZmTTJHYUxfNTFyQU9vRlQ4M1kifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOi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bWUiOiJhZG1pbi11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI1ZjZlMTZhNy0zZGZlLTQ4MjgtOWM2OC03ZWZhZmU4MDM0MWMiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.Z36sw1pORKXh5lMf82U6d0NnFBCvTOf_x8xukQG98mYYa3ExZ-TDKysQDZOonglnPwVCVaP0foc1BdlCUYNfNU2tK-S6lr9l7be00D7RDzEgLMdCXcGkRXXoG-MEmC2yaTHuKOi5AAGR4BDHPpnRhyU2nlU_h-aXlOlA48rST_zp1-Yk2oCzlX9raMyG0wG5FuSva_XPHv9GplJS8s0n5NWE4VAeE4eVAC0-CaaAPXUia_riTXGqHNgD-oCIRmkQS6AHRznp8PGFDv7DrmsCsGxgZMlgfyjdPjpSzrwSQPWoKYSCUSBBIQqQNDwLC5s6t7pm7Q4DU2hkzwPmjgJNmg
```

    
NAVI Production 的 k8s dashboard
- URL
https://navi-prod-k8s.netron.tech/#/log/navi-api-server/ai-apiserver-6d6d5d67dd-k6pjp/pod?namespace=navi-api-server&container=python
- Production Token

```
eyJhbGciOiJSUzI1NiIsImtpZCI6ImVEd1NuQVlSNWltbWhmbExxNkJ0THhvajY0WXhjVGVZOGFjRDJEbms1dlUifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJiZDZkZWJlZi1jNzBiLTRkNTYtYjBmMi1hYzg1ZWY1ZWYzZGIiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.jtOMYsiOjowbubkIuF48p8z3qaF2ExFxyZAcj9xQkuvkkqsIQ6BPkvqRSFo5N1RLWB3rS0TlwBVks7YQTNVYvro96oHBI8NNzkcnq1GkG2Rl1DlyD6WV2dcaeFloHOZTJjMo7nOsbPxVhh9M1lVWvgtKjdkWBJbcJpejJ7nU8FcE6-rO2fPtglz33YK11MMDj652Bq_VlHuwnNfqFRcibgHTYGsW5uOPVE7vZV_nJ53UeQdRoTZl0sYBoNGcZWQytfxMLfQCGMjrGy35Prm9kMAhubDHOwpQOmciU6VH2_RqaKJRiFGGN-2dw4NOH3DfedmxrMX8cKW2lF0eFW3akg
```

ChromaDB connection(production)
```
chmod 400 "NAVI-ChromaDB-Prod-Key.pem" 
ssh -i "NAVI-ChromaDB-Prod-Key.pem" ubuntu@ec2-54-89-4-225.compute-1.amazonaws.com 
```


```kubectl config get-contexts``` 是用來查看你目前 Kubernetes 環境中設定的所有 contexts 的指令
```kubectl config use-context <Namespace>``` 









