Kubernetes入门实验:pod

1,041 阅读2分钟

k8s pod 实验。
注:本文为笔者实验记录,非教程,另会不定时更新。

环境

# kubectl get node
NAME              STATUS     ROLES    AGE   VERSION
edge-node         Ready      <none>   15m   v1.17.0
edge-node2        Ready      <none>   16m   v1.17.0
ubuntu            Ready      master   67d   v1.17.0

pod

pod运行于Node节点上,若干相关容器的组合。Pod内包含的容器运行在同一宿主机上,使用相同的网络命名空间、IP地址和端口,能够通过localhost进行通。Pod是Kurbernetes进行创建、调度和管理的最小单位,它提供了比容器更高层次的抽象,使得部署和管理更加灵活。一个Pod可以包含一个容器或者多个相关容器。

技术总结

创建yaml文件,指定数量。
测试扩容、缩容。 测试滚动升级。

测试yaml文件

# vim busybox-pod.yaml

apiVersion: v1
kind: Pod # 声明pod
metadata:
  name: busybox-pod
spec:
  containers:
  - name: busybox-pod
    image: latelee/busybox # 镜像名称,真实存在
    imagePullPolicy: IfNotPresent
    command:
    - sleep
    - "3600"

创建:

kubectl create -f busybox-pod.yaml 

查看:

# kubectl get pod
busybox-pod      1/1     Running   0          9s

另外有命令:

kubectl get pod -o wide # 有pod运行的节点
kubectl get pod -o yaml # 更详细的信息
kubectl get pod -l app=busybox # 标签配套

停止:

kubectl delete -f busybox-pod.yaml 

多个pod

示例如下,多了一个容器,并添加环境变量:

apiVersion: v1
kind: Pod # 声明pod
metadata:
  name: busybox-pod
spec:
  containers:
  - name: busybox
    image: latelee/busybox # 镜像名称,真实存在
    imagePullPolicy: IfNotPresent
    env:
    - name: NAME
      value: 'hello'
    command:
    - sleep
    - "3600"

  - name: busybox11
    image: latelee/busybox:1.1
    imagePullPolicy: IfNotPresent
    env:
    - name: NAME
      value: 'world'
    command:
    - sleep
    - "3600"

查看日志:

kubectl logs busybox-pod busybox

注:busybox-pod 为 pod 名称,busybox 为容器名称。(此处容器无输出)

执行某个容器命令:

# kubectl exec -it busybox-pod -c busybox cat version
# kubectl exec -it busybox-pod -c busybox11 cat version

另:

# kubectl exec -it busybox-pod -c busybox sh // 进入容器
/ # env  // 查看环境变量
输出:
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=busybox-pod
HOME=/root
NAME=hello  #!!! 自定义的
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1

共享卷

apiVersion: v1
kind: Pod
metadata:
  name: busybox-pod
  labels:
     test: busybox
spec:
  containers:
  # 第一个容器
  - name: hello-write
    image: latelee/busybox
    command: ["sh","-c","for i in {1..10};do echo $i >> /data/hello;sleep 1;done;sleep 5700"]
  # 第二个容器
  - name: hello-read
    image: latelee/busybox
    command: ["sh","-c","for i in {1..10};do cat $i >> /data/hello;sleep 1;done;sleep 5700"]
    volumeMounts:
      - name: data
        mountPath: /data
  # 数据卷
  volumes:
  - name: data
    hostPath:
      path: /data

端口映射

一个容器,多个端口:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: latelee/lidch
    ports:
    - name: http
      containerPort: 80
      hostIP: 0.0.0.0
      hostPort: 80
      protocol: TCP
    - name: https
      containerPort: 443
      hostIP: 0.0.0.0
      hostPort: 443
      protocol: TCP