K8S简单使用记录——使用kubectl 查看应用日志及简单管理

K8S简单使用记录——使用kubectl 查看应用日志及简单管理

朱治龙
2024-08-12 / 0 评论 / 15 阅读 / 正在检测是否收录...

背景说明

近期公司的 devIstio 环境已整体迁移到火山引擎,该环境SRE主要采用K8S进行运维,迁移后相关的应用日志当前只能使用 kubectl 进行查看,所以就根据SRE提供的指导材料做了些了解,当前对K8S没有做深入了解,没有全局观,只能从一个开发者角度做到浅尝辄止。

环境准备

安装 kubectl

开发机用的 Windows 11,可使用下面任意一个方法进行安装:

  • 通过 PowerShell 中安装:
& $([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/coreweave/kubernetes-cloud/master/getting-started/k8ctl_setup.ps1')))
  • 通过 Chocolatey 安装:
choco install kubernetes-cli
  • 通过 Scoop 安装:
scoop install kubectl

本地配置访问 K8S 集群

安装 kubectlho 后,将 SRE 提供的 kubeconfig 文件,复制到系统用户目录下的.kube/cotx/目录(缺省为:%USERPROFILE%.kubeconfig)。
如果不是在缺省目录,请在系统的环境变量中添加 KUBECONFIG 变量,如下图所示:
KUBECONFIG变量

开始使用

基本使用

获取所有 pod

kubectl get pods -A

示例效果:

$ kubectl get pods -A
NAMESPACE     NAME                                      READY   STATUS             RESTARTS          AGE
console       blsc-ui-594555ff77-z49ln                  1/1     Running            0                 32d
console       consent-forward-8499796656-qr9mc          1/1     Running            0                 49d
console       consent-login-8678cdb6f4-2wjh8            1/1     Running            0                 49d
console       console-api-entry-76d7d8bbc-v7kcm         1/1     Running            0                 17d
console       console-batch-f88bf9dd6-62fwm             1/1     Running            0                 49d
console       console-biz-84f458cfcd-v6pjj              1/1     Running            0                 2d21h
console       console-blsc-kbs-7b66548df6-8lwh7         1/1     Running            0                 49d
console       console-cluster-5ddb65ff54-2k4lf          1/1     Running            0                 49d
console       console-config-server-5d6cf99b9b-wt7s9    1/1     Running            0                 46h
console       console-data-sync-556dc4dc8-z2d9t         1/1     Running            0                 18d
console       console-dev-guide-6bf4bcb79c-48crp        1/1     Running            0                 6d5h
console       console-gateway-fbbf58dd-rcw8f            1/1     Running            0                 32d
console       console-kbs-67cc85848f-7tg9h              1/1     Running            0                 49d
console       console-mobile-blsc-ui-846f5f9795-jbhwc   1/1     Running            0                 48d
console       console-mobile-server-8555b7b5cf-x9l26    1/1     Running            0                 49d
console       console-mobile-ui-79df845ffc-445s9        1/1     Running            0                 32d
console       console-notice-5b8dc5fd68-vmtf7           1/1     Running            0                 39d
console       console-order-66b54dcf8-h4rv2             0/1     CrashLoopBackOff   536 (2m59s ago)   46h
console       console-prototypes-68c984cd88-mpzjb       1/1     Running            0                 38d
console       console-singleton-64b7d87877-qp5x4        1/1     Running            0                 28d
console       console-ui-56c8746566-8bbf5               1/1     Running            0                 3d2h
console       dmc-api-doc-7cf59d7f5d-gjxbs              1/1     Running            0                 49d
console       dmc-core-5dcb7dc65b-fb2gv                 1/1     Running            0                 49d
console       dmc-gateway-59fc6b767f-hzvgf              1/1     Running            0                 49d
console       dmc-magic-boot-5d89987468-r5m7r           1/1     Running            0                 49d
console       dmc-magic-boot-naive-8669fdc878-td7n8     1/1     Running            0                 49d
console       dmc-screen-5f5cb57d89-cfskh               1/1     Running            0                 49d
console       dmc-show-kbs-57b67545f5-4cc9k             1/1     Running            0                 126m
console       dmc-ui-d8f5bd4b-fhmb7                     1/1     Running            0                 49d
console       dmc-worktime-backend-5dc69ccc6b-qf97p     1/1     Running            0                 49d
console       dmc-worktime-ui-7db6df7469-d4fxt          1/1     Running            0                 49d
kube-system   cello-7qv8s                               2/2     Running            2 (49d ago)       111d
kube-system   cello-b8d6p                               2/2     Running            3 (49d ago)       111d
kube-system   cello-mrch7                               2/2     Running            2 (49d ago)       111d
kube-system   cello-pp5wz                               2/2     Running            2 (49d ago)       111d
kube-system   coredns-58cd886448-clswr                  1/1     Running            0                 49d
kube-system   coredns-58cd886448-vlrwd                  1/1     Running            0                 49d
kube-system   metrics-server-7769c76b67-rfnvm           1/1     Running            0                 49d
sup           sup-db-query-fd7675849-sjp8g              1/1     Running            0                 49d
sup           sup-mq-5bc8ccf978-5x5mc                   1/1     Running            0                 49d
sup           sup-nginx-8575c78879-27gxs                1/1     Running            0                 31d
sup           sup-oauth2-85ccd96db7-76rtw               1/1     Running            0                 49d

查看日志

kubectl logs [podname] -f

示例效果

$ kubectl logs dmc-ui-d8f5bd4b-fhmb7 -f
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/06/24 03:06:27 [notice] 1#1: using the "epoll" event method
2024/06/24 03:06:27 [notice] 1#1: nginx/1.22.1
2024/06/24 03:06:27 [notice] 1#1: built by gcc 11.2.1 20220219 (Alpine 11.2.1_git20220219)
2024/06/24 03:06:27 [notice] 1#1: OS: Linux 5.10.135-6-velinux1u1-amd64
2024/06/24 03:06:27 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/06/24 03:06:27 [notice] 1#1: start worker processes
2024/06/24 03:06:27 [notice] 1#1: start worker process 22
2024/06/24 03:06:27 [notice] 1#1: start worker process 23
2024/06/24 03:06:27 [notice] 1#1: start worker process 24
2024/06/24 03:06:27 [notice] 1#1: start worker process 25
2024/06/24 03:06:27 [notice] 1#1: start worker process 26
2024/06/24 03:06:27 [notice] 1#1: start worker process 27
2024/06/24 03:06:27 [notice] 1#1: start worker process 28
2024/06/24 03:06:27 [notice] 1#1: start worker process 29

查看最近 10 行日志

kubectl logs --tail 1000 -f [podname]

示例效果

$ kubectl logs --tail 10 dmc-ui-d8f5bd4b-fhmb7
172.17.16.38 - - [26/Jul/2024:00:50:52 +0000] "GET /favicon.ico HTTP/1.0" 200 16958 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" "-"
172.17.16.38 - - [31/Jul/2024:17:09:02 +0000] "GET / HTTP/1.0" 200 625 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" "-"
172.17.16.38 - - [31/Jul/2024:17:09:02 +0000] "GET / HTTP/1.0" 200 625 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "-"
172.17.16.38 - - [31/Jul/2024:17:09:02 +0000] "GET /assets/vendor.9901ae39.js HTTP/1.0" 200 1395988 "https://dmc-ui.console.dev.paratera.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "-"
172.17.16.38 - - [31/Jul/2024:17:09:02 +0000] "GET /assets/index.2058bab1.js HTTP/1.0" 200 100982 "https://dmc-ui.console.dev.paratera.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "-"
172.17.16.38 - - [31/Jul/2024:17:09:07 +0000] "GET /favicon.ico HTTP/1.0" 200 16958 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" "-"
172.17.16.38 - - [01/Aug/2024:11:08:22 +0000] "GET / HTTP/1.0" 200 625 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" "-"
172.17.16.38 - - [01/Aug/2024:11:08:28 +0000] "GET /favicon.ico HTTP/1.0" 200 16958 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" "-"
172.17.16.38 - - [01/Aug/2024:11:08:28 +0000] "GET /assets/index.2058bab1.js HTTP/1.0" 200 100982 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" "-"
172.17.16.38 - - [02/Aug/2024:01:35:56 +0000] "GET / HTTP/1.0" 200 625 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36" "-"

-n 指定命名空间

$ kubectl get pods  -n aps
NAME                                 READY   STATUS    RESTARTS   AGE
aps-api-entry-9c74586db-7djmp        1/1     Running   0          11d
aps-config-server-544bb7677c-nwr6x   1/1     Running   0          5d22h
aps-data-etl-649b79cb79-mbwr7        1/1     Running   0          46m

重启应用

$ kubectl rollout restart deployment aps-data-etl -n aps
deployment.apps/aps-data-etl restarted
0

评论 (0)

取消