|
| 1 | +# DigitalOcean Kubernetes (DOKS) 操作指南 |
| 2 | + |
| 3 | +## 1. 連線到 DOKS Cluster |
| 4 | + |
| 5 | +1. 安裝 doctl CLI 工具 |
| 6 | + [官方下載頁](https://docs.digitalocean.com/reference/doctl/how-to/install/) |
| 7 | +2. 登入 doctl(用 DigitalOcean API Token) |
| 8 | + ```sh |
| 9 | + doctl auth init |
| 10 | + ``` |
| 11 | +3. 取得 cluster 列表,找到你的 cluster 名稱或 ID |
| 12 | + ```sh |
| 13 | + doctl kubernetes cluster list |
| 14 | + ``` |
| 15 | +4. 儲存 kubeconfig(用 cluster 名稱或 ID) |
| 16 | + ```sh |
| 17 | + doctl kubernetes cluster kubeconfig save <your-cluster-name-or-id> |
| 18 | + ``` |
| 19 | +5. 驗證連線 |
| 20 | + ```sh |
| 21 | + kubectl get nodes |
| 22 | + ``` |
| 23 | + |
| 24 | +## 2. 安裝 nginx ingress controller |
| 25 | + |
| 26 | +1. 執行以下指令安裝 nginx ingress controller: |
| 27 | + ```sh |
| 28 | + kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.10.1/deploy/static/provider/cloud/deploy.yaml |
| 29 | + ``` |
| 30 | +2. 等待安裝完成,確認 controller 已啟動: |
| 31 | + ```sh |
| 32 | + kubectl get pods -n ingress-nginx |
| 33 | + ``` |
| 34 | +3. 取得 EXTERNAL-IP: |
| 35 | + ```sh |
| 36 | + kubectl get svc -n ingress-nginx |
| 37 | + ``` |
| 38 | + |
| 39 | +## 3. 設定 Ingress 資源 |
| 40 | + |
| 41 | +1. 在 Ingress YAML 設定 ingress class: |
| 42 | + ```yaml |
| 43 | + apiVersion: networking.k8s.io/v1 |
| 44 | + kind: Ingress |
| 45 | + metadata: |
| 46 | + name: your-app-ingress |
| 47 | + labels: |
| 48 | + app: your-app |
| 49 | + spec: |
| 50 | + ingressClassName: nginx |
| 51 | + rules: |
| 52 | + - host: your-domain.com |
| 53 | + http: |
| 54 | + paths: |
| 55 | + - path: / |
| 56 | + pathType: Prefix |
| 57 | + backend: |
| 58 | + service: |
| 59 | + name: your-app-service |
| 60 | + port: |
| 61 | + number: 80 |
| 62 | + ``` |
| 63 | +2. 套用 Ingress 資源: |
| 64 | + ```sh |
| 65 | + kubectl apply -f <your-ingress.yaml> |
| 66 | + ``` |
| 67 | +3. 驗證 Ingress 是否分配外部 IP: |
| 68 | + ```sh |
| 69 | + kubectl get ingress |
| 70 | + ``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## 4. 安裝 cert-manager 並自動申請 HTTPS 憑證 |
| 75 | + |
| 76 | +### 安裝 cert-manager |
| 77 | + |
| 78 | +```sh |
| 79 | +kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.2/cert-manager.yaml |
| 80 | +``` |
| 81 | + |
| 82 | +### 建立 ClusterIssuer(Let's Encrypt) |
| 83 | + |
| 84 | +建立一個 `cluster-issuer.yaml`: |
| 85 | + |
| 86 | +```yaml |
| 87 | +apiVersion: cert-manager.io/v1 |
| 88 | +kind: ClusterIssuer |
| 89 | +metadata: |
| 90 | + name: letsencrypt-prod |
| 91 | +spec: |
| 92 | + acme: |
| 93 | + server: https://acme-v02.api.letsencrypt.org/directory |
| 94 | + email: your-email@example.com |
| 95 | + privateKeySecretRef: |
| 96 | + name: letsencrypt-prod |
| 97 | + solvers: |
| 98 | + - http01: |
| 99 | + ingress: |
| 100 | + class: nginx |
| 101 | +``` |
| 102 | +
|
| 103 | +套用: |
| 104 | +
|
| 105 | +```sh |
| 106 | +kubectl apply -f cluster-issuer.yaml |
| 107 | +``` |
| 108 | + |
| 109 | +### 建立 Certificate 資源 |
| 110 | + |
| 111 | +建立一個 `certificate.yaml`: |
| 112 | + |
| 113 | +```yaml |
| 114 | +apiVersion: cert-manager.io/v1 |
| 115 | +kind: Certificate |
| 116 | +metadata: |
| 117 | + name: notion-chart-generator-cert |
| 118 | + namespace: default |
| 119 | +spec: |
| 120 | + secretName: notion-chart-generator-tls |
| 121 | + issuerRef: |
| 122 | + name: letsencrypt-prod |
| 123 | + kind: ClusterIssuer |
| 124 | + commonName: your-domain.com |
| 125 | + dnsNames: |
| 126 | + - your-domain.com |
| 127 | +``` |
| 128 | +
|
| 129 | +套用: |
| 130 | +
|
| 131 | +```sh |
| 132 | +kubectl apply -f certificate.yaml |
| 133 | +``` |
| 134 | + |
| 135 | +### 修改 Ingress 讓其使用 TLS |
| 136 | + |
| 137 | +在 Ingress YAML 加入 TLS 設定: |
| 138 | + |
| 139 | +```yaml |
| 140 | +spec: |
| 141 | + tls: |
| 142 | + - hosts: |
| 143 | + - 188-166-204-94.nip.io |
| 144 | + secretName: notion-chart-generator-tls |
| 145 | + rules: |
| 146 | + - host: 188-166-204-94.nip.io |
| 147 | + http: |
| 148 | + paths: |
| 149 | + # ...existing paths... |
| 150 | +``` |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +**注意事項:** |
| 155 | + |
| 156 | +- 請將 `your-email@example.com` 改為你自己的 email。 |
| 157 | +- DNS 必須能指向你的 Ingress EXTERNAL-IP(nip.io 會自動處理)。 |
| 158 | +- cert-manager 會自動申請、續期憑證,無需手動操作。 |
0 commit comments