Kubenetes

资源控制

创建Pod

文件 nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
    version: 1.0.1
spec:
  containers:
  - name: nginx
    image: bitnami/openresty:latest
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080
创建Pod资源
$ kubectl apply -f nginx-pod.yaml
查看Pod资源
$  kubectl get pods -n default
[root@kubelet-node-master test]# kubectl get pods -n default
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          19s
查看Pod描述
[root@kubelet-node-master test]# kubectl describe pod nginx-deployment-55f7ccc75d-cz7qd
Name:         nginx-deployment-55f7ccc75d-cz7qd
Namespace:    default
Priority:     0
Node:         kubelet-node-salve-2/192.168.112.102
Start Time:   Wed, 19 Jan 2022 02:32:55 -0800
Labels:       app=nginx
              pod-template-hash=55f7ccc75d
              version=1.0.1
Annotations:  <none>
Status:       Running
IP:           192.168.2.17
IPs:
  IP:           192.168.2.17
Controlled By:  ReplicaSet/nginx-deployment-55f7ccc75d
Containers:
  nginx:
    Container ID:   docker://e835a7f195d474295ec214e589f704665fa2bcf236154570c30e5a429d7cd325
    Image:          bitnami/openresty:latest
    Image ID:       docker-pullable://docker.io/bitnami/openresty@sha256:65dfce92325bc06eab6e9bee85f490946402847e4748b70cebe5183f23ea2c82
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 19 Jan 2022 02:32:56 -0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-nqfbb (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-nqfbb:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  2m29s  default-scheduler  Successfully assigned default/nginx-deployment-55f7ccc75d-cz7qd to kubelet-node-salve-2
  Normal  Pulled     2m28s  kubelet            Container image "bitnami/openresty:latest" already present on machine
  Normal  Created    2m28s  kubelet            Created container nginx
  Normal  Started    2m28s  kubelet            Started container nginx
删除Pod资源
# 使用创建文件删除
$ kubectl delete -f ./nginx-pod.yaml

# 使用名字删除
$ kubectl delete pod nginx

删除pod提示

[root@kubelet-node-master test]# kubectl delete -f ./nginx-pod.yaml
pod "nginx" deleted

创建Deployment

文件nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx-deployment
    version: 1.0.1
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
        version: 1.0.1
    spec:
      containers:
      - name: nginx
        image: bitnami/openresty:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
创建Deployment
$ kubectl apply -f nginx-deployment.yaml

创建成功

[root@kubelet-node-master test]# kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
查看Deployment
# 使用创建file查看信息
[root@kubelet-node-master test]# kubectl get -f nginx-deployment.yaml
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/2     2            2           66s

# 使用deployment
[root@kubelet-node-master test]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/2     2            2           2m5s

# 添加命名空间
[root@kubelet-node-master test]# kubectl get deployment  -n default
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/2     2            2           2m34s
查看Deployment 描述
[root@kubelet-node-master test]# kubectl describe deployment  nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Wed, 19 Jan 2022 02:32:55 -0800
Labels:                 app=nginx-deployment
                        version=1.0.1
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
           version=1.0.1
  Containers:
   nginx:
    Image:        bitnami/openresty:latest
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-55f7ccc75d (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  37s   deployment-controller  Scaled up replica set nginx-deployment-55f7ccc75d to 2
查看ReplicaSet

deployment 被创建后会自动

[root@kubelet-node-master test]# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-55f7ccc75d   2         2         2       3m51s
查看Pod

查看deployment <> rs <> pods 之间有关系

[root@kubelet-node-master test]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/2     2            2           23s
[root@kubelet-node-master test]# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-55f7ccc75d   2         2         2       26s
[root@kubelet-node-master test]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-55f7ccc75d-87wsc   1/1     Running   0          27s
nginx-deployment-55f7ccc75d-phbg7   1/1     Running   0          27s

nginx-deployment
nginx-deployment-55f7ccc75d
nginx-deployment-55f7ccc75d-87wsc
nginx-deployment-55f7ccc75d-phbg7

删除Deployment
# 使用文件删除deployment
[root@kubelet-node-master test]# kubectl delete -f ./nginx-deployment.yaml
deployment.apps "nginx-deployment" deleted

# 删除deployment
[root@kubelet-node-master test]# kubectl delete deployment nginx-deployment
deployment.apps "nginx-deployment" deleted

创建Service

文件nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: svc-nginx
  labels:
    app: svc-nginx
    version: 1.0.1
spec:
  type: NodePort
  ports:
  - port: 8080
    nodePort: 30001
  selector:
    app: nginx
创建Service
[root@kubelet-node-master test]# kubectl apply -f  nginx-service.yaml
service/svc-nginx created
查看Service
[root@kubelet-node-master test]# kubectl get -f  nginx-service.yaml  -o wide
NAME        TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE     SELECTOR
svc-nginx   NodePort   10.110.91.176   <none>        8080:30001/TCP   2m58s   app=nginx
查看Service 描述
[root@kubelet-node-master test]# kubectl describe svc svc-nginx
Name:                     svc-nginx
Namespace:                default
Labels:                   app=svc-nginx
                          version=1.0.1
Annotations:              <none>
Selector:                 app=nginx
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.110.91.176
IPs:                      10.110.91.176
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30001/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
删除Service
[root@kubelet-node-master test]# kubectl delete -f  nginx-service.yaml
service "svc-nginx" deleted

[root@kubelet-node-master test]# kubectl delete svc svc-nginx
service "svc-nginx" deleted
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