一、 基本概念
deployment / pod    --  镜像
service             --  网络
configmap           --  配置文件
cronjob             --  定时任务
二、 kubectl 常用命令

  1. kubectl create -f yaml文件名

  2. kubectl delete -f yaml文件名
    kubectl delete deployment/pod/service/configmap/cronjob 名称

  3. kubectl apply -f yaml文件名

    1. 查看列表
      kubectl get deployment/pod/service/configmap/cronjob
      kubectl get deployment/pod/service/configmap/cronjob -o wide
    2. 查看配置过程
      kubectl describe deployment/pod/service/configmap/cronjob 名称
    3. 查看日志
      kubectl logs pod名
      kubectl logs -f pod名
  4. 进入 pod 内部
    kubectl exec -it pod名 -- /bin/bash

  5. 将 pod 内部文件/文件夹复制到主机
    kubectl cp pod名:/path/to/pod/file /path/to/host

三、yaml文件示例
  1. deployment / pod + service
# nginx.yaml
---
# deployment + pod(镜像)
apiVersion: apps/v1
kind: Deployment
metadata:
    name: nginx
spec:
    selector:
        matchLabels:
            app: nginx
    replicas: 1
    template:
        metadata:
            labels:
                app: nginx
        spec:
            # 定义本地路径
            volumes:
                -   name: nginx-data-html
                    hostPath:
                        path: /home/vagrant/k8s/dir/nginx/html
                -   name: nginx-data-conf
                    hostPath:
                        path: /home/vagrant/k8s/dir/nginx/conf
            containers:
                -   name: nginx
                    image: nginx
                    ports:
                        -   containerPort: 80
                    # 定义容器路径
                    volumeMounts:
                        -   name: nginx-data-html
                            mountPath: /usr/share/nginx/html
                        -   name: nginx-data-conf
                            mountPath: /etc/nginx/conf.d
---
# service(网络)
apiVersion: v1
kind: Service
metadata:
    name: nginx
spec:
    selector:
        app: nginx
    # 使用 nodeport 对外暴露端口
    type: NodePort
    ports:
        -   name: nginx
            protocol: TCP
            port: 80
            targetPort: 80
            # 外部可以访问的端口号,范围:30000-32767
            nodePort: 30011
  1. configMap + deployment / pod + service
# mysql.yaml
---
# configMap(配置文件)
apiVersion: v1
kind: ConfigMap
metadata:
    name: mysql-config
    namespace: default
data:
    # 定义配置变量
    username: root
    password: "123456"
---
# deployment + pod(镜像)
apiVersion: apps/v1
kind: Deployment
metadata:
    name: mysql
spec:
    selector:
        matchLabels:
            app: mysql
    replicas: 1
    template:
        metadata:
            labels:
                app: mysql
        spec:
            containers:
                -   name: mysql
                    image: mysql:5.7
                    ports:
                        -   containerPort: 3306
                    env:
                        -   name: MYSQL_ROOT_PASSWORD
                            valueFrom:
                                configMapKeyRef:
                                    # 对应 configMap 名称
                                    name: mysql-config
                                    # 对应 configMap 变量名
                                    key: password
---
# service(网络)
apiVersion: v1
kind: Service
metadata:
    name: mysql
spec:
    selector:
        app: mysql
    # 使用 nodeport 对外暴露端口
    type: NodePort
    ports:
        -   name: mysql
            protocol: TCP
            port: 3306
            targetPort: 3306
            # 外部可以访问的端口号,范围:30000-32767
            nodePort: 30012
  1. cronJob
# cronjob.yaml
# cronJob(定时任务)
apiVersion: batch/v1
kind: CronJob
metadata:
    name: cronjob-test
spec:
    # 分时日月周
    schedule: "*/1 * * * *"
    jobTemplate:
        spec:
            template:
                spec:
                    restartPolicy: OnFailure
                    containers:
                        -   name: cronjob-test
                            image: centos:7
                            args:
                                - "bin/sh"
                                - "-c"
                                - "for i in 1 2 3; do echo $i; done"
文档更新时间: 2021-11-26 10:59   作者:lee