煙霧測試

本部分將會運行一系列的測試來驗證 Kubernetes 集群的功能正常。

數據加密

本節將會驗證 encrypt secret data at rest 的功能。

創建一個 Secret:

kubectl create secret generic kubernetes-the-hard-way \
  --from-literal="mykey=mydata"

查詢存在 etcd 裏 16 進位編碼的 kubernetes-the-hard-way secret:

gcloud compute ssh controller-0 \
  --command "sudo ETCDCTL_API=3 etcdctl get \
  --endpoints=https://127.0.0.1:2379 \
  --cacert=/etc/etcd/ca.pem \
  --cert=/etc/etcd/kubernetes.pem \
  --key=/etc/etcd/kubernetes-key.pem\
  /registry/secrets/default/kubernetes-the-hard-way | hexdump -C"

輸出爲

00000000  2f 72 65 67 69 73 74 72  79 2f 73 65 63 72 65 74  |/registry/secret|
00000010  73 2f 64 65 66 61 75 6c  74 2f 6b 75 62 65 72 6e  |s/default/kubern|
00000020  65 74 65 73 2d 74 68 65  2d 68 61 72 64 2d 77 61  |etes-the-hard-wa|
00000030  79 0a 6b 38 73 3a 65 6e  63 3a 61 65 73 63 62 63  |y.k8s:enc:aescbc|
00000040  3a 76 31 3a 6b 65 79 31  3a 8c 7b 16 f3 26 59 d5  |:v1:key1:.{..&Y.|
00000050  c9 65 1c f0 3a 04 e7 66  2a f6 50 93 4e d4 d7 8c  |.e..:..f*.P.N...|
00000060  ca 24 ab 68 54 5f 31 f6  5c e5 5c c6 29 1d cc da  |.$.hT_1.\.\.)...|
00000070  22 fc c9 be 23 8a 26 b4  9b 38 1d 57 65 87 2a ac  |"...#.&..8.We.*.|
00000080  70 11 ea 06 93 b7 de ba  12 83 42 94 9d 27 8f ee  |p.........B..'..|
00000090  95 05 b0 77 31 ab 66 3d  d9 e2 38 85 f9 a5 59 3a  |...w1.f=..8...Y:|
000000a0  90 c1 46 ae b4 9d 13 05  82 58 71 4e 5b cb ac e2  |..F......XqN[...|
000000b0  3b 6e d7 10 ab 7c fc fe  dd f0 e6 0a 7b 24 2e 68  |;n...|......{$.h|
000000c0  5e 78 98 5f 33 40 f8 d2  10 30 1f de 17 3f 06 a1  |^x._3@...0...?..|
000000d0  81 bd 1f 2e be e9 35 26  2c be 39 16 cf ac c2 6d  |......5&,.9....m|
000000e0  32 56 05 7d 80 39 5d c0  a4 43 46 75 96 0c 87 49  |2V.}.9]..CFu...I|
000000f0  3c 17 1a 1c 8e 52 b1 e8  42 6b a5 e8 b2 b3 27 bc  |<....R..Bk....'.|
00000100  80 a6 53 2a 9f 57 d2 de  a3 f8 7f 84 2c 01 c9 d9  |..S*.W......,...|
00000110  4f e0 3f e7 a7 1e 46 b7  47 dc f0 53 d2 d2 e1 99  |O.?...F.G..S....|
00000120  0b b7 b3 49 d0 3c a5 e8  26 ce 2c 51 42 2c 0f 48  |...I.<..&.,QB,.H|
00000130  b1 9a 1a dd 24 d1 06 d8  34 bf 09 2e 20 cc 3d 3d  |....$...4... .==|
00000140  e2 5a e5 e4 44 b7 ae 57  49 0a                    |.Z..D..WI.|

Etcd 的密鑰以 k8s:enc:aescbc:v1:key1 爲前綴, 表示使用密鑰爲 key1aescbc 加密數據。

部署

本節將會驗證建立與管理 Deployments 的功能。

創建一個 Deployment 用來搭建 nginx Web 服務:

kubectl create deployment nginx --image=nginx

列出 nginx deployment 的 pods:

kubectl get pods -l run=nginx

輸出爲

NAME                     READY     STATUS    RESTARTS   AGE
nginx-4217019353-b5gzn   1/1       Running   0          15s

端口轉發

本節將會驗證使用 port forwarding 從遠端進入容器的功能。

查詢 nginx pod 的全名:

POD_NAME=$(kubectl get pods -l run=nginx -o jsonpath="{.items[0].metadata.name}")

將本地機器的 8080 端口轉發到 nginx pod 的 80 端口:

kubectl port-forward $POD_NAME 8080:80

輸出爲

Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80

開一個新的終端來做 HTTP 請求測試:

curl --head http://127.0.0.1:8080

輸出爲

HTTP/1.1 200 OK
Server: nginx/1.13.5
Date: Mon, 02 Oct 2017 01:04:20 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 08 Aug 2017 15:25:00 GMT
Connection: keep-alive
ETag: "5989d7cc-264"
Accept-Ranges: bytes

回到前面的終端並按下 Ctrl + C 停止 port forwarding 命令:

Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
Handling connection for 8080
^C

容器日誌

本節會驗證 獲取容器日誌 的功能。

輸出 nginx Pod 的容器日誌:

kubectl logs $POD_NAME

輸出爲

127.0.0.1 - - [02/Oct/2017:01:04:20 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.54.0" "-"

執行容器命令

本節將驗證 在容器裏執行命令 的功能。

使用 nginx -v 命令在 nginx Pod 中輸出 nginx 的版本:

kubectl exec -ti $POD_NAME -- nginx -v

輸出爲

nginx version: nginx/1.13.7

服務(Service)

本節將驗證 Kubernetes Service

nginx 部署導出爲 NodePort 類型的 Service:

kubectl expose deployment nginx --port 80 --type NodePort

LoadBalancer 類型的 Service 不能使用是因爲沒有設置 cloud provider 集成。 設定 cloud provider 不在本教程範圍之內。

查詢 nginx 服務分配的 Node Port:

NODE_PORT=$(kubectl get svc nginx \
  --output=jsonpath='{range .spec.ports[0]}{.nodePort}')

建立防火牆規則允許外網訪問該 Node 端口:

gcloud compute firewall-rules create kubernetes-the-hard-way-allow-nginx-service \
  --allow=tcp:${NODE_PORT} \
  --network kubernetes-the-hard-way

查詢 worker 節點的外網 IP 地址:

EXTERNAL_IP=$(gcloud compute instances describe worker-0 \
  --format 'value(networkInterfaces[0].accessConfigs[0].natIP)')

對得到的外網 IP 地址 + nginx 服務的 Node Port 做 HTTP 請求測試:

curl -I http://${EXTERNAL_IP}:${NODE_PORT}

輸出爲

HTTP/1.1 200 OK
Server: nginx/1.13.7
Date: Mon, 18 Dec 2017 14:52:09 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 21 Nov 2017 14:28:04 GMT
Connection: keep-alive
ETag: "5a1437f4-264"
Accept-Ranges: bytes

下一步:刪除集群

Last updated