K8s - Hello Minikube
0 - 创建cluster
启动 Minikube 并创建一个集群
minikube start
命令将创建并配置一台虚拟机,使其运行单节点 Kubernetes 集群,并且配置 kubectl 安装,以便使其能与 Kubernetes 集群正确通信。
[anliven@anliven ~]$ minikube start
* minikube v1.14.0 on Centos 7.8.2003 (vbox/amd64)
* Using the docker driver based on existing profile
* Starting control plane node minikube in cluster minikube
* Restarting existing docker container for "minikube" ...
* Preparing Kubernetes v1.19.2 on Docker 19.03.8 ...
* Verifying Kubernetes components...
* Enabled addons: default-storageclass, storage-provisioner
* Done! kubectl is now configured to use "minikube" by default
[anliven@anliven ~]$
1 - 创建 Deployment
Kubernetes Pod 是由一个或多个 为了管理和联网而绑定在一起的容器构成的组。
Kubernetes Deployment 检查 Pod 的健康状况,并在 Pod 中的容器终止的情况下重新启动新的容器。
Deployment 是管理 Pod 创建和扩展的推荐方法。
使用名为 echoserver(一个简单的 HTTP 服务器。) 的镜像创建一个 Kubernetes Deployment
[anliven@anliven ~]$ kubectl create deployment hello-node --image=k8s.gcr.io/echoserver:1.4 # 创建管理 Pod 的 Deployment, Pod 根据提供的 Docker 镜像运行 Container
deployment.apps/hello-node created
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl get deployments # 查看 Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
hello-node 1/1 1 1 36s
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl get pods # 查看 Pod
NAME READY STATUS RESTARTS AGE
hello-node-7567d9fdc9-prltr 1/1 Running 0 51s
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl get events # 查看集群事件
LAST SEEN TYPE REASON OBJECT MESSAGE
73s Normal Scheduled pod/hello-node-7567d9fdc9-prltr Successfully assigned default/hello-node-7567d9fdc9-prltr to minikube
71s Normal Pulling pod/hello-node-7567d9fdc9-prltr Pulling image "k8s.gcr.io/echoserver:1.4"
43s Normal Pulled pod/hello-node-7567d9fdc9-prltr Successfully pulled image "k8s.gcr.io/echoserver:1.4" in 28.355837001s
43s Normal Created pod/hello-node-7567d9fdc9-prltr Created container echoserver
43s Normal Started pod/hello-node-7567d9fdc9-prltr Started container echoserver
73s Normal SuccessfulCreate replicaset/hello-node-7567d9fdc9 Created pod: hello-node-7567d9fdc9-prltr
73s Normal ScalingReplicaSet deployment/hello-node Scaled up replica set hello-node-7567d9fdc9 to 1
35m Normal Starting node/minikube Starting kubelet.
35m Normal NodeHasSufficientMemory node/minikube Node minikube status is now: NodeHasSufficientMemory
35m Normal NodeHasNoDiskPressure node/minikube Node minikube status is now: NodeHasNoDiskPressure
35m Normal NodeHasSufficientPID node/minikube Node minikube status is now: NodeHasSufficientPID
35m Normal NodeAllocatableEnforced node/minikube Updated Node Allocatable limit across pods
35m Normal Starting node/minikube Starting kube-proxy.
35m Normal RegisteredNode node/minikube Node minikube event: Registered Node minikube in Controller
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl config view # 查看 kubectl 配置
apiVersion: v1
clusters:
- cluster:
certificate-authority: /home/anliven/.minikube/ca.crt
server: https://192.168.49.2:8443
name: minikube
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
client-certificate: /home/anliven/.minikube/profiles/minikube/client.crt
client-key: /home/anliven/.minikube/profiles/minikube/client.key
[anliven@anliven ~]$
2 - 创建 Service
默认情况下,Pod 只能通过 Kubernetes 集群中的内部 IP 地址访问。
要使得容器可以从 Kubernetes 虚拟网络的外部访问,必须将 Pod 暴露为 Kubernetes Service。
[anliven@anliven ~]$ kubectl expose deployment hello-node --type=LoadBalancer --port=8080 # 将 Pod 暴露给公网, --type=LoadBalancer 标志表明将 Service 暴露到集群外部
service/hello-node exposed
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl get services # 查看创建的 Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node LoadBalancer 10.109.212.214 <pending> 8080:32765/TCP 15s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h31m
[anliven@anliven ~]$
[anliven@anliven ~]$ minikube service list
|----------------------|---------------------------|--------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|----------------------|---------------------------|--------------|---------------------------|
| default | hello-node | 8080 | http://192.168.49.2:32765 |
| default | kubernetes | No node port |
| kube-system | kube-dns | No node port |
| kubernetes-dashboard | dashboard-metrics-scraper | No node port |
| kubernetes-dashboard | kubernetes-dashboard | No node port |
|----------------------|---------------------------|--------------|---------------------------|
[anliven@anliven ~]$
运行minikube service hello-node
命令: 在 Minikube 上,LoadBalancer 使得服务可以通过命令 minikube service 访问。
3 - 启用插件
Minikube 有一组内置的 插件, 可以在本地 Kubernetes 环境中启用、禁用和打开。
[anliven@anliven ~]$ minikube addons list # 列出当前支持的插件
|-----------------------------|----------|--------------|
| ADDON NAME | PROFILE | STATUS |
|-----------------------------|----------|--------------|
| ambassador | minikube | disabled |
| csi-hostpath-driver | minikube | disabled |
| dashboard | minikube | enabled ✅ |
| default-storageclass | minikube | enabled ✅ |
| efk | minikube | disabled |
| freshpod | minikube | disabled |
| gcp-auth | minikube | disabled |
| gvisor | minikube | disabled |
| helm-tiller | minikube | disabled |
| ingress | minikube | disabled |
| ingress-dns | minikube | disabled |
| istio | minikube | disabled |
| istio-provisioner | minikube | disabled |
| kubevirt | minikube | disabled |
| logviewer | minikube | disabled |
| metallb | minikube | disabled |
| metrics-server | minikube | disabled |
| nvidia-driver-installer | minikube | disabled |
| nvidia-gpu-device-plugin | minikube | disabled |
| olm | minikube | disabled |
| pod-security-policy | minikube | disabled |
| registry | minikube | disabled |
| registry-aliases | minikube | disabled |
| registry-creds | minikube | disabled |
| storage-provisioner | minikube | enabled ✅ |
| storage-provisioner-gluster | minikube | disabled |
| volumesnapshots | minikube | disabled |
|-----------------------------|----------|--------------|
[anliven@anliven ~]$
[anliven@anliven ~]$ minikube addons enable metrics-server # 启用插件
* The 'metrics-server' addon is enabled
[anliven@anliven ~]$
[anliven@anliven ~]$ minikube addons list |grep metrics # 查看状态
| metrics-server | minikube | enabled ✅ |
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl get pod,svc -n kube-system # 查看创建的 Pod 和 Service
NAME READY STATUS RESTARTS AGE
pod/coredns-f9fd979d6-vzcjl 1/1 Running 1 6h1m
pod/etcd-minikube 1/1 Running 1 6h1m
pod/kube-apiserver-minikube 1/1 Running 1 6h1m
pod/kube-controller-manager-minikube 1/1 Running 1 6h1m
pod/kube-proxy-c7hwx 1/1 Running 1 6h1m
pod/kube-scheduler-minikube 1/1 Running 1 6h1m
pod/metrics-server-d9b576748-5mh9l 1/1 Running 0 40s # 刚刚创建的metrics-server
pod/storage-provisioner 1/1 Running 2 6h1m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 6h1m
service/metrics-server ClusterIP 10.103.101.207 <none> 443/TCP 40s # 刚刚创建的metrics-server
[anliven@anliven ~]$
[anliven@anliven ~]$ minikube addons disable metrics-server # 禁用插件
* "The 'metrics-server' addon is disabled
[anliven@anliven ~]$
[anliven@anliven ~]$ minikube addons list |grep metrics # 查看状态
| metrics-server | minikube | disabled |
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl get pod,svc -n kube-system
NAME READY STATUS RESTARTS AGE
pod/coredns-f9fd979d6-vzcjl 1/1 Running 1 6h4m
pod/etcd-minikube 1/1 Running 1 6h4m
pod/kube-apiserver-minikube 1/1 Running 1 6h4m
pod/kube-controller-manager-minikube 1/1 Running 1 6h4m
pod/kube-proxy-c7hwx 1/1 Running 1 6h4m
pod/kube-scheduler-minikube 1/1 Running 1 6h4m
pod/storage-provisioner 1/1 Running 2 6h4m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 6h4m
[anliven@anliven ~]$
4 - 清理
清理在集群中创建的资源。
[anliven@anliven ~]$ kubectl get service # 查看Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node LoadBalancer 10.109.212.214 <pending> 8080:32765/TCP 50m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6h21m
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl get deployment # 查看deployment
NAME READY UP-TO-DATE AVAILABLE AGE
hello-node 1/1 1 1 85m
[anliven@anliven ~]$
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl delete service hello-node # 清除服务
service "hello-node" deleted
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6h22m
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl delete deployment hello-node # 清除deployment
deployment.apps "hello-node" deleted
[anliven@anliven ~]$
[anliven@anliven ~]$ kubectl get deployment
No resources found in default namespace.
[anliven@anliven ~]$
停止和删除Minikube 集群
[anliven@anliven ~]$ docker ps -a |grep minikube # 查看Minikube 集群状态
3e7356dd380b gcr.io/k8s-minikube/kicbase:v0.0.13 "/usr/local/bin/entr…" 5 minutes ago Up 41 seconds 127.0.0.1:32787->22/tcp, 127.0.0.1:32786->2376/tcp, 127.0.0.1:32785->5000/tcp, 127.0.0.1:32784->8443/tcp minikube
[anliven@anliven ~]$
[anliven@anliven ~]$ minikube stop # 停止Minikube 集群
* Stopping node "minikube" ...
* Powering off "minikube" via SSH ...
* 1 nodes stopped.
[anliven@anliven ~]$
[anliven@anliven ~]$ docker ps -a |grep minikube
3e7356dd380b gcr.io/k8s-minikube/kicbase:v0.0.13 "/usr/local/bin/entr…" 7 minutes ago Exited (130) 4 seconds ago minikube
[anliven@anliven ~]$
[anliven@anliven ~]$ minikube status
minikube
type: Control Plane
host: Stopped
kubelet: Stopped
apiserver: Stopped
kubeconfig: Stopped
[anliven@anliven ~]$
[anliven@anliven ~]$ minikube delete # 删除 Minikube 集群
* Deleting "minikube" in docker ...
* Deleting container "minikube" ...
* Removing /home/anliven/.minikube/machines/minikube ...
* Removed all traces of the "minikube" cluster.
[anliven@anliven ~]$
[anliven@anliven ~]$ docker ps -a |grep minikube
[anliven@anliven ~]$
[anliven@anliven ~]$ docker images |grep k8s # 查看Minikube 集群的镜像
gcr.io/k8s-minikube/kicbase v0.0.13 90f1294ff9ac 2 weeks ago 800MB
[anliven@anliven ~]$
5 - 参考消息
行动是绝望的解药!
欢迎转载和引用,但请在明显处保留原文链接和原作者信息!
本博客内容多为个人工作与学习的记录,少数内容来自于网络并略有修改,已尽力标明原文链接和转载说明。如有冒犯,即刻删除!
以所舍,求所得,有所获,方所成。