✅P347_k8s入门-基本操作体验
大约 5 分钟
部署一个 tomcat(Master)
[root@k8s-node1 ~]# kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8
获取到 tomcat 信息
[root@k8s-node1 ~]# kubectl get all -o wide
获取所有的资源
[root@k8s-node1 ~]# kubectl get all
kubectl get pods -o wide
可以获取到tomcat部署信息,能够看到它被部署到了哪个node上了
假如说被部署到了node3节点,查看下载了哪些镜像:最后一个是tomcat
[root@k8s-node3 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flannel/flannel v0.25.2 c49ccdef0ab0 10 days ago 80.9MB
flannel/flannel-cni-plugin v1.4.1-flannel1 1e3c860c213d 6 weeks ago 10.3MB
quay.io/coreos/flannel v0.15.1 e6ea68648f0c 2 years ago 69.5MB
rancher/mirrored-flannelcni-flannel-cni-plugin v1.0.0 cd5235cd7dc2 2 years ago 9.03MB
quay.io/coreos/flannel v0.12.0-amd64 4e9f801d2217 4 years ago 52.8MB
registry.cn-zhangjiakou.aliyuncs.com/test-lab/coreos-flannel amd64 4e9f801d2217 4 years ago 52.8MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy v1.17.3 ae853e93800d 4 years ago 116MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver v1.17.3 90d27391b780 4 years ago 171MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager v1.17.3 b0f1517c1f4b 4 years ago 161MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler v1.17.3 d109c0821a2b 4 years ago 94.4MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns 1.6.5 70f311871ae1 4 years ago 41.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd 3.4.3-0 303ce5db0e90 4 years ago 288MB
quay.io/coreos/flannel v0.11.0-amd64 ff281650a721 5 years ago 52.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 6 years ago 742kB
tomcat 6.0.53-jre8 49ab0583115a 6 years ago 290MB
查看Node3节点上,正在运行的容器:
[root@k8s-node3 ~]# docker ps
在node1上执行:
[root@k8s-node1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat6-5f7ccf4cb9-4k692 1/1 Running 0 77s
[root@k8s-node1 ~]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default tomcat6-5f7ccf4cb9-4k692 1/1 Running 0 83s
kube-system coredns-7f9c544f75-k5nwq 1/1 Running 1 29m
kube-system coredns-7f9c544f75-prjtp 1/1 Running 1 29m
kube-system etcd-k8s-node1 1/1 Running 7 50m
kube-system kube-apiserver-k8s-node1 1/1 Running 7 50m
kube-system kube-controller-manager-k8s-node1 1/1 Running 8 50m
kube-system kube-flannel-ds-49tjl 1/1 Running 1 23m
kube-system kube-flannel-ds-98mgg 1/1 Running 1 23m
kube-system kube-flannel-ds-amd64-2khx8 1/1 Running 4 48m
kube-system kube-flannel-ds-amd64-5jtfr 1/1 Running 1 34m
kube-system kube-flannel-ds-amd64-k426h 1/1 Running 1 34m
kube-system kube-flannel-ds-fvss2 1/1 Running 2 23m
kube-system kube-proxy-bsgqt 1/1 Running 4 50m
kube-system kube-proxy-fwdgg 1/1 Running 1 34m
kube-system kube-proxy-tbdxl 1/1 Running 1 34m
kube-system kube-scheduler-k8s-node1 1/1 Running 8 50m
从前面看到tomcat部署在Node3上,现在模拟因为各种原因宕机的情况,将node3关闭电源,观察情况。
docker stop执行的时候,docker ps发现又有新的容器了,这是k8s又新建了,所以选择关机node3
[root@k8s-node1 k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-node1 Ready master 79m v1.17.3
k8s-node2 Ready <none> 41m v1.17.3
k8s-node3 NotReady <none> 41m v1.17.3
得等个几分钟才能容灾恢复
[root@k8s-node1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat6-5f7ccf4cb9-4k692 1/1 Terminating 0 11m 10.244.2.4 k8s-node3 <none> <none>
tomcat6-5f7ccf4cb9-rgqcg 1/1 Running 0 2m3s 10.244.1.5 k8s-node2 <none> <none>
暴露nginx访问
在master上执行
tomcat镜像端口8080,转发到pod的80端口上,然后转发到虚拟机的XXX端口上(自动生成)
kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort
查看服务:
[root@k8s-node1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 54m
tomcat6 NodePort 10.96.38.225 <none> 80:32510/TCP 7s
[root@k8s-node1 ~]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 55m <none>
tomcat6 NodePort 10.96.38.225 <none> 80:32510/TCP 28s app=tomcat6
浏览器输入:http://192.168.56.101:32510/ ,可以看到tomcat首页;
输入下面命令可以看到pod和封装pod 的service,pod是部署产生的,部署还有一个副本
[root@k8s-node1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcat6-5f7ccf4cb9-4k692 1/1 Terminating 0 12m
pod/tomcat6-5f7ccf4cb9-rgqcg 1/1 Running 0 3m7s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 61m
service/tomcat6 NodePort 10.96.38.225 <none> 80:32510/TCP 6m40s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcat6 1/1 1 1 12m
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcat6-5f7ccf4cb9 1 1 1 12m
动态扩容测试
扩容
命令:kubectl get deployment
[root@k8s-node1 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
tomcat6 1/1 1 1 12m
应用升级: kubectl set image
(–help查看帮助)
扩容: kubectl scale --replicas=3 deployment tomcat6
[root@k8s-node1 ~]# kubectl scale --replicas=3 deployment tomcat6
deployment.apps/tomcat6 scaled
[root@k8s-node1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat6-5f7ccf4cb9-748gk 1/1 Running 0 7s 10.244.2.6 k8s-node3 <none> <none>
tomcat6-5f7ccf4cb9-mrdsn 1/1 Running 0 19s 10.244.2.5 k8s-node3 <none> <none>
tomcat6-5f7ccf4cb9-rgqcg 1/1 Running 0 12m 10.244.1.5 k8s-node2 <none> <none>
[root@k8s-node1 ~]# kubectl get svc -o wide
.....
扩容了多份,所有无论访问哪个node的指定端口(kubectl get svc -o wide生成的),都可以访问到tomcat6;
缩容
缩容:kubectl scale --replicas=2 deployment tomcat6
[root@k8s-node1 ~]# kubectl scale --replicas=1 deployment tomcat6
deployment.apps/tomcat6 scaled
[root@k8s-node1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat6-5f7ccf4cb9-rgqcg 1/1 Running 0 15m 10.244.1.5 k8s-node2 <none> <none>
[root@k8s-node1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcat6-5f7ccf4cb9-rgqcg 1/1 Running 0 16m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 74m
service/tomcat6 NodePort 10.96.38.225 <none> 80:32510/TCP 19m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcat6 1/1 1 1 25m
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcat6-5f7ccf4cb9 1 1 1 25m
以上操作的yaml获取
# 尝试运行,并不会真正的创建镜像
kubectl create deployment web --image=nginx -o yaml --dry-run
删除
kubectl get all
kubectl delete deploy/nginx
kubectl delete service/nginx-service
kubectl get all
[root@k8s-node1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcat6-5f7ccf4cb9-rgqcg 1/1 Running 0 18m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 76m
service/tomcat6 NodePort 10.96.38.225 <none> 80:32510/TCP 21m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcat6 1/1 1 1 27m
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcat6-5f7ccf4cb9 1 1 1 27m
#删除deployment.apps/tomcat6
[root@k8s-node1 ~]# kubectl delete deployment.apps/tomcat6
deployment.apps "tomcat6" deleted
#查看剩余的资源
[root@k8s-node1 ~]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 77m
service/tomcat6 NodePort 10.96.38.225 <none> 80:32510/TCP 22m
# 此时没有了部署,但是有service,没有pod只有service也是没有对应的服务的
# 查看pod信息
[root@k8s-node1 ~]# kubectl get pods
No resources found in default namespace.
流程:创建 deployment 会管理 replicas,replicas 控制 pod 数量,有 pod 故障会自动拉起新的 pod