k8s之pod
Pod介绍
pod是k8s集群进行管理的最小单元,程序要运行必须部署到容器中,而容器必须存在于pod中。
pod可以认为是容器的封装,一个pod中可以存在一个或多个容器
k8s在集群启动之后,集群中的各个组件也都是以pod方式运行的,可以通过下面命令查看:
[root@master ~]# kubectl get pod -n kube-system NAME READY STATUS RESTARTS AGE coredns-6955765f44-j2qtj 1/1 Running 5 46h coredns-6955765f44-kqf4b 1/1 Running 5 46h etcd-master 1/1 Running 64 46h kube-apiserver-master 1/1 Running 61 46h kube-controller-manager-master 1/1 Running 32 46h kube-flannel-ds-kkfm4 1/1 Running 2 28h kube-flannel-ds-qldst 1/1 Running 2 28h kube-flannel-ds-zbm7d 1/1 Running 4 28h kube-proxy-5dvpv 1/1 Running 5 46h kube-proxy-ckgb7 1/1 Running 1 45h kube-proxy-sb5wb 1/1 Running 1 45h kube-scheduler-master 1/1 Running 33 46h
创建并运行
k8s没有提供单独运行pod的命令,都是通过pod控制器来实现的
[root@master ~]# kubectl create ns dev namespace/dev created #命令格式:kubectl run (pod控制器名称) [参数] # --image 指定pod的镜像 # --port 指定端口 # --namespace 指定namespace [root@master ~]# kubectl run nginx --image=nginx:1.17.1 --port=80 --namespace dev kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead. deployment.apps/nginx created
查看pod
[root@master ~]# kubectl get pod -n dev NAME READY STATUS RESTARTS AGE nginx-64777cd554-t5d7g 1/1 Running 0 2m4s [root@master ~]# kubectl describe pod nginx-64777cd554-t5d7g -n dev
访问pod
#获取pod ip [root@master ~]# kubectl get pods -n dev -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-64777cd554-t5d7g 1/1 Running 0 29m 10.244.1.7 node2 <none> <none> #访问pod [root@master ~]# curl 10.244.1.7 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
删除指定pod
pod是由控制器创建的,控制器会监控pod状况,一旦发现pod死亡,会立即重建
因此想要删除pod,必须删除pod控制器
#获取控制器 [root@master ~]# kubectl get deployment -n dev NAME READY UP-TO-DATE AVAILABLE AGE nginx 1/1 1 1 46m #删除控制器 [root@master ~]# kubectl delete deployment nginx -n dev deployment.apps "nginx" deleted #查看pod [root@master ~]# kubectl get pod -n dev No resources found in dev namespace.
配置操作
创建一个pod-nginx.yaml,内容如下:
apiVersion: v1 kind: Pod metadata: name: nginx namespace: dev spec: containers: - image: nginx:1.17.1 imagePullPolicy: IfNotPresent name: pod ports: - name: nginx-port containerPort: 80 protocol: TCP
执行对应的创建和删除指令
#创建 [root@master ~]# kubectl create -f pod-nginx.yaml pod/nginx created [root@master ~]# kubectl get pod -n dev NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 5m34s #删除 [root@master ~]# kubectl delete -f pod-nginx.yaml pod "nginx" deleted