kubernates实战
使用k8s来部署tomcat
1、创建一个部署,并指定镜像地址
kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8
2、查看部署pod状态
kubectl get pods # 获取default名称空间下的pods
kubectl get pods --all-namespaces # 获取所有名称空间下的pods
kubectl get pods -o wide # 获取pods详细信息
3、查看所有pods,deployment,service,replicaset信息
[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcat6-5f7ccf4cb9-8qjhm 1/1 Running 0 19m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h
service/tomcat6 NodePort 10.96.226.207 <none> 80:32383/TCP 3m16s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcat6 1/1 1 1 19m
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcat6-5f7ccf4cb9 1 1 1 19m
4、向外部暴露service服务
kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort
–port:pod的端口
–target-port:容器端口
–type=NodePort:指定暴露的方式
5、查看暴露的service
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h
tomcat6 NodePort 10.96.226.207 <none> 80:32383/TCP 12s
在上面我们可以看到k8s向外部暴露tomcat端口为32383
结合下面信息我们可以看到tomcat6是在node3上面的
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat6-5f7ccf4cb9-8qjhm 1/1 Running 0 20m 10.244.2.2 k8s-node3 <none> <none>
这里node3的地址是192.168.1.109,所以访问192.168.1.109:32383即可访问tomcat首页
6、动态扩容
扩容三个tomcat
kubectl scale --replicas=3 deployment tomcat6
查看pod信息
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat6-5f7ccf4cb9-2bjhq 1/1 Running 0 5m56s 10.244.2.3 k8s-node3 <none> <none>
tomcat6-5f7ccf4cb9-8qjhm 1/1 Running 0 33m 10.244.2.2 k8s-node3 <none> <none>
tomcat6-5f7ccf4cb9-s42g5 1/1 Running 0 5m56s 10.244.1.4 k8s-node2 <none> <none>
此时使用node2地址也能访问tomcat了(http://192.168.1.105:32383/)
7、故障转移和容量伸缩
假如node3节点挂点,会重新在node2节点上拉起两个pod,保持pod数量保持3个,
假如手动stop节点上的容器,也会自动重新拉起。
重新设置pod复制数量为1时(kubectl scale --replicas=1 deployment tomcat6),会选择停掉三个pod中的两个。
8、删除
[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcat6-5f7ccf4cb9-2bjhq 1/1 Running 0 9m50s
pod/tomcat6-5f7ccf4cb9-8qjhm 1/1 Running 0 37m
pod/tomcat6-5f7ccf4cb9-s42g5 1/1 Running 0 9m50s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h
service/tomcat6 NodePort 10.96.226.207 <none> 80:32383/TCP 21m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcat6 3/3 3 3 37m
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcat6-5f7ccf4cb9 3 3 3 37m
可以删除deployment,service,pod
kubectl delete pod/tomcat6-5f7ccf4cb9-2bjhq
kubectl delete service/tomcat6
kubectl delete deployment.apps/tomcat6