前言

在pod中,可以存在多个不同的容器,其各容器共享网络、存储,实现对pod的管理

对pod中多个容器的管理
部署pod示例
]# cat > zookeeper-statefulset.yaml <<-EOF
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zookeeper
  namespace: cex-devops
spec:
  serviceName: zookeeper
  replicas: 1
  selector:
    matchLabels:
      app: zookeeper
  template:
    metadata:
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9141"        
      labels:
        app: zookeeper
    spec:
      containers:       
        - name: zookeeper
          image: zookeeper:3.6
          imagePullPolicy: Always
          ports:
            - containerPort: 2181
          resources:
            requests:
              cpu: "500m"
              memory: "500M"
            #limits:
            #  cpu: "1000m"
            #  memory: "1000M"
          livenessProbe:
            tcpSocket:
              port: 2181
            initialDelaySeconds: 120
            periodSeconds: 60
          readinessProbe:
            tcpSocket:
              port: 2181
            initialDelaySeconds: 5
            periodSeconds: 5
          volumeMounts:
            - name: timezone
              mountPath: /etc/timezone
              subPath: timezone
              readOnly: true
            - name: zoneinfo
              mountPath: /etc/localtime
              readOnly: true
            - name: zookeeper-data
              mountPath: /data
            - name: zookeeper-datalog
              mountPath: /datalog       
        - name: zookeeper-exporter
          image: dabealu/zookeeper-exporter
          imagePullPolicy: Always
          ports:
          - containerPort: 9141       
          command:
            - /usr/local/bin/zookeeper-exporter
          args:
            - --zk-hosts=127.0.0.1:2181
          volumeMounts:
          - name: timezone
            mountPath: /etc/timezone
            subPath: timezone
            readOnly: true
          - name: zoneinfo
            mountPath: /etc/localtime
            readOnly: true                                  
      volumes:
        - name: timezone
          configMap:
            name: timezone
        - name: zoneinfo
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
        - name: zookeeper-data
          persistentVolumeClaim:
            claimName: local-devops-zookeeper
        - name: zookeeper-datalog
          persistentVolumeClaim:
            claimName: local-devops-zookeeper-datalog
EOF
]# kubectl apply -f zookeeper-statefulset.yaml
进入容器
  • 命令
    kubectl exec -it pod/po名称 /bin/sh或者/bin/bash或者bash或者sh -n 命名空间 # 默认进入第一个容器
    kubectl exec -it pod/po名称 -c 容器名称 /bin/sh或者/bin/bash或者bash或者sh -n 命名空间 # 进入到指定容器

  • 示例
    默认进入第一个容器

]# kubectl exec -it pod/zookeeper-0 /bin/sh -n cex-devops
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulting container name to zookeeper.
Use 'kubectl describe pod/zookeeper-0 -n cex-devops' to see all of the containers in this pod.
# 

进入到指定容器

]# kubectl exec -it pod/zookeeper-0 -c zookeeper /bin/sh -n cex-devops
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# 
结语

… …

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