How to Create Pod in kubernetes

  • 2021-10-25 08:17:32
  • OfStack

How do directories create Pod? kubectl Tools

How do I create an Pod?

In the previous article, we introduced the differences and relationships between containers and Pod. We know that Pod is the smallest unit of k8s scheduling, and one Pod can have multiple containers, so how do we define one of our own Pod?

In k8s, we usually create an Pod by writing a configuration file. The format of the configuration file is usually yaml format. (How does yaml format represent list, key-value key-value pairs? These knowledge have been said in the previous article). After writing yaml file, start an Pod by the following methods:


kubectl create -f   Configuration file 

The definition, parameters, configuration and other information of containers in Pod are all in yaml file, and the contents of a common yaml file are as follows:


apiVersion: v1
kind: Pod
metadata :
  name: volume-pod
spec:
  containers :
  - name: tomcat
    image: tomcat
    ports:
    - containerPort: 8080
    volumeMounts:
    - name: app-logs
      mountPath: /usr/local/tomcat/logs
  - name: busybox
    image: busybox
    command: ["sh","-c","tail -f /logs/catalina*.log"]
    volumeMounts:
    - name: app-logs
      mountPath: /logs
  volumes:
  - name: app-logs
    emptyDir: {}

Of course, it may have many fields, and depending on the Pod you create, the fields can be set by themselves. When we submit an yaml file to k8s, k8s will help us create the corresponding API object. In this case, our object is an Pod (because the value after the kind field in the yaml file is Pod). Of course, there are others.

At this point, we have understood the creation method of Pod. For the above process, let's look at two more problems:

1. What is kubectl? What commands can it follow?

2. What do the fields in the yaml file of Pod mean?

Let's look at the first question first.

kubectl Tools

kubectl tool is a client CLI tool, which allows users to manage k8s cluster through command line. The basic syntax of this command is:


kubectl 【command】 【type】 【name】 【flags】
command取值:get、create、delete、describe、get、apply等
type取值,资源对象的类型,可以取pod、deployment等
name取值:资源对象的名称,
flags:可选参数,可以通过--help来查看

eg:
kubectl create � 通过文件名或控制台输入,创建资源。
kubectl delete � 通过文件名、控制台输入、资源名或者label selector删除资源。
kubectl annotate � 更新资源的注解。
kubectl api-versions � 以“组/版本”的格式输出服务端支持的API版本。
kubectl apply � 通过文件名或控制台输入,对资源进行配置。
kubectl attach � 连接到1个正在运行的容器。
kubectl autoscale � 对replication controller进行自动伸缩。
kubectl cluster-info � 输出集群信息。
kubectl config � 修改kubeconfig配置文件。
kubectl describe � 输出指定的1个/多个资源的详细信息。
kubectl edit � 编辑服务端的资源。
kubectl exec � 在容器内部执行命令。
kubectl expose � 输入replication controller,service或者pod,并将其暴露为新的kubernetes service。
kubectl get � 输出1个/多个资源。
kubectl label � 更新资源的label。
kubectl logs � 输出pod中1个容器的日志。
kubectl namespace -(已停用)设置或查看当前使用的namespace。
kubectl patch � 通过控制台输入更新资源中的字段。
kubectl port-forward � 将本地端口转发到Pod。
kubectl proxy � 为Kubernetes API server启动代理服务器。
kubectl replace � 通过文件名或控制台输入替换资源。
kubectl rolling-update � 对指定的replication controller执行滚动升级。
kubectl run � 在集群中使用指定镜像启动容器。
kubectl scale � 为replication controller设置新的副本数。
kubectl stop � (已停用)通过资源名或控制台输入安全删除资源。
kubectl version � 输出服务端和客户端的版本信息。

Now we know that it is a command line tool, and there are so many common operations as above. You can use the create subcommand to create an Pod.

Other subsequent functions, every time we use them, we will expand and talk about them. Of course, you can use kubectl-help to view its instructions.

The above is how to create Pod in kubernetes in detail, more about kubernetes to create Pod information please pay attention to other related articles on this site!


Related articles: