Pod是可以创建和管理Kubernetes计算的最小可部署单元,一个Pod代表着集群中运行的一个进程,每个pod都有一个唯一的ip。
一个pod类似一个豌豆荚,包含一个或多个容器(通常是docker),多个容器间共享IPC、Network和UTC namespace。
kubectl命令:https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
1.超亲密容器之间的通信,一个pod内的容器共享一个网络栈
2.共享数据,提供相对持久化的存储
pod环境来自于pause镜像
[root@server2 ~]# kubectl get pod ##获取pod
[root@server2 ~]# kubectl get ns ##查看namespace
NAME STATUS AGE
default Active 42h
kube-node-lease Active 42h
kube-public Active 42h
kube-system Active 42h
[root@server2 ~]# kubectl get pod -n kube-system ##查看相应namespace对应的pod,不加namespace就自动选择默认的namespace
NAME READY STATUS RESTARTS AGE
coredns-7f89b7bc75-4r9x6 1/1 Running 1 42h
coredns-7f89b7bc75-sxgkp 1/1 Running 1 42h
etcd-server2 1/1 Running 4 42h
kube-apiserver-server2 1/1 Running 3 42h
kube-controller-manager-server2 1/1 Running 3 42h
kube-flannel-ds-amd64-9m29g 1/1 Running 1 36h
kube-flannel-ds-amd64-c2rc7 1/1 Running 1 41h
kube-flannel-ds-amd64-xnb9p 1/1 Running 1 36h
kube-proxy-dlxmm 1/1 Running 1 36h
kube-proxy-mh677 1/1 Running 1 36h
kube-proxy-p87p4 1/1 Running 3 42h
kube-scheduler-server2 1/1 Running 4 42h
[root@server2 ~]# kubectl describe pod coredns-7f89b7bc75-4r9x6 -n kube-system ##如果出错可以使用这条命令来查看错误信息,要标明对应的namespace
[root@server2 ~]# kubectl delete pod cDoredns-7f89b7bc75-4r9x6 -n kube-system ##删除对应的pod
创建自主式pod :没有自愈性(生产不推荐)
[root@k8s2 ~]# kubectl run demo --image=myapp:v1
创建控制器(推荐)
[root@k8s2 ~]# kubectl create deployment myapp --image=myapp:v1 --replicas=3
[root@server2 ~]# kubectl run -h ##查看创建pod帮助
[root@server2 ~]# kubectl run nginx --image=myapp:v1 ##创建一个pod应用,nginx是名字,myapp是镜像
#--replicas=2 扩容参数 --record 记录参数
# kubectl run nginx --image=nginx --replicas=2 --record #参数使用方法
[root@server2 ~]# kubectl get pod ##查看pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 11s
[root@server2 ~]# kubectl get pod -n default ##查看指定namespace的节点
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 25s
[root@server2 ~]# kubectl get pod -o wide ##查看节点的具体信息
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 71s 10.244.2.3 server4
[root@server2 ~]# kubectl run demo --image=busyboxplus -it ##交互式界面运行一个pod,ctrl+p+q退出,可以通过kubectl attach demo -c demo -i -t重新进入
[root@server2 ~]# kubectl run demo --image=busyboxplus -it ##再次进入
[root@server2 ~]# kubectl delete pod nginx ##删除pod
pod "nginx" deleted
[root@server2 ~]# kubectl describe pod demo ##查看详细描述
[root@server2 ~]# kubectl logs demo ##查看日志
控制器自动维护pod副本数
[root@server2 ~]# kubectl create deployment nginx --image=myapp:v1 ##创建一个pod
deployment.apps/nginx created ##生成一个新的deployment控制器的pod,删除之后会自动重新生成不同id的pod
[root@server2 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/demo 1/1 Running 0 70m
pod/nginx-67f9d9c97f-mb6zb 1/1 Running 0 13s ##id为mb6zb
[root@server2 ~]# kubectl delete pod nginx-67f9d9c97f-mb6zb ##删除pod后再查看
pod "nginx-67f9d9c97f-mb6zb" deleted
[root@server2 ~]# kubectl get pod ##生成新的id的pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 0 73m
nginx-67f9d9c97f-4stvr 1/1 Running 0 16s
[root@server2 ~]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 1/1 1 1 16s
[root@server2 ~]# kubectl delete deployment nginx ##彻底删除deployment
[root@server2 ~]# kubectl get pod ##删除成功
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 0 74m
在远程pod中执行命令
[root@k8s2 ~]# kubectl exec nginx-67f9d9c97f-4stvr – ls /usr/share/nginx/html
50x.html
index.html
[root@server2 ~]# kubectl scale deployment --replicas=2 nginx
[root@server2 ~]# kubectl scale deployment nginx --replicas=2 ##以上两种书写方式都可以
deployment.apps/nginx scaled
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 0 76m
nginx-67f9d9c97f-48gfz 1/1 Running 0 27s
nginx-67f9d9c97f-jh76r 1/1 Running 0 2s
[root@server2 ~]# kubectl scale deployment --replicas=4 nginx ##扩容为4个
deployment.apps/nginx scaled
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 0 76m
nginx-67f9d9c97f-48gfz 1/1 Running 0 43s
nginx-67f9d9c97f-7gh4m 1/1 Running 0 3s
nginx-67f9d9c97f-hcd5z 1/1 Running 0 3s
nginx-67f9d9c97f-jh76r 1/1 Running 0 18s
[root@server2 ~]# kubectl scale deployment nginx --replicas=3 ##缩容为3个
deployment.apps/myapp scaled
[root@server2 ~]# kubectl get pod
- service是一个抽象概念,定义了一个服务的多个pod逻辑合集和访问pod的策略,一般把service称为微服务。
- 创建service
$ kubectl expose deployment(控制器) nginx --port=80 --target-port=80
此时pod客户端可以通过service的名称访问后端的两个Pod
ClusterIP: 默认类型,自动分配一个仅集群内部可以访问的虚拟IP
- 使用NodePort类型暴露端口,让外部客户端访问Pod(即真机也可以访问)
$ kubectl edit svc nginx //修改service的type为NodePort
$ kubectl expose deployment nginx --port=80 --target-port=80 --type=NodePort //也可以在创建service时指定类型
NodePort: 在ClusterIP基础上为Service在每台机器上绑定一个端口,这样就可以通过 NodeIP:NodePort 来访问该服务
service自动发现pod扩容与缩容,自动更新endpoints,实现对应用的负载均衡
service默认使用 ClusterIP类型,只能在集群中访问
NodePort类型,可以在集群外部访问
[root@server2 ~]# kubectl expose deployment nginx --port=80 ##ClusterIP默认类型暴露端口
service/nginx exposed
[root@server2 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 45h
nginx ClusterIP 10.104.139.148 80/TCP 9s
[root@server2 ~]# curl 10.104.139.148
Hello MyApp | Version: v1 | Pod Name
[root@server2 ~]# curl 10.104.139.148/hostname.html
nginx-67f9d9c97f-48gfz
[root@server2 ~]# curl 10.104.139.148/hostname.html
nginx-67f9d9c97f-hcd5z
[root@server2 ~]# curl 10.104.139.148/hostname.html
nginx-67f9d9c97f-hcd5z
[root@server2 ~]# curl 10.104.139.148/hostname.html
nginx-67f9d9c97f-48gfz
[root@server2 ~]# kubectl describe svc nginx ##查看deployment详细信息
[root@server2 ~]# kubectl get pod -n kube-system -o wide ##查看namespace对应的所有pod详细信息
##server3和server4上同样可以访问到对应信息
[root@server3 ~]# curl 10.104.139.148/hostname.html
[root@server4 ~]# curl 10.104.139.148/hostname.html
通过service暴露pod
[root@k8s2 ~]# kubectl expose deployment myapp --port=80 --target-port=80
查看svc详情
[root@k8s2 ~]# kubectl describe svc myapp
Name: myapp
Namespace: default
Labels: app=myapp
Annotations:
Selector: app=myapp
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.106.225.101
IPs: 10.106.225.101
Port: 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.2:80,10.244.1.3:80,10.244.2.4:80 ##3个地址为pod内的3个ip地址,svc对Endpoints进行随机的负载均衡
Session Affinity: None
Events:
[root@server2 ~]# kubectl get svc ##查看TYPE,svc为service的简写
[root@server2 ~]# kubectl edit svc nginx ##type改成NodePort ##集群外真机可以访问
[root@server2 ~]# kubectl get svc ##80映射31147端口
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 45h
nginx NodePort 10.104.139.148 80:31147/TCP 30m
给集群所有节点绑定31147端口(端口为一对一),真机可以访问集群任何节点
##真机测试:访问集群任意节点+端口
[root@server1 docker]# curl 192.168.117.12:31147/hostname.html
myapp-678fcbc488-gkf4z
[root@server1 docker]# curl 192.168.117.13:31147/hostname.html
myapp-678fcbc488-gqdgk
[root@server1 docker]# curl 192.168.117.14:31147/hostname.html
myapp-678fcbc488-v7ftf
[root@server2 ~]# kubectl set image deployment/nginx myapp=myapp:v2 ##更新到v2
[root@server2 ~]# kubectl rollout history deployment nginx ##查看更新记录
更新应用版本【吴分享】
[root@k8s2 ~]# kubectl set image deployment/myapp myapp=myapp:v2
[root@k8s1 docker]# curl 192.168.56.14:30280
Hello MyApp | Version: v2 | Pod Name
$ kubectl rollout history deployment nginx //查看历史版本
$ kubectl rollout undo deployment nginx --to-revision=1 //回滚版本
[root@server2 ~]# kubectl rollout history deployment nginx
[root@server2 ~]# kubectl rollout undo deployment nginx --to-revision=1 ##回滚之前版本
deployment.apps/nginx rolled back
[root@server2 ~]# kubectl rollout history deployment nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
2
3
[root@server2 ~]# kubectl get pod ##rs值变成版本1的rs值
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 0 136m
nginx-67f9d9c97f-mv6n2 1/1 Running 0 22s
nginx-67f9d9c97f-zgpgh 1/1 Running 0 23s
[root@server2 ~]# kubectl describe deployments.apps nginx
[root@server2 ~]# kubectl delete deployments.apps myapp
[root@server2 ~]# kubectl delete svc myapp
[root@server2 ~]# kubectl get pod
No resources found in default namespace.
集群通过namespace来做资源隔离,默认操作的资源都指向default
[root@k8s2 ~]# kubectl get ns
NAME STATUS AGE
default Active 2d22h
kube-flannel Active 2d22h
kube-node-lease Active 2d22h
kube-public Active 2d22h
kube-system Active 2d22h