ToB企服应用市场:ToB评测及商务社交产业平台

标题: pod管理及优化 [打印本页]

作者: 一给    时间: 2024-10-10 02:21
标题: pod管理及优化
 一、k8s中的资源

1、资源先容





   [root@k8s-master ~]# kubectl --namespace timinglee get po
No resources found in timinglee namespace.
  [root@k8s-master ~]# kubectl run testpod --image timinglee/nginx
  [root@k8s-master ~]# kubectl get pods -w
NAME      READY   STATUS              RESTARTS   AGE
testpod   0/1     ContainerCreating   0          19s
  [root@k8s-master ~]# kubectl  delete  pods  testpod --force
  2、资源管理方式


2.1 下令式对象管理


2.2  资源范例

k8s中所有的内容都抽象为资源
   kubectl api-resources
  常用资源范例


kubect常见下令操纵


2.3 基本下令示例





[root@k8s-master ~]# kubectl get deployments.apps  lee
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    2/2     2            2           7m10s
[root@k8s-master ~]# kubectl edit  deployments.apps lee
deployment.apps/lee edited

   [root@k8s-master ~]# kubectl get deployments.apps  lee
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    4/4     4            4           8m44s
  #####打补丁
  [root@k8s-master ~]# kubectl  patch  deployments.apps lee -p '{"spec":{"replicas":2}}'
deployment.apps/lee patched
[root@k8s-master ~]# kubectl get deployments.apps  lee
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    2/2     2            2           11m
[root@k8s-master ~]# kubectl delete  deployments.apps  lee
deployment.apps "lee" deleted
[root@k8s-master ~]# kubectl get pods
NAME     READY   STATUS    RESTARTS      AGE
myapp1   1/1     Running   1 (16m ago)   18m
  3、运行和调试下令

运行pod

[root@k8s-master ~]# kubectl run testpod --image nginx
pod/testpod created
[root@k8s-master ~]# kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
testpod   1/1     Running   0          5s
[root@k8s-master ~]# kubectl get pods -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP         NODE       NOMINATED NODE   READINESS GATES
testpod   1/1     Running   0          44s   10.244.1.3   k8s-node2.timinglee.org   <none>           <none>
端口暴露



####编写控制器设置

[root@k8s-master ~]# kubectl  edit service testpod
service/testpod edited




###使用补丁修改控制器设置

[root@k8s-master ~]# kubectl patch service testpod -p '{"spec":{"type":"ClusterIP"}}'
service/testpod patched

###删除

[root@k8s-master ~]# kubectl delete service testpod
检察资源详细信息 


检察日志 

 运行交互pod


 [root@reg ~]# docker push  reg.timinglee.org/library/busybox:latest
The push refers to repository [reg.timinglee.org/library/busybox]
d51af96cf93e: Layer already exists
latest: digest: sha256:28e01ab32c9dbcbaae96cf0d5b472f22e231d9e603811857b295e61197e40a9b size: 527
###ctrl+pq退出不停止pod


 运行非交互pod

[root@k8s-master ~]# kubectl run nginx --image nginx
pod/nginx created
##进入已经运行的容器,且容器有交互环境


###在已经运行的pod中运行指定下令



 ###日志文件到pod中


 复制pod文件到本机

