在 Virtualbox 中访问本地 Kubernetes 集群 运行
Access local Kubernetes cluster running in Virtualbox
我已经使用 kubeadm 配置了一个 Kubernetes 集群,创建了 3 个 Virtualbox 节点,每个节点 运行 CentOS(master、node1、node2)。每个 virtualbox 虚拟机都使用 'Bridge' 网络配置。
因此,我有以下设置:
- 主节点 'master.k8s' 运行 at 192.168.19.87 (virtualbox)
- 工作节点 1 'node1.k8s' 运行 在 192.168.19.88 (virtualbox)
- 工作节点 2 'node2.k8s' 运行 位于 192.168.19.89(virtualbox
现在我想从我的本地机器(虚拟机节点所在的物理机器运行)访问集群中的服务运行。
运行 kubectl cluster-info
我看到以下输出:
Kubernetes master is running at https://192.168.19.87:6443
KubeDNS is running at ...
举个例子,假设我在我的集群中部署了仪表板,我如何在我的物理机器上使用浏览器 运行 打开仪表板 UI?
访问 kubernetes 仪表板的传统方法是 documented in their readme,即使用 kubectl proxy
。
一个人不应该通过 ssh 进入集群来访问任何 kubernetes 服务,因为这会破坏拥有集群的目的,并且 绝对 会在集群的安全模型。任何到 Nodes
的 ssh 都应保留用于 "in case of emergency, break glass" 情况。
更一般地说,配置良好的 Ingress controller 将提供服务 en-masse 并且还具有非常令人愉悦的 side-effect 这意味着您的本地集群将与您的 "for real" 集群,不需要任何不正当的 ssh-ery 规则
传统的方式是使用kubectl proxy
或一个Load Balancer
,但是由于你在开发机器中,一个NodePort
可以用于发布应用程序,因为负载均衡器在 VirtualBox 中不可用。
以下示例部署了回显服务器 运行 nginx 的 3 个副本,并使用 NodePort
:
发布了 http 端口
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: my-echo
image: gcr.io/google_containers/echoserver:1.8
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service-np
labels:
name: nginx-service-np
spec:
type: NodePort
ports:
- port: 8082 # Cluster IP http://10.109.199.234:8082
targetPort: 8080 # Application port
nodePort: 30000 # Example (EXTERNAL-IP VirtualBox IPs) http://192.168.50.11:30000/ http://192.168.50.12:30000/ http://192.168.50.13:30000/
protocol: TCP
name: http
selector:
app: nginx
您可以使用任何 VirtualBox IP 访问服务器,例如
http://192.168.50.11:30000 or http://192.168.50.12:30000 or http://192.168.50.13:30000
在 Building a Kubernetes Cluster with Vagrant and Ansible (without Minikube) 查看完整示例。
我已经使用 kubeadm 配置了一个 Kubernetes 集群,创建了 3 个 Virtualbox 节点,每个节点 运行 CentOS(master、node1、node2)。每个 virtualbox 虚拟机都使用 'Bridge' 网络配置。 因此,我有以下设置:
- 主节点 'master.k8s' 运行 at 192.168.19.87 (virtualbox)
- 工作节点 1 'node1.k8s' 运行 在 192.168.19.88 (virtualbox)
- 工作节点 2 'node2.k8s' 运行 位于 192.168.19.89(virtualbox
现在我想从我的本地机器(虚拟机节点所在的物理机器运行)访问集群中的服务运行。
运行 kubectl cluster-info
我看到以下输出:
Kubernetes master is running at https://192.168.19.87:6443
KubeDNS is running at ...
举个例子,假设我在我的集群中部署了仪表板,我如何在我的物理机器上使用浏览器 运行 打开仪表板 UI?
访问 kubernetes 仪表板的传统方法是 documented in their readme,即使用 kubectl proxy
。
一个人不应该通过 ssh 进入集群来访问任何 kubernetes 服务,因为这会破坏拥有集群的目的,并且 绝对 会在集群的安全模型。任何到 Nodes
的 ssh 都应保留用于 "in case of emergency, break glass" 情况。
更一般地说,配置良好的 Ingress controller 将提供服务 en-masse 并且还具有非常令人愉悦的 side-effect 这意味着您的本地集群将与您的 "for real" 集群,不需要任何不正当的 ssh-ery 规则
传统的方式是使用kubectl proxy
或一个Load Balancer
,但是由于你在开发机器中,一个NodePort
可以用于发布应用程序,因为负载均衡器在 VirtualBox 中不可用。
以下示例部署了回显服务器 运行 nginx 的 3 个副本,并使用 NodePort
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: my-echo
image: gcr.io/google_containers/echoserver:1.8
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service-np
labels:
name: nginx-service-np
spec:
type: NodePort
ports:
- port: 8082 # Cluster IP http://10.109.199.234:8082
targetPort: 8080 # Application port
nodePort: 30000 # Example (EXTERNAL-IP VirtualBox IPs) http://192.168.50.11:30000/ http://192.168.50.12:30000/ http://192.168.50.13:30000/
protocol: TCP
name: http
selector:
app: nginx
您可以使用任何 VirtualBox IP 访问服务器,例如 http://192.168.50.11:30000 or http://192.168.50.12:30000 or http://192.168.50.13:30000
在 Building a Kubernetes Cluster with Vagrant and Ansible (without Minikube) 查看完整示例。