Linux企业运维之k8s-yaml编写

文章目录

  • 一、Pod管理
  • 二、资源清单
    • 资源清单
    • 打标签以及添加资源选择器

一、Pod管理

• Pod是可以创建和管理Kubernetes计算的最小可部署单元,一个Pod代表着集群
中运行的一个进程,每个pod都有一个唯一的ip。
• 一个pod类似一个豌豆荚,包含一个或多个容器(通常是docker),多个容器间
共享IPC、Network和UTC namespace。

[root@k8s1 ~]# kubectl run demo --image=nginx
pod/demo created
[root@k8s1 ~]# kubectl get pod
NAME   READY   STATUS    RESTARTS   AGE
demo   1/1     Running   0          55s
[root@k8s1 ~]# kubectl get pod -o wide
NAME   READY   STATUS    RESTARTS   AGE     IP           NODE   NOMINATED NODE   READINESS GATES
demo   1/1     Running   0          4m46s   10.244.2.2   k8s3              

kubectl scale deployment demo --replicas=1这是它的一个控制器,可以控制pod的数量,我们可以对其进行扩容和缩容,根据我们的需求

[root@k8s1 ~]# kubectl create deployment demo --image=nginx
deployment.apps/demo created
[root@k8s1 ~]# kubectl get pod
NAME                    READY   STATUS              RESTARTS   AGE
demo-6c54f77c95-tkvfv   0/1     ContainerCreating   0          13s
[root@k8s1 ~]# kubectl get pod
NAME                    READY   STATUS              RESTARTS   AGE
demo-6c54f77c95-tkvfv   0/1     ContainerCreating   0          17s
[root@k8s1 ~]# kubectl get pod
NAME                    READY   STATUS              RESTARTS   AGE
demo-6c54f77c95-tkvfv   0/1     ContainerCreating   0          23s
[root@k8s1 ~]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
demo-6c54f77c95-tkvfv   1/1     Running   0          28s
[root@k8s1 ~]# kubectl delete pod demo-6c54f77c95-tkvfv 
pod "demo-6c54f77c95-tkvfv" deleted
[root@k8s1 ~]# kubectl get pod
NAME                    READY   STATUS              RESTARTS   AGE
demo-6c54f77c95-h2db7   0/1     ContainerCreating   0          3s
[root@k8s1 ~]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
demo-6c54f77c95-h2db7   1/1     Running   0          15s
[root@k8s1 ~]# kubectl scale deployment demo --replicas=3
deployment.apps/demo scaled
[root@k8s1 ~]# kubectl get pod
NAME                    READY   STATUS              RESTARTS   AGE
demo-6c54f77c95-2f7gh   0/1     ContainerCreating   0          7s
demo-6c54f77c95-h2db7   1/1     Running             0          49s
demo-6c54f77c95-m2tj6   0/1     ContainerCreating   0          7s
[root@k8s1 ~]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
demo-6c54f77c95-2f7gh   1/1     Running   0          21s
demo-6c54f77c95-h2db7   1/1     Running   0          63s
demo-6c54f77c95-m2tj6   1/1     Running   0          21s

kubectl expose deployment demo --port=80 --target-port=80可以将其发布出去,并且暴露端口,svc可以起到pod的负载均衡的作用,由于现在我们只有一个pod,所以在这里体现不出来

[root@k8s1 ~]# kubectl expose deployment demo --port=80 --target-port=80
service/demo exposed
[root@k8s1 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
demo         ClusterIP   10.102.98.54           80/TCP    10s
kubernetes   ClusterIP   10.96.0.1              443/TCP   9h
[root@k8s1 ~]# kubectl describe svc demo 
Name:              demo
Namespace:         default
Labels:            app=demo
Annotations:       
Selector:          app=demo
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.102.98.54
IPs:               10.102.98.54
Port:                80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.2.4:80
Session Affinity:  None
Events:            
[root@k8s1 ~]# kubectl delete svc demo 
service "demo" deleted

二、资源清单

pod.yaml/中的就是标准化的资源定义,

[root@k8s1 ~]# kubectl delete -f pod.yaml 
pod "demo" deleted
[root@k8s1 ~]# cat pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: demo
  name: demo
spec:
  containers:
  - image: nginx
    name: demo
    resources: {}

我们对其做资源限制,比如限制cpu或者内存,这个点在以后会比较重要

[root@k8s1 ~]# kubectl describe pod demo 
Name:             demo
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s3/192.168.1.13
Start Time:       Sun, 13 Nov 2022 02:26:50 +0800
Labels:           run=demo
Annotations:      
Status:           Running
IP:               10.244.2.5
IPs:
  IP:  10.244.2.5
Containers:
  demo:
    Container ID:   docker://827337d551044d248111a4729644b11d9f259abe373f4b72ac7df36286012385
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:bab399017a659799204147065aab53838ca6f5aeed88cf7d329bc4fda1d2bac7
    Port:           80/TCP
    Host Port:      80/TCP
    State:          Running
      Started:      Sun, 13 Nov 2022 02:27:06 +0800
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     200m
      memory:  512Mi
    Requests:
      cpu:        100m
      memory:     200Mi
    Environment:  
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-xs6lb (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-xs6lb:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              
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  63s   default-scheduler  Successfully assigned default/demo to k8s3
  Normal  Pulled     48s   kubelet            Container image "nginx" already present on machine
  Normal  Created    48s   kubelet            Created container demo
  Normal  Started    47s   kubelet            Started container demo

资源清单

Linux企业运维之k8s-yaml编写_第1张图片
Linux企业运维之k8s-yaml编写_第2张图片
Linux企业运维之k8s-yaml编写_第3张图片
Linux企业运维之k8s-yaml编写_第4张图片
Linux企业运维之k8s-yaml编写_第5张图片

打标签以及添加资源选择器

我们可以打标签并且在yaml文件中增加标签选择器

[root@k8s1 ~]# kubectl label nodes k8s3 disktype=ssd
spec:
  hostNetwork: true
  nodeSelector:
    disktype: ssd
  containers:
  - image: nginx
    name: demo
    resources:
      limits:
        cpu: 200m
        memory: 512Mi
      requests:
        cpu: 100m
        memory: 200Mi
    imagePullPolicy: IfNotPresent

[root@k8s1 ~]# kubectl get pod
NAME   READY   STATUS    RESTARTS   AGE
demo   1/1     Running   0          6m32s
[root@k8s1 ~]# kubectl get pod -o wide
NAME   READY   STATUS    RESTARTS   AGE     IP             NODE   NOMINATED NODE   READINESS GATES
demo   1/1     Running   0          6m36s   192.168.1.13   k8s3              

你可能感兴趣的:(Linux企业运维,kubernetes,运维,linux)