k8s实战示例1:创建命名空间和简单pod

本示例的文件可以从 https://github.com/hyperjiang/k8s-lessons/tree/master/demo1
获取。

创建命名空间

我们创建命名空间的namespace.yaml如下:

apiVersion: v1
kind: Namespace
metadata:
  name: demo

然后运行 kubectl apply -f namespace.yaml 就创建了一个名字叫demo的命名空间。
运行kubectl get ns可以查看namespace是否创建成功。

创建一个pod

我们创建pod的pod.yaml如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: demo # An empty namespace is equivalent to the "default" namespace.
  name: alpine # Name must be unique within a namespace.
  labels: # Map of string keys and values that can be used to organize and categorize (scope and select) objects.
    project: alpine
    app: alpine
    namespace: demo
spec: # Specification of the desired behavior of the Deployment.
  selector: # Label selector for pods. It must match the pod template's labels.
    matchLabels: # The requirements are ANDed.
      project: alpine
      app: alpine
      namespace: demo
  replicas: 1 # Number of desired pods, defaults to 1.
  strategy: # The deployment strategy to use to replace existing pods with new ones.
    type: RollingUpdate # Can be "Recreate" or "RollingUpdate". Default is RollingUpdate.
  template: # Template describes the pods that will be created.
    metadata: # Standard object's metadata.
      labels:
        project: alpine
        app: alpine
        namespace: demo
    spec: # Specification of the desired behavior of the pod.
      restartPolicy: Always # Restart policy for all containers within the pod. One of Always, OnFailure, Never. Default to Always.
      containers: # List of containers belonging to the pod. Containers cannot be added or removed or updated. There must be at least one container in a Pod.
        - name: alpine # Name of the container specified as a DNS_LABEL. Each container in a pod must have a unique name (DNS_LABEL). Cannot be updated.
          image: alpine:3.11 # Docker image name.
          resources: # Compute Resources required by this container. Cannot be updated.
            limits: # Limits describes the maximum amount of compute resources allowed.
              cpu: 200m # 100m means one hundred millicpu = 0.1 cpu
              memory: 256Mi # or 256M
            requests: # Requests describes the minimum amount of compute resources required. If Requests is omitted for a container, it defaults to Limits if that is explicitly specified.
              cpu: 100m
              memory: 128Mi
          imagePullPolicy: IfNotPresent # One of Always, Never, IfNotPresent. Defaults to Always if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.
          command: ["/bin/sh"]
          args: ["-c", "while true; do echo hello; sleep 10; done"] # In order to keep the pod live

然后运行 kubectl apply -f pod.yaml 就创建了一个名字叫alpine的pod。

查看和访问pod

运行kubectl get po -n demo可以查看命名空间demo下面的pod的状态,会看到类似下面的输出:

NAME                      READY   STATUS    RESTARTS   AGE
alpine-5b4987ffb9-9n6j6   1/1     Running   0          10s

然后我们可以运行kubectl exec -n demo -ti alpine-5b4987ffb9-9n6j6 /bin/sh连上这个pod的shell(注意中间的pod名字每次创建都是不同的)

删除pod和namespace

实验完了可以把刚才创建的pod和namespace清理掉:

kubectl delete -f pod.yaml
kubectl delete -f namespace.yaml

你可能感兴趣的:(k8s实战示例1:创建命名空间和简单pod)