2.3 在Kubernetes上运行第一个应用
2021-07-06 本文已影响0人
众神开挂
参考文章
作者:老谭涮菜
链接:https://www.jianshu.com/p/6549ef181e7b
Creating a Deployment
$ kubectl create deployment kubia --image=luksa/kubia:1.0
deployment.apps/kubia created
Listing deployments
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubia 0/1 1 0 6s
Listing pods
Listing 3.11 Listing pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubia-9d785b578-p449x 0/1 Pending 0 1m
Listing 3.12 The events displayed by kubectl describe pod
$kubectl describe pod
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 25s default-scheduler Successfully assigned
default/kubia-9d785b578-p449x
to worker2
Normal Pulling 23s kubelet, worker2 Pulling image "luksa/kubia:1.0"
Normal Pulled 21s kubelet, worker2 Successfully pulled image
Normal Created 21s kubelet, worker2 Created container kubia
Normal Started 21s kubelet, worker2 Started container kubia
Creating a Service
$ kubectl expose deployment kubia --type=LoadBalancer --port 8080
service/kubia exposed
Listing services
Listing 3.13 Listing Services
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.19.240.1 <none> 443/TCP 34m
kubia LoadBalancer 10.19.243.17 <pending> 8080:30838/TCP 4s
Listing 3.14 Getting a single service
$ kubectl get svc kubia
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubia LoadBalancer 10.19.243.17 35.246.179.22 8080:30838/TCP 82s
可以运行 minikube ssh 登录到Minikube集群内部
curl http://10.19.243.17:8080
Accessing your application through the load balance
$ curl 35.246.179.22:8080
Hey there, this is kubia-9d785b578-p449x. Your IP is ::ffff:1.2.3.4.
使用外部 IP 地址(LoadBalancer Ingress)访问 Hello World 应用程序:
curl http://<external-ip>:<port>
其中 <external-ip> 是您的服务的外部 IP 地址(LoadBalancer Ingress), <port> 是您的服务描述中的 port 的值。如果您正在使用 minikube,输入 minikube service kubia 获取可以访问服务的IP和端口 并在浏览器中自动打开 Hello World 应用程序。
水平伸缩应用
$ kubectl scale deployment kubia --replicas=3
deployment.apps/kubia scaled
Seeing the results of the scale-out
$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
kubia 3/3 3 3 18m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubia-9d785b578-58vhc 1/1 Running 0 17s
kubia-9d785b578-jmnj8 1/1 Running 0 17s
kubia-9d785b578-p449x 1/1 Running 0 18m
Displaying the pods’ host node when listing pods
$ kubectl get pods -o wide
NAME ... IP NODE
kubia-9d785b578-58vhc ... 10.244.1.5 worker1
kubia-9d785b578-jmnj8 ... 10.244.2.4 worker2
kubia-9d785b578-p449x ... 10.244.2.3 worker2
Understanding the API objects representing your application
Listing 3.16 Requests sent to the service are spread across all the pods
$ curl 35.246.179.22:8080
Hey there, this is kubia-9d785b578-58vhc. Your IP is ::ffff:1.2.3.4.
$ curl 35.246.179.22:8080
Hey there, this is kubia-9d785b578-p449x. Your IP is ::ffff:1.2.3.4.
$ curl 35.246.179.22:8080
Hey there, this is kubia-9d785b578-jmnj8. Your IP is ::ffff:1.2.3.4.
$ curl 35.246.179.22:8080
Hey there, this is kubia-9d785b578-p449x. Your IP is ::ffff:1.2.3.4.
删除 Service:
kubectl delete services kubia
清理
现在可以清理你在集群中创建的资源:
kubectl delete service kubia
kubectl delete deployment kubia
可选地,停止 Minikube 虚拟机(VM):
minikube stop
可选地,删除 Minikube 虚拟机(VM):
minikube delete