日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Pod創建多個容器并訪問指定容器

作者:她丶如月中來 更新時間: 2022-07-18 編程語言

Pod創建多個容器并訪問指定容器


本文章案例是采用helm提供的模板來管理kubernates,如果您想了解helm,您可以訪問helm docs進行了解。

文章目錄

  • Pod創建多個容器并訪問指定容器
  • 一、創建Deployment模板
  • 二、運行并查看Pod信息
  • 三、訪問指定Pod中的某一個容器


一、創建Deployment模板

如果您對Kubernates還沒有了解的話,建議您可以先訪問kubernates中文文檔, 否者以下內容可能讓您覺得難以理解。

#版本號
apiVersion: apps/v1
#資源類型
kind: Deployment
# 源數據
metadata:
  name: {{ .Values.componentName }}
  ## 命名空間
  namespace: {{ .Values.namespace }}
  ## 標簽
  labels:
    {{- include "seeker-file-server.labels" . | nindent 4 }}
spec:
  ## 副本數
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "seeker-file-server.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "seeker-file-server.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      ## 鏡像拉取時的認證
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: default
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      ##Pod要運行的鏡像,這里可以是多個。如果您以前的沒有看懂也沒關系,這里才是重點。  
      containers:
          #容器名字
        - name: "file-server"
          ## file-server容器的鏡像地址
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag}}
          ## 鏡像拉取策略(IfNotPresent 默認值,表示宿主機上沒有該鏡像時才拉取。Always 表示每次創建pod時都會重新拉取鏡像。Never 表示只使用本地鏡像,從不主動拉取鏡像。)
          imagePullPolicy: "IfNotPresent"
          ## 啟動容器時的命令
          command: [ "/bin/sh","-c","/bin/sh /opt/fileServer/startup.sh"]
          ## 容器啟動時的一些掛載信息
          resources:
          volumeMounts:
            - name: localtime
              mountPath: /etc/localtime
              省略......
          ## 容器啟動時一些環境變量的設置    
          env:
            - name: SERVER_PORT
              value: "80"
            - name: TZ
              value: "Asia/Shanghai"
            - name: LANG
              value: "en_US.UTF-8"
              省略....
          ports:
            - name: http
            # 容器啟動時的端口
              containerPort: "80"
            # 采用的協議
              protocol: TCP
         ## 創建第二個容器
        - name: nginx
          image: "{{ .Values.image.nginxRepository }}:{{ .Values.image.nginxTag }}"
          imagePullPolicy: "IfNotPresent"
          ports:
            - name: nginx
              containerPort: {{ .Values.containerPort.nginx }}
              protocol: TCP
          env:
            - name: TZ
              value: "Asia/Shanghai"
            - name: LANG
              value: "en_US.UTF-8"
          resources:
          command: [ "/bin/sh", "-c", "nginx && tail -f /dev/null" ]
      volumes: 
        - name: localtime
          hostPath:
            path: /etc/localtime
        - name: timezone
          hostPath:
            path: /etc/timezone
         省略...
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}

以上模板其實就是創建一個deployment控制器,在containers有兩個容器,一個是file-server,另一個是nginx。分別給它們掛載了一些信息和初始化一些環境變量。然后指定啟動容器時的指令。

二、運行并查看Pod信息

如果只是使用k8s運行,那么就只需要使用命令kubectl create 文件名即可,如果采用helm指令,那么采用helm install 文件名。
(1)查看Pod信息
kubectl get pod -n nameSpace,可以看到此時Pod在REDAY狀態的已經有兩個,也就是file-server和nginx容器。

(2)查看指定pod詳細信息
kubectl describe pod podName -n nameSpace,比如我要查看rtm-dashboard-file-server-5c89974c9c-2mlrj的信息,我可以使用指令:kubectl describe pod rtm-dashboard-file-server-5c89974c9c-2mlrj -n shared
在這里插入圖片描述

三、訪問指定Pod中的某一個容器

絕大數情況下,一個pod應該只創建一個類型的容器,但是也有偶然情況。就好上面模板在一個pod中創建了兩種容器,一個叫file-server,一個叫nginx,如果使用 kubectl exec -it podName -n nameSpace bash`命令進入容器,那么k8s會默認進入第一個容器。但是如果我想進入第二個容器怎么辦呢?
使用指令:kubectl exec -it pod/podName -c containerName -n nameSpace bash
以剛剛創建的Pod為例,創建了兩個容器,分別叫file-server和nginx。
(1)通過kubectl get pod -n nameSpace查看pod名字,看到Pod的名字叫rtm-dashboard-file-server-5c89974c9c-2mlrj
在這里插入圖片描述
(2) 進入nginx容器
kubectl exec -it pod/rtm-dashboard-file-server-5c89974c9c-2mlrj -c nginx -n shared bash,可以查到已經進入了nginx這個容器。
在這里插入圖片描述

(3) 進入file-server容器
kubectl exec -it pod/rtm-dashboard-file-server-5c89974c9c-2mlrj -c file-server -n shared bash

原文鏈接:https://blog.csdn.net/qq_43600166/article/details/124544895

欄目分類
最近更新