可以复制kubectl客户端配置文件(kubeconfig)可以访问正在运行的 Kubernetes 群集。
通过 cluster/kube-up.sh 产生的,配置文件在 $HOME/.kube/config 中,生成并使用kubeconfig的详细步骤如下:
1. 创建一个群集
$ cluster/kube-up.sh
2. 复制 kubeconfig 到新主机
$ scp $HOME/.kube/config user@remotehost:/path/to/.kube/config
3. 在新主机中,让kubectl可以使用config副本
$ mv /path/to/.kube/config $HOME/.kube/config
$ mv /path/to/.kube/config $PWD
kubectl到kubeconfig 所在位置# 通过环境变量
$ export KUBECONFIG=/path/to/.kube/config
# 通过命令行参数
$ kubectl ... --kubeconfig=/path/to/.kube/config
kubeconfigkubeconfig 是由 kube-up 生成的,你也可以用下面的命令自己生成它(任何指令集):
# 创建 kubeconfig 入口
$ kubectl config set-cluster $CLUSTER_NICK \
    --server=https://1.1.1.1 \
    --certificate-authority=/path/to/apiserver/ca_file \
    --embed-certs=true \
    # 如果不需要 TLS ,则用下面两行替换 --certificate-authority 和 --embed-certs
    --insecure-skip-tls-verify=true \
    --kubeconfig=/path/to/standalone/.kube/config
# 创建用户入口
$ kubectl config set-credentials $USER_NICK \
    # 加载在主机生成的 token。
    --token=$token \
    # 使用 username|password 或者 token,任意一种方式
    --username=$username \
    --password=$password \
    --client-certificate=/path/to/crt_file \
    --client-key=/path/to/key_file \
    --embed-certs=true \
    --kubeconfig=/path/to/standalone/.kube/config
# 创建 context 入口
$ kubectl config set-context $CONTEXT_NAME \
    --cluster=$CLUSTER_NICK \
    --user=$USER_NICK \
    --kubeconfig=/path/to/standalone/.kube/config
笔记:
--embed-certs 是指需要产生一个独立的 kubeconfig,将在另一台主机上运行。--kubeconfig 既加载配置参数,也加载保存的配置文件。如果第一次运行,上面的--kubeconfig文件可能会被忽略$ export KUBECONFIG=/path/to/standalone/.kube/config
/srv/kubernetes文件夹下。
承载的 token/basic 身份认证 也由 kube 主机上产生。查看kubeconfig-file.md 来了解更多 kubeconfig 细节 ,
或者运行 kubectl config -h.
kubeconfig 的例子kubectl 加载并合并配置文件(按顺序)
--kubeconfig=/path/to/.kube/config 命令行标识KUBECONFIG=/path/to/.kube/config 环境变量$PWD/.kube/config$HOME/.kube/config如果在 host1创建群集 A,B, 在 host2上创建群集 C,D,你可以在两台主机上运行所有四组群集。
# 在 host2,复制 host1 的默认 kubeconfig,并且配置到环境变量
$ scp host1:/path/to/home1/.kube/config /path/to/other/.kube/config
$ export $KUBECONFIG=/path/to/other/.kube/config
# 在 host1,复制 host2 的默认 kubeconfig,并且配置到环境变量
$ scp host2:/path/to/home2/.kube/config /path/to/other/.kube/config
$ export $KUBECONFIG=/path/to/other/.kube/config
加载/合并 kubeconfig的详细的例子和说明在kubeconfig-file。