Namespace是kubernetes系统中的一种非常重要资源,它的主要作用是用来实现多套环境的资源隔离或者多租户的资源隔离。
默认情况下,kubernetes集群中的所有的Pod都是可以相互访问的。但是在实际中,可能不想让两个Pod之间进行互相的访问,那此时就可以将两个Pod划分到不同的namespace下。kubernetes通过将集群内部的资源分配到不同的Namespace中,可以形成逻辑上的"组",以方便不同的组的资源进行隔离使用和管理。
可以通过kubernetes的授权机制,将不同的namespace交给不同租户进行管理,这样就实现了多租户的资源隔离。此时还能结合kubernetes的资源配额机制,限定不同租户能占用的资源,例如CPU使用量、内存使用量等等,来实现租户可用资源的管理。
kubernetes在集群启动之后,会默认创建几个namespace
[root@master ~]# kubectl get namespace
NAME STATUS AGE
default Active 11d
kube-flannel Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
下面来看namespace资源的具体操作:
查看所有的namespace
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 11d
kube-flannel Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
查看指定的ns
[root@master ~]# kubectl get ns default
NAME STATUS AGE
default Active 11d
指定输出格式 命令:kubectl get ns ns名称 -o 格式参数# kubernetes支持的格式有很多,比较常见的是wide、json、yaml
[root@master ~]# kubectl get ns default -o yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: "2022-11-17T06:56:00Z"
labels:
kubernetes.io/metadata.name: default
name: default
resourceVersion: "193"
uid: 0e917826-5598-4933-8942-20a5d1574bc2
spec:
finalizers:
- kubernetes
status:
phase: Active
查看ns详情 命令:kubectl describe ns ns名称
[root@master ~]# kubectl describe ns default
Name: default
Labels: kubernetes.io/metadata.name=default
Annotations:
Status: Active
No resource quota.
No LimitRange resource.
ResourceQuota 针对namespace做的资源限制# LimitRange针对namespace中的每个组件做的资源限制
创建命名空间
[root@master ~]# kubectl create ns lh
namespace/lh created
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 11d
kube-flannel Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
lh Active 9s
删除namespace
[root@master ~]# kubectl delete ns lh
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 11d
kube-flannel Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
配置方式
首先准备一个yaml文件:ns-dev.yaml
apiVersion: v1
kind: Namespace
metadata:
name: dev
创建
[root@master ~]# kubectl create -f ns-dev.yaml
namespace/dev created
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 11d
dev Active 10s
kube-flannel Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
删除
[root@master ~]# kubectl delete -f ns-dev.yaml
namespace "dev" deleted
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 11d
kube-flannel Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
Pod是kubernetes集群进行管理的最小单元,程序要运行必须部署在容器中,而容器必须存在于Pod中。
Pod可以认为是容器的封装,一个Pod中可以存在一个或者多个容器。
kubernetes在集群启动之后,集群中的各个组件也都是以Pod方式运行的。可以通过下面命令查看:
[root@master ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-c676cc86f-qxmh5 1/1 Running 1 (21m ago) 11d
coredns-c676cc86f-w2hwh 1/1 Running 1 (21m ago) 11d
etcd-master.example.com 1/1 Running 1 (21m ago) 11d
kube-apiserver-master.example.com 1/1 Running 1 (21m ago) 11d
kube-controller-manager-master.example.com 1/1 Running 1 (21m ago) 11d
kube-proxy-4r7jl 1/1 Running 1 (21m ago) 11d
kube-proxy-l7bkp 1/1 Running 1 (21m ago) 11d
kube-proxy-q6xtl 1/1 Running 1 (21m ago) 11d
kube-scheduler-master.example.com 1/1 Running 1 (21m ago) 11d
创建
[root@master ~]# kubectl create deployment nginx --image nginx --port 80 -n lh
deployment.apps/nginx created
[root@master ~]# kubectl run nginx1 --image=nginx --port=81 --namespace lh
pod/nginx created
image:指定镜像
port: 指定端口
namespace:指定命名空间
查看pod基本信息
[root@master ~]# kubectl get pod -n lh
NAME READY STATUS RESTARTS AGE
nginx-ff6774dc6-5dw9c 1/1 Running 0 43s
nginx1 1/1 Running 0 4m1s
查看pod详细信息
[root@master ~]# kubectl describe pod nginx -n lh
Name: nginx
Namespace: lh
Priority: 0
Service Account: default
Node: node2.example.com/192.168.226.30
Start Time: Mon, 28 Nov 2022 17:26:54 +0800
Labels: run=nginx
Annotations:
Status: Running
IP: 10.244.2.2
IPs:
IP: 10.244.2.2
Containers:
nginx:
Container ID: containerd://15db12697df3ae48fce13371cc511101547685593f5f2e06a29034db37f52819
Image: nginx
Image ID: docker.io/library/nginx@sha256:e209ac2f37c70c1e0e9873a5f7231e91dcd83fdf1178d8ed36c2ec09974210ba
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 28 Nov 2022 17:27:08 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-424fz (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-424fz:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional:
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors:
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m29s default-scheduler Successfully assigned lh/nginx to node2.example.com
Normal Pulling 2m29s kubelet Pulling image "nginx"
Normal Pulled 2m16s kubelet Successfully pulled image "nginx" in 13.388009229s
Normal Created 2m16s kubelet Created container nginx
Normal Started 2m16s kubelet Started container nginx
访问Pod
获取podip
[root@master ~]# kubectl get pods -n lh -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ff6774dc6-5dw9c 1/1 Running 1 (20s ago) 73s 10.244.1.4 node1.example.com
nginx1 1/1 Running 0 4m31s 10.244.2.4 node2.example.com
访问
[root@master ~]# curl http://10.244.2.4
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
删除指定Pod
[root@master ~]# kubectl delete pod nginx1 -n lh
pod "nginx1" deleted
[root@master ~]# kubectl delete pod nginx-ff6774dc6-5dw9c -n lh
pod "nginx-ff6774dc6-5dw9c" deleted
查看可以发现此时create创建的pod还存在着,但是名字发生了改变,这是因为当前Pod是由Pod控制器创建的,控制器会监控Pod状况,一旦发现Pod死亡,会立即重建# 此时要想删除Pod,必须删除Pod控制器
[root@master ~]# kubectl get pod -n lh
NAME READY STATUS RESTARTS AGE
nginx-ff6774dc6-9bp2c 1/1 Running 0 58s
查看当前namespace下的pod控制器
[root@master ~]# kubectl get deploy -n lh
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 5m21s
删除此PodPod控制器
[root@master ~]# kubectl delete deploy nginx -n lh
deployment.apps "nginx" deleted
[root@master ~]# kubectl get pods -n lh
No resources found in lh namespace.
配置操作
创建一个pod-nginx.yaml,内容如下:
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: lh
spec:
containers:
- image: nginx:latest
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
创建
[root@master ~]# kubectl create -f pod-nginx.yaml
pod/nginx created
[root@master ~]# kubectl get pod -n lh
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 10s
删除
[root@master ~]# kubectl delete -f pod-nginx.yaml
pod "nginx" deleted
[root@master ~]# kubectl get pod -n lh
No resources found in lh namespace.
Label是kubernetes系统中的一个重要概念。它的作用就是在资源上添加标识,用来对它们进行区分和选择。
Label的特点:
• 一个Label会以key/value键值对的形式附加到各种对象上,如Node、Pod、Service等等
• 一个资源对象可以定义任意数量的Label ,同一个Label也可以被添加到任意数量的资源对象上去
• Label通常在资源对象定义时确定,当然也可以在对象创建后动态添加或者删除
可以通过Label实现资源的多维度分组,以便灵活、方便地进行资源分配、调度、配置、部署等管理工作。
一些常用的Label 示例如下:
• 版本标签:“version”:“release”, “version”:“stable”…
• 环境标签:“environment”:“dev”,“environment”:“test”,“environment”:“pro”
• 架构标签:“tier”:“frontend”,“tier”:“backend”
标签定义完毕之后,还要考虑到标签的选择,这就要使用到Label Selector,即:
Label用于给某个资源对象定义标识
Label Selector用于查询和筛选拥有某些标签的资源对象
当前有两种Label Selector:
基于等式的Label Selector
name = slave: 选择所有包含Label中key="name"且value="slave"的对象
env != production: 选择所有包括Label中的key="env"且value不等于"production"的对象
基于集合的Label Selector
name in (master, slave): 选择所有包含Label中的key="name"且value="master"或"slave"的对象
name not in (frontend): 选择所有包含Label中的key=“name"且value不等于"frontend"的对象
标签的选择条件可以使用多个,此时将多个Label Selector进行组合,使用逗号”,"进行分隔即可。例如:
name=slave,env!=production
name not in (frontend),env!=production
为pod打上标签
[root@master ~]# kubectl label pod nginx dd=1 -n lh
pod/nginx labeled
查看标签
[root@master ~]# kubectl get pod nginx -n lh --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 78s dd=1
更新标签
[root@master ~]# kubectl label pod nginx dd=2 -n lh --overwrite
pod/nginx labeled
[root@master ~]# kubectl get pod -n lh --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 5m56s dd=2
筛选标签
[root@master ~]# kubectl get pod -l dd=2 -n lh --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 6m45s dd=2
[root@master ~]# kubectl get pod -l dd=1 -n lh --show-labels
No resources found in lh namespace.
删除标签
[root@master ~]# kubectl label pod nginx dd- -n lh
pod/nginx unlabeled
[root@master ~]# kubectl get pod -n lh --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 7m44s
配置方式
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: lh
labels:
dd: "2"
spec:
containers:
- image: nginx:latest
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
然后就可以执行对应的更新命令了:kubectl apply -f pod-nginx.yaml
[root@master ~]# kubectl apply -f pod-nginx.yaml
[root@master ~]# kubectl get pod -n lh --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 1 (8m39s ago) 19m env=test,version=3.0
在kubernetes中,Pod是最小的控制单元,但是kubernetes很少直接控制Pod,一般都是通过Pod控制器来完成的。Pod控制器用于pod的管理,确保pod资源符合预期的状态,当pod的资源出现故障时,会尝试进行重启或重建pod。
在kubernetes中Pod控制器的种类有很多,此处只介绍deployment
创建pod
[root@master ~]# kubectl create deploy nginx --image=nginx:latest --port=80 --replicas=2 -n lh
deployment.apps/nginx created
--image 指定pod的镜像
--port 指定端口
--replicas 指定创建pod数量
--namespace 指定namespace
查看创建的pod
[root@master ~]# kubectl get pod -n lh
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 1 (13m ago) 24m
nginx-cd55c47f5-g4llx 1/1 Running 0 44s
nginx-cd55c47f5-wsbt5 1/1 Running 0 44s
查看deployment的信息
[root@master ~]# kubectl get deploy -n lh
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 2/2 2 2 79s
UP-TO-DATE:成功升级的副本数量
AVAILABLE:可用副本的数量
查看deployment的详细信息
[root@master ~]# kubectl describe deploy nginx -n lh
Name: nginx
Namespace: lh
CreationTimestamp: Mon, 28 Nov 2022 18:18:53 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets:
NewReplicaSet: nginx-cd55c47f5 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 2m31s deployment-controller Scaled up replica set nginx-cd55c47f5 to 2
删除
[root@master ~]# kubectl delete deploy nginx -n lh
deployment.apps "nginx" deleted
[root@master ~]# kubectl get pod -n lh
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 1 (16m ago) 27m
配置文件
创建一个deploy-nginx.yaml,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: lh
spec:
replicas: 3
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:latest
name: nginx
ports:
- containerPort: 80
protocol: TCP
然后就可以执行对应的创建和删除命令了:
创建:kubectl create -f deploy-nginx.yaml
删除:kubectl delete -f deploy-nginx.yaml