k8s初级实战05--pod & container

k8s初级实战05--配置container & pod

1 基础概念

Pod 是一组紧密关联的容器集合,它们共享 IPC 和 Network namespace,是 Kubernetes 调度的基本单位。Pod 的设计理念是支持多个容器在一个 Pod 中共享网络和文件系统,可以通过进程间通信和文件共享这种简单高效的方式组合完成服务。
k8s 中既可以部署单容器的pod,也可以部署多容器的pod。

pod 中既可以包括一个容器,也可以包括多个容器;容器既可以全部是业务容器,也可以包括Init容器。
Init 容器是一种特殊容器,在 Pod 内的应用容器启动之前运行。Init 容器可以包括一些应用镜像中不存在的实用工具和安装脚本。

2 常见用法

  1. 创建pod
    vim pod_web_single.yaml 
    kind: Pod
    apiVersion: v1
    metadata:
      name: web-sg
    spec:
      containers:
      - name: web-sg
        image: nginx:1.19.6
    $ kubectl apply -f pod_web_single.yaml 
    pod/web-sg created
    
  2. 查看pod
    $ kubectl get po [|pod|pods]
    $ kubectl get pods|grep web-sg
    web-sg                                      1/1     Running   0          2m43s
    
    查看pod日志(若pod内只有1个容器,不需要后缀容器名称,若有多个容器则需要后缀 [-c] 容器名称):
    $ kubectl logs -n test-online my-web-9bf9f89fd-92pbz flask-app 
     * Serving Flask app "app" (lazy loading)
     * Environment: production
       WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: off
     * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    10.244.0.0 - - [26/Dec/2020 09:51:10] "GET / HTTP/1.1" 200 -
    10.244.0.0 - - [26/Dec/2020 09:51:11] "GET /favicon.ico HTTP/1.1" 404 -
    $ kubectl logs -n test-online my-web-9bf9f89fd-92pbz -c nginx 
    /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
    /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
    /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
    10-listen-on-ipv6-by-default.sh: info: ipv6 not available
    /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
    /docker-entrypoint.sh: Configuration complete; ready for start up
    
  3. 删除pod
    $ kubectl delete pod web-sg 
    pod "web-sg" deleted
    或者
    $ kubectl delete -f pod_web_single.yaml 
    pod "web-sg" deleted
    
  4. 创建多容器pod
    vim pod_web_double.yaml 
    kind: Pod
    apiVersion: v1
    metadata:
      name: web-db
    spec:
      containers:
      - name: web-nginx
        image: nginx:1.19.6
      - name: busybox
        image: busybox:1.32
        command: [sh,-c,'sleep 100']
    
    $ kubectl apply -f pod_web_double.yaml 
    pod/web-db created
    $ kubectl get po|grep web-db
    web-db                                      2/2     Running   0          87s
    由于该pod有2个容器,因此ready状态为2/2
    
  5. 创建多容器pod共享存储
    此处创建2个容器的pod,挂载同一个卷,进入busybox中新建busybox后,可以在ngixn中发现该目录也有同样的文件,即同一个pod内可以通过挂载卷实现文件共享。
    vim  pod_web_sharefile.yaml 
    kind: Pod
    apiVersion: v1
    metadata:
      name: web-sharefile
    spec:
      containers:
      - name: web-nginx
        image: nginx:1.19.6
        volumeMounts:
        - mountPath: /data
          name: data
      - name: busybox
        image: busybox:1.32
        command: [sh,-c,'sleep 1000']
        volumeMounts:
        - mountPath: /data
          name: data
      volumes:
      - name: data
        emptyDir: {
          
          }
    
    $ kubectl apply -f pod_web_sharefile.yaml 
    pod/web-sharefile created
    
    测试文件共享:
    $ kubectl exec -it  web-sharefile -c busybox -- sh
    / # cd data/
    /data # touch busybox.txt
    /data # exit
    $ kubectl exec -it  web-sharefile -c web-nginx -- sh
    # ls /data	
    busybox.txt
    
  6. 创建包含init容器的pod
    vim pod_web_init.yaml 
    kind: Pod
    apiVersion: v1
    metadata:
      name: web-init
    spec:
      containers:
      - name: web-nginx
        image: nginx:1.19.6
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: data
      initContainers:
      - name: busybox
        image: busybox:1.32
        command: [sh,-c,'echo hello initContainers >/usr/share/nginx/html/index.html;sleep 30']
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: data
      volumes:
      - name: data
        emptyDir: {
          
          }
    $ kubectl apply -f pod_web_init.yaml 
    pod/web-init created
    
    创建开始至sleep 30期间,都为Init:0/1
    $ kubectl get pod |grep web-init
    web-init                                    0/1     Init:0/1           0          15s
    init容器退出后,才会开始初始化nginx容器,正常启动后出现Runing状态
    $ kubectl get pod |grep web-init
    web-init                                    0/1     Init:0/1   0          34s
    $ kubectl get pod |grep web-init
    web-init                                    0/1     PodInitializing   0          35s
    $ kubectl get pod |grep web-init
    web-init                                    1/1     Running   0          40s
    
    此时进入 web-init 的 nginx 容器中,curl 127.0.0.1 发现返回的内容正好是initContainer 写入的内容,如下图:在这里插入图片描述

3 注意事项

  1. 多容器pod一般适用于容器生命周期一致的场景。
  2. init 容器运行完后需要退出,若init一直为running状态,则应用容器无法运行。

4 说明

概念->工作负载->Pods
Kubernetes指南-Pod

猜你喜欢

转载自blog.csdn.net/u011127242/article/details/111773766