聊聊kubernetes——跑个应用
kubernetes学习笔记,分享出来,希望能帮助一些有需要的人,有问题一起交流。
本文是带领大家一起用kubernetes跑个应用,体验一下kubernetes。
1. 使用官网交互环境跑个应用
去官网,进入kubernetes交互环境。
顺序执行以下命令
# 启动kubernetes
$ minikube start
# 查看nodes信息
$ kubectl get nodes
# 查看cluster(集群)信息
$ kubectl cluster-info
# 跑个应用
$ kubectl run kubernetes-bootcamp --image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port=8080
最后会输出:deployment.apps "kubernetes-bootcamp" created,说明我们应用跑起来了。
查看Pod:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Running 0 3m
kubernetes-bootcamp-56cdd766d-zjx5就是应用所在的Pod
默认情况下Pod只能在集群(cluster)内部访问,外部要访问应用需要通过service暴露的端口来访问。
使用service暴露应用,使得外部可以访问:
$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
输出:service "kubernetes-bootcamp" exposed
查看以下services:
$ kubectl get services
看一下结果:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12m
kubernetes-bootcamp NodePort 10.103.30.96 <none> 8080:30623/TCP 40s
kubernetes是默认的service,我们暂且不管,kubernetes-bootcamp就是我们应用的service。8080:30623 后面的端口30623是随机分配的。
2. Scale 应用
默认情况下应用只有一个副本,通过kubectl get deployments
命令查看副本的情况:
$ kubectl get deployments
结果:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1 1 1 1 11m
我们看到了1个副本。
通过以下命令增加副本:
$ kubectl scale deployments/kubernetes-bootcamp --replicas=3
输出:deployment.extensions "kubernetes-bootcamp" scaled
然后,查看现在的副本:
$ kubectl get deployments
结果:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 3 3 3 3 15m
看到了3个副本,扩容速度很快的。
kubectl get pods
命令查看我们的pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Running 0 1m
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Running 0 11m
kubernetes-bootcamp-56cdd766d-8sdjc 1/1 Running 0
1m
通过curl nodeName:port
来访问应用:
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-zjx5j |v=1
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-f74cg |v=1
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-zjx5j |v=1
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-f74cg |v=1
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-8sdjc |v=1
$
看以上结果,发现应用自动负载均衡了。
其中minikube:30623 中的minikube是我们的node名称,通过kubectl get nodes
获取的。
我们来试一下减少副本:
$ kubectl scale deployments/kubernetes-bootcamp --replicas=2
输出:deployment.extensions "kubernetes-bootcamp" scaled
同样的命令,只是改变了一下--replicas
的参数
这时候来看一下当前的pods:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Running 0 7m
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Running 0 22m
发现少了一个应用(pods)kubernetes-bootcamp-56cdd766d-8sdjc
3. 应用更新
应用升级
为了演示应用更新,我们将副本设置为5个,来看看应用更新过程
$ kubectl scale deployments/kubernetes-bootcamp --replicas=5
deployment.extensions "kubernetes-bootcamp" scaled
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-722v7 1/1 Running 0 12s
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Running 0 12m
kubernetes-bootcamp-56cdd766d-vft4v 1/1 Running 0 12s
kubernetes-bootcamp-56cdd766d-z525n 1/1 Running 0 12s
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Running 0 26m
这时候已经运行了5个副本,下一步开始更新应用,我们用到了镜像v2版本:
$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=docker.io/jocatalin/kubernetes-bootcamp:v2
deployment.apps "kubernetes-bootcamp" image updated
$ kubectl get podsNAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-722v7 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Terminating 0 14m
kubernetes-bootcamp-56cdd766d-vft4v 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-z525n 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Running 0 29m
kubernetes-bootcamp-5bddb78d8f-7b2jr 0/1 Pending 0 3s
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 5s
kubernetes-bootcamp-5bddb78d8f-g5mc7 0/1 ContainerCreating 0 3s
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Running 0 4s
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Running 0 5s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-722v7 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Terminating 0 14m
kubernetes-bootcamp-56cdd766d-vft4v 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-z525n 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Terminating 0 29m
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Running 0 12s
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 14s
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Running 0 12s
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Running 0 13s
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Running 0 14s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-722v7 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Terminating 0 14m
kubernetes-bootcamp-56cdd766d-vft4v 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-z525n 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Terminating 0 29m
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Running 0 19s
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 21s
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Running 0 19s
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Running 0 20s
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Running 0 21s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Terminating 0 29m
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Running 0 35s
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 37s
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Running 0 35s
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Running 0 36s
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Running 0 37s
$
我们快速的用kubectl get pods
查看pod的情况,发现它先创建了5个新的pod然后逐渐删除旧的pod
应用降级
应用降级很简单,使用undo命令就可以搞定:
$ kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps "kubernetes-bootcamp"
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-2j2tp 1/1 Running 0 4s
kubernetes-bootcamp-56cdd766d-gt45v 0/1 Pending 0 2s
kubernetes-bootcamp-56cdd766d-x6wxb 1/1 Running 0 3s
kubernetes-bootcamp-56cdd766d-xwbc6 0/1 ContainerCreating 0 2s
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 4m
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Running 0 4m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-2j2tp 1/1 Running 0 13s
kubernetes-bootcamp-56cdd766d-gt45v 1/1 Running 0 11s
kubernetes-bootcamp-56cdd766d-kfxcj 1/1 Running 0 8s
kubernetes-bootcamp-56cdd766d-x6wxb 1/1 Running 0 12s
kubernetes-bootcamp-56cdd766d-xwbc6 1/1 Running 0 11s
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Terminating 0 4m
$
看输出结果,同应用升级一样的过程,先创建5个pod,然后逐渐删除旧的pod。
这时候可以用curl
来验证一下版本
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-xwbc6 |v=1
看到最后的v=1,说明版本已回退。
4. 总结
kubernetes看起来有点复杂,但想想她用简单的命令完成的操作,比如:应用部署、应用更新,还觉得她复杂吗?我只是觉得:强大!