K8学习空间

Kubectl 命令详解 、 K8S资源对象管理 、 K8S集群

2021-09-02  本文已影响0人  秋天丢了李姑娘

kubernetes

kubernetes 基础管理

kubectl 命令

命令说明
命令格式 命令说明
kubectl run 资源名称 -参数 --image=镜像名称:标签 创建资源对象,常用参数-i交互,-t终端
kubectl get 查询资源 可选参数 -o wide 显示主机信息 常用查询的资源 node|deployment|pod
kubectl exec -it 容器id 执行的命令 同 docker exec 指令,进入容器内
kubectl describe 资源类型 资源名称 查询资源的详细信息
kubectl attach 同 docker attach 指令,连接容器
kubectl logs 容器id 查看容器控制台的标准输出
kubectl delete 资源类型 资源名称 删除指定的资源
kubectl create|apply -f 资源文件 执行指定的资源文件
命令案例
# get 查询信息
[root@master ~]# kubectl get nodes
NAME        STATUS   ROLES    AGE   VERSION
master      Ready    master   19h   v1.17.6
node-0001   Ready    <none>   16h   v1.17.6
[root@master ~]# kubectl get pod
No resources found in default namespace.
[root@master ~]# kubectl get namespaces 
NAME                STATUS    AGE
default             Active    9h
kube-node-lease     Active    9h
kube-public         Active    9h
kube-system         Active    9h
[root@master ~]# kubectl -n kube-system get pod
NAME                             READY   STATUS    RESTARTS   AGE
kube-flannel-ds-amd64-hf2jp      1/1     Running   0          41m
kube-flannel-ds-amd64-rtl4l      1/1     Running   0          41m
... ...

# describe 查询详细信息
[root@master ~]# kubectl -n kube-system describe pod kube-flannel-ds-amd64-rtl4l
Name:                 kube-proxy-4tbp6
Namespace:            kube-system
... ...

# 使用run启动容器
[root@master ~]# kubectl run testos -it --image=192.168.1.100:5000/myos:v1804 
[root@testos-79778b4895-s8mxl /]# exit
# 启动服务
[root@master ~]# kubectl run testweb --image=192.168.1.100:5000/myos:httpd
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/testweb created
# 访问节点
[root@master ~]# kubectl get pod -o wide
NAME                        READY   STATUS    RESTARTS   AGE     IP
testos-79778b4895-s8mxl     1/1     Running   1          6m33s   10.244.3.2   ... ...
testweb-7bf98b9576-v566c    1/1     Running   0          4m24s   10.244.4.2   ... ...
[root@master ~]# curl http://10.244.4.2/info.php
<pre>
Array
(
    [REMOTE_ADDR] => 10.244.0.0
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /info.php
)
php_host:   testweb-7bf98b9576-v566c
1229

# 进入容器
[root@master ~]# kubectl exec -it testos-79778b4895-s8mxl -- /bin/bash
[root@testos-79778b4895-s8mxl /]# 
[root@master ~]# kubectl attach -it testos-79778b4895-s8mxl 
[root@testos-79778b4895-s8mxl /]#

# 查看终端日志
[root@master ~]# kubectl logs testweb-7bf98b9576-v566c 
AH00558: httpd: Could not reliably determine the server fully qualified domain name, using 10.244.4.2. Set the 'ServerName' directive globally to suppress this message

# 执行指定的资源文件
[root@master flannel]# kubectl apply -f kube-flannel.yml

# 删除资源,直接删除POD会自动重建
[root@master ~]# kubectl delete pod testos-79778b4895-s8mxl 
pod "testos-79778b4895-s8mxl" deleted
[root@master ~]# kubectl delete deployments testos
deployment.apps "testos" deleted

资源文件

在k8s集群中,latest标签是不会被缓存,如果想缓存可以使用其他标签

[root@master ~]# curl http://192.168.1.100:5000/v2/myos/tags/list
{"name":"myos","tags":["nginx","php-fpm","v1804","httpd"]}
pod 资源文件
[root@master config]# vim mypod.yaml 
---
kind: Pod
apiVersion: v1
metadata:
  name: mypod
spec:
  containers:
  - name: mylinux
    image: 192.168.1.100:5000/myos:v1804
    stdin: true
    tty: true

