目录
服务器版本 | docker软件版本 | CPU架构 |
CentOS Linux release 7.4.1708 (Core) | Docker version 20.10.12 | x86_64 |
docker可以创建容器,Kubernetes不能直接创建容器,Kubernetes创建的是pod,pod里面包含了一个或者多个容器。
创建pod的 前提 是已经有一套可以正常运行的Kubernetes集群,关于Kubernetes(k8s)集群的安装部署,可以查看博客《Centos7 安装部署Kubernetes(k8s)集群》 https://www.cnblogs.com/renshengdezheli/p/16686769.html
Pod 是可以在 Kubernetes 中创建和管理的、最小的可部署的计算单元。
Pod(就像在鲸鱼荚或者豌豆荚中)是一组(一个或多个) 容器; 这些容器共享存储、网络、以及怎样运行这些容器的声明。 Pod 中的内容总是并置(colocated)的并且一同调度,在共享的上下文中运行。 Pod 所建模的是特定于应用的 “逻辑主机”,其中包含一个或多个应用容器, 这些容器相对紧密地耦合在一起。 在非云环境中,在相同的物理机或虚拟机上运行的应用类似于在同一逻辑主机上运行的云应用。
除了应用容器,Pod 还可以包含在 Pod 启动期间运行的 Init 容器。 你也可以在集群中支持临时性容器 的情况下,为调试的目的注入临时性容器。
pod里面有一个或者多个容器,常见的容器有docker容器,containerd容器,除了 Docker 之外,Kubernetes 支持 很多其他容器运行时, Docker 是最有名的容器运行时, 使用 Docker 的术语来描述 Pod 会很有帮助。
Pod 的共享上下文包括一组 Linux 命名空间、控制组(cgroup)和可能一些其他的隔离方面, 即用来隔离 Docker 容器的技术。 在 Pod 的上下文中,每个独立的应用可能会进一步实施隔离。
就 Docker 概念的术语而言,Pod 类似于共享命名空间和文件系统卷的一组 Docker 容器。
Kubernetes集群架构:k8scloude1作为master节点,k8scloude2,k8scloude3作为worker节点
服务器 | 操作系统版本 | CPU架构 | 进程 | 功能描述 |
k8scloude1/192.168.110.130 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico | k8s master节点 |
k8scloude2/192.168.110.129 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kubelet,kube-proxy,calico | k8s worker节点 |
k8scloude3/192.168.110.128 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kubelet,kube-proxy,calico | k8s worker节点 |
Kubernetes创建pod一般有两种方式:
创建pod的命令为 kubectl run ,kubectl创建pod的帮助可以查看网页版,网页版地址为: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#create
查看kubectl run创建pod的帮助
[root@k8scloude1 ~]# kubectl run --help
Create and run a particular image in a pod.
Examples:
# Start a nginx pod.
kubectl run nginx --image=nginx
# Start a hazelcast pod and let the container expose port 5701.
kubectl run hazelcast --image=hazelcast/hazelcast --port=5701
......
Usage:
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json]
[--command] -- [COMMAND] [args...] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
首先拉取我们需要的镜像,先在worker节点拉取nginx镜像
[root@k8scloude2 ~]# docker pull nginx
[root@k8scloude3 ~]# docker pull nginx
使用nginx镜像创建一个pod
#nginx为pod名字 --image=nginx表示使用Nginx镜像
[root@k8scloude1 pod]# kubectl run nginx --image=nginx
pod/nginx created
查看pod,STATUS为Running就表示pod创建成功
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 6s
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 54s
删除pod
[root@k8scloude1 pod]# kubectl delete pod nginx
pod "nginx" deleted
使用nginx镜像创建一个pod,镜像的下载策略为IfNotPresent本地没有才下载镜像
镜像的下载策略:Always:每次都下载最新的镜像;Never:只使用本地镜像,从不下载,IfNotPresent:本地没有才下载镜像 默认值为:Always
#nginx为pod名字 --image=nginx表示使用Nginx镜像 --image-pull-policy=IfNotPresent表示镜像下载策略为IfNotPresent本地没有才下载镜像
[root@k8scloude1 pod]# kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent
pod/nginx created
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 5s
#删除pod
[root@k8scloude1 pod]# kubectl delete pod nginx
pod "nginx" deleted
注意:kubectl delete pod nginx会有点慢,加了--force 强制删除pod,删除pod会快很多
[root@k8scloude1 pod]# kubectl delete pod nginx --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nginx" force deleted
查看pod标签的帮助
[root@k8scloude1 ~]# kubectl run --help | grep labels
# Start a hazelcast pod and set labels "app=hazelcast" and "env=prod" in the container.
kubectl run hazelcast --image=hazelcast/hazelcast --labels="app=hazelcast,env=prod"
-l, --labels='': Comma separated labels to apply to the pod(s). Will override previous values.
创建pod,指定环境变量和标签
#nginx为pod名字 --image=nginx表示使用Nginx镜像 --image-pull-policy=IfNotPresent表示镜像下载策略为IfNotPresent本地没有才下载镜像 --env:设置变量xx=1,yy=2 --labels设置标签,给pod打上“xx=1,yy=2”的标签
[root@k8scloude1 pod]# kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent --env "xx=1" --env "yy=2" --labels="xx=1,yy=2"
pod/nginx created
#查看pod
[root@k8scloude1 pod]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 20s 10.244.112.136 k8scloude2
进入到pod里,可以看到变量xx,yy
[root@k8scloude1 pod]# kubectl exec -it nginx -- bash
root@nginx:/# echo $xx
1
root@nginx:/# echo $yy
2
root@nginx:/# exit
exit
查看pod的标签,--show-labels参数显示标签
[root@k8scloude1 pod]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
nginx 1/1 Running 0 4m59s 10.244.112.136 k8scloude2 xx=1,yy=2
YAML是"YAML Ain’t a Markup Language"(YAML不是一种标记语言)的递归缩写。YAML的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。主要强调这种语言是以数据为中心,而不是以标记语言为重心,例如像xml语言就会使用大量的标记。
YAML是一个可读性高,易于理解,用来表达数据序列化的格式。它的语法和其他高级语言类似,并且可以简单表达清单(数组)、散列表,标量等数据形态。它使用空白符号缩进和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种配置文件等。YAML的配置文件后缀为 .yaml。
接下里使用yaml文件的方式创建pod
--dry-run 模拟运行,并不会真的创建一个pod , --dry-run=client输出信息少 ,--dry-run=server输出信息多, -o yaml以yaml文件的格式输出
--dry-run=server输出信息如下:内容很多
#nginx为pod名字 --image=nginx表示使用Nginx镜像 --image-pull-policy=IfNotPresent表示镜像下载策略为IfNotPresent本地没有才下载镜像
[root@k8scloude1 pod]# kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent --dry-run=server -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2022-01-12T07:51:11Z"
labels:
run: nginx
name: nginx
namespace: pod
uid: afa40310-3efd-4d9f-8337-8e1db955c6f7
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-w56gz
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
preemptionPolicy: PreemptLowerPriority
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: kube-api-access-w56gz
projected:
defaultMode: 420
sources:
- serviceAccountToken:
expirationSeconds: 3607
path: token
- configMap:
items:
- key: ca.crt
path: ca.crt
name: kube-root-ca.crt
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: namespace
status:
phase: Pending
qosClass: BestEffort
--dry-run=client输出信息如下:输出内容恰到好处
[root@k8scloude1 pod]# kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
生成创建pod的yaml文件
[root@k8scloude1 pod]# kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent --dry-run=client -o yaml >nginx.yaml
yaml文件里有很多变量,可以使用kubectl explain查看yaml文件每个字段的含义
# kubectl explain pods查看一级字段有哪些,每个的含义
[root@k8scloude1 ~]# kubectl explain pods
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata
查看一级字段下包含了哪些字段,对某个字段不了解,先使用kubectl explain查看
[root@k8scloude1 ~]# kubectl explain pod.spec
[root@k8scloude1 ~]# kubectl explain pods.spec.containers
[root@k8scloude1 ~]# kubectl explain pods.spec.containers.ports
容器containers里也可以自定义变量,定义变量的时候:变量的值如果是数字,需要加“”
[root@k8scloude1 pod]# vim nginx.yaml
#kind: Pod表示资源类型为Pod labels指定pod标签 metadata下面的name指定pod名字 containers下面全是容器的定义
#image指定镜像名字 imagePullPolicy指定镜像下载策略 containers下面的name指定容器名
#resources指定容器资源(CPU,内存等) env指定容器里的环境变量 dnsPolicy指定DNS策略
#restartPolicy容器重启策略
[root@k8scloude1 pod]# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
env:
- name: xx
value: "12"
- name: yy
value: "21"
- name: zz
value: hello
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
kubectl apply -f 应用配置文件,创建pod
[root@k8scloude1 pod]# kubectl apply -f nginx.yaml
pod/nginx created
[root@k8scloude1 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 26s
删除pod
[root@k8scloude1 pod]# kubectl delete -f nginx.yaml
pod "nginx" deleted
[root@k8scloude1 pod]# kubectl get pods
No resources found in pod namespace.
定义一个pod指明容器的端口,containerPort: 80 只是告知外界nginx这个pod使用80端口,真正要修改端口需要修改镜像
[root@k8scloude1 pod]# vim nginx.yaml
#kind: Pod表示资源类型为Pod labels指定pod标签 metadata下面的name指定pod名字 containers下面全是容器的定义
#image指定镜像名字 imagePullPolicy指定镜像下载策略 containers下面的name指定容器名
#resources指定容器资源(CPU,内存等) env指定容器里的环境变量 dnsPolicy指定DNS策略
#restartPolicy容器重启策略 ports指定容器端口
[root@k8scloude1 pod]# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: xx
value: "12"
- name: yy
value: "21"
- name: zz
value: hello
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
创建pod
[root@k8scloude1 pod]# kubectl apply -f nginx.yaml
pod/nginx created
[root@k8scloude1 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 3s
强制删除pod
[root@k8scloude1 pod]# kubectl delete pod nginx --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nginx" force deleted
先生成一个yaml文件
[root@k8scloude1 pod]# kubectl run nginx --image=nginx --image-pull-policy=IfNotPresent --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
我们注意到,有的字段前面有- ,有的没有- ,没有-的是字典,字典里不能有重复变量,有-的是列表,列表里的每个对象都是一个匿名字典,在列表中,对象的第一个字段要加-
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
不同的Kubernetes资源类型,apiVersion是不一样的,Pod的apiVersion为v1
kubectl api-versions 查看所有的apiVersion
[root@k8scloude1 ~]# kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
crd.projectcalico.org/v1
discovery.k8s.io/v1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
extensions/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
metrics.k8s.io/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
生成yaml文件
[root@k8scloude1 pod]# kubectl run pod1 --image=nginx --image-pull-policy=IfNotPresent --dry-run=client -o yaml >pod1.yaml
使用同一个yaml文件创建2个pod
[root@k8scloude1 pod]# sed 's/pod1/pod2/' pod1.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod2
name: pod2
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: pod2
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
[root@k8scloude1 pod]# sed 's/pod1/pod2/' pod1.yaml | kubectl apply -f -
pod/pod2 created
[root@k8scloude1 pod]# sed 's/pod1/pod3/' pod1.yaml | kubectl apply -f -
pod/pod3 created
[root@k8scloude1 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod2 1/1 Running 0 30s
pod3 1/1 Running 0 9s
[root@k8scloude1 pod]# ls
pod1.yaml
页面更新:2024-03-11
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号