展开

基本命令

最后发布时间 : 2023-12-24 22:06:49 浏览量 :

学习资料

kubectl run testapp --image=ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v1

kubectl create namespace <namespace-name>
kubectl cluster-info
kubectl --server=https://192.168.58.2:8443   get nodes

生信小木屋

kubectl get nodes
kubectl get pod

Pod

创建文件pod.yaml,写入下面内容

apiVersion: v1
kind: Pod
metadata:
  name: test-k8s
spec:
  # 定义容器,可以多个
  containers:
    - name: test-k8s # 容器名字
      image: ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v1 # 镜像
kubectl apply -f pod.yaml
kubectl get pod
NAME             READY   STATUS              RESTARTS      AGE
nginx            0/1     ContainerCreating   0             8m18s
test-k8s         1/1     Running             0             36s
testapp          1/1     Running             3 (21m ago)   21d

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  # 部署名字
  name: test-k8s
spec:
  replicas: 5
  # 用来查找关联的 Pod,所有标签都匹配才行
  selector:
    matchLabels:
      app: test-k8s
  # 定义 Pod 相关数据
  template:
    metadata:
      labels:
        app: test-k8s
    spec:
      # 定义容器,可以多个
      containers:
        - name: test-k8s # 容器名字
          image: ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v1 # 镜像
kubectl get pod -o  wide
NAME                        READY   STATUS              RESTARTS      AGE     IP            NODE       NOMINATED NODE   READINESS GATES
nginx                       0/1     ContainerCreating   0             11m     <none>        minikube   <none>           <none>
slurm-nextflow              0/1     ContainerCreating   0             12m     <none>        minikube   <none>           <none>
test-k8s                    1/1     Running             0             3m37s   10.244.0.21   minikube   <none>           <none>
test-k8s-6c97f5c8d5-22bkk   1/1     Running             0             2m2s    10.244.0.24   minikube   <none>           <none>
test-k8s-6c97f5c8d5-7bwv8   1/1     Running             0             2m2s    10.244.0.26   minikube   <none>           <none>
test-k8s-6c97f5c8d5-bpzft   1/1     Running             0             2m2s    10.244.0.25   minikube   <none>           <none>
test-k8s-6c97f5c8d5-jc4ll   1/1     Running             0             2m1s    10.244.0.22   minikube   <none>           <none>
test-k8s-6c97f5c8d5-nzm2m   1/1     Running             0             2m1s    10.244.0.23   minikube   <none>           <none>
testapp                     1/1     Running             3 (24m ago)   21d     10.244.0.17   minikube   <none>           <none>
testapp1                    1/1     Running             0             22m     10.244.0.18   minikube   <none>           <none>
 kubectl describe pod test-k8s-6c97f5c8d5-22bkk
kubectl logs slurm-nextflow -f
kubectl exec -it  slurm-nextflow  -- bash
 kubectl  scale deployment test-k8s --replicas=10
kubectl port-forward test-k8s-6c97f5c8d5-nzm2m  8080:8080
kubectl logs  test-k8s-6c97f5c8d5-nzm2m -f
# 查看历史
kubectl rollout history deployment test-k8s
# 回到上个版本
kubectl rollout undo deployment test-k8s
# 回到指定版本
kubectl rollout undo deployment test-k8s --to-revision=2
# 删除部署
kubectl delete deployment test-k8s
# 查看全部
kubectl get all
# 重新部署
kubectl rollout restart deployment test-k8s
# 命令修改镜像,--record 表示把这个命令记录到操作历史中
kubectl set image deployment test-k8s test-k8s=ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v2-with-error --record
# 暂停运行,暂停后,对 deployment 的修改不会立刻生效,恢复后才应用设置
kubectl rollout pause deployment test-k8s
# 恢复
kubectl rollout resume deployment test-k8s
# 输出到文件
kubectl get deployment test-k8s -o yaml >> app2.yaml
# 删除全部资源
kubectl delete all --all

将 Pod 指定到某个节点运行:nodeselector
限定 CPU、内存总量:文档

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    disktype: ssd

工作负载分类
Deployment

  • 适合无状态应用,所有pod等价,可替代
    StatefulSet
  • 有状态的应用,适合数据库这种类型。
    DaemonSet
  • 在每个节点上跑一个 Pod,可以用来做节点监控、节点日志收集等
    Job & CronJob
  • Job 用来表达的是一次性的任务,而 CronJob 会根据其时间规划反复运行。

文档

现存问题

  • 每次只能访问一个 pod,没有负载均衡自动转发到不同 pod
  • 访问还需要端口转发
  • Pod 重创后 IP 变了,名字也变了