目錄
Pod(容器組):Pod是Kubernetes中最小的部署單元,可以包含一個或多個容器。Pod提供了一種邏輯上的封裝,使得容器可以一起共享網(wǎng)絡(luò)和存儲資源
1、創(chuàng)建一個pod
2、pod管理
pod操作
目錄
創(chuàng)建Pod會很慢
Pod(容器組):Pod是Kubernetes中最小的部署單元,可以包含一個或多個容器。Pod提供了一種邏輯上的封裝,使得容器可以一起共享網(wǎng)絡(luò)和存儲資源
1、創(chuàng)建一個pod
2、pod管理
pod操作
使用 Kubectl 工具容器資源Kubectl 是管理Kubernetes集群的命令行工具,通過生成的 json 格式傳遞給 API Server 進(jìn)行創(chuàng)建、查看、管理的操作。使用 kubectl --help 命令可以查看 Kubectl 幫助命令,其中包括基本命令、部署命令、群集管理命令、調(diào)試命令以及高級命令等。
創(chuàng)建Podkubectl run 命令可以創(chuàng)建并運(yùn)行一個或多個Pod,也可以創(chuàng)建一個 deployment 或job 來管理Pod。此命令和 docker run 相類似,也是實(shí)現(xiàn)Pod的創(chuàng)建,先從倉庫中拉取基礎(chǔ)鏡像,然后對容器進(jìn)行操作。
發(fā)布服務(wù)容器資源創(chuàng)建完成,就需要完成發(fā)布工作,確保 Pod 能夠?qū)ν馓峁┓?wù),保證客戶端能夠正常訪問,使用 kubectl expose 命令可以實(shí)現(xiàn)該目的。
創(chuàng)建Pod會很慢
Pod(容器組):Pod是Kubernetes中最小的部署單元,可以包含一個或多個容器。Pod提供了一種邏輯上的封裝,使得容器可以一起共享網(wǎng)絡(luò)和存儲資源
1、創(chuàng)建一個pod
1)創(chuàng)建yuml文件
[root@k8s-master ~]# mkdir k8s
[root@k8s-master ~]# vim ./k8s/nginx.yaml
piVersion: v1
kind: Pod
metadata:
??name: nginx
??labels:
????app: web
spec:
??containers:
????- name: nginx
??????image: nginx:1.13
??????ports:
????????- containerPort: 80
2)創(chuàng)建容器
方法一. ?yum安裝
[root@k8s-master ~]#yum install *rhsm*
方法二
執(zhí)行命令:
[root@k8s-master ~]#wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
[root@k8s-master ~]# yum -y install *rhsm*
?[root@k8s-master ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
[root@k8s-master ~]# rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem ?
前兩個命令會生成/etc/rhsm/ca/redhat-uep.pem文件. ?
[root@k8s-master ~]# docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
Trying to pull repository registry.access.redhat.com/rhel7/pod-infrastructure ...
latest: Pulling from registry.access.redhat.com/rhel7/pod-infrastructure
26e5ed6899db: Downloading 34.95 MB/74.87 MB
66dbe984a319: Download complete
9138e7863e08: Download complete
創(chuàng)建pod
[root@k8s-master ~]# kubectl create -f ./k8s/nginx.yaml
pod "nginx" created
3)查看所有pod創(chuàng)建運(yùn)行狀態(tài)
[root@k8s-master ~]# kubectl get pod
NAME ?????READY ????STATUS ?????????????RESTARTS ??AGE
nginx ????0/1 ??????ContainerCreating ??0 ?????????17s
4)查看指定pod資源
[root@k8s-master ~]# kubectl get pod nginx
NAME ?????READY ????STATUS ?????????????RESTARTS ??AGE
nginx ????0/1 ??????ContainerCreating ??0 ?????????41s
5)查看pod運(yùn)行的詳細(xì)信息
[root@k8s-master ~]# kubectl describe pod nginx
Name: nginx
Namespace: default
Node: k8s-node2/192.168.50.51
Start Time: Thu, 10 Aug 2023 20:33:09 +0800
Labels: app=web
Status: Pending
IP:
Controllers: <none>
Containers:
??nginx:
????Container ID:
????Image: nginx:1.13
????Image ID:
????Port: 80/TCP
????State: Waiting
??????Reason: ContainerCreating
????Ready: False
????Restart Count: 0
????Volume Mounts: <none>
????Environment Variables: <none>
Conditions:
??Type Status
??Initialized True
??Ready False
??PodScheduled True
No volumes.
QoS Class: BestEffort
Tolerations: <none>
Events:
??FirstSeen LastSeen Count From SubObjectPath Type ReasonMessage
??--------- -------- ----- ---- ------------- -------- -------------
??1m 1m 1 {default-scheduler } Normal Scheduled Successfully assigned nginx to k8s-node2
??25s 25s 1 {kubelet k8s-node2} Warning FailedSync Error syncing pod, skipping: failed to "StartContainer" for "POD" with ImagePullBackOff: "Back-off pulling image \"registry.access.redhat.com/rhel7/pod-infrastructure:latest\""
??1m 4s 3 {kubelet k8s-node2} Warning FailedSync Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request. ?details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"
顯示調(diào)度節(jié)點(diǎn) 驗(yàn)證運(yùn)行的pod
[root@k8s-master ~]# kubectl get pod nginx -o wide
NAME ?????READY ????STATUS ?????????????RESTARTS ??AGE ??????IP ???????NODE
nginx ????0/1 ??????ContainerCreating ??0 ?????????2m ???????<none> ???k8s-node2
2、pod管理
1)刪除pod
[root@k8s-master ~]# kubectl delete pod nginx
pod "nginx" deleted
2)查看刪除pod無法找到
[root@k8s-master ~]# kubectl get pod nginx -o wide
Error from server (NotFound): pods "nginx" not found
3)創(chuàng)建pod
[root@k8s-master ~]# kubectl create -f ./k8s/nginx.yaml
pod "nginx" created
4)發(fā)現(xiàn)最先創(chuàng)建的pod運(yùn)行在k8s-master節(jié)點(diǎn)上,下載鏡像速度太慢沒法運(yùn)行
[root@k8s-master ~]# kubectl get pod nginx -o wide
NAME ?????READY ????STATUS ?????????????RESTARTS ??AGE ??????IP ???????NODE
nginx ????0/1 ??????ContainerCreating ??0 ?????????38s ??????<none> ???k8s-master
5)訪問pod節(jié)點(diǎn)中的服務(wù)
[root@k8s-master ~]# kubectl get pod nginx -o wide
NAME????? READY???? STATUS??? RESTARTS?? AGE?????? IP??????????? NODE
nginx???? 1/1?????? Running?? 0????????? 21m?????? 172.16.48.2?? k8s-master
[root@k8s-master ~]# curl -l http://172.16.48.2
<!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 >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
pod操作
1)在一個pod中修改配置文件運(yùn)行多個業(yè)務(wù)容器
[root@k8s-master ~]# vim ./k8s/nginx.yaml
apiVersion: v1
kind: Pod
metadata:
? name: test3
? labels:
??? app: web
spec:
? containers:
??? - name: nginx02
????? image: nginx:1.13
????? ports:
??????? - containerPort: 80
??? - name: busybox
????? image: docker.io/busybox:latest
????? command: ["sleep","3600"]
????? ports:
??????? - containerPort: 80
~????????????????????????????
?2)創(chuàng)建資源
[root@k8s-master ~]#? kubectl create -f ./k8s/nginx.yaml
3)查看資源使用情況??? Pod會 分配了各自的 IP??? 過程會有點(diǎn)慢正?,F(xiàn)象
[root@k8s-master ~]#? kubectl get pod -o wide
NAME????? READY???? STATUS????????????? RESTARTS?? AGE?????? IP??????????? NODE
nginx???? 1/1?????? Running???????????? 0????????? 25m?????? 172.16.48.2?? k8s-master
test1???? 0/2?????? ContainerCreating?? 0????????? 9m??????? <none>??????? k8s-node2
test2???? 2/2?????? Running???????????? 0????????? 5m??????? 172.16.48.3?? k8s-master
test3???? 0/2?????? ContainerCreating?? 0????????? 4m??????? <none>??????? k8s-node2
使用 Kubectl 工具容器資源
Kubectl 是管理Kubernetes集群的命令行工具,通過生成的 json 格式傳遞給 API Server 進(jìn)行創(chuàng)建、查看、管理的操作。
使用 kubectl --help 命令可以查看 Kubectl 幫助命令,其中包括基本命令、部署命令、群集管理命令、調(diào)試命令以及高級命令等。
[root@k8s-master ~]# kubectl --help
kubectl controls the Kubernetes cluster manager.?Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
? create??????? Create a resource from a file or from stdin.
? expose??????? 使用 replication controller, service, deployment 或者 pod
并暴露它作為一個 新的 Kubernetes Service
? run?????????? 在集群中運(yùn)行一個指定的鏡像
? set?????????? 為 objects 設(shè)置一個指定的特征Basic Commands (Intermediate):
? explain?????? 查看資源的文檔
? get?????????? 顯示一個或更多 resources
? edit????????? 在服務(wù)器上編輯一個資源
? delete??????? Delete resources by filenames, stdin, resources and names, or by resources and label
selectorDeploy Commands:
? rollout?????? Manage the rollout of a resource
? scale???????? Set a new size for a Deployment, ReplicaSet or Replication Controller
? autoscale???? 自動調(diào)整一個 Deployment, ReplicaSet, 或者 ReplicationController
的副本數(shù)量
創(chuàng)建Pod
kubectl run 命令可以創(chuàng)建并運(yùn)行一個或多個Pod,也可以創(chuàng)建一個 deployment 或job 來管理Pod。此命令和 docker run 相類似,也是實(shí)現(xiàn)Pod的創(chuàng)建,先從倉庫中拉取基礎(chǔ)鏡像,然后對容器進(jìn)行操作。
通過 kubectl run 命令創(chuàng)建 Nginx 容器, 指定名稱為 nginx-deployment,指定基礎(chǔ)鏡像為 Nginx 目前最新版本,指定對外暴露的端口為 80 以及副本數(shù)為 3。Nginx 容器創(chuàng)建完成后使用 get pod 命令查看 Pod 的信息,可以發(fā)現(xiàn)確實(shí)有 3 個容器資源,并且處于 Running狀態(tài)。還可以查看 deployment,也顯示的是 3。
[root@k8s-master ~]#? kubectl run nginx-do --image=nginx:1.14 --port=80 --replicas=2
deployment "nginx-do" created
[root@k8s-master ~]# kubectl get pods
NAME??????????????????????????????? READY?? STATUS??? RESTARTS?? AGEnginx-do????????? 1/1?????? Running???????????? 0?????????? 1m
nginx-do???? 1/1 ????? Running???????????? 0???????????? 1m
發(fā)布服務(wù)
容器資源創(chuàng)建完成,就需要完成發(fā)布工作,確保 Pod 能夠?qū)ν馓峁┓?wù),保證客戶端能夠正常訪問,使用 kubectl expose 命令可以實(shí)現(xiàn)該目的。
?通過 kubectl expose 命令創(chuàng)建 Service,設(shè)置內(nèi)部通信端口和外部暴露的端口均為 80、名稱為 nginx-service、類型是 NodePort。創(chuàng)建 Service 完后,就可以通過 kubectl get svc命令查看到對外暴露的端口是30464,內(nèi)部通信的地址是10.254.108.146。
[root@k8s-master ~]#? kubectl expose deployment nginx-do --port=80 --target-port=80 --name=nginx --type=NodePort
service "nginx" exposed
查看
[root@k8s-master ~]# kubectl get pods,svc
NAME??????????????????????????????????? READY?? STATUS??? RESTARTS?? AGE
po/nginx-do-3943939860-d925b????????? 1/1?????? Running???????????? 0?????????? 1m
po/nginx-do-3943939860-fq4zh????? 1/1 ????? Running???????????? 0???????????? 1mNAME??????????????? CLUSTER-IP?????? EXTERNAL-IP?? PORT(S)??????? AGE
svc/kubernetes????? 10.254.0.1?????? <none>??????? 443/TCP??????? 1d
svc/nginx?????????? 10.254.229.91??? <nodes>?????? 80:30040/TCP?? 38s
svc/nginx-service?? 10.254.108.146?? <nodes>?????? 80:30233/TCP?? 5m
執(zhí)行 kubectl get endpoints 查看容器自帶的負(fù)載均衡。從執(zhí)行結(jié)果可以得知容器自帶的負(fù)載均衡分別是 10.254.0.1? 10.254.229.91??? 10.254.108.146?
[root@k8s-master ~]# kubectl get endpoints
NAME??????????? ENDPOINTS??????????? AGE
kubernetes????? 192.168.1.117:6443?? 1d
nginx?????????? 172.16.48.6:80?????? 25m
nginx-service?? 172.16.48.4:80?????? 30m
執(zhí)行 kubectl get pods 命令時加參數(shù) -o wide 可以查看到 Node 真實(shí)節(jié)點(diǎn)上的分布。
[root@k8s-master ~]# kubectl get pods -o wide
NAME??????????????????????????????? READY?? STATUS??? RESTARTS?? AGE??? IP?????????? NODE???????? NOMINATED NODE?? READINESS GATESnginx-do-3943939860-d925b????????? 1/1?????? Running???????????? 0????????? 27m?????? 172.16.48.6?? k8s-master
nginx-do-3943939860-fq4zh????????? 0/1?????? ContainerCreating?? 0????????? 27m?????? <none>??????? k8s-node2
完成上述步驟就已經(jīng)完成了發(fā)布,并且可以進(jìn)行對其訪問。?
[root@k8s-master ~]# curl 192.168.50.53:30040
<!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 >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
修改其配置文件
[root@k8s-master ~]# docker exec -it 0a163ad392dc /bin/bash
root@nginx-do-3943939860-d925b:/# cd /usr/share/nginx/html/
root@nginx-do-3943939860-d925b:/usr/share/nginx/html# echo "sdfsdfsdfsd" > index.html
驗(yàn)證
[root@k8s-master ~]# curl 192.168.50.53:30040
sdfsdfsdfsd文章來源:http://www.zghlxwxcb.cn/news/detail-639351.html
今天先到這里再見文章來源地址http://www.zghlxwxcb.cn/news/detail-639351.html
到了這里,關(guān)于k8s常用資源管理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!