k8s常用指令

 

常用指令

 

 

案例演示1: 通过对web应用的部署,暴露,升级,回滚,扩容来演示kubectl指令的使用

1 部署一个web应用
[root@master kubectl-cmd]# kubectl create deployment java-demo --image=tomcat 
deployment.apps/java-demo created
[root@master kubectl-cmd]# 
[root@master kubectl-cmd]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
java-demo-6bdbd5ffbf-fr8wc                1/1     Running   0          102s
[root@master kubectl-cmd]# 

2 把这个应用暴露出去
[root@master kubectl-cmd]# kubectl expose deployment java-demo  --port=80  --target-port=8080 --name=java-demo --type=NodePort
service/java-demo exposed
[root@master kubectl-cmd]#
[root@master kubectl-cmd]# kubectl  get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
java-demo    NodePort    10.98.211.51   <none>        80:32166/TCP   11s
[root@master kubectl-cmd]# 
命令格式:
 kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name][--name=name][--external-ip=external-ip-of-service] [--type=type] [options] 
命令解析:
      暴露的资源类型是deployment
        deployment的名字是depname-java
        --port=80                 对外暴露集群内容的端口是80
      --target-port=8080 镜像中应用程序提供的端口是8080
    --name=java-demo 暴露的这个service的名字是java-demo
      --type=NodePort   暴露出去的端口类型是NodePort


   3  对这个应用进行升级
通过替换容器的方式,把名称为java-demo的deployment中containers中镜像的名字由tomcat替换成nginx
[root@master kubectl-cmd]# kubectl set image deployment java-demo tomcat=nginx
deployment.apps/java-demo image updated
[root@master kubectl-cmd]# 
查看containters中容器名称的方法:
[root@master kubectl-cmd]# kubectl get deployment java-demo -o yaml
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: java-demo
    spec:
      containers:
      - image: tomcat
        imagePullPolicy: Always
        name: tomcat
注意:
升级之前web是tomcat,默认端口是8080,升级之后web是nginx 默认端口是80,所以需要手动修改这个要修改service.的targetPort,把它修改为80
[root@master kubectl-cmd]# kubectl edit svc java-demo
spec:
  clusterIP: 10.98.211.51
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 32166
    port: 80
    protocol: TCP
    targetPort: 80

4  应用回滚
[root@master kubectl-cmd]# kubectl rollout undo deployment java-demo   #回滚到上一个版本
deployment.apps/java-demo rolled back
[root@master kubectl-cmd]#
[root@master kubectl-cmd]# kubectl get pods
NAME                                      READY   STATUS        RESTARTS   AGE
java-demo-6bdbd5ffbf-75l8f                1/1     Running       0          19s
java-demo-759fc8c66c-qqwxz                1/1     Terminating   0          28m
[root@master kubectl-cmd]# 
[root@master kubectl-cmd]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
java-demo-6bdbd5ffbf-75l8f                1/1     Running   0          86s
[root@master kubectl-cmd]# 
注意:
要把service中端口修改为8080

5  扩容/缩容
[root@master kubectl-cmd]# kubectl scale deployment java-demo --replicas=3
deployment.apps/java-demo scaled
[root@master kubectl-cmd]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
java-demo-6bdbd5ffbf-75l8f                1/1     Running   0          5m40s
java-demo-6bdbd5ffbf-bjzjd                1/1     Running   0          45s
java-demo-6bdbd5ffbf-xdhgp                1/1     Running   0          45s
[root@master kubectl-cmd]# 

 

案例演示2:通过对yaml文件的介绍来演示kubectl指令的使用
YAML的介绍
        它是一种简介的非标记语言
          YAML文件格式	
		    缩进表示层级关系
		    不支持制表符“tab”缩进,使用空格缩进
		    通常开头缩进 2 个空格
		    字符后缩进 1 个空格,如冒号、逗号等
		    “---” 表示YAML格式,一个文件的开始
		    “#”注释
		
 1   从官网拷贝ymal案例
 https://kubernetes.io/docs/tutorials/kubernetes-basics/官网搜索deployment,找到一个案例,用vim编辑器进行部分修改
apiVersion:
查看k8s所支持的aipVersion
[root@master ~]# kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
discovery.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
[root@master ~]# 

原数据metadata:
通常包括:资源的名称和命名空间
[root@master ~]# kubectl get ns
NAME                   STATUS   AGE
default                Active   293d
ingress-nginx          Active   40d
kube-node-lease        Active   293d
kube-public            Active   293d
kube-system            Active   293d
kubernetes-dashboard   Active   293d
ops                    Active   7d19h
test                   Active   3d11h
[root@master ~]#

2  用指令生成yaml文件
        生成一个deployment文件
[root@master kubectl-cmd]# kubectl create deployment web --image=nginx --dry-run=client -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
[root@master kubectl-cmd]# 
          生成一个service文件
[root@master kubectl-cmd]# kubectl apply -f deploy1.yaml 
deployment.apps/web created
[root@master kubectl-cmd]# kubectl expose deployment web --port=80 --type=NodePort --target-port=80  --name=web --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  type: NodePort
status:
  loadBalancer: {}
[root@master kubectl-cmd]#

3 有get 指令导出现有资源的yaml文件
[root@master kubectl-cmd]# kubectl get deployment
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
java-demo                3/3     3            3           22h
nfs-client-provisioner   1/1     1            1           7d20h
web                      1/1     1            1           6m6s
[root@master kubectl-cmd]# 
[root@master kubectl-cmd]# 
[root@master kubectl-cmd]# kubectl get deployment java-demo -o yaml --export


查看当前k8s系统中资源类型的缩写
[root@master kubectl-cmd]# kubectl  api-resources


查看某一资源层级下有哪些资源可以使用
             kubectl explain <type>.<fieldName>[.<fieldName>]
[root@master kubectl-cmd]# kubectl explain deployment.spec.template.spec.containers
[root@master kubectl-cmd]# kubectl explain pods.spec.containers

指令补全功能
[root@master kubectl-cmd]# yum search bash-comp
[root@master kubectl-cmd]# yum install -y bash-completion.noarch
[root@master kubectl-cmd]# source /usr/share/bash-completion/bash_completion 
[root@master kubectl-cmd]# source <(kubectl completion bash)
[root@master kubectl-cmd]# 

  

 

posted @ 2021-09-12 16:25  流浪剑客+  阅读(318)  评论(0编辑  收藏  举报