POD资源的介绍
1、kubernetes资源对象操作
资源对象
增
删
改
查
nodes
kubectl delete node
kubectl get nodes
namespace
kubectl create ns [资源名]
kubectl delete ns [资源名]
kubectl edit ns [资源名]
kubectl get ns
pod
kubectl create deployment [资源名]
kubectl delete pod [pod名]
kubectl get pod
1、nodes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 增 [root@master01 ~]# kubeadm token create --print-join-command 删 kubectl delete node 查 [root@master01 ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master01 Ready master 5d2h v1.19.3 node01 Ready <none> 5d2h v1.19.3 node02 Ready <none> 5d2h v1.19.3 node03 Ready <none> 5d2h v1.19.3 [root@master01 ~]# kubectl get ns NAME STATUS AGE default Active 5d2h kube-flannel Active 5d1h kube-node-lease Active 5d2h kube-public Active 5d2h kube-system Active 5d2h
2、namespace
1 2 3 4 5 6 7 8 9 10 11 增 kubectl create ns [资源名] 删 kubectl delete ns [资源名] 改 kubectl edit ns [资源名] 查 kubectl get ns
3、pod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 查 [root@master01 ~]# kubectl get pod [root@master01 ~]# kubectl get pod -n kube-flannel [root@master01 ~]# kubectl get pod --help -o或者--output:指定输出格式 json:输出json格式 yaml:输出yaml格式 wide:输出详细信息 -n:指定名称空间 [root@master01 ~]# kubectl get pod -n kube-flannel -o json [root@master01 ~]# kubectl get pod -n kube-flannel NAME READY STATUS RESTARTS AGE kube-flannel-ds-4ncf5 1/1 Running 0 5d2h kube-flannel-ds-7dnxx 1/1 Running 1 5d2h kube-flannel-ds-dpzqp 1/1 Running 0 5d2h kube-flannel-ds-knmch 1/1 Running 0 5d2h [root@master01 ~]# kubectl get pod -n kube-flannel kube-flannel-ds-4ncf5 -o json [root@master01 ~]# kubectl get pod nginx-5f5d9d69c4-pk4c4 -o yaml [root@master01 ~]# kubectl get pod -n kube-flannel -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES kube-flannel-ds-4ncf5 1/1 Running 0 5d2h 10.0.0.203 node03 <none> <none> kube-flannel-ds-7dnxx 1/1 Running 1 5d2h 10.0.0.200 master01 <none> <none> kube-flannel-ds-dpzqp 1/1 Running 0 5d2h 10.0.0.201 node01 <none> <none> kube-flannel-ds-knmch 1/1 Running 0 5d2h 10.0.0.202 node02 <none> <none> 增 kubectl create deployment pod名 --image=镜像:版本 [root@master01 ~]# kubectl create deployment nginx --image=nginx:alpine 删 [root@master01 ~]# kubectl delete pod nginx-65cc99f84f-8k9rj 改 [root@master01 ~]# kubectl edit deployment nginx 就会进入像vi编辑器一样的东西,找到image,改后面的镜像名 等待一会,这次查看pod,就会正常运行 [root@master01 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-5f5d9d69c4-pk4c4 1/1 Running 0 8m14s
2、POD的资源清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@master01 ~]# vim nginx.yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod namespace: default spec: containers: - image: nginx:alpine imagePullPolicy: IfNotPresent name: nginx-containers Always:总是拉取,不管当前机器上是否有改镜像都拉取 Never:从不拉取镜像,需要提前docker pull IfNotPresent:不存在则拉取 apiVersion: v1 可以理解为https://master的ip:6443/pod/v1一开始init初始化的时候就写了master的ip为10.0.0.200,会自动补全,可以理解为前面可以不写,会自动补全,这些接口全都注册在etcd里面 https://10.0.0.200:6443/pod/v1
1 2 3 4 5 6 7 8 9 10 [root@master01 ~]# kubectl create -f nginx.yml pod/nginx-pod created [root@master01 ~]# kubectl get pod nginx-pod 0/1 ContainerCreating 0 13s [root@master01 ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-pod 0/1 ImagePullBackOff 0 4m49s 10.2.3.6 node03 <none> <none>
3、手写资源清单
1 2 3 4 5 参考网址:http://k8s.driverzeng.com/v1.19/ 这4个必写的 object数据类型写法:直接回车,和上一个缩进写key: value的形式 []列表数据类型写法:回车之后写一个减号-,再空格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 1、编写资源清单 [root@master01 ~]# vim busybox.yml apiVersion: "v1" kind: "Namespace" metadata: name: aaa --- apiVersion: "v1" kind: "Pod" metadata: name: busybox-pod namespace: aaa spec: containers: - name: busybox-containers image: busybox imagePullPolicy: IfNotPresent - name: nginx-containers image: nginx:alpine imagePullPolicy: IfNotPresent 2、启动资源 [root@master01 ~]# kubectl apply -f busybox.yml namespace/aaa created pod/busybox-pod created 3、查看名称空间里面起的资源,只看到一个 [root@master01 ~]# kubectl get pod -n aaa NAME READY STATUS RESTARTS AGE busybox-pod 1/2 CrashLoopBackOff 3 94s 4、由于名称空间里面起的资源,只看到一个,所以有问题,要检查原因排错 [root@master01 ~]# kubectl describe pod busybox-pod -n aaa [root@master01 ~]# kubectl describe namespace aaa 其实原因就是busybox没卡住,这个是网络相关的镜像,需要加命令卡住
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 5、更新资源清单 [root@master01 ~]# vim busybox.yml apiVersion: "v1" kind: "Namespace" metadata: name: aaa --- apiVersion: "v1" kind: "Pod" metadata: name: busybox-pod namespace: aaa spec: containers: - name: busybox-containers image: busybox imagePullPolicy: IfNotPresent command : ["/bin/tail" ,"-f" ,"/etc/hosts" ] - name: nginx-containers image: nginx:alpine imagePullPolicy: IfNotPresent 6、删除就得pod [root@master01 ~]# kubectl delete -f busybox.yml 7、再次创建pod,并查看 [root@master01 ~]# kubectl apply -f busybox.yml [root@master01 ~]# kubectl get pod -n aaa NAME READY STATUS RESTARTS AGE busybox-pod 2/2 Running 0 13s [root@master01 ~]# kubectl get pod -n aaa -o wide
4、使用VScode编写资源清单
1、配置VScode
先远程连接主节点
统一资源清单的位置
1 2 [root@master01 ~]# mkdir /opt/kubernetes [root@master01 ~]# mv *.yml /opt/kubernetes
连接成功,打开创建的/opt/kubernetes目录
配置梯子加速下载,安装kubernetes插件,安装插件可能需要7分钟左右
1 2 3 4 5 6 7 8 9 10 [root@master01 ~]# vim ~/.bashrc export https_proxy=https://192.168.10.200:7890[root@master01 ~]# source .bashrc [root@master01 ~]# vim ~/.bashrc [root@master01 ~]# unset https_proxy
安装插件可能需要7分钟左右,安装完成
2、使用插件
查看镜像的状态和ip
编写资源清单
根据模版修改内容 资源限制不需要,所以删除,删除之后有黄线,不用管