kubernets基础 – 基础对象讲解

欢迎加入本站的kubernetes技术交流群,QQ群号:450203387,微信群:加Blue_L。


kubernetes中api资源由特定的规范进行定义,api资源由组,版本和类型三部分组成,简称gvk。一个gvk确定一种对象类型,对该类型由对应的结构对其进行定义。kubernetes中对象的存储和传输可以使用protobuf,yaml和json三种序列化形式。

pod

pod是kubernetes中最小可部署单元。pod由一组container构成,包括业务容器,init容器,还有一个特殊的pause容器。

下面是一个简单的pod定义。

apiVersion: v1
kind: Pod
metadata:
  name: webserver
  labels:
    app: webserver
spec:
  containers:
  - name: webserver
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
      # hostPort: 8080
      # hostIP: "169.169.1.1"
      name: http
      protocol: TCP
  initContainers:
  - name: init-dir
    image: busybox:latest
    workingDir: /tmp
    command:
    - /bin/sh
    - -c
    args:
    - "mkdir -p test"
    env:
    - name: MY_ENV
      value: "123456"
    - name: ANOTHER_ENV
      valueFrom:
        configMapKeyRef:
          name: test-cm
          key: CM_ENV
          optional: true
        # fieldRef:
        #  fieldPath: metadata.name
        # secretKeyRef
    resources:
      requests:
        cpu: "200m"
        memory: 1024Mi
      limits:
        cpu: "500m"
        memory: 2048Mi
  # nodeSelector:
  #   diskType: ssd
  # nodeName: 192.168.3.31
  restartPolicy: OnFailure
  hostname: mytest

概念:https://kubernetes.io/zh/docs/concepts/workloads/pods/

参考:https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec

service

apiVersion: v1
kind: Service
metadata:
  name: webserver
spec:
  selector:
    app: webserver
  ports:
  - port: 18080
    targetPort: 80
    protocol: TCP
    name: http-nginx
    appProtocol: http
    # nodePort: 32119
  type: LoadBalancer
  # clusterIP: None
  # externalIPs:
  # - 192.168.2.1
  # externalName: www.baidu.com
  publishNotReadyAddresses: false

概念:https://kubernetes.io/zh/docs/concepts/services-networking/service/

参考:https://kubernetes.io/docs/reference/kubernetes-api/service-resources/service-v1/

configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: webserver-config
data:
  default.conf: |
    server {
      location / {
        return 301 $scheme://www.baidu.com.com$request_uri;
      }
    }
​
immutable: true

概念:https://kubernetes.io/docs/concepts/configuration/configmap/

参考:https://kubernetes.io/docs/reference/kubernetes-api/config-and-storage-resources/config-map-v1/

secret

概念:https://kubernetes.io/zh/docs/concepts/configuration/secret/

参考:https://kubernetes.io/docs/reference/kubernetes-api/config-and-storage-resources/secret-v1/

volume

apiVersion: v1
kind: Pod
metadata:
  name: webserver
  labels:
    app: webserver
spec:
  containers:
  - name: webserver
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
      name: http
      protocol: TCP
    volumeMounts:
    - name: config-vol
      mountPath: /etc/nginx/conf.d
  volumes:
  - name: config-vol
    configMap:
      name: webserver-config
      items:
        - key: default.conf
          path: default.conf
  # - name: foo
  #   secret:
  #     secretName: mysecret
  #     items:
  #     - key: username
  #       path: my-group/my-username
  # - name: cache-volume
  #   emptyDir: {}
  # - name: hostpath-volume
  #   hostPath:
  #     # 宿主上目录位置
  #     path: /data
  #     # 此字段为可选
  #     type: Directory
  # - name: mypd
  #   persistentVolumeClaim:
  #   claimName: myclaim

概念:https://kubernetes.io/zh/docs/concepts/storage/volumes/

pv/pvc/sc

apiVersion: v1
kind: PersistentVolume
metadata:
  name: test-pv
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: manual-class
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /data/nfs/test
    server: 192.168.3.27
​
# persistentvolumeclaim
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: webserver-nfs-pvc
spec:
  storageClassName: managed-nfs-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: managed-nfs-storage
provisioner: cluster.local/nfs-subdir-external-provisioner
reclaimPolicy: Delete
volumeBindingMode: Immediate

概念:https://kubernetes.io/zh/docs/concepts/storage/persistent-volumes/

参考:

https://kubernetes.io/docs/reference/kubernetes-api/config-and-storage-resources/persistent-volume-claim-v1/

https://kubernetes.io/docs/reference/kubernetes-api/config-and-storage-resources/persistent-volume-v1/

https://kubernetes.io/docs/concepts/storage/storage-classes/

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注