本章节将介绍如何在kubernetes集群中部署一个nginx服务,并且能够对其进行访问。
1 namespace
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 45h # 所有未指定Namespace的对象都会被分配在default命名空间
kube-node-lease Active 45h # 集群节点之间的心跳维护,v1.13开始引入
kube-public Active 45h # 此命名空间下的资源可以被所有人访问(包括未认证用户)
kube-system Active 45h # 所有由Kubernetes系统创建的资源都处于这个命名空间
下面来看namespace资源的具体操作:
# 1 查看所有的ns 命令:kubectl get ns
[root@k8s-master k8s]# kubectl get ns
NAME STATUS AGE
default Active 9d
kube-flannel Active 9d
kube-node-lease Active 9d
kube-public Active 9d
kube-system Active 9d
kubernetes-dashboard Active 6d2h
# 2 查看指定的ns 命令:kubectl get ns ns名称
[root@k8s-master k8s]# kubectl get ns default
NAME STATUS AGE
default Active 9d
# 3 指定输出格式 命令:kubectl get ns ns名称 -o 格式参数
# kubernetes支持的格式有很多,比较常见的是wide、json、yaml
[root@k8s-master k8s]# kubectl get ns default -o yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: "2022-12-23T06:12:27Z"
name: default
resourceVersion: "148"
selfLink: /api/v1/namespaces/default
uid: 43c48f2d-1d2b-4009-b79a-e49eabf8a121
spec:
finalizers:
- kubernetes
status:
phase: Active
[root@k8s-master k8s]#
# 4 查看ns详情 命令:kubectl describe ns ns名称
[root@k8s-master k8s]# kubectl describe ns default
Name: default
Labels: <none>
Annotations: <none>
Status: Active # Active 命名空间正在使用中 Terminating 正在删除命名空间
# ResourceQuota 针对namespace做的资源限制
# LimitRange针对namespace中的每个组件做的资源限制
No resource quota.
No LimitRange resource.
1.1 创建namespace
# 创建namespace
[root@master ~]# kubectl create ns dev
namespace/dev created
1.2 删除namespace
# 删除namespace
[root@master ~]# kubectl delete ns dev
namespace "dev" deleted
1.3 yaml配置
apiVersion: v1
kind: Namespace
metadata:
name: dev
然后就可以执行对应的创建和删除命令了:
创建:kubectl create -f ns-dev.yaml
删除:kubectl delete -f ns-dev.yaml
2 pod
Pod是kubernetes集群进行管理的最小单元,程序要运行必须部署在容器中,而容器必须存在于Pod中。
Pod可以认为是容器的封装,一个Pod中可以存在一个或者多个容器。
kubernetes在集群启动之后,集群中的各个组件也都是以Pod方式运行的。可以通过下面命令查看:
[root@k8s-master k8s]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-9d85f5447-ffwwg 1/1 Running 0 9d
coredns-9d85f5447-hjf6l 1/1 Running 0 9d
etcd-k8s-master 1/1 Running 3 9d
kube-apiserver-k8s-master 1/1 Running 2 9d
kube-controller-manager-k8s-master 1/1 Running 4 9d
kube-proxy-5khr9 1/1 Running 1 9d
kube-proxy-d4cd7 1/1 Running 4 9d
kube-proxy-nz72g 1/1 Running 1 9d
kube-scheduler-k8s-master 1/1 Running 4 9d
2.1 创建并运行
kubernetes没有提供单独运行Pod的命令,都是通过Pod控制器来实现的
# 命令格式: kubectl run (pod控制器名称) [参数]
# --image 指定Pod的镜像
# --port 指定端口
# --namespace 指定namespace
[root@k8s-master k8s]# kubectl run nginx --image=nginx:latest --port=80 --namespace dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
2.2 查看pod信息
# 查看Pod基本信息
[root@k8s-master k8s]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
nginx-dd6b5d745-2mfnr 1/1 Running 0 10m
# 查看Pod的详细信息
[root@k8s-master k8s]# kubectl describe pod nginx -n dev
Name: nginx-dd6b5d745-2mfnr
Namespace: dev
Priority: 0
Node: node2/192.168.192.120
Start Time: Sun, 01 Jan 2023 14:40:12 +0800
Labels: pod-template-hash=dd6b5d745
run=nginx
Annotations: <none>
Status: Running
IP: 10.244.1.14
IPs:
IP: 10.244.1.14
Controlled By: ReplicaSet/nginx-dd6b5d745
Containers:
nginx:
Container ID: docker://5c95a1e5c98c189fc6a3a725019296dd4777e3387251e147d222b201f8db16ae
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Sun, 01 Jan 2023 14:40:43 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-c8bnw (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-c8bnw:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-c8bnw
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 10m default-scheduler Successfully assigned dev/nginx-dd6b5d745-2mfnr to node2
Normal Pulling 9m12s kubelet, node2 Pulling image "nginx:latest"
Normal Pulled 8m44s kubelet, node2 Successfully pulled image "nginx:latest"
Normal Created 8m44s kubelet, node2 Created container nginx
Normal Started 8m43s kubelet, node2 Started container nginx
2.3 访问pod
# 获取podIP
[root@k8s-master k8s]# kubectl get pods -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-dd6b5d745-2mfnr 1/1 Running 0 13m 10.244.1.14 node2 <none> <none>
#访问POD
[root@k8s-master k8s]# curl http://10.244.1.14
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
2.4 删除指定pod
# 删除指定Pod
[root@k8s-master k8s]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
nginx-dd6b5d745-2mfnr 1/1 Running 0 15m
[root@k8s-master k8s]# kubectl delete pod nginx-dd6b5d745-2mfnr -n dev
pod "nginx-dd6b5d745-2mfnr" deleted
# 此时,显示删除Pod成功,但是再查询,发现又新产生了一个
[root@k8s-master k8s]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
nginx-dd6b5d745-qbsrv 1/1 Running 0 7s
# 这是因为当前Pod是由Pod控制器创建的,控制器会监控Pod状况,一旦发现Pod死亡,会立即重建
# 此时要想删除Pod,必须删除Pod控制器
# 先来查询一下当前namespace下的Pod控制器
[root@k8s-master k8s]# kubectl get deploy -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 15m
# 接下来,删除此PodPod控制器
[root@k8s-master k8s]# kubectl delete deploy nginx -n dev
deployment.apps "nginx" deleted
# 稍等片刻,再查询Pod,发现Pod被删除了
[root@k8s-master k8s]# kubectl get pods -n dev
No resources found in dev namespace.
2.5 yaml配置操作
创建一个pod-nginx.yaml,内容如下:
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: dev
spec:
containers:
- image: nginx:latest
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
然后就可以执行对应的创建和删除命令了:
创建:kubectl create -f pod-nginx.yaml
删除:kubectl delete -f pod-nginx.yaml
3 label
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
3.1 label操作命令
# 为pod资源打标签
[root@k8s-master k8s]# kubectl label pod wp-pod version=2
pod/wp-pod labeled
# 查看标签
[root@k8s-master k8s]# kubectl get pod wp-pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
wp-pod 1/1 Running 0 6d2h app=wordpress,role=website,version=2
# 为pod资源更新标签
[root@k8s-master k8s]# kubectl label pod wp-pod version=3 --overwrite
pod/wp-pod labeled
[root@k8s-master k8s]# kubectl get pod wp-pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
wp-pod 1/1 Running 0 6d2h app=wordpress,role=website,version=3
# 筛选标签
[root@k8s-master k8s]# kubectl get pod -l version=3 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
wp-pod 1/1 Running 0 6d2h app=wordpress,role=website,version=3
[root@k8s-master k8s]# kubectl get pod -l version=2 --show-labels
No resources found in default namespace.
#删除标签
[root@k8s-master k8s]# kubectl label pod wp-pod version-
pod/wp-pod labeled
[root@k8s-master k8s]# kubectl get pod wp-pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
wp-pod 1/1 Running 0 6d2h app=wordpress,role=website
3.2 yaml配置
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: dev
labels:
version: "3.0"
env: "test"
spec:
containers:
- image: nginx:latest
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
然后就可以执行对应的更新命令了:kubectl apply -f pod-nginx.yaml
4 Deployment
在kubernetes中,Pod是最小的控制单元,但是kubernetes很少直接控制Pod,一般都是通过Pod控制器来完成的。Pod控制器用于pod的管理,确保pod资源符合预期的状态,当pod的资源出现故障时,会尝试进行重启或重建pod。
在kubernetes中Pod控制器的种类有很多,本章节只介绍一种:Deployment。
4.1 deployment操作命令
# 命令格式: kubectl create deployment 名称 [参数]
# --image 指定pod的镜像
# --port 指定端口
# --replicas 指定创建pod数量
# --namespace 指定namespace
[root@k8s-master k8s]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3 -n dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
# 查看创建的Pod
[root@k8s-master k8s]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
nginx-dd6b5d745-fm7qw 1/1 Running 0 9s
nginx-dd6b5d745-jgzrv 0/1 ContainerCreating 0 9s
nginx-dd6b5d745-mbdvb 1/1 Running 0 9s
# 查看deployment的信息
[root@k8s-master k8s]# kubectl get deploy -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 2/3 3 2 18s
# UP-TO-DATE:成功升级的副本数量
# AVAILABLE:可用副本的数量
[root@k8s-master k8s]# kubectl get deploy -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 3/3 3 3 35s nginx nginx:latest run=nginx
# 查看deployment的详细信息
[root@k8s-master k8s]# kubectl describe deploy nginx -n dev
Name: nginx
Namespace: dev
CreationTimestamp: Sun, 01 Jan 2023 15:24:11 +0800
Labels: run=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=nginx
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-dd6b5d745 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 50s deployment-controller Scaled up replica set nginx-dd6b5d745 to 3
# 删除
[root@k8s-master k8s]# kubectl delete deploy nginx -n dev
deployment.apps "nginx" deleted
4.2 yaml配置操作
查看yaml配置
[root@k8s-master k8s]# export out="--dry-run=1 -o yaml"
[root@k8s-master k8s]# kubectl create deploy nginx-3 --image=nginx:alpine $out
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx-3
name: nginx-3
spec:
replicas: 1
selector:
matchLabels:
app: nginx-3
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx-3
spec:
containers:
- image: nginx:alpine
name: nginx
resources: {}
status: {}
创建一个deploy-nginx.yaml,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:latest
name: nginx
ports:
- containerPort: 80
protocol: TCP
5 service
虽然每个Pod都会分配一个单独的Pod IP,然而却存在如下两问题:
- Pod IP 会随着Pod的重建产生变化
- Pod IP 仅仅是集群内可见的虚拟IP,外部无法访问
这样对于访问这个服务带来了难度。因此,kubernetes设计了Service来解决这个问题。
Service可以看作是一组同类Pod对外的访问接口。借助Service,应用可以方便地实现服务发现和负载均衡。
5.1 创建集群内部可访问的Service
[root@k8s-master ~]# kubectl run nginx --image=nginx:latest -n dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
# 暴露Service
[root@k8s-master ~]# kubectl expose deploy nginx --name=svc-nginx1 --type=ClusterIP --port=80 --target-port=80 -n dev
service/svc-nginx1 exposed
# 查看service
[root@k8s-master ~]# kubectl get svc svc-nginx1 -n dev -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc-nginx1 ClusterIP 10.105.141.2 <none> 80/TCP 46s run=nginx
# 这里产生了一个CLUSTER-IP,这就是service的IP,在Service的生命周期中,这个地址是不会变动的
# 可以通过这个IP访问当前service对应的POD
[root@k8s-master ~]# curl 10.105.141.2:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
5.2 创建集群外部也可访问的Service
# 上面创建的Service的type类型为ClusterIP,这个ip地址只用集群内部可访问
# 如果需要创建外部也可以访问的Service,需要修改type为NodePort
[root@k8s-master ~]# kubectl expose deploy nginx --name=svc-nginx2 --type=NodePort --port=80 --target-port=80 -n dev
service/svc-nginx2 exposed
# 此时查看,会发现出现了NodePort类型的Service,而且有一对Port(80:31955/TC)
[root@k8s-master ~]# kubectl get svc svc-nginx2 -n dev -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc-nginx2 NodePort 10.109.60.88 <none> 80:31955/TCP 16s run=nginx
# 接下来就可以通过集群外的主机访问 节点IP:31955访问服务了
# 例如在的电脑主机上通过浏览器访问下面的地址
[root@k8s-master ~]# curl http://192.168.192.144:31955
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
5.3 删除service
[root@k8s-master ~]# kubectl delete svc svc-nginx1 -n dev
service "svc-nginx1" deleted
5.4 yaml配置方式
apiVersion: v1
kind: Service
metadata:
name: svc-nginx
namespace: dev
spec:
clusterIP: 10.109.179.231 #固定svc的内网ip
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: nginx
type: ClusterIP
然后就可以执行对应的创建和删除命令了:
创建:kubectl create -f svc-nginx.yaml
删除:kubectl delete -f svc-nginx.yaml
上一篇:三、k8s资源管理
下一篇:五、k8s pod详解