美文网首页容器化进阶之路
k8s实战示例1:创建命名空间和简单pod

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

作者: hyperjiang | 来源:发表于2020-04-13 09:25 被阅读0次

本示例的文件可以从 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

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

  • k8s操作

    k8s的基本操作 命名空间 创建命名空间 或者 kubectl create -f ./my-namespace....

  • k8s知识点总结(一)

    k8s架构和原理(附件1) k8s网络机制介绍(附件2) 分为3种情况 同一个Pod组的容器共享同一个网络命名空间...

  • Python 学习笔记 - 第七天

    1.作用域和命名空间 示例: 如何引用不同作用域和命名空间 输出: 2.初识类 2.1.类定义语法 类定义最简单的...

  • 资源抽象对象

    容器组(Pod) 在k8s中是最小的资源分配和调度单位,由一个或多个容器组成。同一个Pod中,不同容器共享命名空间...

  • Kubernetes创建Namespace命名空间

    命名空间可以隔离不同的资源(service、pod、pvc等),不同的项目创建不同的命名空间有助于管理不同的资源。...

  • k8S常用命令(2)

    1. 查询命名空间下pod、service、ingresskubectl get all -n 命名空间 2. 搜...

  • 网络策略--简单示例

    本示例使用基于calico的网络策略实验。 实验目的,使用策略规则,建立简单的网络隔离。 创建命名空间 polic...

  • k8s-乱七八糟

    2020.5.29 1、k8s: 1、namespace:命名空间,(群组) 2、service: 服...

  • 清除k8s中node节点无用的镜像

    1、使用kubectl get po –namespace 命名空间,查看该命名空间已有的pod 2、重新部署po...

网友评论

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

    本文链接:https://www.haomeiwen.com/subject/cjvwmhtx.html