加载中...

K8s基础命令


一、kubectl 基本命令

kubectl get
查看pod节点详细信息

kubectl get pods -o wide

查看deploy

kubectl get deploy

查看service

kubectl get svc

kubectl create

创建pod并指定deploy

kubectl create deploy nginx --image=nginx

创建pod并指定deploy 和副本数量为3

kubectl create deployment my-dep --image=nginx --replicas=3

创建pod并指定deploy 和副本数量为3 开放80端口

kubectl create deployment my-dep --image=nginx --replicas=3 --port=80

模拟创建pod,并输出yaml文件到deploy.yaml(不真正创建,只是向apiserver提交请求)

kubectl create deployment my-dep --image=nginx --replicas=3 --port=80 --dry-run=client -o yaml > deploy.yaml

kubectl describe
查看pod详细信息

kubectl describe pod nginx-6799fc88d8-hr8bg

查看调度器详细信息

kubectl describe deploy nginx

查看service详细信息

kubectl describe svc nginx

kubectl logs
查看pod日志

kubectl logs nginx-6799fc88d8-hr8bg

查看日志并指定ns

kubectl logs nginx-6799fc88d8-hr8bg -n default

动态输出日志

kubectl logs -f nginx-6799fc88d8-hr8bg

kubectl expose
映射pod端口

kubectl expose deployment springboot-k8s --port=8080

映射pod端口,并指定模式为NodePort

kubectl expose deployment springboot-k8s
--port=8080 --type=NodePort

kubectl scale
修改pod的副本数量

kubectl scale --replicas=5 deploy myapp

kubectl set image
pod的动态更新

kubectl set image deploy myapp myapp=ikubernetes/myapp:v2
kubectl rollout status deploy myapp

kubectl rollout
pod的动态回滚

kubectl rollout undo deploy myapp

configmap
通过from-literal指定配置项

kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com

通过from-file指定配置文件

kubectl create configmap nginx_www --from-file=./www.conf

secret
指定mysql的登陆密码(secret默认会进行编码)

kubectl creare secret generic mysql-root-password --frome-literal=password=123456

explain
查看pod清单列表

kubectl explain pod.spec

autoscale
对delpoy进行动态伸缩

kubectl autoscale deployment myapp --min=1 --max=8 --cpu-percent=60

K8s集群默认svc访问域名

#示例
mysvc.myns.svc.cluster.local
bash-4.2# curl -vo /dev/null http://wiz.wiz.svc.cluster.local
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to connect() to wiz.wiz.svc.cluster.local port 80 (#0)
*   Trying 10.101.24.253...
* Connected to wiz.wiz.svc.cluster.local (10.101.24.253) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: wiz.wiz.svc.cluster.local
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.20.1
< Date: Tue, 02 Nov 2021 08:43:37 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 2986
< Connection: keep-alive
< Vary: Accept-Encoding
< Last-Modified: Fri, 15 Oct 2021 04:13:04 GMT
< Cache-Control: max-age=0
<
{ [data not shown]
100  2986  100  2986    0     0   374k      0 --:--:-- --:--:-- --:--:--  416k
* Connection #0 to host wiz.wiz.svc.cluster.local left intact

文章作者: huhuhahei
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 huhuhahei !
评论
  目录