[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
检察资源详细信息
[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
Pod是可以创建和管理Kubernetes计算的最小可部署单元
一个Pod代表着集群中运行的一个历程,每个pod都有一个唯一的ip
一个pod雷同一个豌豆荚,包含一个或多个容器(通常是docker)
多个容器间共享IPC、Network和UTC namespace
1、创建自主式pod (生产不推荐)
长处:
灵活性高:
可以精确控制 Pod 的各种设置参数,包罗容器的镜像、资源限定、环境变量、下令和参数等,满足特定的应用需求。
学习和调试方便:
对于学习 Kubernetes 的原理和机制非常有资助,通过手动创建 Pod 可以深入了解 Pod 的结构和设置方式。在调试问题时,可以更直接地观察和调整Pod的设置。
适用于特殊场景:
在一些特殊情况下,如进行一次性任务、快速验证概念或在资源受限的环境中进行特定设置时,手动创建 Pod 可能是一种有效的方式
[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 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