目录
一、应用部署
二、编写yaml文件
三、init容器
四、探针
1、存活探针
2、就绪探针
一、应用部署
下载测试镜像
docker pull yakexi007/myapp:v1
docker pull yakexi007/myapp:v2
docker tag yakexi007/myapp:v1 reg.westos.org/library/myapp:v1
docker tag yakexi007/myapp:v2 reg.westos.org/library/myapp:v2
docker push reg.westos.org/library/myapp:v1
docker push reg.westos.org/library/myapp:v2
创建自主式pod (生产不推荐)
kubectl run demo --image=myapp:v1
kubectl get pod -o wide
查看pod详情 及删除
kubectl describe pod demo
kubectl delete pod demo
创建控制器(推荐)
kubectl create deployment myapp --image=myapp:v1 --replicas=3
控制器自动维护pod副本数
kubectl get pod
kubectl get deployments.apps
kubectl delete pod myapp-67984c8646-kgxl5
kubectl get deployments.apps
kubectl get pod
在远程pod中执行命令
kubectl exec myapp-67984c8646-98zh4 -- ls /usr/share/nginx/html
扩容pod数量
kubectl scale deployment myapp --replicas=6
kubectl get pod
缩容
kubectl scale deployment myapp --replicas=3
kubectl get pod
通过service暴露pod
kubectl expose deployment myapp --port=80 --target-port=80
查看svc详情
kubectl describe svc myapp
curl 10.98.121.24
curl 10.98.121.24/hostname.html
service自动发现pod扩容与缩容,自动更新endpoints,实现对应用的负载均衡
service默认使用clusterip类型,只能在集群中访问
nodeport类型,可以在集群外部访问
kubectl edit svc myapp
kubectl get svc
访问集群任意节点+端口
curl 192.168.67.11:32209/hostname.html
curl 192.168.67.12:32209/hostname.html
curl 192.168.67.13:32209/hostname.html
更新应用版本
kubectl set image deployment/myapp myapp=myapp:v2
curl 192.168.67.13:32209
查看应用历史版本
kubectl rollout history deployment myapp
回滚
kubectl rollout undo deployment myapp --to-revision=1
curl 192.168.67.11:32209
删除应用
kubectl delete deployments.apps myapp
kubectl delete svc myapp
kubectl get pod
集群通过namespace来做资源隔离,默认操作的资源都指向default
kubectl get ns
二、编写yaml文件
随着微服务的使用,在linux上使用vim或者vi命令编辑yml文件的时候,会自动缩进,导致文件根本 无法使用。
解决办法
粘贴前先执行如下命令: 再粘贴的时候,里面的内容就不再缩进了,完美解决。
:set paste
mkdir pod
cd pod/
获取帮助
kubectl explain pod.spec.containers
获取yaml模板
kubectl run demo --image nginx --dry-run=client -o yaml > pod.yaml
vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: demo
name: demo
spec:
containers:
- image: nginx
name: demo
imagePullPolicy: IfNotPresent
创建pod
kubectl create -f pod.yaml
查看详情
kubectl get pod -o wide
kubectl describe pod demo
kubectl get pod demo -o yaml
vim pod.yml
cat pod.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: demo
name: demo
spec:
containers:
- image: nginx
name: demo
- image: redis:6.2.4
name: redis
kubectl apply -f pod.yml
kubectl describe pod
kubectl delete -f pod.yml
vim pod.yml
cat pod.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: demo
name: demo
spec:
containers:
- image: nginx
name: demo
imagePullPolicy: IfNotPresent
kubectl apply -f pod.yml
kubectl get pod demo -o yaml
kubectl delete -f pod.yml
3 资源限制
vim pod.yml
cat pod.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: demo
name: demo
spec:
containers:
- image: nginx
name: demo
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 1
memory: 200M
requests:
cpu: 0.5
memory: 100M
kubectl apply -f pod.yml
kubectl get pod demo -o yaml
Guaranteed,保证级别:Pod 中的容器都设置了 CPU 和 Memory 的 requests 和 limits,并且这些值相等。此时,如果资源满足所有 Guaranteed 类别的 Pod 的需求,则这些 Pod 会被同时调度在集群中,保证它们的运行。
Burstable,突发级别:Pod 中至少有一个容器设置了 CPU 和 Memory 的 requests 和 limits,但是这些值不相等。此时,如果资源足够,那么这些 Pod 会被相应地调度,但是它们的性能可能会受到限制,因为它们能够使用的资源是有上限的。
BestEffort,尽力级别:Pod 中的容器没有设置 CPU 和 Memory 的 requests 和 limits。此时,这些容器会尽力地获取集群中未被占用的资源,但是它们的资源使用是无法保证的,因为它们需要等待集群中已分配的所有资源使用完毕才能够获得更多的资源。
4.端口映射
##有配置NodePort,外部流量可访问k8s中的服务
vim pod.yml
cat pod.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: demo
name: demo
spec:
containers:
- image: nginx
name: demo
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
hostPort: 80
kubectl apply -f pod.yml
kubectl get pod -o wide
kubectl describe pod demo
vim pod.yml
cat pod.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: demo
name: demo
spec:
hostNetwork: true
nodeSelector:
kubernetes.io/hostname: k8s2
containers:
- image: nginx
name: demo
imagePullPolicy: IfNotPresent
kubectl apply -f pod.yml
kubectl describe pod demo
kubectl delete -f pod.yml
三、init容器
vim init-pod.yaml
cat init-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app.kubernetes.io/name: MyApp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
kubectl apply -f init-pod.yaml
kubectl get pod
vim myservice.yaml
cat myservice.yaml
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
kubectl apply -f myservice.yaml
kubectl get svc
svc解析成功后,init容器退出,主容器运行
yum install -y bind-utils
dig -t A myservice.default.svc.cluster.local. @10.96.0.10
kubectl get pod
回收资源
kubectl delete -f init-pod.yaml
kubectl delete -f myservice.yaml
四、探针
1、存活探针
vim liveness-pod.yaml
cat liveness-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: nginx
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
在存活探针检测失败导致容器不断被重启
kubectl apply -f liveness-pod.yaml
kubectl get pod -w
kubectl describe pod liveness-http
kubectl delete -f liveness-pod.yaml
2、就绪探针
vim liveness-pod.yaml
cat liveness-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: nginx
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe:
httpGet:
path: /test.html
port: 80
initialDelaySeconds: 5
periodSeconds: 5
kubectl apply -f liveness-pod.yaml
就绪探针失败导致容器一直未就绪
kubectl get pod
kubectl describe pod liveness-http
创建测试页面
kubectl exec liveness-http -- touch /usr/share/nginx/html/test.html
就绪探针成功
kubectl get pod
创建svc
kubectl expose pod liveness-http --port 80 --target-port 80
就绪容器自动上线
kubectl describe svc liveness-http
删除测试页面
kubectl exec liveness-http -- rm /usr/share/nginx/html/test.html
就绪探针失败,容器未就绪
kubectl get pod
在svc中容器自动下线
kubectl describe svc liveness-http
回收文章来源:https://www.toymoban.com/news/detail-727510.html
kubectl delete -f liveness-pod.yaml
文章来源地址https://www.toymoban.com/news/detail-727510.html
到了这里,关于Kubernetes的pod管理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!