問題描述
如何在 Docker Desktop 上創建新的 Kubernetes 集群? (How to create a new Kubernetes cluster on Docker Desktop?)
我似乎無法弄清楚如何在我的計算機上運行 Docker Desktop 的實例上創建一個全新的 Kubernetes 集群。(這應該是 Mac 還是 PC 無關緊要)。
我知道如何設置當前集群上下文,但我只有一個集群,所以我無法設置其他任何東西。
### What's my current context pointing to?
$ kubectl config current‑context
docker‑for‑desktop
### Set the context to be "docker‑for‑desktop" cluster
$ kubectl config use‑context docker‑for‑desktop
Switched to context “docker‑for‑desktop”
更多問題:
- 如果我有多個集群,那麼只有其中一個(當前“設置”的一個)將同時運行,而另一個停止/休眠?
- 集群之間是相互獨立的,所以如果我可以在一個集群上閒逛,那麼這應該不會影響另一個集群
參考解法
方法 1:
Kubernetes config file describes 3 objects: clusters, users, and contexts.
cluster ‑ cluster name + details ‑ the host and the certificates.
user ‑ user name and credentials, to authorise you against any cluster host.
the context role is to make the connection between a user and a cluster, so when you use that context,kubectl
will authorise you against the cluster specified in the context object, using the credentials of the user specified in the context object. an example context
object:
apiVersion: v1
current‑context: ""
kind: Config
preferences: {}
clusters:
‑ cluster:
certificate‑authority: xxxxxxxxx
server: xxxxxxxxx
name: gke_dev‑yufuyjfvk_us‑central1‑a_standard‑cluster‑1
users:
‑ name: efrat‑dev
user:
client‑certificate: xxxxxxxxx
client‑key: xxxxxxxxx
contexts:
‑ context:
cluster: gke_dev‑yufuyjfvk_us‑central1‑a_standard‑cluster‑1
user: efrat‑dev
name: gke‑dev
the kubectl config
subcommand has a set of commands to generate cluster, user & context entries in the config file.
multiple k8s clusters from docker‑desktop
under the hood, when you enable k8s, docker desktop downloads kubernetes components as docker images, and the server listens https://localhost:6443
. it is all done automatically so unless you have any intention to run the entire structure by yourself i dont suppose you can configure it to run multiple clusters.
about your further questions:
when you set a context, kubectl
will set current‑context
to that one, and every kubectl
you run will go to the context's cluster, using the context's user credentials. it doesnt mean the clusters are dead. it wont affect them at all.
方法 2:
Make sure that the checkbox is enabled
方法 3:
Using Docker Desktop you can create multiple clusters using a development tool such as k3d. Disable Kubernetes under Docker Desktop preferences as you won't need it for this. Then install XCode CLT, Homebrew and use Brew to install k3d locally. Next issue the commands:
k3d cluster create one && \
k3d cluster create two
This will create two clusters, automatically switching contexts. Run k3d cluster list
to list available clusters and use k3d ‑‑help
for online help. A more in‑depth tutorial is available for macOS without fee.
(by Pure.Krome、Efrat Levitan、Mazzy、vhs)