在上一篇博文里
kubernetes快速学习(二):搭建kubernetes学习环境_omage的专栏-CSDN博客
我们学习了如何搭建kubernetes的学习环境,那么接下来我们学习如何在这个环境里部署你的demo应用
kubectl get pods -A
输出如下
还记得在第一篇 kubernetes快速学习(一):基本概念和架构_omage的专栏-CSDN博客
里讲的概念吗,这里可以对应上了。
kubectl describe pods -A
创建一个部署
kubectl create deployment hello-minikube --image=cilium/echoserver
注:minikube官网教程里用的image是k8s.gcr.io/echoserver:1.4,因为被墙的原因无法下载镜像,这里我使用了类似的镜像cilium/echoserver来代替。
将该部署暴露出一个服务
kubectl expose deployment hello-minikube --type=NodePort --port=80
查询当前集群的所有服务
kubectl get services
可以看到有我们刚刚创建的hello-minikube的demo服务和kubernetes本身的服务
如果要删除服务,输入
kubectl delete service hello-minikube
如果要删除部署,输入
kubectl delete deployment hello-minikube
如果要查询当前部署,输入
kubectl get deployment
为了测试hello-minikube这个demo应用,必须设置端口转发,使得外部主机可以通过宿主机访问到这个应用,命令如下:
kubectl port-forward service/hello-minikube 7080:80 --address='0.0.0.0'
运行后,出现如下提示,且命令不会返回(按Ctrl+C退出后,端口转发也退出)
Forwarding from 0.0.0.0:7080 -> 80
在浏览器输入http://<宿主机IP>:7080
页面出现
刚才的命令行窗口出现如下信息,这说明访问到了这个demo应用。
Forwarding from 0.0.0.0:7080 -> 80
Handling connection for 7080
Handling connection for 7080
再次验证信息,查询pods
kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-minikube-db945bd49-ntsvs 1/1 Running 0 19m
查询pod详细信息
kubectl describe pod hello-minikube-db945bd49-ntsvs
Name: hello-minikube-db945bd49-ntsvs
Namespace: default
Priority: 0
Node: minikube/172.17.0.3
Start Time: Wed, 09 Feb 2022 17:51:10 +0800
Labels: app=hello-minikube
pod-template-hash=db945bd49
Annotations:
Status: Running
IP: 172.18.0.5
IPs:
IP: 172.18.0.5
Controlled By: ReplicaSet/hello-minikube-db945bd49
Containers:
echoserver:
Container ID: docker://36658cff51be81189684ed1bab30d7e86ad78f6a5a74b8987002f073751f2399
Image: cilium/echoserver
Image ID: docker-pullable://cilium/echoserver@sha256:76120aab2583d2283578dfbd3025057f874f12893475db0c1a945d7be211e7f4
Port:
Host Port:
State: Running
Started: Wed, 09 Feb 2022 17:51:21 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-l244g (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-l244g:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional:
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors:
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 21m default-scheduler Successfully assigned default/hello-minikube-db945bd49-ntsvs to minikube
Normal Pulling 21m kubelet Pulling image "cilium/echoserver"
Normal Pulled 21m kubelet Successfully pulled image "cilium/echoserver" in 4.355960852s
Normal Created 21m kubelet Created container echoserver
Normal Started 21m kubelet Started container echoserver
minikube start | minikubehttps://minikube.sigs.k8s.io/docs/start/