三、k8s快速入门之Kubectl 命令基础操作

发布于:2024-11-02 ⋅ 阅读:(11) ⋅ 点赞:(0)
⭐️创建Pod
[root@master ~]# kubectl run nginx --image=harbor.tanc.com/library/ngix:latest 
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be rmoved in a future version. Use kubectl run --generator=run-pod/v1 or kbectl create instead.
deployment.apps/nginx created

⭐️查看容器状态

[root@master ~]# kubectl run nginx --image=harbor.tanc.com/library/ngix:latest 
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be rmoved in a future version. Use kubectl run --generator=run-pod/v1 or kbectl create instead.
deployment.apps/nginx created         
⭐️开放端口
kubectl expose [pod名] --port [端口号]     
⭐️查看服务service
kubectl get svc              

⭐️ 增加副本数

kubectl scale deployment [pod名] --repolicas=[副本数]

⭐️删除pod-如果你的删除后你设定的副本数大于你当前pod数,rs就会给你在重新创建一个

kubectl delete pod [pod名]   

🌟 实列

创建nginx pod,端口映射为80-->8000,期待副本数为3个

kubectl run nginx --image=harbor.tanc.com/library/nginx:latest 
kubectl scale deployment nginx --replicas=3
kubectl expose deployment nginx --port=8000 --target-port=80

查看一下有几个副本数

[root@master ~]# kubectl get pod -o wide
NAME                     READY   STATUS     RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
nginx-6b48575596-bsdt2   1/1     Running   0          16m   10.244.2.3   node2   <none>           <none>
nginx-6b48575596-fbdjf   1/1     Running   0          22m   10.244.2.2   node2   <none>           <none>
nginx-6b48575596-g892j   1/1     Running   0          22m   10.244.1.3   node1   <none>           <none>

删除一个副本试一下

[root@master ~]# kubectl delete pod nginx-6b48575596-g892j
pod "nginx-6b48575596-g892j" deleted

在来查看一下 它又重新运行了一个

[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6b48575596-bsdt2   1/1     Running   0          22m
nginx-6b48575596-fbdjf   1/1     Running   0          27m
nginx-6b48575596-wzmdp   1/1     Running   0          44s

由于我们映射了一个端口号我们可以试着访问一下

[root@master ~]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE    NOMINATED NODE   READINESS GATES
nginx-6b48575596-bsdt2   1/1     Running   0          23m     10.244.2.3   node2   <none>           <none>
nginx-6b48575596-fbdjf   1/1     Running   0          29m     10.244.2.2   node2   <none>           <none>
nginx-6b48575596-wzmdp   1/1     Running   0          2m34s   10.244.1.4   node1   <none>           <none>
⭐️增加\减少副本期望数
kubectl scale deploy [deploy名] --replica=[副本数]
⭐️隔离和恢复隔离
kubectl cordon <node_name>
kubectl uncordon <node_name>
⭐️驱逐
kubectl drain <node>
🌟 滚动更新

创建一个deploy在创建三个nginx:v1pod

[root@master test]# cat apply.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: dp-test1
  labels:
    test: dp1
spec:
  replicas: 3
  template:
    metadata:
      name: web
      labels:
       app: webapp
    spec:
      containers:
      - name: nginx-app
        image: harbor.tanc.com/library/nginx:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: web
          containerPort: 80

执行

kubectl apply -f apply.yaml --record=true

–record的作用是将当前命令记录到revision中,可以知道每个revision对应的是哪个配置文件。

查看deploy信息

kubectl describe dp-test1

把镜像换成v2在试一下

 kubectl describe deploy dp-test1
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  10m   deployment-controller  Scaled up replica set dp-test1-688f6479b to 3
  Normal  ScalingReplicaSet  7s    deployment-controller  Scaled up replica set dp-test1-b5bc484f9 to 1
  Normal  ScalingReplicaSet  7s    deployment-controller  Scaled down replica set dp-test1-688f6479b to 2
  Normal  ScalingReplicaSet  7s    deployment-controller  Scaled up replica set dp-test1-b5bc484f9 to 2
  Normal  ScalingReplicaSet  5s    deployment-controller  Scaled down replica set dp-test1-688f6479b to 1
  Normal  ScalingReplicaSet  5s    deployment-controller  Scaled up replica set dp-test1-b5bc484f9 to 3
  Normal  ScalingReplicaSet  5s    deployment-controller  Scaled down replica set dp-test1-688f6479b to 0
  上面的日志信息就描述了滚动升级的过程:
① 启动一个新版Pod。
② 把旧版Pod数量降为2。
③ 再启动一个新版,数量变为2。
④ 把旧版Pod数量降为1。
⑤ 再启动一个新版,数量变为3。
⑥ 把旧版Pod数量降为0。

查看revision历史记录,

kubectl rollout history deployment dp-test1
deployment.extensions/dp-test1 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=apply.yaml --record=true
2         kubectl create --filename=apply.yaml --record=true      

回滚

kubectl rollout undo deployment  dp-test1 --to-revision=1 ##这样他就会到v1了

查看

[root@master test]# kubectl get deploy -o wide
NAME       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                             SELECTOR
dp-test1   3/3     3            3           27m   nginx-app    harbor.tanc.com/library/nginx:v1   app=webapp
⭐️ 进入容器
kubectl exec -it [pod]

​ 未仅允许禁止转载