陈述式资源管理方法
kubernetes 集群管理集群资源的唯一入口是通过相应的方法调用apiserver的接口
kubectl 是官方的CLI命令行工具,用于与apiserver进行通信,将用户在命令行输入的命令,组织转换成apiserver能识别的信息,进而实现管理k8s各种资源的一种有效途径
kubectl命令
============================kubectl命令=======================================k8s中文文档:http://docs.kubernetes.org.cn/683.html#kubectl命令帮助
kubectl --help #kubectl 对资源的增删查操作,比较方便,但是对改的操作就不容易了#查看版本信息
kubectl versionClient Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.11", GitCommit:"27522a29febbcc4badac257763044d0d90c11abd", GitTreeState:"clean", BuildDate:"2021-09-15T19:21:44Z", GoVersion:"go1.15.15", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.11", GitCommit:"27522a29febbcc4badac257763044d0d90c11abd", GitTreeState:"clean", BuildDate:"2021-09-15T19:16:25Z", GoVersion:"go1.15.15", Compiler:"gc", Platform:"linux/amd64"}#查看资源对象简写,太多了,就不展示了
kubectl api-resources#查看集群信息
kubectl cluster-infoKubernetes control plane is running at https://192.168.111.7:6443
KubeDNS is running at https://192.168.111.7:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.#配置kubectl自动补全,这是临时性的,可以将其写入到 /root/.bashrc下 bash刷新配置source <(kubectl completion bash)#node节点查看日志,太多了不展示
journalctl -u kubelet -f---------- 基本信息查看 ----------
#命令格式
kubectl get <resource> [-o wide|json|yaml] [-n namespace]
获取资源的相关信息,-n 指定命名空间 -o 指定输出格式resource可以是具体资源名称,如pod nginx-xxxx 也可以是资源类型,如pod 或者 all(仅展示几种核心资源,并不完整)--all-namespaces 或 -A :表示显示所有命名空间
--show-labels : 显示所有标签
-l app : 仅显示标签为app的资源
-l app=nginx : 仅显示包含app=nginx标签的,且值为nginx 的资源#查看master的资源 cs 的全称(componentstatuses)
kubect get cs #查看命令空间 ns 的全称 namespace 命名空间:允许不同空间的资源可以重名,但同一空间的资源不能重名
kubectl get ns#查看default命名空间的所有资源
kubectl get all [-n defautl]#创建命名空间app
kubectl create ns app#查看命名空间ns
kubectl get nsNAME STATUS AGE
app Active 4s
default Active 25h
kube-flannel Active 24h
kube-node-lease Active 25h
kube-public Active 25h
kube-system Active 25h#删除命名空间
kubectl delete ns appkubect get ns
NAME STATUS AGE
default Active 5d6h
kube-flannel Active 5d5h
kube-node-lease Active 5d6h
kube-public Active 5d6h
kube-system Active 5d6h#在命名空间kube-public创建副本控制器(deployment)来启动Pod(nginx-w1)kubectl create deployment nginx-w1 --image=nginx -n kube-public#描述某个资源的详细信息
kubectl describe deployment nginx-w1 -n kube-publicName: nginx-w1
Namespace: kube-public
CreationTimestamp: Tue, 21 May 2024 22:40:44 +0800
Labels: app=nginx-w1
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx-w1
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:Labels: app=nginx-w1Containers:nginx:Image: nginxPort: <none>Host Port: <none>Environment: <none>Mounts: <none>Volumes: <none>
Conditions:Type Status Reason---- ------ ------Available True MinimumReplicasAvailableProgressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-w1-578fdbcf44 (1/1 replicas created)
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal ScalingReplicaSet 88s deployment-controller Scaled up replica set nginx-w1-578fdbcf44 to 1kubectl describe pod nginx-wl-d47f99cb6-hv6gz -n kube-publicName: nginx-w1-578fdbcf44-9tmh2
Namespace: kube-public
Priority: 0
Node: node01/192.168.111.8
Start Time: Tue, 21 May 2024 22:40:44 +0800
Labels: app=nginx-w1pod-template-hash=578fdbcf44
Annotations: <none>
Status: Running
IP: 10.244.1.22
IPs:IP: 10.244.1.22
Controlled By: ReplicaSet/nginx-w1-578fdbcf44
Containers:nginx:Container ID: docker://43cbd4bbeb94b06cb40e640f0818e308afff675be59cec3f6c083714d945b9b2Image: nginxImage ID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31Port: <none>Host Port: <none>State: RunningStarted: Tue, 21 May 2024 22:41:01 +0800Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from default-token-ggbkn (ro)
Conditions:Type StatusInitialized True Ready True ContainersReady True PodScheduled True
Volumes:default-token-ggbkn:Type: Secret (a volume populated by a Secret)SecretName: default-token-ggbknOptional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal Scheduled 2m40s default-scheduler Successfully assigned kube-public/nginx-w1-578fdbcf44-9tmh2 to node01Normal Pulling 2m40s kubelet Pulling image "nginx"Normal Pulled 2m24s kubelet Successfully pulled image "nginx" in 15.87745213sNormal Created 2m24s kubelet Created container nginxNormal Started 2m24s kubelet Started container nginx#查看命名空间kube-public中的pod信息
kubectl get pods -n kube-publicNAME READY STATUS RESTARTS AGE
nginx-w1-578fdbcf44-9tmh2 1/1 Running 0 3m33s
nginx-w1-7464d66457-4srmd 1/1 Running 0 4d12h#kubectl exec 可以跨主机登录容器,docker exec只能在容器所在容器主机上登录
kubectl exec -it nginx-w1-578fdbcf44-9tmh2 bash -n kube-publickubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-w1-578fdbcf44-9tmh2:/#
root@nginx-w1-578fdbcf44-9tmh2:/#
root@nginx-w1-578fdbcf44-9tmh2:/#
root@nginx-w1-578fdbcf44-9tmh2:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@nginx-w1-578fdbcf44-9tmh2:/# #删除(重启)pod资源,由于存在deployment/rc之类的副本控制器,删除pod也会重新拉起来
kubectl delete pod nginx-w1-578fdbcf44-9tmh2 -n kube-publickubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-w1-578fdbcf44-gmdd9 1/1 Running 0 46s#若pod无法删除,总是处于terminate状态,则要强行删除podkubectl delete pod <pod-name> -n <namespace> --force --grace-period=0
#grace-period表示过渡存活期,默认30s,在删除pod之前允许POD慢慢终止其上的容器进程,从而优雅退出,0表示立即终止podkubectl delete pod nginx-w1-578fdbcf44-gmdd9 -n kube-public --force --grace-period=0warning: 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-w1-578fdbcf44-gmdd9" force deleted#扩缩容kubectl scale deployment nginx-wl --replicas=2 -n kube-public # 扩容deployment.apps/nginx-w1 scaled
[root@master01 ~]#kubectl get deployment nginx-w1 -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-w1 2/2 2 2 19mkubectl scale deployment nginx-wl --replicas=1 -n kube-public # 缩容ubectl get deployment nginx-w1 -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-w1 1/1 1 1 20m#删除副本控制器
kubectl delete deployment nginx-w1 -n kube-public #kubectl delete deployment nginx-w1 -n kube-public
deployment.apps "nginx-w1" deletedkubectl get deployments -n kube-public
No resources found in kube-public namespace.#项目的生命周期:创建---》发布---》更新---》回滚---》删除#创建命令 kubectl create
创建并运行一个或多个容器镜像
创建一个deployment 或 job来管理容器
kubectl create --help#创建命名空间
kubectl create emmm#启动nginx实例,暴露端口容器 80 ,设置副本数 3
Kubectl create deployment nginx-emmm --image=nginx:1.14 --port 80 --replicas=3 -n emmmkubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-emmm-7bcc48d894-28c9r 1/1 Running 0 3m29s
nginx-emmm-7bcc48d894-9fbvd 1/1 Running 0 3m29s
nginx-emmm-7bcc48d894-9jk65 1/1 Running 0 3m29skubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-emmm-7bcc48d894-28c9r 1/1 Running 0 4m39s
pod/nginx-emmm-7bcc48d894-9fbvd 1/1 Running 0 4m39s
pod/nginx-emmm-7bcc48d894-9jk65 1/1 Running 0 4m39sNAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-emmm 3/3 3 3 5m20sNAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-emmm-7bcc48d894 3 3 3 4m39s#发布命令 kubectl expose
将资源暴露为新的 Service
kubectl expose --help#为deployment的nginx-emmm创建serivce,并通过service的80端口转发至容器的80端口上,Service的名称为nginx-service,类型为NodePort
kubectl expose deployment nginx-emmm --port=80 --target-port=80 --name=nginx-service --type=NodePort ------------------------------------------------------------------------------------------
Kubernetes 之所以需要 Service,一方面是因为 Pod 的 IP 不是固定的(Pod可能会重建),另一方面则是因为一组 Pod 实例之间总会有负载均衡的需求。
Service 通过 Label Selector 实现的对一组的 Pod 的访问。
对于容器应用而言,Kubernetes 提供了基于 VIP(虚拟IP) 的网桥的方式访问 Service,再由 Service 重定向到相应的 Pod。service 的 type 类型:
●ClusterIP:提供一个集群内部的虚拟IP以供Pod访问(service默认类型)●NodePort:在每个Node上打开一个端口以供外部访问,Kubernetes将会在每个Node上打开一个端口并且每个Node的端口都是一样的,通过 NodeIp:NodePort 的方式Kubernetes集群外部的程序可以访问Service。
每个端口只能是一种服务,端口范围只能是 30000-32767。●LoadBalancer:通过设置LoadBalancer映射到云服务商提供的LoadBalancer地址。这种用法仅用于在公有云服务提供商的云平台上设置Service的场景。通过外部的负载均衡器来访问,通常在云平台部署LoadBalancer还需要额外的费用。
在service提交后,Kubernetes就会调用CloudProvider在公有云上为你创建一个负载均衡服务,并且把被代理的Pod的IP地址配置给负载均衡服务做后端。●externalName:将service名称映射到一个DNS域名上,相当于DNS服务的CNAME记录,用于让Pod去访问集群外部的资源,它本身没有绑定任何的资源。扩展
●port
port 是 k8s 集群内部访问service的端口,即通过 clusterIP: port 可以从 Pod 所在的 Node 上访问到 service●nodePort
nodePort 是外部访问 k8s 集群中 service 的端口,通过 nodeIP: nodePort 可以从外部访问到某个 service。●targetPort
targetPort 是 Pod 的端口,从 port 或 nodePort 来的流量经过 kube-proxy 反向代理负载均衡转发到后端 Pod 的 targetPort 上,最后进入容器。●containerPort
containerPort 是 Pod 内部容器的端口,targetPort 映射到 containerPort。------------------------------------------------------------------------------------------
#查看pod网络状态详细信息和 Service暴露的端口
kubectl get pods,svc -o wide -n emmmNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-emmm-7bcc48d894-28c9r 1/1 Running 0 25m 10.244.1.24 node01 <none> <none>
pod/nginx-emmm-7bcc48d894-9fbvd 1/1 Running 0 25m 10.244.1.25 node01 <none> <none>
pod/nginx-emmm-7bcc48d894-9jk65 1/1 Running 0 25m 10.244.2.16 node02 <none> <none>NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/nginx-service NodePort 10.96.101.158 <none> 80:30920/TCP 8m2s app=nginx-emmm#修改三个nginx副本内的页面内容
kubectl exec -it nginx-emmm-7bcc48d894-28c9r bash -n emmm
echo 123 > /usr/share/html/index.htmlkubectl exec -it nginx-emmm-7bcc48d894-9fbvd bash -n emmm
echo 123 > /usr/share/html/index.htmlkubectl exec -it nginx-emmm-7bcc48d894-9jk65 bash -n emmm
echo 123 > /usr/share/html/index.html#访问 10.96.101.158
[root@master01 ~]#curl 10.96.101.158
12345
[root@master01 ~]#curl 10.96.101.158
1234
[root@master01 ~]#curl 10.96.101.158
123
[root@master01 ~]#curl 10.96.101.158
12345#查看关联后端的节点
kubectl get endpoints -n emmmNAME ENDPOINTS AGE
nginx-service 10.244.1.24:80,10.244.1.25:80,10.244.2.16:80 30m#尝试杀掉一个nginx kubectl get pods -n emmm
NAME READY STATUS RESTARTS AGE
nginx-emmm-7bcc48d894-28c9r 1/1 Running 0 50m
nginx-emmm-7bcc48d894-9fbvd 1/1 Running 0 50m
nginx-emmm-7bcc48d894-9jk65 1/1 Running 0 50mkubectl delete pod -n emmm nginx-emmm-7bcc48d894-28c9r
pod "nginx-emmm-7bcc48d894-28c9r" deleted#查看endpoint,发生了改变
NAME ENDPOINTS AGE
nginx-service 10.244.1.25:80,10.244.2.16:80,10.244.2.17:80 33m#查看 service 的描述信息
kubectl describe svc nginx -n emmm
Name: nginx-service
Namespace: emmm
Labels: app=nginx-emmm
Annotations: <none>
Selector: app=nginx-emmm
Type: NodePort
IP Families: <none>
IP: 10.96.101.158
IPs: 10.96.101.158
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 30920/TCP
Endpoints: 10.244.1.25:80,10.244.2.16:80,10.244.2.17:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>#在 node01 节点上操作,查看负载均衡端口yum install ipvsadm -y
ipvsadm -LnTCP 127.0.0.1:30920 rr-> 10.244.1.25:80 Masq 1 0 0 -> 10.244.2.16:80 Masq 1 0 0 -> 10.244.2.17:80 Masq 1 0 0 TCP 192.168.111.8:30920 rr-> 10.244.1.25:80 Masq 1 0 0 -> 10.244.2.16:80 Masq 1 0 0 -> 10.244.2.17:80 Masq 1 0 0 TCP 192.168.122.1:30920 rr-> 10.244.1.25:80 Masq 1 0 0 -> 10.244.2.16:80 Masq 1 0 0 -> 10.244.2.17:80 Masq 1 0 0 #在 node02 节点上操作,同样方式查看负载均衡端口
yum install ipvsadm -y
ipvsadm -LnTCP 127.0.0.1:30920 rr-> 10.244.1.25:80 Masq 1 0 0 -> 10.244.2.16:80 Masq 1 0 0 -> 10.244.2.17:80 Masq 1 0 0 TCP 192.168.111.9:30920 rr-> 10.244.1.25:80 Masq 1 0 0 -> 10.244.2.16:80 Masq 1 0 0 -> 10.244.2.17:80 Masq 1 0 0 TCP 192.168.122.1:30920 rr-> 10.244.1.25:80 Masq 1 0 0 -> 10.244.2.16:80 Masq 1 0 0 -> 10.244.2.17:80 Masq 1 0 0 #在master01操作 查看访问日志
kubectl logs nginx-emmm-7bcc48d894-28c9r
kubectl logs nginx-emmm-7bcc48d894-9fbvd
kubectl logs nginx-emmm-7bcc48d894-9jk65#更新命令 kubectl set
更改现有应用资源一些信息。
kubectl set --help#获取修改模板
kubectl set image --help
Examples:# Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'.kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1#查看当前 nginx 的版本号
curl -I 192.168.111.7:30920
curl -I 192.168.111.8:30920HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 22 May 2024 07:31:21 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes#将nginx 版本更新为 1.15 版本
kubectl set image deployment nginx-emmm nginx=nginx:1.15 -n emmm#处于动态监听 pod 状态,由于使用的是滚动更新方式,所以会先生成一个新的pod,然后删除一个旧的pod,往后依次类推
kubectl get pods -w -n emmmmNAME READY STATUS RESTARTS AGE
nginx-emmm-5f86fcf5b9-5xtkq 0/1 ContainerCreating 0 19s
nginx-emmm-7bcc48d894-9fbvd 1/1 Running 0 69m
nginx-emmm-7bcc48d894-9jk65 1/1 Running 0 69m
nginx-emmm-7bcc48d894-dn5l6 1/1 Running 0 18m
nginx-emmm-5f86fcf5b9-5xtkq 1/1 Running 0 23s
nginx-emmm-7bcc48d894-dn5l6 1/1 Terminating 0 18m
nginx-emmm-5f86fcf5b9-m2tps 0/1 Pending 0 0s
nginx-emmm-5f86fcf5b9-m2tps 0/1 Pending 0 0s
nginx-emmm-5f86fcf5b9-m2tps 0/1 ContainerCreating 0 0s
nginx-emmm-7bcc48d894-dn5l6 0/1 Terminating 0 18m
nginx-emmm-5f86fcf5b9-m2tps 1/1 Running 0 2s
nginx-emmm-7bcc48d894-9fbvd 1/1 Terminating 0 69m
nginx-emmm-5f86fcf5b9-n9n6h 0/1 Pending 0 0s
nginx-emmm-5f86fcf5b9-n9n6h 0/1 Pending 0 0s
nginx-emmm-5f86fcf5b9-n9n6h 0/1 ContainerCreating 0 0s
nginx-emmm-7bcc48d894-dn5l6 0/1 Terminating 0 18m
nginx-emmm-7bcc48d894-dn5l6 0/1 Terminating 0 18m
nginx-emmm-7bcc48d894-9fbvd 0/1 Terminating 0 69m
nginx-emmm-7bcc48d894-9fbvd 0/1 Terminating 0 69m
nginx-emmm-7bcc48d894-9fbvd 0/1 Terminating 0 69m
nginx-emmm-5f86fcf5b9-n9n6h 1/1 Running 0 24s
nginx-emmm-7bcc48d894-9jk65 1/1 Terminating 0 69m
nginx-emmm-7bcc48d894-9jk65 0/1 Terminating 0 70m
nginx-emmm-7bcc48d894-9jk65 0/1 Terminating 0 70m
nginx-emmm-7bcc48d894-9jk65 0/1 Terminating 0 70m
....#再看更新好后的 Pod 的 ip 会改变kubectl get pods -n emmm -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-emmm-5f86fcf5b9-5xtkq 1/1 Running 0 11m 10.244.1.26 node01 <none> <none>
pod/nginx-emmm-5f86fcf5b9-m2tps 1/1 Running 0 10m 10.244.1.27 node01 <none> <none>
pod/nginx-emmm-5f86fcf5b9-n9n6h 1/1 Running 0 10m 10.244.2.18 node02 <none> <none>#再看 nginx 的版本号
curl -I 192.168.111.7:30920
curl -I 192.168.111.8:30920HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Wed, 22 May 2024 07:49:24 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes#回滚命令 kubectl rollout
对资源进行回滚管理
kubectl rollout --help#查看历史版本
kubectl rollout history deployment nginx-emmm -n emmm deployment.apps/nginx-emmm
REVISION CHANGE-CAUSE
1 <none>
2 <none>#回滚到上一个版本
kubectl rollout undo deployment/nginx-emmm -n emmmHTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 23 May 2024 13:22:06 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes#回滚到指定版本
kubectl rollect undo deployment/nginx-emmm -n emmm -to-revision=1
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Thu, 23 May 2024 13:25:34 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes#检查回滚状态
kubectl rollect status deployment/nginx-emmm -n emmm
deployment "nginx-emmm" successfully rolled out#删除命令 kubectl delete#删除副本控制器
kubectl delete deployment/nginx-emmm -n emmm#删除service
kubectl delete svc nginx-service -n emmm
金丝雀发布
==============================金丝雀发布(灰度发布)=================================
Deployment控制器支持自定义控制更新过程中的滚动节奏,如“暂停(pause)”或“继续(resume)”更新操作。比如等待第一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,再筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布#创建deployment
kubectl create deployment nginx --image=nginx:1.14 --port=80 --replicas=3 -n emmm
#创建service
kubectl expose -n emmm deployment/nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort #更新deployment的版本,并配置暂停deployment
kubectl set image deployment/nginx nginx=:1.15 -n emmm && kubectl rollect pasue deployment/nginx -n emmm#监控更新的过程,可以看到已经新增了一个资源,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause暂停命令kubectl get pods -w -n emmm -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-698676d7f8-fxcqs 1/1 Running 0 5m55s 10.244.1.47 node01 <none> <none>
nginx-d9d8cf5c7-59nmk 1/1 Running 0 11m 10.244.2.32 node02 <none> <none>
nginx-d9d8cf5c7-r5hsv 1/1 Running 0 11m 10.244.2.33 node02 <none> <none>
nginx-d9d8cf5c7-sd89w 1/1 Running 0 11m 10.244.1.46 node01 <none> <none>#修改一下nginx-d9d8cf5c7-7dtjk内nginx主页的内容kubectl exec -it -n emmm nginx-698676d7f8-fxcqs bashecho 123123 > /usr/share/nginx/html/index.html#可以查看对应版本,nginx-d9d8cf5c7-7dtjk和其他三个版本不同
curl -I 10.244.1.45
#在看看其他三个
curl -I 10.244.2.31
curl -I 10.244.1.44
curl -I 10.244.2.30#多访问几次
curl 192.168.111.7
=====================================================================================
[root@master01 ~]#curl 192.168.111.7:32421
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>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>
[root@master01 ~]#curl 192.168.111.7:32421
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>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>
[root@master01 ~]#curl 192.168.111.7:32421
123123
[root@master01 ~]#curl 192.168.111.7:32421
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>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>
======================================================================================#查看标签,可以看到有个标签 pod-template和其他三个pod-template有不同之处
kubectl get pod,svc -owide --show-labels -n emmmNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/nginx-d9d8cf5c7-59nmk 1/1 Running 0 10m 10.244.2.32 node02 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7
pod/nginx-d9d8cf5c7-r5hsv 1/1 Running 0 10m 10.244.2.33 node02 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7
pod/nginx-d9d8cf5c7-sd89w 1/1 Running 0 10m 10.244.1.46 node01 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR LABELS
service/nginx-service NodePort 10.96.44.163 <none> 80:31865/TCP 9m app=nginx app=nginx#创建一个新的service
kubectl expose -n emmm deployment nginx --port=80 --target-port=80 --name=new-nginx-service --type=NodePort#再次查看
kubectl get pod,svc -owide --show-labels -n emmmNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/nginx-698676d7f8-fxcqs 1/1 Running 0 4m33s 10.244.1.47 node01 <none> <none> app=nginx,pod-template-hash=698676d7f8
pod/nginx-d9d8cf5c7-59nmk 1/1 Running 0 10m 10.244.2.32 node02 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7
pod/nginx-d9d8cf5c7-r5hsv 1/1 Running 0 10m 10.244.2.33 node02 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7
pod/nginx-d9d8cf5c7-sd89w 1/1 Running 0 10m 10.244.1.46 node01 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR LABELS
service/new-nginx-service NodePort 10.96.143.97 <none> 80:32474/TCP 65s app=nginx app=nginx
service/nginx-service NodePort 10.96.44.163 <none> 80:31865/TCP 9m app=nginx app=nginx#查看nginx-service 的yaml文件
kubectl edit svc -n emmm new-nginx-service#拷贝其中的文件内容
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:creationTimestamp: "2024-05-23T14:36:36Z"labels:app: nginxname: new-nginx-servicenamespace: emmmresourceVersion: "88192"uid: b7ae9b1f-10ed-488e-a682-5253628ec97f
spec:clusterIP: 10.96.143.97clusterIPs:- 10.96.143.97externalTrafficPolicy: Clusterports:- nodePort: 32474port: 80protocol: TCPtargetPort: 80selector:app: nginxsessionAffinity: Nonetype: NodePort
status:loadBalancer: {}#切换 /mnt下
cd /mntvim new-nginx.yamlapiVersion: v1
kind: Service
metadata:labels:app: nginxname: new-nginx-servicenamespace: emmm
spec:clusterIP: 10.96.143.97clusterIPs:- 10.96.143.97externalTrafficPolicy: Clusterports:- nodePort: 32474port: 80protocol: TCPtargetPort: 80selector:pod-template-hash: 698676d7f8 #将原本的app: nginx替换成 pod-template-hash: 698676d7f8 sessionAffinity: Nonetype: NodePort#删除new-nginx-service
kubectl delete -n emmm svc new-nginx-service#使用apply -f 导入yaml文件
kubectl apply -f new-nginx.yaml#再次查看
kubectl get pod,svc -owide --show-labels -n emmmNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/nginx-698676d7f8-fxcqs 1/1 Running 0 13m 10.244.1.47 node01 <none> <none> app=nginx,pod-template-hash=698676d7f8
pod/nginx-d9d8cf5c7-59nmk 1/1 Running 0 19m 10.244.2.32 node02 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7
pod/nginx-d9d8cf5c7-r5hsv 1/1 Running 0 19m 10.244.2.33 node02 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7
pod/nginx-d9d8cf5c7-sd89w 1/1 Running 0 19m 10.244.1.46 node01 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR LABELS
service/new-nginx-service NodePort 10.96.143.97 <none> 80:32474/TCP 2s pod-template-hash=698676d7f8 app=nginx
service/nginx-service NodePort 10.96.44.163 <none> 80:31865/TCP 17m app=nginx app=nginx#使用curl -I 192.168.111.7:32474 查看版本
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Thu, 23 May 2024 14:49:19 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes#但使用31865依旧能访问到nginx1.15 ,所以需要再次操作kubectl edit svc -n emmm nginx-service# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:creationTimestamp: "2024-05-23T14:28:41Z"labels:app: nginxname: nginx-servicenamespace: emmmresourceVersion: "87501"uid: 40c61cb7-d77d-41d3-9427-f1ec368e667a
spec:clusterIP: 10.96.44.163clusterIPs:- 10.96.44.163externalTrafficPolicy: Clusterports:- nodePort: 31865port: 80protocol: TCPtargetPort: 80selector:app: nginxsessionAffinity: Nonetype: NodePort
status:loadBalancer: {}#cd /mnt下cd /mntvim nginx.yamlapiVersion: v1
kind: Service
metadata:creationTimestamp: "2024-05-23T14:28:41Z"labels:app: nginxname: nginx-servicenamespace: emmmresourceVersion: "87501"uid: 40c61cb7-d77d-41d3-9427-f1ec368e667a
spec:clusterIP: 10.96.44.163clusterIPs:- 10.96.44.163externalTrafficPolicy: Clusterports:- nodePort: 31865port: 80protocol: TCPtargetPort: 80selector:pod-template-hash: d9d8cf5c7sessionAffinity: Nonetype: NodePort#删除nginx-service
kubectl delete svc -n emmm nginx-service#使用apply -f 导入
kubectl apply -f nginx.yaml#查看状态
kubectl get pod,svc -owide --show-labels -n emmmNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod/nginx-698676d7f8-fxcqs 1/1 Running 0 21m 10.244.1.47 node01 <none> <none> app=nginx,pod-template-hash=698676d7f8
pod/nginx-d9d8cf5c7-59nmk 1/1 Running 0 27m 10.244.2.32 node02 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7
pod/nginx-d9d8cf5c7-r5hsv 1/1 Running 0 27m 10.244.2.33 node02 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7
pod/nginx-d9d8cf5c7-sd89w 1/1 Running 0 27m 10.244.1.46 node01 <none> <none> app=nginx,pod-template-hash=d9d8cf5c7NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR LABELS
service/new-nginx-service NodePort 10.96.143.97 <none> 80:32474/TCP 8m15s pod-template-hash=698676d7f8 app=nginx
service/nginx-service NodePort 10.96.44.163 <none> 80:31865/TCP 3s pod-template-hash=d9d8cf5c7 app=nginx#使用curl查看状态,多看几次,都是1.14版本
curl -I 192.168.111.7:31865
声明式资源管理方法
Kubernetes 支持 YAML 和 JSON 格式管理资源对象
JSON 格式:主要用于 api 接口之间消息的传递
YAML 格式:用于配置和管理,YAML 是一种简洁的非标记性语言,内容格式人性化,较易读YAML 语法格式:
●大小写敏感
●使用缩进表示层级关系
●不支持Tab键制表符缩进,只使用空格缩进
●缩进的空格数目不重要,只要相同层级的元素左侧对齐即可,通常开头缩进两个空格
●符号字符后缩进一个空格,如冒号,逗号,短横杆(-)等
●“---”表示YAML格式,一个文件的开始,用于分隔文件间
●“#”表示注释kubectl api-versionsadmissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1 #如果是业务场景一般首选使用 apps/v1
apps/v1beta1 #带有beta字样的代表的是测试版本,不用在生产环境中
apps/v1beta2
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/v1beta1
coordination.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1#编写一个yaml文件启动nginx
mkdir /mnt/demo
cd /mnt/demovim nginx-deployment.yamlapiVersion: apps/v1 #指定api版本标签
kind: Deployment #定义资源的类型或者/角色,deployment为副本控制器,此处资源类型可以是Deployment Job Ingress Service等
metadata: #定义资源的元数据信息,比如资源的名称,namespace,标签等信息name: nginx-deployment #定义资源的名称,在同一个namespace空间中必须是唯一的labels: #定义Deployment资源标签app: nginx
spec: #定义deployment资源需要的参数属性,诸如是否在容器失败时重新启动容器的属性replicas: 3 #定义副本数量selector: #定义标签选择器matchLabels: #定义匹配标签app: nginx #需与 .spec.templete.metadata.labels 定义的标签保持一致spec:containers: #定义容器属性- name: nginx #定义一个容器名,一个 - name: 定义一个容器image: nginx:1.15.4 #定义容器使用的镜像以及版本ports: - contionerPort: 80 #定义容器对外的端口#创建资源对象
kubectl apply -f nginx-deployment.yaml#查看创建的pod资源
kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-746ccc65d8-7th2c 0/1 ContainerCreating 0 10s <none> node01 <none> <none>
nginx-deployment-746ccc65d8-8mzjx 0/1 ContainerCreating 0 10s <none> node02 <none> <none>
nginx-deployment-746ccc65d8-mtkmn 0/1 ContainerCreating 0 10s <none> node02 <none> <none>#创建service服务对外提供访问并测试 #也可以和上面的yaml 文件写在一起,kubectl delete 删除即可,使用 --- 分隔
vim nginx-service.yaml
apiVersion: v1
kind: Service
metadata:name: nginx-servicelabels:apps: nginx
spec:type: NodePortports: - port: 80targetPort: 80selector:app: nginx#创建资源对象
kubectl create -f nginx-service.yamlkubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8d
nginx-service NodePort 10.96.64.1 <none> 80:32663/TCP 30s#使用curl访问 192.168.111.7:32663
curl 192.168.111.1:32663-----------------------------------------------------------------------------------------
详解k8s中的port:
●port
port 是 k8s 集群内部访问service的端口,即通过 clusterIP: port 可以从 Pod 所在的 Node 上访问到 service●nodePort
nodePort 是外部访问 k8s 集群中 service 的端口,通过 nodeIP: nodePort 可以从外部访问到某个 service。●targetPort
targetPort 是 Pod 的端口,从 port 或 nodePort 来的流量经过 kube-proxy 反向代理负载均衡转发到后端 Pod 的 targetPort 上,最后进入容器。●containerPort
containerPort 是 Pod 内部容器的端口,targetPort 映射到 containerPort。
-----------------------------------------------------------------------------------------#kubectl run --dry-run=client 打印相应的 API 对象而不执行创建
kubectl run nginx-test --image=nginx --port=80 --dry-run=client
kubectl create deployment nginx-deploy --image=nginx --port=80 --replicas=3 --dry-run=cli#查看生成yaml格式
kubectl run nginx-test --image=nginx --port=80 --dry-run=client -o yamlapiVersion: v1
kind: Pod
metadata:creationTimestamp: nulllabels:run: nginx-testname: nginx-test
spec:containers:- image: nginxname: nginx-testports:- containerPort: 80resources: {}dnsPolicy: ClusterFirstrestartPolicy: Always
status: {}kubectl create deployment nginx-deploy --image=nginx --port=80 --replicas=3 --dry-run=client -o yamlapiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: nginx-deployname: nginx-deploy
spec:replicas: 3selector:matchLabels:app: nginx-deploystrategy: {}template:metadata:creationTimestamp: nulllabels:app: nginx-deployspec:containers:- image: nginxname: nginxports:- containerPort: 80resources: {}
status: {}#查看生成json格式
kubectl run nginx-test --image=nginx --port=80 --dry-run=client -o json{"kind": "Pod","apiVersion": "v1","metadata": {"name": "nginx-test","creationTimestamp": null,"labels": {"run": "nginx-test"}},"spec": {"containers": [{"name": "nginx-test","image": "nginx","ports": [{"containerPort": 80}],"resources": {}}],"restartPolicy": "Always","dnsPolicy": "ClusterFirst"},"status": {}
}kubectl create deployment nginx-deploy --image=nginx --port=80 --replicas=3 --dry-run=client -o json{"kind": "Deployment","apiVersion": "apps/v1","metadata": {"name": "nginx-deploy","creationTimestamp": null,"labels": {"app": "nginx-deploy"}},"spec": {"replicas": 3,"selector": {"matchLabels": {"app": "nginx-deploy"}},"template": {"metadata": {"creationTimestamp": null,"labels": {"app": "nginx-deploy"}},"spec": {"containers": [{"name": "nginx","image": "nginx","ports": [{"containerPort": 80}],"resources": {}}]}},"strategy": {}},"status": {}
}#使用yaml格式导出生成模板,并进行修改以及删除一些不必要的参数
kubectl run nginx-test --image=nginx --port=80 --dry-run=client -o yaml > nginx-test.yamlkubectl create deployment nginx-deploy --image=nginx --port=80 --replicas=3 --dry-run=client -o yaml > nginx-deploy.yaml#以nginx-test.yaml举例
vim nginx-test.yaml
apiVersion: v1
kind: Pod
metadata:creationTimestamp: null #删除labels:run: nginx-testname: nginx-test
spec:containers:- image: nginxname: nginx-testports:- containerPort: 80resources: {} #删除dnsPolicy: ClusterFirstrestartPolicy: Always
status: {} #删除#将现有的资源生成模板导出
kubectl get svc nginx-service -o yaml#保存到文件中
kubectl get svc nginx-service -o yaml > my-svc.yaml#查看字段帮助信息,可一层层的查看相关资源对象的帮助信息
kubectl explain deployments.spec.template.spec.containers
或
kubectl explain pods.spec.containers#创建一个yaml文件,创建一个pod,资源名称my-nginx-emmm 命名空间my-emmm 容器镜像nginx-1.14
#容器端口80 标签abcd:my-emmm 创建service管理上面的pod 修改页面,显示 abc emmm,可以对外访问vim ns-pod-service.yamlapiVersion: v1
kind: Namespace
metadata:name=my-emmm
---
apiVersion: v1
kind: Pod
metadata:labels:abcd: emmmname: my-nginx-emmmnamespace: my-emmm
spec:containers:- image: nginx:1.14name: my-nginx-emmmports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:labels:abcd: emmmname: my-nginx-emmmnamespace: my-emmm
spec:ports:- nodePort: 32000port: 80protocol: TCPtargetPort: 80selector:abcd: emmmtype: NodePort#导入
kubectl apply -f ns-pod-service.yaml#查看详细情况
kubectl get pod,svc -n my-emmm -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/my-nginx-emmm 1/1 Running 0 38s 10.244.1.50 node01 <none> <none>NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/my-nginx-emmm NodePort 10.96.114.17 <none> 80:32000/TCP 38s abcd=emmm#修改主页
kubectl exec -it my-nginx-emmm bash -n my-emmm echo abc emmm > /usr/share/nginx/html/index.htmlcurl 192.168.111.7:32000
abc emmm
外部浏览器访问