[root@k8s-master ~]# kubectl cp nginx:/boot/ anaconda-ks.cfg
tar: Removing leading `/' from member names
4、高级下令示例


[root@k8s-master ~]# vim testpod1.yml 


   [root@k8s-master ~]# docker tag timinglee/myapp:v1 reg.timinglee.org/library/myapp:v1
[root@k8s-master ~]# docker tag timinglee/myapp:v2 reg.timinglee.org/library/myapp:v2
[root@k8s-master ~]# docker tag busyboxplus:latest reg.timinglee.org/library/busyboxplus:latest
[root@k8s-master ~]# docker push reg.timinglee.org/library/myapp:v1
 [root@k8s-master ~]# docker push reg.timinglee.org/library/myapp:v2
[root@k8s-master ~]# docker push reg.timinglee.org/library/busyboxplus:latest
  [root@k8s-master ~]# kubectl get pods
No resources found in default namespace.
[root@k8s-master ~]# kubectl run testpod1 --image nginx --dry-run=client -o yaml > testpod1.yml
[root@k8s-master ~]# vim testpod1.yml

[root@k8s-master ~]# kubectl create -f testpod1.yml
pod/myapp1 created
[root@k8s-master ~]# vim testpod1.yml

[root@k8s-master ~]# kubectl create -f testpod1.yml
Error from server (AlreadyExists): error when creating "testpod1.yml": pods "myapp1" already exists

################################## 

[root@k8s-master ~]# kubectl get pods
NAME     READY   STATUS    RESTARTS      AGE
myapp1   1/1     Running   1 (16m ago)   18m
[root@k8s-master ~]# kubectl delete  pods myapp1
pod "myapp1" deleted
[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.

 ####天生控制器


[root@k8s-master ~]# kubectl create deployment  lee --image myapp:v1 --dry-run=client -o yaml > deployment.yml
[root@k8s-master ~]# vim deployment.yml

[root@k8s-master ~]# vim deployment.yml

[root@k8s-master ~]# kubectl  apply -f deployment.yml
deployment.apps/lee created
[root@k8s-master ~]# kubectl get deployments.apps  lee
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    2/2     2            2           12s
检察详细信息

删除

[root@k8s-master ~]# kubectl delete -f  deployment.yml
deployment.apps "lee" deleted
[root@k8s-master ~]# kubectl get deployments.apps
No resources found in default namespace.
资源标签

[root@k8s-master ~]# kubectl  apply -f deployment.yml
deployment.apps/lee created
[root@k8s-master ~]# kubectl get pods --show-labels
NAME                   READY   STATUS    RESTARTS   AGE   LABELS
lee-579c757b65-8lvlh   1/1     Running   0          20s   app=lee,pod-template-hash=579c757b65
lee-579c757b65-ptmkt   1/1     Running   0          20s   app=lee,pod-template-hash=579c757b65
[root@k8s-master ~]# watch -n 1 kubectl get pods --show-labels




二、什么是pod



1、创建自主式pod (生产不推荐)

长处:

灵活性高:

学习和调试方便:

适用于特殊场景:

缺点:

管理复杂:

缺乏高级功能:

可维护性差:


2、使用控制器管理pod (推荐)

高可用性和可靠性:


可扩展性:


版本管理和更新:


声明式设置:


服务发现和负载平衡:


多环境一致性:


 
实行

[root@k8s-master ~]# vim testpod1.yml
 

[root@k8s-master ~]# kubectl logs pods/myapp1 web2
####web2的80端口被web1占用
[root@k8s-master ~]# kubectl delete -f testpod1.yml
pod "myapp1" deleted
[root@k8s-master ~]# vim testpod1.yml


[root@k8s-master ~]# kubectl delete -f testpod1.yml
pod "myapp1" deleted



自动注册和发现 

[root@k8s-master ~]# vim deployment.yml

[root@k8s-master ~]# kubectl apply -f deployment.yml
deployment.apps/lee created
[root@k8s-master ~]# kubectl get deployments.apps
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    2/2     2            2           16s



   扩容
  [root@k8s-master ~]# kubectl scale deployment lee --replicas 4
deployment.apps/lee scaled
[root@k8s-master ~]# kubectl get deployments.apps
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    4/4     4            4           11m
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-jpb6g
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-hstck
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-j98ss
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-jpb6g
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-hstck
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-5mp22
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-5mp22

  [root@k8s-master ~]# kubectl delete service lee
service "lee" deleted
[root@k8s-master ~]# kubectl delete -f deployment.yml
deployment.apps "lee" deleted
    建立pod
  
 

  

  3、k8s中pod更新及回滚 



[root@k8s-master ~]# curl  10.244.1.19
Hello MyApp | Version: v1 | <a href="hostname.html">od Name</a>

更新版本

[root@k8s-master ~]# kubectl rollout history deployment timinglee
deployment.apps/timinglee
REVISION  CHANGE-CAUSE
1         <none>

[root@k8s-master ~]# curl 10.108.221.114
Hello MyApp | Version: v2 | <a href="hostname.html">od Name</a>
回滚

[root@k8s-master ~]# kubectl rollout undo deployment timinglee --to-revision 1
deployment.apps/timinglee rolled back
[root@k8s-master ~]# curl 10.108.221.114
Hello MyApp | Version: v1 | <a href="hostname.html">od Name</a>
[root@k8s-master ~]# curl 10.108.221.114
Hello MyApp | Version: v1 | <a href="hostname.html">od Name</a>
4、使用yaml文件部署应用

4.1用yaml文件部署应用有以下长处

声明式设置:

与工具集成:

 4.2 资源清单参数






实行



[root@k8s-master pod]# kubectl run lee --image myapp:v1 --dry-run=client -o yaml > lee.yml
[root@k8s-master pod]# vim lee.yml

[root@k8s-master pod]# kubectl apply -f lee.yml
pod/lee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
lee                         1/1     Running   0          5s
timinglee-c56f584cf-6jxb7   1/1     Running   0          23m
timinglee-c56f584cf-mwrx8   1/1     Running   0          23m
[root@k8s-master pod]# kubectl create namespace leespace
namespace/leespace created
[root@k8s-master pod]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   11h
kube-flannel      Active   11h
kube-node-lease   Active   11h
kube-public       Active   11h
kube-system       Active   11h
leespace          Active   15s
[root@k8s-master pod]# vim lee.yml

[root@k8s-master pod]# kubectl apply -f lee.yml
pod/lee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
lee                         1/1     Running   0          12m
timinglee-c56f584cf-6jxb7   1/1     Running   0          35m
timinglee-c56f584cf-mwrx8   1/1     Running   0          35m
[root@k8s-master pod]#  kubectl delete pods lee
pod "lee" deleted
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee-c56f584cf-6jxb7   1/1     Running   0          37m
timinglee-c56f584cf-mwrx8   1/1     Running   0          37m
[root@k8s-master pod]# kubectl -n leespace get pods
NAME   READY   STATUS    RESTARTS   AGE
lee    1/1     Running   0          2m2s
[root@k8s-master pod]# kubectl delete -f lee.yml
pod "lee" deleted

4.3 获取资源资助


4.4 编写示例

k8s中pod的yml写法及资源详解
运行一个简单的单个容器pod

[root@k8s-master yaml]# kubectl run timinglee --image myapp:v1 --dry-run=client -o yaml > pod.yml
[root@k8s-master yaml]# vim pod.yml

运行多个容器pod-------共享一个网络站

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods timinglee
NAME        READY   STATUS    RESTARTS   AGE
timinglee   2/2     Running   0          36s
[root@k8s-master yaml]# kubectl describe pods timinglee

[root@k8s-master yaml]# kubectl delete -f pod.yml --force
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl  apply  -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods timinglee
NAME        READY   STATUS   RESTARTS     AGE
timinglee   2/3     Error    1 (4s ago)   10s
[root@k8s-master yaml]# kubectl describe pods timinglee




[root@k8s-master yaml]# kubectl delete  -f pod.yml
pod "timinglee" deleted
明白pod间的网络整合

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl  apply  -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl exec pods/timinglee -c busyboxplus -it -- /bin/sh
/ # curl localhost
Hello MyApp | Version: v1 | <a href="hostname.html">od Name</a>
[root@k8s-master yaml]# kubectl delete  -f pod.yml --force
端口映射

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl  apply  -f pod.yml
pod/timinglee created

若端口没有暴露,则不能访问
[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created



[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
怎样设定环境变量

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   1/1     Running   0          62s
timinglee-c56f584cf-6jxb7   1/1     Running   0          11h
timinglee-c56f584cf-mwrx8   1/1     Running   0          11h
[root@k8s-master yaml]# kubectl logs pods/timinglee busybox
timinglee
[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
资源限定


[root@k8s-master yaml]# vim pod.yml
 
[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods  timinglee
NAME        READY   STATUS    RESTARTS   AGE
timinglee   1/1     Running   0          22s
[root@k8s-master yaml]# kubectl describe pods timinglee

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
资源敏感型
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl describe pods timinglee

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
容器启动管理

[root@k8s-master yaml]# vim pod.yml



[root@k8s-master yaml]# kubectl describe pods timinglee

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
[root@k8s-master yaml]# vim pod.yml


[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created

####异常
[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   0/1     Error     0          2s
timinglee-c56f584cf-6jxb7   1/1     Running   0          12h
timinglee-c56f584cf-mwrx8   1/1     Running   0          12h
[root@k8s-master yaml]# kubectl get pods
NAME                        READY   STATUS             RESTARTS      AGE
timinglee                   0/1     CrashLoopBackOff   2 (12s ago)   30s
timinglee-c56f584cf-6jxb7   1/1     Running            0             12h
timinglee-c56f584cf-mwrx8   1/1     Running            0             12h
[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
选择运行节点

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
共享宿主机网络 

[root@k8s-master yaml]# vim pod.yml


[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
宿主机网络
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl exec pods/timinglee -c busybox -it -- /bin/sh
/ #
/ # ifconfig

三、pod的生命周期



1、INIT容器的功能


2、INIT容器示例


[root@k8s-master pod]# kubectl run timinglee --image myapp:v1 --dry-run=client -o yaml > timinglee.yml
[root@k8s-master pod]# vim timinglee.yml

[root@k8s-master pod]# kubectl apply -f timinglee.yml
pod/timinglee created
[root@k8s-master pod]# kubectl  get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   1/1     Running   0          43s
timinglee-c56f584cf-6jxb7   1/1     Running   0          12h
timinglee-c56f584cf-mwrx8   1/1     Running   0          12h
[root@k8s-master pod]# kubectl delete -f timinglee.yml --force
[root@k8s-master pod]# vim timinglee.yml



[root@k8s-master pod]# kubectl delete -f timinglee.yml
pod "timinglee" deleted
3、 探针

探针是由kubelet对容器执行的定期诊断



每次探测都将获得以下三种结果之一

Kubelet 可以选择是否执行在容器上运行的三种探针执行和做出反应


ReadinessProbe 与 LivenessProbe 的区别


ReadinessProbe、LivenessProbe的区别


3.1 探针实例

3.1.1存活探针示例:

[root@k8s-master pod]# ls
lee.yml  timinglee.yml
[root@k8s-master pod]# vim timinglee.yml

[root@k8s-master pod]# kubectl apply -f timinglee.yml
pod/timinglee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS     AGE
timinglee                   1/1     Running   2 (2s ago)   12s
timinglee-c56f584cf-6jxb7   1/1     Running   0            13h
timinglee-c56f584cf-mwrx8   1/1     Running   0            13h
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS             RESTARTS     AGE
timinglee                   0/1     CrashLoopBackOff   4 (3s ago)   44s
timinglee-c56f584cf-6jxb7   1/1     Running            0            13h
timinglee-c56f584cf-mwrx8   1/1     Running            0            13h
[root@k8s-master pod]# kubectl describe pods

[root@k8s-master pod]# kubectl delete -f timinglee.yml
pod "timinglee" deleted
[root@k8s-master pod]# vim timinglee.yml

[root@k8s-master pod]# kubectl apply -f timinglee.yml
pod/timinglee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   1/1     Running   0          13s
timinglee-c56f584cf-6jxb7   1/1     Running   0          13h
timinglee-c56f584cf-mwrx8   1/1     Running   0          13h
[root@k8s-master pod]# kubectl describe pods

[root@k8s-master pod]# kubectl delete -f timinglee.yml
pod "timinglee" deleted
3.1.2停当探针示例:

[root@k8s-master pod]# vim timinglee.yml

[root@k8s-master pod]# kubectl apply -f timinglee.yml
pod/timinglee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   0/1     Running   0          12s
timinglee-c56f584cf-6jxb7   1/1     Running   0          13h
timinglee-c56f584cf-mwrx8   1/1     Running   0          13h
[root@k8s-master pod]# kubectl describe pods


[root@k8s-master pod]# kubectl exec pods/timinglee -c timinglee -- /bin/sh -c "echo hello test > /usr/share/nginx/html/test.html"

[root@k8s-master pod]# curl 10.244.1.34
Hello MyApp | Version: v1 | <a href="hostname.html">od Name</a>
[root@k8s-master pod]# curl 10.244.1.34/test.html
hello test



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4