Saul's blog Saul's blog
首页
后端
分布式
前端
更多
分类
标签
归档
友情链接
关于
GitHub (opens new window)

Saul.J.Wu

立身之本,不在高低。
首页
后端
分布式
前端
更多
分类
标签
归档
友情链接
关于
GitHub (opens new window)
  • 安全框架(认证、授权)

  • 分布式事务

  • 消息队列

  • K8S

    • K8S搭建
    • K8s加入集群报错
    • K8s新版本搭建
    • K8s入门
      • 1、安装Tomcat
        • 1、部署一个tomcat(主节点部署)
        • 2、暴露Nginx访问
        • 3、动态扩容测试
      • 2、yaml基本使用
        • 1、K8S 创建资源的方式
        • 2、文件模板
    • K8S核心概念
    • kubesphere
    • kubesphere 定制化安装及建立多租户系统
    • PV和PVC
    • 创建WordPress发布到K8s
    • K8S之DevOps
    • K8S之DevOps最佳实践
  • 分布式
  • K8S
SaulJWu
2021-08-07

K8s入门

# 1、安装Tomcat

# 1、部署一个tomcat(主节点部署)

# 创建一个部署,名字为tomcat6,使用的镜像是--image=tomcat:6.0.53-jre8
kubectl create deployment tomcat6  --image=tomcat:6.0.53-jre8
1
2

打印结果:

deployment.apps/tomcat6 created
1
# 获取所有资源
kubectl get all
1
2

打印结果:

image-20210807165903286

  • pod
    • tomcat6,带上唯一标识,ready代表已经准备好
  • deployment
    • 部署,但是ready代表还没成功
  • replicaset
    • 复制一份,但是ready为0,代表还没成功
# 可以获取到Tomcat信息(Tomcat具体部署在哪个节点)
kubectl get pods -o wide  
1
2

打印结果:

image-20210807162616364

可以看到Tomcat已经部署在 k8s-node2 节点上了。在 k8s-node2 服务器查看镜像,在主节点安装的,已经在从节点上安装好了。

如果降 node2的Tomcat停掉,则主节点node3从 node1 拉起一份镜像,启动,这个就是k8s的容灾恢复,不需要人为去干涉。

# 2、暴露Nginx访问

 # 主节点执行Pod 的 80 映射器的8080;service会代理 Pod 的80
kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort 
1
2
  • --type=NodePort 作为一个节点暴露出去,不指定端口的时候会随机端口

底层其实是docker端口8080,外面一层是pod:8080,外面一层是service,端口随机。

查看:

