Kubernetes集群——(k8s)pod管理
一、pod简介Pod是可以创建和管理Kubernetes计算的最小可部署单元,一个Pod代表着集群中运行的一个进程,每个pod都有一个唯一的ip。一个pod类似一个豌豆荚,包含一个或多个容器(通常是docker),多个容器间共享IPC、Network和UTC namespace二、pod管理kubectl命令:https://kubernetes.io/docs/reference/generat
·
一、pod简介
Pod是可以创建和管理Kubernetes计算的最小可部署单元,一个Pod代表着集群
中运行的一个进程,每个pod都有一个唯一的ip。
一个pod类似一个豌豆荚,包含一个或多个容器(通常是docker),多个容器间
共享IPC、Network和UTC namespace
二、pod管理
kubectl命令:https://kubernetes.io/docs/reference/generated/kubectl/kubectl- commands
harbor仓库中导入后续实验所需的镜像,可以直接在阿里云镜像官网下载
https://developer.aliyun.com/mirror/
2.1创建自主式pod
当前节点状态
[root@server2 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
server2 Ready master 41h v1.18.4
server3 Ready <none> 3m32s v1.18.4
server4 Ready <none> 40h v1.18.4
创建pod;run:指定名称Nginx也可以是其他名称,--image:指定镜像
[root@server2 ~]# kubectl run nginx --image=nginx
pod/nginx created
[root@server2 ~]# kubectl get pod 查看运行的pod;正常运行
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 6s
[root@server2 ~]# kubectl get pod -o wide 查看pod:IP和运行主机
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 19s 10.244.2.2 server4 <none> <none>
[root@server2 ~]# kubectl describe pod nginx 查看pod的详细信息
只要是在集群当中都可以访问,集群外部无法访问
在server4节点上查看
删除pod
[root@server2 ~]# kubectl delete pod nginx
pod "nginx" deleted
[root@server2 ~]# kubectl get pod
No resources found in default namespace.
2.2使用控制器来创建pod
[root@server2 ~]# kubectl create deployment myapp --image=nginx 创建pod
deployment.apps/myapp created
[root@server2 ~]# kubectl get pod 查看
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-ldxpk 1/1 Running 0 8s
myapp:控制器名称 ;687598b8b4 :(replicaset)简称:rs ;ldxpk:容器名称默认只要一个可以做拉伸
[root@server2 ~]# kubectl delete pod myapp-687598b8b4-ldxpk 删除pod之后
pod "myapp-687598b8b4-ldxpk" deleted
[root@server2 ~]# kubectl get pod 再次可查看,自动拉取一个副本
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-p5wcs 1/1 Running 0 15s
完全彻底删除:直接删掉控制器
[root@server2 ~]# kubectl delete deployments.apps myapp 删除控制器
deployment.apps "myapp" deleted
[root@server2 ~]# kubectl get pod 查看
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-p5wcs 0/1 Terminating正在回收中 0 6m24s
[root@server2 ~]# kubectl get pod 再次查看回首成功
No resources found in default namespace.
2.3pod手动扩容和缩容
[root@server2 ~]# kubectl create deployment myapp --image=nginx 创建一个控制器pod或自主式
deployment.apps/myapp created
[root@server2 ~]# kubectl get pod 查看
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-bpf59 1/1 Running 0 4s
[root@server2 ~]# kubectl scale deployment myapp --replicas=2 扩容两个
deployment.apps/myapp scaled
[root@server2 ~]# kubectl get pod 查看
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-4z7ns 1/1 Running 0 2s
myapp-687598b8b4-bpf59 1/1 Running 0 78s
[root@server2 ~]# kubectl scale deployment myapp --replicas=5 扩容五个
deployment.apps/myapp scaled
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-4slxf 1/1 Running 0 5s
myapp-687598b8b4-bpf59 1/1 Running 0 4m39s
myapp-687598b8b4-crcm7 1/1 Running 0 5s
myapp-687598b8b4-gl9gb 1/1 Running 0 3m15s
myapp-687598b8b4-sxcx7 1/1 Running 0 5s
[root@server2 ~]# kubectl scale deployment myapp --replicas=2 缩容
deployment.apps/myapp scaled
[root@server2 ~]# kubectl get pod 其他的正在回收
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-4q6mw 1/1 Terminating 0 114s
myapp-687598b8b4-4z7ns 1/1 Terminating 0 2m2s
myapp-687598b8b4-bpf59 1/1 Running 0 3m18s
myapp-687598b8b4-gl9gb 1/1 Running 0 114s
myapp-687598b8b4-kgpqw 1/1 Terminating 0 114s
[root@server2 ~]# kubectl get pod 查看
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-bpf59 1/1 Running 0 3m57s
myapp-687598b8b4-gl9gb 1/1 Running 0 2m33s
2.4.service设定
service是一个抽象概念,定义了一个服务的多个pod逻辑合集和访问pod的策略,
一般把service称为微服务;主要是提供负载均衡和服务自动发现。
创建service
此时pod客户端可以通过service的名称访问后端的两个Pod
kubectl expose deployment nginx --port=80 --target-port=80
ClusterIP: 默认类型,自动分配一个仅集群内部可以访问的虚拟IP;
仅仅作用于Kubernetes Service这个对象,由Kubernetes自己来进行管理和分配地址;
当然我们也无法ping这个地址,他没有一个真正的实体对象来响应;
只能结合Service Port来组成一个可以通信的服务。
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-7j5q5 1/1 Running 0 4s
myapp-687598b8b4-grmbb 1/1 Running 0 8s
[root@server2 ~]# kubectl get deployments.apps 查看deployment名称
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 2/2 2 2 3m7s
端口映射 指定端口:--port=80 --target-port=80 :容器监听端口目标端口
[root@server2 ~]# kubectl expose deployment myapp --port=80 --target-port=80
service/myapp exposed
[root@server2 ~]# kubectl get svc -o wide 查看服务
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h <none>
myapp ClusterIP 10.96.77.38 <none> 80/TCP 29s app=myapp
[root@server2 ~]# kubectl get pod --show-labels 查看pod的标签通过标签绑定IP
NAME READY STATUS RESTARTS AGE LABELS
myapp-687598b8b4-7j5q5 1/1 Running 0 10m app=myapp,pod-template-hash=687598b8b4
myapp-687598b8b4-grmbb 1/1 Running 0 10m app=myapp,pod-template-hash=687598b8b4
2.5pod镜像更新和回滚
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-687598b8b4-7j5q5原始 1/1 Running 0 37m
myapp-687598b8b4-grmbb原始 1/1 Running 0 37m
[root@server2 ~]# kubectl set image deployments myapp nginx=myapp:v1 --record 更新
deployment.apps/myapp image updated
[root@server2 ~]# kubectl get pod
NAME rs:也已经更新 READY STATUS RESTARTS AGE
myapp-76df559f68-995gd 已更新 1/1 Running 0 16s
myapp-76df559f68-pr6pr 已更新 1/1 Running 0 19s
查看历史版本
[root@server2 ~]# kubectl rollout history deployment myapp
deployment.apps/myapp
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deployments myapp nginx=myapp:v1 --record=true
创建一个交互式的shell
[root@server1 ~]# docker search busyboxplus 搜索
[root@server1 ~]# docker pull busyboxplus 下载
打标签并上传harbor仓库
[root@server2 ~]# kubectl get svc 查看服务信息
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 44h
myapp ClusterIP 10.96.77.38 <none> 80/TCP 116m
[root@server2 ~]# kubectl run demo --image=busyboxplus -it 运行交互式shell
[root@server2 ~]# kubectl attach demo -it 进入交互式shell
curl 10.96.77.38
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
curl 10.96.77.38/hostname.html 再次测试实现负载均衡
kubectl rollout undo deployment myapp --to-revision=2 回退版本
kubectl set image deployments myapp nginx=myapp:v2 --record=true 更换pod 镜像
kubectl get rs 查看rs
2.6让外部节点访问pod
此端口随机分配:32195
查看节点端口
[root@server3 ~]# netstat -tnpl | grep 32195
tcp 0 0 0.0.0.0:32195 0.0.0.0:* LISTEN 70165/kube-proxy
[root@server4 ~]# netstat -tnpl | grep 32195
tcp 0 0 0.0.0.0:32195 0.0.0.0:* LISTEN 63968/kube-proxy
更多推荐
已为社区贡献8条内容
所有评论(0)