Reference book: "Kubernetes Authoritative Guide 4th Edition Full Contact from Docker to Kubernetes Practice"
Article Directory
1. Pod concept
Pod is the smallest unit of k8s, which contains a set of containers, one of which is Pause container, also known as "root container".
Multiple service containers in the Pod share the network and Volume of the Pause container.
Pod is short-lived.
Each Pod has a unique IP address, called Pod IP. In a K8S cluster, a container in a Pod can communicate directly with a Pod container on another host
Two, define a Pod
2.1. YAML文件tomcat.yaml
apiVersion: v1
kind: Pod
metadata:
name: tomcat
labels:
name: tomcat
namespace: cka
spec:
containers:
- name: tomcat
image: kubeguide/tomcat-app:v1
imagePullPolicy: IfNotPresent
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 8080
env:
- name: MYSQL_SERVICE_HOST
value: 'mysql'
2.2. 相关解释
apiVersion: v1 #核心API
kind #指明资源类型,此处为Pod
metadata #元数据,用于描述当前资源类型。
metadata.name #Pod的名称为tomcat
metadata.labels.name #定义该Pod有一个名为name=tomcat的标签
metadata.namespace #指定该Pod属于哪个命名空间
spec #定义Pod里面的容器组
spec.containers #定义容器组
containers.name #容器的名字为tomcat
containers.image #容器使用的镜像为kubeguide/tomcat-app:v1
containers.imagePullPolicy #IfNotPresent表示如果本地存在就不去镜像仓库拉取,不存在则拉取
containers.resources #定义容器的资源配额
resources.requests #定义请求的资源,现只支持CPU和内存,此处申请0.25个CPU和64MiB内存,
# 该值必须小于或者等于limits设置的值
resources.limits #资源最多申请0.5个CPU和128MiB内存
containers.ports #定义端口
ports.containerPort #容器应用监听的端口为8080
containers.env #往容器注入环境变量,以KV键值对的形式。此处注入了MYSQL_SERVICE_HOST='mysql'的环境变量
kubectl apply -f tomcat.yaml #Create Pod
2.3. 验证
kubectl get pod --show-labels
The name of the Pod is tomcat, and there is a label of name=tomcat
kubectl describe pod tomcat
The container is named tomcat, the image used is kubeguide/tomcat-app:v1, and the listening port is 8080.
Resource quota and environment variables
indicate that the image is already available locally
kubectl exec -it tomcat bash #登陆到Pod的第一个容器,由于这里只有一个业务容器,默认就是tomcat容器
Environment variables have been successfully injected
kubectl get pod -o wide
Pod IP is 10.244.1.9 and
any host in the cluster can be accessed through curl 10.244.1.9:8080
Three, static Pod
概念
A static Pod is a Pod that only exists on a specific Node and is managed by kubelet. It cannot be managed by API Server, cannot be associated with RC, Deployment, or
DaemonSet, and kubelet cannot perform health checks on them.
Static Pods are always created by kubelet and always run on the Node where the kubelet is located.
In the K8s cluster built by kubeadm, the default configuration directory of static Pod is **/etc/kubernetes/manifests/**. Kubelet will periodically scan the configuration directory of static Pod and start or delete Pod according to the yaml in the directory.
You can see that the following system components are all started in the form of a
static Pod. The configuration directory of the static Pod can be /var/lib/kubelet/config.yaml
set to modify the value of staticPodPath, and then restart Kubelet.
创建一个静态Pod
Create the following yaml file in /etc/kubernetes/manifests/
apiVersion: v1
kind: Pod
metadata:
name: static-pod
labels:
name: static-pod
spec:
containers:
- name: static-pod
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
The name of the static pod will be followed by the name of the current node.
Use kubectl to delete the pod, kubectl delete pod static-pod-k8smaster, and find that it can’t be deleted, how to delete it?
Just delete the yaml file of the static Pod directly