欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

k8s的pod资源管理

程序员文章站 2024-01-15 20:31:46
...

1,陈述式对象配置:
编写develop-ns.yaml,在此之前可以查看kubectl get ns

aaa@qq.com ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   23h
kube-node-lease   Active   23h
kube-public       Active   23h
kube-system       Active   23h
[aaa@qq.com ~]# kubectl get ns defaule -o yaml
Error from server (NotFound): namespaces "defaule" not found
[aaa@qq.com ~]# kubectl get ns default -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2019-09-18T03:16:48Z"
  name: default
  resourceVersion: "150"
  selfLink: /api/v1/namespaces/default
  uid: 2e9fc72c-69ae-4a4c-98e4-2c94ae8a1e47
spec:
  finalizers:
  - kubernetes
status:
  phase: Active

k8s的pod资源管理
tip:kubectl get ns defaule -o yaml中有5个字段:apiVersion、kind、metadata、spec、status
编写develop-ns.yaml

[aaa@qq.com basics]# cat develop-ns.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: develop

k8s的pod资源管理
创建:

[aaa@qq.com basics]# kubectl create -f develop-ns.yaml 
[aaa@qq.com basics]# kubectl get ns
NAME              STATUS   AGE
default           Active   24h
develop           Active   14m

k8s的pod资源管理

2,声明式对象配置
编写prod-ns.yaml

[aaa@qq.com basics]# cat prod-ns.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: prod

k8s的pod资源管理
创建:

[aaa@qq.com basics]# kubectl apply -f prod-ns.yaml 
[aaa@qq.com basics]# kubectl get ns
NAME              STATUS   AGE
default           Active   24h
develop           Active   17m
kube-node-lease   Active   24h
kube-public       Active   24h
kube-system       Active   24h
prod              Active   15m

k8s的pod资源管理
注:create和apply区别即陈述式和声明式区别:create只能创建一次,apply可以修改配置文件继续创建。

3,编写pod
模板:

[aaa@qq.com basics]# kubectl get pods jh-fb6f55f99-qbgrq -o yaml --export
Flag --export has been deprecated, This flag is deprecated and will be removed in future.
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  generateName: jh-fb6f55f99-
  labels:
    app: jh
    pod-template-hash: fb6f55f99
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: jh-fb6f55f99
    uid: 559ddefd-ce84-432c-84e9-04c0c0a60249
  selfLink: /api/v1/namespaces/default/pods/jh-fb6f55f99-qbgrq
spec:
  containers:
  - image: nginx:1.14-alpine
    imagePullPolicy: IfNotPresent
    name: nginx
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-swrt5
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: node02
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-swrt5
    secret:
      defaultMode: 420
      secretName: default-token-swrt5
status:
  phase: Pending
  qosClass: BestEffort
[aaa@qq.com basics]# 
编写pod1:
[aaa@qq.com basics]# cat pod-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  name: pod-demo
  namespace: develop
spec:
  containers:
  - image: nginx:1.14-alpine
    imagePullPolicy: IfNotPresent
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
[aaa@qq.com basics]# 

k8s的pod资源管理
启动:

[aaa@qq.com basics]# kubectl apply -f pod-demo.yaml 
pod/pod-demo created
[aaa@qq.com basics]# kubectl get pods -n develop
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   1/1     Running   0          45s
编写pod2
[aaa@qq.com basics]# vi pod-demo-2.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: prod
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
  - name: bbox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","sleep 86400"]
查看:
[aaa@qq.com basics]# kubectl apply -f pod-demo-2.yaml 
pod/pod-demo created
[aaa@qq.com basics]# kubectl get pods -n prod
NAME       READY   STATUS              RESTARTS   AGE
pod-demo   0/2     ContainerCreating   0          30s

k8s的pod资源管理
在此你的namespace是什么就-n…

3,容器与宿主机交互
与bbox容器交互:

[aaa@qq.com basics]# kubectl get pods -n prod
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   0          6m41s
[aaa@qq.com basics]# kubectl exec pod-demo -c bbox -n prod -it -- /bin/sh
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 62:AC:18:73:CA:22  
          inet addr:10.244.3.4  Bcast:0.0.0.0  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:9 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:698 (698.0 B)  TX bytes:42 (42.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # 

k8s的pod资源管理
查看容器日志:

[aaa@qq.com basics]# kubectl logs pod-demo -n prod -c myapp

与宿主机交互
编写pod:

[aaa@qq.com basics]# vi host-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
  namespace: default
spec:
  containers:
  - name: mynginx
    image: nginx:1.14-alpine
    ports: 
    - protocol: TCP
      containerPort: 80
      name: http
      hostPort: 8080

k8s的pod资源管理
apply pod:

[aaa@qq.com basics]# kubectl apply -f host-pod.yaml 
pod/mypod created
[aaa@qq.com basics]# kubectl get pods -o wide
NAME                        READY   STATUS    RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
jh-fb6f55f99-qbgrq          1/1     Running   0          4h53m   10.244.2.3   node02   <none>           <none>
mypod                       1/1     Running   0          12s     10.244.2.5   node02   <none>           <none>
ngx-dep-5b87ccccb8-cct95    1/1     Running   0          6h7m    10.244.1.4   node01   <none>           <none>
ngx-dep1-69b57c7d65-7rd49   1/1     Running   0          5h11m   10.244.3.3   node03   <none>           <none>
ngx-dep1-69b57c7d65-mqbc9   1/1     Running   0          6h37m   10.244.1.3   node01   <none>           <none>
[aaa@qq.com basics]# 
在此可见mypod运行在node02节点上,所以在浏览器访问node02ip:8080.

4,pod标签
编写pod:

[aaa@qq.com basics]# vi pod-demo-2.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: prod
  labels:
    app: pod-demo
    rel: stable
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
  - name: bbox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","sleep 86400"]

k8s的pod资源管理

[aaa@qq.com basics]# kubectl get pods -n prod  --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   0          87m   app=pod-demo,rel=stable
修改标签:
[aaa@qq.com basics]# kubectl label pods pod-demo -n prod tier=frontend
pod/pod-demo labeled
[roaaa@qq.com basics]# kubectl get pods -n prod  --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   0          90m   app=pod-demo,rel=stable,tier=frontend

k8s的pod资源管理
覆盖标签:

[aaa@qq.com basics]# kubectl label pods pod-demo -n prod --overwrite=true app=myapp
pod/pod-demo labeled
[aaa@qq.com basics]# kubectl get pods -n prod  --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   0          92m   app=myapp,rel=stable,tier=frontend
[aaa@qq.com basics]# 

k8s的pod资源管理
删除标签:

[aaa@qq.com basics]# kubectl label pods pod-demo -n prod rel-
pod/pod-demo labeled
[aaa@qq.com basics]# kubectl get pods -n prod  --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   0          94m   app=myapp,tier=frontend
[aaa@qq.com basics]# 

k8s的pod资源管理
标签选择:

[aaa@qq.com basics]# kubectl get pods  --show-labels -l app=ngx-dep1
NAME                        READY   STATUS    RESTARTS   AGE     LABELS
ngx-dep1-69b57c7d65-7rd49   1/1     Running   0          6h25m   app=ngx-dep1,pod-template-hash=69b57c7d65
ngx-dep1-69b57c7d65-mqbc9   1/1     Running   0          7h51m   app=ngx-dep1,pod-template-hash=69b57c7d65

k8s的pod资源管理

相关标签: k8s