筆記本/桌上型電腦電腦的性能足夠強勁(記憶體不低于 8G),可以直接安裝 docker-desktop,并啟用其中内嵌的 Kubernetes 叢集,用做學習、測試,也可以用作日常開發。
下載下傳 docker-desktop
從 docker 下載下傳 docker-desktop (opens new window),并完成安裝
啟用 Kubernetes 叢集
MAC
啟動 docker-desktop
打開 docker-desktop 的 preference 面闆
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5yMwI2N3YWYxkDZyYjZjRTYmVWN0QTY3UTY1Q2NyI2Yx8CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.png)
切換到 Kubernetes 标簽頁
并勾選啟動 Enable Kubernetes,點選 Apply
kubectl config view
kubectl config get-contexts
kubectl cluster-info
kubectl get pods -o wide
docker network inspect bridge
[
{
"Name": "bridge",
"Id": "e6a5227c83898fa2904259426de838145d62539d01fb7f3fe241f65db38a3c3b",
"Created": "2020-12-30T02:51:44.917984338Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
另一種進入nginx容器的方法
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] – [COMMAND] instead.
因為剛才建立的 nginx pod 裡隻有一個容器,是以我們就進入了那個。可如果nginx pod 裡有倆容器,預設隻會進第一個,如何進第二個呢?
kubectl describe pods nginx
➜ pod-basic kubectl describe pods nginx
Name: nginx
Namespace: default
Priority: 0
Node: docker-desktop/192.168.65.3
Start Time: Wed, 30 Dec 2020 12:35:54 +0800
Labels: app=nginx
Annotations: <none>
Status: Running
IP: 10.1.0.6
IPs:
IP:
Containers:
nginx:
Container ID: docker://f4e45055b4b33430ecd775494c6bd8a8fe0c351ab1333016fcdc588182f40e41
Image: nginx
Image ID: docker-pullable://nginx@sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 30 Dec 2020 12:40:42 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-tqpkf (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-tqpkf:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-tqpkf
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Pulled 60m kubelet Successfully pulled image "nginx" in 35.578083503s
Normal Created 60m kubelet Created container nginx
Normal Started 60m kubelet Started container nginx
删除資源
# 删除一個配置檔案對應的資源對象
kubectl delete -f xxx.yaml
# 删除名字為baz或foo的pod和service
kubectl delete pod,service baz foo
# -l 參數可以删除包含指定label的資源對象
kubectl delete pods,services -l name=myLabel
# 強制删除一個pod,在各種原因pod一直terminate不掉的時候很有用
kubectl delete pod foo --grace-period=0 --force
kubectl delete pods imagename
更新資源
# 将foo.yaml中描述的對象擴充為3個
kubectl scale --replicas=3 -f foo.yaml
# 增加description='my frontend'備注,已有保留不覆寫
kubectl annotate pods foo description='my frontend'
# 增加status=unhealthy 标簽,已有則覆寫
kubectl label --overwrite pods foo status=unhealthy
kubectl scale rs nginx --replicas=2 更新某 pod 内叢集數
建立資源
# 建立一個service,暴露 nginx 這個rc
kubectl expose deployment nginx-deployment --type=NodePort
kubectl get svc
切換上下文
kubectl config use-context xxx 即可