Kubernetes--Pod资源管理

喜你入骨 提交于 2020-02-26 00:09:43

Pod特点

k8s的最小管理单元
一组容器的集合
一个Pod中的容器共享网络命令空间
Pod是短暂的

Pod容器分类

1.infrastructure container 基础容器(维护整个Pod网络空间)

  • node节点操作
#查看容器的网络
cat /opt/kubernetes/cfg/kubelet

#每次创建Pod时候就会创建,与Pod对应的,对于用户是透明的,网络组件会被自动加载成一个组件提供出去
docker ps

2.initcontainers 初始化容器

pod在进行创建时一定会被执行当中的初始化initcontainers,
在老版本中执行时不会区分前后顺序(在系统进行加载时PID号数字越小,优先级别越高,越先被启动),
随着云平台的改进,启动模式改为主机形式,分隔出的初始化容器会被优先加载,
在初始化容器加载完成之后后面的业务容器才能正常接着运行

3.container 业务容器,并行启动

示例

Init containers in use

This example defines a simple Pod that has two init containers. 
The first waits for myservice, and the second waits for mydb. 
Once both init containers complete, the Pod runs the app container from its spec section.
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']

镜像拉取策略(image PullPolicy)

IfNotPresent:默认值,镜像在宿主机上不存在时才拉取

Always:每次创建Pod都会重新拉取一次镜像

Never:Pod永远不会主动拉取这个镜像

示例:

Verify by creating a pod that uses a private image, e.g.:
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: private-image-test-1
spec:
  containers:
    - name: uses-private-image
      image: $PRIVATE_IMAGE_NAME
      imagePullPolicy: Always
      command: [ "echo", "SUCCESS" ]
EOF

在master1端操作

kubectl get pods

kubectl edit deployment/nginx

cd demo/

vim pod1.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: nginx
      image: nginx
      imagePullPolicy: Always
      command: [ "echo", "SUCCESS" ]

kubectl create -f pod1.yaml 

kubectl get pods
#此时会出现CrashLoopBackOff创建之后又关闭的状态提示
#失败的状态的原因是因为命令启动冲突
#删除 command: [ "echo", "SUCCESS" ]
#同时更改一下版本
image: nginx:1.14

#删除原有的资源
kubectl delete -f pod1.yaml 

#更新资源
kubectl apply -f pod1.yaml 

#查看分配节点
kubectl get pods -o wide

NAME          READY   STATUS    RESTARTS   AGE     IP            NODE           NOMINATED NODE
mypod         1/1     Running   0          1m42s   172.17.56.3   192.168.142.130  <none>

#在任意node节点使用curl查看头部信息
curl -I 172.17.56.3
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Sat, 18 Feb 2020 19:32:55 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes

谢谢阅读!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!