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

⭐️创建Pod
shell 复制代码
[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

查看一下有几个副本数

shel 复制代码
[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

yaml 复制代码
[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]

​ 未仅允许禁止转载

相关推荐
志凌海纳SmartX22 分钟前
趋势洞察|AI 能否带动裸金属 K8s 强势崛起?
云原生·容器·kubernetes
锅总23 分钟前
nacos与k8s service健康检查详解
云原生·容器·kubernetes
BUG弄潮儿1 小时前
k8s 集群安装
云原生·容器·kubernetes
何遇mirror2 小时前
云原生基础-云计算概览
后端·云原生·云计算
颜淡慕潇3 小时前
【K8S系列】kubectl describe pod显示ImagePullBackOff,如何进一步排查?
后端·云原生·容器·kubernetes
Linux运维日记3 小时前
k8s1.31版本最新版本集群使用容器镜像仓库Harbor
linux·docker·云原生·容器·kubernetes
一名路过的小码农5 小时前
ceph 18.2.4二次开发,docker镜像制作
ceph·docker·容器
AI_小站5 小时前
RAG 示例:使用 langchain、Redis、llama.cpp 构建一个 kubernetes 知识库问答
人工智能·程序人生·langchain·kubernetes·llama·知识库·rag
xiangshangdemayi7 小时前
Windows环境GeoServer打包Docker极速入门
windows·docker·容器·geoserver·打包·数据挂载
程序员JerrySUN8 小时前
熟悉的 Docker,陌生的 Podman
linux·docker·容器·系统架构·podman