[root@master ~]# kubectl apply -f mypod.yaml 
pod/mypod created
[root@master ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          13s
[root@master ~]# kubectl delete -f mypod.yaml
pod "mypod" deleted
[root@master ~]# 
deployment 资源文件
[root@master ~]# vim myapache.yaml 
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: myapache
  annotations:
    kubernetes.io/change-cause: httpd.v1
spec:
  selector:
    matchLabels:
      myapp: httpd
  replicas: 1
  template:
    metadata:
      labels:
        myapp: httpd
    spec:
      containers:
      - name: webcluster
        image: 192.168.1.100:5000/myos:httpd
        stdin: false
        tty: false
        ports:
        - protocol: TCP
          containerPort: 80
      restartPolicy: Always

[root@master ~]# kubectl apply -f myapache.yaml 
deployment.apps/myapache created
[root@master ~]# kubectl get deployments.apps
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
myapache   1/1     1            1           19s
[root@master ~]# kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE
myapache-69b494dc5c-bff95   1/1     Running   0          28s
[root@master ~]# 

集群调度

扩容与缩减
[root@master ~]# kubectl get deployments.apps
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
myapache   1/1     1            1           113s
[root@master ~]# kubectl scale deployment myapache --replicas=3
deployment.apps/myapache scaled
[root@master ~]# kubectl get deployments.apps
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
myapache   3/3     3            3           2m12s
[root@master ~]# 
集群更新与回滚
[root@master config]# kubectl rollout history deployment myapache 
deployment.apps/myapache 
REVISION  CHANGE-CAUSE
1         <none>
[root@master ~]# curl http://10.244.3.5
this is apache
[root@master ~]# kubectl edit deployments.apps myapache 
deployment.apps/myapache edited
[root@master ~]# curl http://10.244.2.6
this is nginx
[root@master ~]# kubectl rollout history deployment myapache 
deployment.apps/myapache 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
[root@master ~]# kubectl rollout undo deployment myapache --to-revision=1
deployment.apps/myapache rolled back
[root@master ~]# curl http://10.244.3.6
this is apache
[root@master ~]#
节点标签选择器

使用 nodeName 参数让容器运行在指定节点上

[root@master ~]# vim myapache.yaml 
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: myapache
spec:
  selector:
    matchLabels:
      myapp: httpd
  replicas: 1
  template:
    metadata:
      labels:
        myapp: httpd
    spec:
      nodeName: node-0001  # 新增一行
      containers:
      - name: webcluster
        image: 192.168.1.100:5000/myos:httpd
        stdin: false
        tty: false
        ports:
        - protocol: TCP
          containerPort: 80
      restartPolicy: Always

[root@master ~]# kubectl delete -f myapache.yaml 
deployment.apps "myapache" deleted
[root@master ~]# kubectl apply -f myapache.yaml 
deployment.apps/myapache created
[root@master ~]# kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE     IP            NODE
myapache-xxx            1/1     Running   0          3m49s   10.244.3.9    node-0001

使用 标签 让容器运行在一些节点上

[root@master ~]# kubectl delete -f myapache.yaml
deployment.apps "myapache" deleted
[root@master ~]# kubectl get nodes --show-labels 
NAME        STATUS   ROLES    AGE   VERSION   LABELS
master      Ready    master   10h   v1.17.6   kubernetes.io/hostname=master    ... ...
node-0001   Ready    <none>   10h   v1.17.6   kubernetes.io/hostname=node-0001 ... ...
node-0002   Ready    <none>   10h   v1.17.6   kubernetes.io/hostname=node-0002 ... ...
node-0003   Ready    <none>   10h   v1.17.6   kubernetes.io/hostname=node-0003 ... ...
[root@master ~]# kubectl label nodes node-0002 node-0003 disktype=ssd
node/node-0002 labeled
node/node-0003 labeled
[root@master ~]# kubectl get nodes --show-labels 
NAME        STATUS   ROLES    AGE   VERSION   LABELS
master      Ready    master   10h   v1.17.6   kubernetes.io/hostname=master    ... ...
node-0001   Ready    <none>   10h   v1.17.6   kubernetes.io/hostname=node-0001 ... ...
node-0002   Ready    <none>   10h   v1.17.6   disktype=ssd ... ...
node-0003   Ready    <none>   10h   v1.17.6   disktype=ssd ... ...
[root@master ~]# vim myapache.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: myapache
spec:
  selector:
    matchLabels:
      myapp: httpd
  replicas: 1
  template:
    metadata:
      labels:
        myapp: httpd
    spec:
      nodeSelector:     # 新添加
        disktype: ssd   # 新添加
      containers:
      - name: webcluster
        image: 192.168.1.100:5000/myos:httpd
        stdin: false
        tty: false
        ports:
        - protocol: TCP
          containerPort: 80
      restartPolicy: Always

[root@master ~]# kubectl scale deployment myapache --replicas=3
deployment.apps/myapache scaled
[root@master ~]# kubectl get pod -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   
myapache-xxx    1/1     Running   0          9s    10.244.1.7   node-0003   <none>           
myapache-xxx    1/1     Running   0          9s    10.244.2.8   node-0002   <none>           
myapache-xxx    1/1     Running   0          21s   10.244.2.7   node-0002   <none>           
[root@master ~]# kubectl delete -f myapache.yaml
deployment.apps "myapache" deleted
[root@master ~]# kubectl label nodes node-0002 node-0003 disktype-
node/node-0002 labeled
node/node-0003 labeled
[root@master ~]# kubectl get nodes --show-labels 
NAME        STATUS   ROLES    AGE   VERSION   LABELS
master      Ready    master   10h   v1.17.6   kubernetes.io/hostname=master    ... ...
node-0001   Ready    <none>   10h   v1.17.6   kubernetes.io/hostname=node-0001 ... ...
node-0002   Ready    <none>   10h   v1.17.6   kubernetes.io/hostname=node-0002 ... ...
node-0003   Ready    <none>   10h   v1.17.6   kubernetes.io/hostname=node-0003 ... ...
[root@master ~]# 
上一篇下一篇

猜你喜欢

热点阅读