出于学习目的,我在家里搭建了一个小型三节点 Kubernetes 集群。每个 16GB 节点都运行 Ubuntu Server 和 MicroK8S。我设置了一个领导者 (arran) 和两个追随者 (nikka 和 yamazaki)。
root@arran:/home/me# microk8s kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
arran Ready <none> 5d3h v1.26.4 192.168.50.251 <none> Ubuntu 22.04.2 LTS 5.15.0-71-generic containerd://1.6.15
nikka Ready <none> 4d14h v1.26.4 192.168.50.74 <none> Ubuntu 22.04.2 LTS 5.15.0-71-generic containerd://1.6.15
yamazaki Ready <none> 3d16h v1.26.4 192.168.50.135 <none> Ubuntu 22.04.2 LTS 5.15.0-71-generic containerd://1.6.15
这是集群的状态,有ingress
和dashboard
手动启用。你可以看到它已经切换到 HA 模式:
root@arran:/home/me# microk8s status
microk8s is running
high-availability: yes
datastore master nodes: 192.168.50.251:19001 192.168.50.74:19001 192.168.50.135:19001
datastore standby nodes: none
addons:
enabled:
dashboard # (core) The Kubernetes dashboard
ha-cluster # (core) Configure high availability on the current node
helm # (core) Helm - the package manager for Kubernetes
helm3 # (core) Helm 3 - the package manager for Kubernetes
hostpath-storage # (core) Storage class; allocates storage from host directory
ingress # (core) Ingress controller for external access
metrics-server # (core) K8s Metrics Server for API access to service metrics
registry # (core) Private image registry exposed on localhost:32000
storage # (core) Alias to hostpath-storage add-on, deprecated
disabled:
cert-manager # (core) Cloud native certificate management
community # (core) The community addons repository
dns # (core) CoreDNS
gpu # (core) Automatic enablement of Nvidia CUDA
host-access # (core) Allow Pods connecting to Host services smoothly
kube-ovn # (core) An advanced network fabric for Kubernetes
mayastor # (core) OpenEBS MayaStor
metallb # (core) Loadbalancer for your Kubernetes cluster
minio # (core) MinIO object storage
observability # (core) A lightweight observability stack for logs, traces and metrics
prometheus # (core) Prometheus operator for monitoring and logging
rbac # (core) Role-Based Access Control for authorisation
这是我正在运行的 pod,来自我的清单(见下文):
root@arran:/home/me# microk8s kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-world-app 1/1 Running 1 (14h ago) 47h 10.1.134.199 yamazaki <none> <none>
my-pod 1/1 Running 2 (14h ago) 5d1h 10.1.150.208 arran <none> <none>
目前提供的服务如下:
root@arran:/home/me# microk8s kubectl get services -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 5d3h <none>
nginx-service NodePort 10.152.183.120 <none> 80:30000/TCP 2d12h app.kubernetes.io/name=hello-world-app
hello-world-service NodePort 10.152.183.205 <none> 80:30032/TCP 47h app.kubernetes.io/name=hello-world-app
dashboard-service NodePort 10.152.183.237 <none> 443:32589/TCP 47h app.kubernetes.io/name=kubernetes
我怀疑问题出在清单中,这是我从 K8S 和 MicroK8s 手册中以一种复制粘贴的方式构建的:
apiVersion: v1
kind: Pod
metadata:
name: hello-world-app
labels:
app.kubernetes.io/name: hello-world-app
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- name: http
containerPort: 80
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: hello-world-service
spec:
selector:
app.kubernetes.io/name: hello-world-app
ports:
- port: 80
targetPort: 80
type: NodePort
---
# Not sure this will work - do we need a NodePort to the dashboard?
apiVersion: v1
kind: Service
metadata:
name: dashboard-service
spec:
selector:
app.kubernetes.io/name: kubernetes
ports:
- port: 443
targetPort: 443
type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: http-ingress
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: dashboard-ingress
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: kubernetes
port:
number: 443
现在,我有一个“hello world”应用程序,我给它一个节点端口,然后使用入口插件公开它。这已在http://192.168.50.251/(端口 80)上可用。但是,我尝试通过添加端口和入口路由(端口 443)对 Kubernetes 仪表板执行相同的操作,但https://192.168.50.251/指向“hello world”,而不是我想要的仪表板。
单文件清单文件已完全应用microk8s kubectl apply -f manifest.yml
.
接下来我可以尝试什么?
我以一种非常不同的方式解决了这个问题,根本不需要清单。MicroK8S 提供了一些帮助脚本来实现这一点。我需要在 K8S 领导服务器上进行两个会话。
在第一个会话中,我运行了这个:
在第二个会话中,我运行了这个:
然后 K8S 仪表板暴露在领导主机上:https ://192.168.50.251:8080/ 。从这里,只需从上方粘贴冗长的令牌即可登录。