k8s的pod资源管理

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

root@node01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   23h
kube-node-lease   Active   23h
kube-public       Active   23h
kube-system       Active   23h
[root@node01 ~]# kubectl get ns defaule -o yaml
Error from server (NotFound): namespaces "defaule" not found
[root@node01 ~]# 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资源管理_第1张图片
tip:kubectl get ns defaule -o yaml中有5个字段:apiVersion、kind、metadata、spec、status
编写develop-ns.yaml

[root@master basics]# cat develop-ns.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: develop

在这里插入图片描述
创建:

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

在这里插入图片描述

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

[root@master basics]# cat prod-ns.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: prod

在这里插入图片描述
创建:

[root@master basics]# kubectl apply -f prod-ns.yaml 
[root@master 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资源管理_第2张图片
注:create和apply区别即陈述式和声明式区别:create只能创建一次,apply可以修改配置文件继续创建。

3,编写pod
模板:

[root@master 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
[root@master basics]# 
编写pod1:
[root@master 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: {}
[root@master basics]# 

k8s的pod资源管理_第3张图片
启动:

[root@master basics]# kubectl apply -f pod-demo.yaml 
pod/pod-demo created
[root@master basics]# kubectl get pods -n develop
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   1/1     Running   0          45s
编写pod2
[root@master 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"]
查看:
[root@master basics]# kubectl apply -f pod-demo-2.yaml 
pod/pod-demo created
[root@master basics]# kubectl get pods -n prod
NAME       READY   STATUS              RESTARTS   AGE
pod-demo   0/2     ContainerCreating   0          30s

在这里插入图片描述
在此你的namespace是什么就-n…

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

[root@master basics]# kubectl get pods -n prod
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   2/2     Running   0          6m41s
[root@master 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资源管理_第4张图片
查看容器日志:

[root@master basics]# kubectl logs pod-demo -n prod -c myapp

与宿主机交互
编写pod:

[root@master 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资源管理_第5张图片
apply pod:

[root@master basics]# kubectl apply -f host-pod.yaml 
pod/mypod created
[root@master 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              
mypod                       1/1     Running   0          12s     10.244.2.5   node02              
ngx-dep-5b87ccccb8-cct95    1/1     Running   0          6h7m    10.244.1.4   node01              
ngx-dep1-69b57c7d65-7rd49   1/1     Running   0          5h11m   10.244.3.3   node03              
ngx-dep1-69b57c7d65-mqbc9   1/1     Running   0          6h37m   10.244.1.3   node01              
[root@master basics]# 
在此可见mypod运行在node02节点上,所以在浏览器访问node02ip:8080.

4,pod标签
编写pod:

[root@master 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资源管理_第6张图片

[root@master 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
修改标签:
[root@master basics]# kubectl label pods pod-demo -n prod tier=frontend
pod/pod-demo labeled
[root@master 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

在这里插入图片描述
覆盖标签:

[root@master basics]# kubectl label pods pod-demo -n prod --overwrite=true app=myapp
pod/pod-demo labeled
[root@master 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
[root@master basics]# 

在这里插入图片描述
删除标签:

[root@master basics]# kubectl label pods pod-demo -n prod rel-
pod/pod-demo labeled
[root@master 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
[root@master basics]# 

在这里插入图片描述
标签选择:

[root@master 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)