kubernetes集群编排——pod管理,探针

pod管理

应用部署

下载测试镜像

[root@k8s1 docker]# docker pull yakexi007/myapp:v1

[root@k8s1 docker]# docker pull yakexi007/myapp:v2

[root@k8s1 docker]# docker tag yakexi007/myapp:v1  reg.westos.org/library/myapp:v1

[root@k8s1 docker]# docker tag yakexi007/myapp:v2 reg.westos.org/library/myapp:v2

[root@k8s1 docker]# docker push reg.westos.org/library/myapp:v1

[root@k8s1 docker]# docker push reg.westos.org/library/myapp:v2

创建自主式pod (生产不推荐)

[root@k8s2 ~]# kubectl run demo --image=myapp:v1

[root@k8s2 ~]# kubectl get pod -o wide

查看pod详情

[root@k8s2 ~]# kubectl describe  pod demo

删除pod

[root@k8s2 ~]# kubectl delete  pod demo

创建控制器(推荐)

[root@k8s2 ~]# kubectl create deployment myapp --image=myapp:v1 --replicas=3

控制器自动维护pod副本数

[root@k8s2 ~]# kubectl get pod

[root@k8s2 ~]# kubectl get deployments.apps

[root@k8s2 ~]# kubectl delete pod myapp-67984c8646-rm49k

[root@k8s2 ~]# kubectl get deployments.apps

[root@k8s2 ~]# kubectl get pod

在远程pod中执行命令

[root@k8s2 ~]# kubectl exec myapp-67984c8646-flb5t -- ls /usr/share/nginx/html

扩容pod数量

[root@k8s2 ~]# kubectl  scale deployment myapp --replicas=6

[root@k8s2 ~]# kubectl get pod

缩容

[root@k8s2 ~]# kubectl  scale deployment myapp --replicas=3

[root@k8s2 ~]# kubectl get pod

通过service暴露pod

[root@k8s2 ~]# kubectl expose deployment myapp --port=80 --target-port=80

查看svc详情

[root@k8s2 ~]# kubectl describe  svc myapp

[root@k8s2 ~]# curl 10.106.225.101

[root@k8s2 ~]# curl 10.106.225.101/hostname.html

service自动发现pod扩容与缩容,自动更新endpoints,实现对应用的负载均衡

service默认使用clusterip类型,只能在集群中访问

nodeport类型,可以在集群外部访问

[root@k8s2 ~]# kubectl edit svc myapp

[root@k8s2 ~]# kubectl get svc

访问集群任意节点+端口

[root@k8s1 harbor]# curl 192.168.52.131:30260/hostname.html

[root@k8s1 harbor]# curl 192.168.52.132:30260/hostname.html

[root@k8s1 harbor]# curl 192.168.52.133:30260/hostname.html

更新应用版本

[root@k8s2 ~]# kubectl set image deployment/myapp myapp=myapp:v2

[root@k8s1 harbor]# curl 192.168.52.131:30260

[root@k8s2 ~]# kubectl get all

查看应用历史版本

[root@k8s2 ~]# kubectl rollout history deployment myapp

回滚

[root@k8s2 ~]# kubectl rollout undo deployment myapp --to-revision=1

[root@k8s1 docker]# curl  192.168.56.14:30280

删除应用

[root@k8s2 ~]# kubectl delete  deployments.apps myapp

[root@k8s2 ~]# kubectl delete svc myapp

[root@k8s2 ~]# kubectl get pod

集群通过namespace来做资源隔离,默认操作的资源都指向default

[root@k8s2 ~]# kubectl get ns

编写yaml文件

获取帮助

[root@k8s2 pod]# kubectl explain pod.spec.containers

获取yaml模板

[root@k8s2 pod]# kubectl run demo --image nginx --dry-run=client  -o yaml > pod.yaml

[root@k8s2 pod]# vim pod.yaml

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: demo
  name: demo
spec:
  containers:
  - image: nginx
    name: demo
    imagePullPolicy: IfNotPresent

创建pod

[root@k8s2 pod]# kubectl create -f pod.yaml

查看详情

[root@k8s2 pod]# kubectl get pod -o wide

[root@k8s2 pod]# kubectl describe  pod demo

[root@k8s2 pod]# kubectl get pod demo -o yaml

示例

[root@k8s2 pod]# vim pod.yml

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: demo
  name: demo
spec:
  hostNetwork: true
  nodeSelector:
    kubernetes.io/hostname: k8s3
  containers:
  - image: nginx
    name: demo
    imagePullPolicy: IfNotPresent
   # ports:                               #端口映射
   # - name: http
   #   containnerPort: 80
   #   hosPort: 80
   #resources:                            #资源限制
   #  limits:
   #    cpu: 1
   #    memory: 200Mi
   #  requests:
   #    cpu: 0.5
   #    memory: 100Mi
 # - image: redis:6.2.4                    #定义多个容器
 #   name: redis

[root@k8s2 pod]# kubectl apply -f pod.yml

[root@k8s2 pod]# kubectl delete  -f pod.yml

init容器

[root@k8s2 pod]# vim 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"]

[root@k8s2 pod]# kubectl apply -f init-pod.yaml

[root@k8s2 pod]# kubectl get pod

在init容器没有成功运行之前,主容器不会被运行

添加svc定义

[root@k8s2 pod]# vim myservice.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376

[root@k8s2 pod]# kubectl apply -f myservice.yaml

[root@k8s2 pod]# kubectl get svc

svc解析成功后,init容器退出,主容器运行

[root@k8s2 pod]# yum install -y bind-utils

[root@k8s2 pod]# dig -t A myservice.default.svc.cluster.local. @10.96.0.10

[root@k8s2 pod]# kubectl get pod

回收资源

[root@k8s2 pod]# kubectl delete  -f init-pod.yml

[root@k8s2 pod]# kubectl delete  -f myservice.yml

探针

存活探针

[root@k8s2 pod]# vim 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

[root@k8s2 pod]# kubectl apply -f liveness-pod.yaml

在存活探针检测失败导致容器不断被重启

[root@k8s2 pod]# kubectl get pod -w

[root@k8s2 pod]# kubectl describe  pod liveness-http

[root@k8s2 pod]# kubectl delete  -f liveness-pod.yaml

就绪探针

[root@k8s2 pod]# vim 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

[root@k8s2 pod]# kubectl apply -f liveness-pod.yaml

就绪探针失败导致容器一直未就绪

[root@k8s2 pod]# kubectl get pod 

[root@k8s2 pod]# kubectl describe  pod liveness-http

创建测试页面

[root@k8s2 pod]# kubectl exec  liveness-http -- touch /usr/share/nginx/html/test.html

就绪探针成功

[root@k8s2 pod]# kubectl get pod

创建svc

[root@k8s2 pod]# kubectl expose pod liveness-http --port 80 --target-port 80

就绪容器自动上线

[root@k8s2 pod]# kubectl describe  svc liveness-http

删除测试页面

[root@k8s2 pod]# kubectl exec  liveness-http -- rm /usr/share/nginx/html/test.html

就绪探针失败,容器未就绪

[root@k8s2 pod]# kubectl get pod

在svc中容器自动下线

[root@k8s2 pod]# kubectl describe  svc liveness-http

回收

[root@k8s2 pod]# kubectl delete  -f liveness-pod.yaml

[root@k8s2 pod]# kubectl delete   svc liveness-http

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值