【k8s】3-kubectl 命令以及 yaml文件学习

Kubectl apiserver 管理对象命令

  Pod service replicaset deployment statefulet daemonset job cronjob node

Kubectl get cs 查看信息等 

 

命令启动pod

Kubectl run -> create a deployment or job manage the created container

Kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80   普通pod

Kubectl run pi --schedule=”0/5 * * * ?” --image=perl  job控制器   

 

# 查看信息

kubectl get svc

kubectl get pods

kubectl get deployments

kubectl describe pods nginx-deploy   # 列出所有pod详情

kubectl describe deployment nginx-deploy  # 列出deployment控制器详情

 

# 暴露端口 创建svc

expose deployment nginx-deploy --name=nginx-deploy --port=80 --protocol=TCP --target-port=80

 

# 修改副本集 数量为5

kubectl scale --replicas=5 deployment nginx-deploy 

 

# 滚动更新

kubectl set image deployment nginx-deploy nginx=nginx:1.5

  

# 查看滚动更新情况

kubectl rollout status deployment nginx-deploy

 

# 修改对外访问

# 手动在外部做负载均衡器到内部端口

kubectl edit svc nginx-deploy 

Type: ClusterIP ---> Nodeport

kubectl get svc

Curl nodeip:随机端口 

 

 

yaml文件的学习

配置清单

 

# 查看pod配置清单 以yaml文件输出

kubectl get pods nginx-deploy-5bf87f5f59-bgzl7 -o yaml

 

# 大部分资源配置清单

Apiversiongroup/versi on   # Kubectl api-versions 所属群组

kind: 资源类别

metadata:元数据

  Name:名称唯一

  Namespace:名称空间

  Labels:标签

  Annotations

  SelfLink: /api/GROUP/VERSION/namespaces/NAMESPACE_NAME/TYPE/NAME

spec: 期望状态 disired state

status: 让当前状态 current state 本字段由kubernetes集群维护

 

kubectl explain pods 内嵌说明

Kubectl explain pods.spec 

Yaml文件
# 创建
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:    
  containers:
  - name: myapp
    image: nginx
  - name: busybox
    image: busybox:latest
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 3500"

kubectl create -f pod-demo.yaml  # 创建pod 

kubectl exec -it pod-demo -c myapp -- /bin/sh    # 进入pod

Kubectl delete -f pod-demo.yaml  # 删除pod

 

 Pod资源:

 

spec.containers <[]object>
- name <string> Image <string> imagePullPolicy <string> # Always拉取最新的,Never 本地没有不拉取,if本地没有再拉取 Always, Never 镜像中默认的应用: command,args
标签:
   key=value
   Key: 字母 数字 _ 开头、 Value: 可为空 字母数字 

       标记标签

Kubectl label pods pod-demo release=canary

修改标签

Kubectl label pods pod-demo release=stable --overwrite

选择标签

Kubectl get pods -l release

标签选择器

等值关系 Kubectl get pods -l release=stable,app=myapp

集合关系 -l “release notin (canary,beta,alpha)”

许多资源支持内嵌字段

matchLabels: 直接给定键值

matchExpressions: 基于给定的表达式来使用选择器

In,Notin:values

                 Exists,NotExists:values指必须为空列表

nodeSelector <map[string]string>

节点标签选择器

kebectl label nodes node_name disktype=ssd

Cat >>nginx-deploy.yaml<< EOF

nodeSelector:

Disktype: ssd

EOF

Annotations:

label不同的地方在于不能用于挑选资源对象 仅用于为对象提供元数据

 

Pod生命周期

状态:pending 已经创建 但是没有适合配置的节点

    Running 运行状态

                  Failed 失败......

创建Pod

Pod生命周期中重要行为:

初始化容器

容器探测

Liveness

Readiness

         RestartPolicy:

         Always, OnFailure, Never 

    探针类型三种

         ExecActionTcpsocketActionHttpGetAction

 

# kubectl explain pod.spec.containers.livenessProbe

HTTPGET 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
  - name: liveness-httpget-container
    image: ikubernetes/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    livenessProbe:
      httpGet:
        port: 80
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
文件检查 
EXEC
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-container
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","touch /tmp/healthy; slepp 30; rm -rf /tmp/healthy; sleep 3600"]
    livenessProbe:
      exec:
        command: ["test","-e","/tmp/healthy"]
  #restartPolicy:
      initialDelaySeconds: 1
      periodSeconds: 3

#  pods yaml回顾

 apiVersion, kind, metadata, spec statu(只读)

Spec:

containers

nodeSelector

nodeName

restartPolicy:

Always,Never,OnFailure

containers:

name

image

imagePullPolicy

Ports:

name

containerPort

livenessProbe

readinessProbe

liftcycle

ExecAction: exec

TCPSocketAction: tcpSocket

HTTPGetAction: httpGet

 

Pod控制器

ReplicaSet: 控制副本数量 支持滚动更新

Deployment: 工作在ReplicaSet 滚动更新回滚 声明

Daemon: 每个节点只运行一个POD副本 系统级守护进程

Job:一次性任务

Cronjob: 周期性JOB

StatefulSet:  有状态服务

 

kubectl edit rs myapp

Kubectl explain deploy

# 打补丁 更新

kubectl apply -f deploy-demo.yaml

Kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3

Kubectl path deployment myapp-deploy -p ‘{“spec”:{“strategy”:”{“rollingUpdate”:{“maxsurge”:1,”maxUnavailable”:0}}}}’

 

# 回滚

kubectl get deployment -o wide

kubectl rollout history deployment

rollout undo deployment myapp-deploy --to-revision=1

 

 

  

 

 

posted @ 2020-04-27 18:10  阿里云的奥斯卡  阅读(2273)  评论(0编辑  收藏  举报