[root@k8s-node1 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        24d
tomcat6      NodePort    10.96.223.184   <none>        80:31784/TCP   21s
[root@k8s-node1 ~]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        24d   <none>
tomcat6      NodePort    10.96.223.184   <none>        80:31784/TCP   39s   app=tomcat6
1
2
3
4
5
6
7
8

可以看到,tomcat6的service暴露在了 32711 这个端口,然后可以通过 http://192.168.31.21:31784/ 访问。

image-20210807162951413

# 3、动态扩容测试

kubectl get deployment
1

应用升级:kubectl set image (--help 查看帮助) 扩容:kubectl scale --replicas=3 deployment tomcat6 扩容了多份,所有无论访问哪个node的指定端口,都可以访问到 tocat6

扩容:

kubectl scale --replicas=3 deployment tomcat6
1

打印:

deployment.apps/tomcat6 scaled
1

查看:

[root@k8s-node1 ~]# kubectl get pods -o wide
NAME                       READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
tomcat6-5f7ccf4cb9-9jr8z   1/1     Running   0          52s     10.244.1.45    k8s-node2   <none>           <none>
tomcat6-5f7ccf4cb9-xdhh2   1/1     Running   0          7m19s   10.244.2.110   k8s-node3   <none>           <none>
tomcat6-5f7ccf4cb9-xk4ms   1/1     Running   0          52s     10.244.1.44    k8s-node2   <none>           <none>
1
2
3
4
5
[root@k8s-node1 ~]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        24d   <none>
tomcat6      NodePort    10.96.223.184   <none>        80:31784/TCP   40m   app=tomcat6
1
2
3
4

无论哪个node,只要用31784端口都可以访问到tomat6。

缩容:

kubectl scale --replicas=1 deployment tomcat6
1

它就随机缩容剩下一个程序。

创建deployment会管理replicas,replicas控制pod数量,有pod故障会自动拉起新的pod。

# 4、删除部署

删除之前查看一下

[root@k8s-node1 ~]# kubectl get all
NAME                           READY   STATUS    RESTARTS   AGE
pod/tomcat6-5f7ccf4cb9-xdhh2   1/1     Running   0          11m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        24d
service/tomcat6      NodePort    10.96.223.184   <none>        80:31784/TCP   44m

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/tomcat6   1/1     1            1           47m

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/tomcat6-5f7ccf4cb9   1         1         1       47m
kubectl delete deployment.apps/tomcat6 
1
2
3
4
5
6
7
8
9
10
11
12
13
14

执行删除

[root@k8s-node1 ~]# kubectl delete deployment.apps/tomcat6 
deployment.apps "tomcat6" deleted
1
2

删除完查看一下

[root@k8s-node1 ~]# kubectl get all
NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        24d
service/tomcat6      NodePort    10.96.223.184   <none>        80:31784/TCP   46m
1
2
3
4

service还在,查看一下pod还在不在

[root@k8s-node1 ~]# kubectl get pods
No resources found in default namespace.
1
2

pod已经不在了,就算service还在也没什么用,service也可以删除

[root@k8s-node1 ~]# kubectl delete service/tomcat6 
service "tomcat6" deleted
1
2

删除后,再查看一下所有资源

[root@k8s-node1 ~]# kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   24d
1
2
3

只剩下这个安全连接端口。

# 2、yaml基本使用

# 1、K8S 创建资源的方式

K8S有两种创建资源的方式:kubectl 命令和 yaml 配置文件。

kubectl命令行:最为简单,一条命令就OK,但缺点也很明显,你并不知道这条命令背后到底做了哪些事! yaml配置文件:提供了一种让你知其然更知其所以然的方式。优势如下:

  • 完整性:配置文件描述了一个资源的完整状态,可以很清楚地知道一个资源的创建背后究竟做了哪些事;
  • 灵活性:配置文件可以创建比命令行更复杂的结构;
  • 可维护性:配置文件提供了创建资源对象的模板,能够重复使用;
  • 可扩展性:适合跨环境、规模化的部署。

# 2、文件模板

yml模板:

image-20210807172035707

跟k8s打交道,这个yml必须非常熟悉,特别是可视化界面,自动化运维,都是要用yml的。

yaml 是一种用来写配置文件的语言,没错,它是一门语言。如果你用过 json,那么对它就不会陌生,yaml 又被称为是 json 的超集,使用起来比 json 更方便。

结构上它有两种可选的类型:Lists 和 Maps。List 用 -(破折号) 来定义每一项,Map 则是一个 key:value 的键值对来表示。

# yaml格式的pod定义文件完整内容:

apiVersion: v1       #必选,版本号,例如v1
kind: Pod       #必选,Pod
metadata:       #必选,元数据
  name: string       #必选,Pod名称
  namespace: string    #必选,Pod所属的命名空间
  labels:      #自定义标签
    - name: string     #自定义标签名字
  annotations:       #自定义注释列表
    - name: string
spec:         #必选,Pod中容器的详细定义
  containers:      #必选,Pod中容器列表
  - name: string     #必选,容器名称
    image: string    #必选,容器的镜像名称
    imagePullPolicy: [Always | Never | IfNotPresent] #获取镜像的策略 Alawys表示下载镜像 IfnotPresent表示优先使用本地镜像,否则下载镜像,Nerver表示仅使用本地镜像
    command: [string]    #容器的启动命令列表,如不指定,使用打包时使用的启动命令
    args: [string]     #容器的启动命令参数列表
    workingDir: string     #容器的工作目录
    volumeMounts:    #挂载到容器内部的存储卷配置
    - name: string     #引用pod定义的共享存储卷的名称,需用volumes[]部分定义的的卷名
      mountPath: string    #存储卷在容器内mount的绝对路径,应少于512字符
      readOnly: boolean    #是否为只读模式
    ports:       #需要暴露的端口库号列表
    - name: string     #端口号名称
      containerPort: int   #容器需要监听的端口号
      hostPort: int    #容器所在主机需要监听的端口号,默认与Container相同
      protocol: string     #端口协议,支持TCP和UDP,默认TCP
    env:       #容器运行前需设置的环境变量列表
    - name: string     #环境变量名称
      value: string    #环境变量的值
    resources:       #资源限制和请求的设置
      limits:      #资源限制的设置
        cpu: string    #Cpu的限制,单位为core数,将用于docker run --cpu-shares参数
        memory: string     #内存限制,单位可以为Mib/Gib,将用于docker run --memory参数
      requests:      #资源请求的设置
        cpu: string    #Cpu请求,容器启动的初始可用数量
        memory: string     #内存清楚,容器启动的初始可用数量
    livenessProbe:     #对Pod内个容器健康检查的设置,当探测无响应几次后将自动重启该容器,检查方法有exec、httpGet和tcpSocket,对一个容器只需设置其中一种方法即可
      exec:      #对Pod容器内检查方式设置为exec方式
        command: [string]  #exec方式需要制定的命令或脚本
      httpGet:       #对Pod内个容器健康检查方法设置为HttpGet,需要制定Path、port
        path: string
        port: number
        host: string
        scheme: string
        HttpHeaders:
        - name: string
          value: string
      tcpSocket:     #对Pod内个容器健康检查方式设置为tcpSocket方式
         port: number
       initialDelaySeconds: 0  #容器启动完成后首次探测的时间,单位为秒
       timeoutSeconds: 0   #对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
       periodSeconds: 0    #对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
       successThreshold: 0
       failureThreshold: 0
       securityContext:
         privileged:false
    restartPolicy: [Always | Never | OnFailure]#Pod的重启策略,Always表示一旦不管以何种方式终止运行,kubelet都将重启,OnFailure表示只有Pod以非0退出码退出才重启,Nerver表示不再重启该Pod
    nodeSelector: obeject  #设置NodeSelector表示将该Pod调度到包含这个label的node上,以key:value的格式指定
    imagePullSecrets:    #Pull镜像时使用的secret名称,以key:secretkey格式指定
    - name: string
    hostNetwork:false      #是否使用主机网络模式,默认为false,如果设置为true,表示使用宿主机网络
    volumes:       #在该pod上定义共享存储卷列表
    - name: string     #共享存储卷名称 (volumes类型有很多种)
      emptyDir: {}     #类型为emtyDir的存储卷,与Pod同生命周期的一个临时目录。为空值
      hostPath: string     #类型为hostPath的存储卷,表示挂载Pod所在宿主机的目录
        path: string     #Pod所在宿主机的目录,将被用于同期中mount的目录
      secret:      #类型为secret的存储卷,挂载集群与定义的secre对象到容器内部
        scretname: string  
        items:     
        - key: string
          path: string
      configMap:     #类型为configMap的存储卷,挂载预定义的configMap对象到容器内部
        name: string
        items:
        - key: string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

# yaml实例文件

apiVersion: extensions/v1beta1   #接口版本
kind: Deployment                 #接口类型
metadata:
  name: ptengine-demo               #Deployment名称
  namespace: ptengine-prd           #namespace 名称
  labels:
    app: ptengine-demo              #标签
spec:
  replicas: 3
   strategy:
    rollingUpdate:  ##由于replicas为3,则整个升级,pod个数在2-4个之间
      maxSurge: 1       #滚动升级时会先启动1个pod
      maxUnavailable: 1 #滚动升级时允许的最大Unavailable的pod个数
  template:         
    metadata:
      labels:
        app: ptengine-demo  #模板名称必填
    sepc: #定义容器模板,该模板可以包含多个容器
      containers:                                                                   
        - name: ptengine-demo                                                           #镜像名称
          image: reg.pt1.com/ptengine-prd/ptengine-demo:0.0.1-SNAPSHOT #镜像地址
          CMD: [ "/bin/sh","-c","cat /etc/config/path/to/special-key" ]    #启动CMD
          args:                                                                #启动参数
            - '-storage.local.retention=$(STORAGE_RETENTION)'
。。。。。。。。
            - '-web.external-url=$(EXTERNAL_URL)'

          imagePullPolicy: IfNotPresent  #如果不存在则拉取
          livenessProbe:       #表示container是否处于live状态。如果LivenessProbe失败,LivenessProbe将会通知kubelet对应的container不健康了。随后kubelet将kill掉container,并根据RestarPolicy进行进一步的操作。默认情况下LivenessProbe在第一次检测之前初始化值为Success,如果container没有提供LivenessProbe,则也认为是Success;
            httpGet:
              path: /health #如果没有心跳检测接口就为/
              port: 8080
              scheme: HTTP
            initialDelaySeconds: 60 ##启动后延时多久开始运行检测
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 5
            readinessProbe:
          readinessProbe:
            httpGet:
              path: /health #如果没有健康检测接口就为/
              port: 8080
              scheme: HTTP
            initialDelaySeconds: 30 ##启动后延时多久开始运行检测
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 5
          resources:              ##CPU内存限制
            requests:
              cpu: 2
              memory: 2048Mi
            limits:
              cpu: 2
              memory: 2048Mi
          env:                    ##通过环境变量的方式,直接传递pod=自定义Linux OS环境变量
            - name: LOCAL_KEY     #本地Key
              value: value
            - name: CONFIG_MAP_KEY  #local策略可使用configMap的配置Key,
              valueFrom:
                configMapKeyRef:
                  name: special-config   #configmap中找到name为special-config
                  key: special.type      #找到name为special-config里data下的key
          ports:
            - name: http
              containerPort: 8080 #对service暴露端口
          volumeMounts:     #挂载volumes中定义的磁盘
          - name: log-cache
            mount: /tmp/log
          - name: sdb       #普通用法,该卷跟随容器销毁,挂载一个目录
            mountPath: /data/media    
          - name: nfs-client-root    #直接挂载硬盘方法,如挂载下面的nfs目录到/mnt/nfs
            mountPath: /mnt/nfs
          - name: example-volume-config  #高级用法第1种,将ConfigMap的log-script,backup-script分别挂载到/etc/config目录下的一个相对路径path/to/...下,如果存在同名文件,直接覆盖。
            mountPath: /etc/config       
          - name: rbd-pvc                #高级用法第2中,挂载PVC(PresistentVolumeClaim)

#使用volume将ConfigMap作为文件或目录直接挂载,其中每一个key-value键值对都会生成一个文件,key为文件名,value为内容,
  volumes:  # 定义磁盘给上面volumeMounts挂载
  - name: log-cache
    emptyDir: {}
  - name: sdb  #挂载宿主机上面的目录
    hostPath:
      path: /any/path/it/will/be/replaced
  - name: example-volume-config  # 供ConfigMap文件内容到指定路径使用
    configMap:
      name: example-volume-config  #ConfigMap中名称
      items:
      - key: log-script           #ConfigMap中的Key
        path: path/to/log-script  #指定目录下的一个相对路径path/to/log-script
      - key: backup-script        #ConfigMap中的Key
        path: path/to/backup-script  #指定目录下的一个相对路径path/to/backup-script
  - name: nfs-client-root         #供挂载NFS存储类型
    nfs:
      server: 10.42.0.55          #NFS服务器地址
      path: /opt/public           #showmount -e 看一下路径
  - name: rbd-pvc                 #挂载PVC磁盘
    persistentVolumeClaim:
      claimName: rbd-pvc1         #挂载已经申请的pvc磁盘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

# 查看yml示例

上面我们命令的方式部署了一个tomcat,其实可以还用来查看的

# 查看帮助
[root@k8s-node1 ~]# kubectl create deployment tomcat6  --image=tomcat:6.0.53-jre8 --help
Create a deployment with the specified name.

Aliases:
deployment, deploy

Examples:
  # Create a new deployment named my-dep that runs the busybox image.
  kubectl create deployment my-dep --image=busybox

Options:
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
      --dry-run=false: If true, only print the object that would be sent, without sending it.
      --generator='': The name of the API generator to use.
      --image=[]: Image name to run.
  -o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
      --save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the
annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
      --template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
      --validate=true: If true, use a schema to validate the input before sending it

Usage:
  kubectl create deployment NAME --image=image [--dry-run] [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

看到的一个--dry run可以用来测试

[root@k8s-node1 ~]# kubectl create deployment tomcat6  --image=tomcat:6.0.53-jre8 --dry-run -o yaml
apiVersion: apps/v1
kind: Deployment #类型是部署
metadata:
  creationTimestamp: null
  labels:
    app: tomcat6
  name: tomcat6
spec:
  replicas: 1 #默认复制一份
  selector:
    matchLabels:
      app: tomcat6
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: tomcat6
    spec:
      containers:
      - image: tomcat:6.0.53-jre8 # 容器
        name: tomcat
        resources: {}
status: {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

这就是tomcat6的yaml文件,上边输出的文件就是我们要部署的Tomcat6文件的yaml详细信息,或者也可以将上边的打印输出到yaml文件,然后对这个文件进行修改,执行操作:

[root@k8s-node1 ~]# kubectl create deployment tomcat6  --image=tomcat:6.0.53-jre8 --dry-run -o yaml > tomcat6.yaml
[root@k8s-node1 ~]# ls
anaconda-ks.cfg             demo-openebs-hostpath.yaml  helm-v2.16.12-linux-amd64.tar.gz  kube-flannel.yml           kubesphere-mini.yaml  master_images.sh     original-ks.cfg
cluster-configuration.yaml  get_helm.sh                 helm_rbac.yaml                    kubesphere-installer.yaml  linux-amd64           operator-1.5.0.yaml  tomcat6.yaml
1
2
3
4
vi tomcat6.yaml
1

退出插入模式,按ESC,然后dd就会删除光标所在行,把所有空行和空对象全部删除

修改好后

# 运行yaml

[root@k8s-node1 ~]# kubectl apply -f tomcat6.yaml 
deployment.apps/tomcat6 created
1
2
[root@k8s-node1 ~]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
tomcat6-5f7ccf4cb9-9866p   1/1     Running   0          15s
tomcat6-5f7ccf4cb9-9pqxs   1/1     Running   0          15s
tomcat6-5f7ccf4cb9-qdkkp   1/1     Running   0          15s
1
2
3
4
5

发现改3份也能复制三个实例了。

只要有yaml文件,就可以替换上边那种超长的kubectl 命令,所以,熟悉yaml文件非常重要。

# 端口暴露(service):

上边的暴露端口命令我们也可以使用yaml文件来执行。

[root@k8s-node1 ~]# kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort --dry-run -o yaml 
apiVersion: v1
kind: Service #类型是服务
metadata:
  creationTimestamp: null
  labels:
    app: tomcat6
  name: tomcat6
spec:
  ports:
  - port: 80 #pod容器端口
    protocol: TCP
    targetPort: 8080 #docker容器端口
  selector:
    app: tomcat6
  type: NodePort #暴露类型
status:
  loadBalancer: {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

可以看到,kind类型为 service,这里相当于暴露服务。

# 定义pod:

[root@k8s-node1 ~]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
tomcat6-5f7ccf4cb9-9866p   1/1     Running   0          5m17s
tomcat6-5f7ccf4cb9-9pqxs   1/1     Running   0          5m17s
tomcat6-5f7ccf4cb9-qdkkp   1/1     Running   0          5m17s
[root@k8s-node1 ~]# kubectl get pod tomcat6-5f7ccf4cb9-9866p 
NAME                       READY   STATUS    RESTARTS   AGE
tomcat6-5f7ccf4cb9-9866p   1/1     Running   0          5m28s
[root@k8s-node1 ~]# kubectl get pod tomcat6-5f7ccf4cb9-9866p -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-08-07T09:39:50Z"
  generateName: tomcat6-5f7ccf4cb9-
  labels:
    app: tomcat6
    pod-template-hash: 5f7ccf4cb9
  name: tomcat6-5f7ccf4cb9-9866p
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: tomcat6-5f7ccf4cb9
    uid: 83869aa1-603d-44b9-ac51-252e5006a4b9
  resourceVersion: "7596654"
  selfLink: /api/v1/namespaces/default/pods/tomcat6-5f7ccf4cb9-9866p
  uid: ca35be75-951d-44d6-b98e-48a9d71c2089
spec:
  containers:
  - image: tomcat:6.0.53-jre8
    imagePullPolicy: IfNotPresent
    name: tomcat
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-f9gtb
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: k8s-node2
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-f9gtb
    secret:
      defaultMode: 420
      secretName: default-token-f9gtb
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2021-08-07T09:39:50Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2021-08-07T09:39:51Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2021-08-07T09:39:51Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2021-08-07T09:39:50Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://4c19bec35971e0e55ae0f48841103bc86826d5b29afa13e32379ed222b61ce4c
    image: tomcat:6.0.53-jre8
    imageID: docker-pullable://tomcat@sha256:8c643303012290f89c6f6852fa133b7c36ea6fbb8eb8b8c9588a432beb24dc5d
    lastState: {}
    name: tomcat
    ready: true
    restartCount: 0
    started: true
    state:
      running:
        startedAt: "2021-08-07T09:39:50Z"
  hostIP: 10.0.2.5
  phase: Running
  podIP: 10.244.1.46
  podIPs:
  - ip: 10.244.1.46
  qosClass: BestEffort
  startTime: "2021-08-07T09:39:50Z"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

事实上我们不需要填写那么完整的信息,而且一个yaml还可以定义多个pod

vi mypod.yaml
1
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: tomcat6-new
  name: tomcat6-new
  namespace: default
spec:
  containers:
  - image: tomcat:6.0.53-jre8
    imagePullPolicy: IfNotPresent
    name: tomcat6-new
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

运行实例

[root@k8s-node1 ~]# kubectl apply -f mypod.yaml 
pod/tomcat6-new created

# 查看是否创建成功了
[root@k8s-node1 ~]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
tomcat6-5f7ccf4cb9-9866p   1/1     Running   0          18m
tomcat6-5f7ccf4cb9-9pqxs   1/1     Running   0          18m
tomcat6-5f7ccf4cb9-qdkkp   1/1     Running   0          18m
tomcat6-new                2/2     Running   0          29s
1
2
3
4
5
6
7
8
9
10
帮我改善此页面 (opens new window)
上次更新: 2021/08/09, 11:55:32
K8s新版本搭建
K8S核心概念

← K8s新版本搭建 K8S核心概念→

最近更新
01
zabbix学习笔记二
02-28
02
zabbix学习笔记一
02-10
03
Linux访问不了github
12-08
更多文章>
Theme by Vdoing | Copyright © 2020-2022 Saul.J.Wu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式