欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

程序员文章站 2022-03-01 13:28:15
本文系列:kubernetes(k8s)容器集群管理环境完整部署详细教程-上篇kubernetes(k8s)容器集群管理环境完整部署详细教程-中篇kubernetes(k8s)容器集群管理环境完整部署...

本文系列:

kubernetes(k8s)容器集群管理环境完整部署详细教程-上篇

kubernetes(k8s)容器集群管理环境完整部署详细教程-中篇

kubernetes(k8s)容器集群管理环境完整部署详细教程-下篇

在前一篇文章中详细介绍了kubernetes(k8s)容器集群管理环境完整部署详细教程-中篇,这里继续记录下kubernetes集群插件等部署过程:

十一、kubernetes集群插件

插件是kubernetes集群的附件组件,丰富和完善了集群的功能,这里分别介绍的插件有coredns、dashboard、metrics server,需要注意的是:kuberntes 自带插件的 manifests yaml 文件使用 gcr.io 的 docker registry,国内被墙,需要手动替换为其它registry 地址或提前在fq服务器上下载,然后再同步到对应的k8s部署机器上。

11.1 - kubernetes集群插件 - coredns

可以从微软中国提供的 gcr.io 免费代理下载被墙的镜像;下面部署命令均在k8s-master01节点上执行。

1)修改配置文件
将下载的 kubernetes-server-linux-amd64.tar.gz 解压后,再解压其中的 kubernetes-src.tar.gz 文件。
[root@k8s-master01 ~]# cd /opt/k8s/work/kubernetes
[root@k8s-master01 kubernetes]# tar -xzvf kubernetes-src.tar.gz

解压之后,coredns 目录是 cluster/addons/dns。

[root@k8s-master01 kubernetes]# cd /opt/k8s/work/kubernetes/cluster/addons/dns/coredns
[root@k8s-master01 coredns]# cp coredns.yaml.base coredns.yaml
[root@k8s-master01 coredns]# source /opt/k8s/bin/environment.sh
[root@k8s-master01 coredns]# sed -i -e "s/__pillar__dns__domain__/${cluster_dns_domain}/" -e "s/__pillar__dns__server__/${cluster_dns_svc_ip}/" coredns.yaml

2)创建 coredns
[root@k8s-master01 coredns]# fgrep "image" ./*
./coredns.yaml:        image: k8s.gcr.io/coredns:1.3.1
./coredns.yaml:        imagepullpolicy: ifnotpresent
./coredns.yaml.base:        image: k8s.gcr.io/coredns:1.3.1
./coredns.yaml.base:        imagepullpolicy: ifnotpresent
./coredns.yaml.in:        image: k8s.gcr.io/coredns:1.3.1
./coredns.yaml.in:        imagepullpolicy: ifnotpresent
./coredns.yaml.sed:        image: k8s.gcr.io/coredns:1.3.1
./coredns.yaml.sed:        imagepullpolicy: ifnotpresent

提前fq下载"k8s.gcr.io/coredns:1.3.1"镜像,然后上传到node节点上, 执行"docker load ..."命令导入到node节点的images镜像里面
或者从微软中国提供的gcr.io免费代理下载被墙的镜像,然后在修改yaml文件里更新coredns的镜像下载地址

然后确保对应yaml文件里的镜像拉取策略为ifnotpresent,即本地有则使用本地镜像,不拉取

接着再次进行coredns的创建
[root@k8s-master01 coredns]# kubectl create -f coredns.yaml

3)检查coredns功能 (执行下面命令后,稍微等一会儿,确保ready状态都是可用的)
[root@k8s-master01 coredns]# kubectl get all -n kube-system
name                           ready   status    restarts   age
pod/coredns-5b969f4c88-pd5js   1/1     running   0          55s

name               type        cluster-ip   external-ip   port(s)                  age
service/kube-dns   clusterip   10.254.0.2   <none>        53/udp,53/tcp,9153/tcp   56s

name                      ready   up-to-date   available   age
deployment.apps/coredns   1/1     1            1           57s

name                                 desired   current   ready   age
replicaset.apps/coredns-5b969f4c88   1         1         1       56s

查看创建的coredns的pod状态,确保没有报错
[root@k8s-master01 coredns]# kubectl describe pod/coredns-5b969f4c88-pd5js -n kube-system
.............
.............
events:
  type    reason     age    from                 message
  ----    ------     ----   ----                 -------
  normal  scheduled  2m12s  default-scheduler    successfully assigned kube-system/coredns-5b969f4c88-pd5js to k8s-node03
  normal  pulled     2m11s  kubelet, k8s-node03  container image "k8s.gcr.io/coredns:1.3.1" already present on machine
  normal  created    2m10s  kubelet, k8s-node03  created container coredns
  normal  started    2m10s  kubelet, k8s-node03  started container coredns

4)新建一个 deployment
[root@k8s-master01 coredns]# cd /opt/k8s/work
[root@k8s-master01 work]# cat > my-nginx.yaml <<eof
apiversion: extensions/v1beta1
kind: deployment
metadata:
  name: my-nginx
spec:
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.7.9
        ports:
        - containerport: 80
eof

接着执行这个deployment的创建
[root@k8s-master01 work]# kubectl create -f my-nginx.yaml

export 该 deployment, 生成 my-nginx 服务:
[root@k8s-master01 work]# kubectl expose deploy my-nginx

[root@k8s-master01 work]# kubectl get services --all-namespaces |grep my-nginx
default       my-nginx     clusterip   10.254.170.246   <none>        80/tcp                   19s

创建另一个 pod,查看 /etc/resolv.conf 是否包含 kubelet 配置的 --cluster-dns 和 --cluster-domain,
是否能够将服务 my-nginx 解析到上面显示的 cluster ip 10.254.170.246

[root@k8s-master01 work]# cd /opt/k8s/work
[root@k8s-master01 work]# cat > dnsutils-ds.yml <<eof
apiversion: v1
kind: service
metadata:
  name: dnsutils-ds
  labels:
    app: dnsutils-ds
spec:
  type: nodeport
  selector:
    app: dnsutils-ds
  ports:
  - name: http
    port: 80
    targetport: 80
---
apiversion: extensions/v1beta1
kind: daemonset
metadata:
  name: dnsutils-ds
  labels:
    addonmanager.kubernetes.io/mode: reconcile
spec:
  template:
    metadata:
      labels:
        app: dnsutils-ds
    spec:
      containers:
      - name: my-dnsutils
        image: tutum/dnsutils:latest
        command:
          - sleep
          - "3600"
        ports:
        - containerport: 80
eof

接着创建这个pod
[root@k8s-master01 work]# kubectl create -f dnsutils-ds.yml

查看上面创建的pod状态(需要等待一会儿,确保status状态为"running"。如果状态失败,可以执行"kubectl describe pod ...."查看原因)
[root@k8s-master01 work]# kubectl get pods -lapp=dnsutils-ds
name                ready   status    restarts   age
dnsutils-ds-5sc4z   1/1     running   0          52s
dnsutils-ds-h546r   1/1     running   0          52s
dnsutils-ds-jx5kx   1/1     running   0          52s

[root@k8s-master01 work]# kubectl get svc
name          type        cluster-ip       external-ip   port(s)        age
dnsutils-ds   nodeport    10.254.185.211   <none>        80:32767/tcp   7m14s
kubernetes    clusterip   10.254.0.1       <none>        443/tcp        7d13h
my-nginx      clusterip   10.254.170.246   <none>        80/tcp         9m11s
nginx-ds      nodeport    10.254.41.83     <none>        80:30876/tcp   27h

然后验证coredns 功能。
先依次登陆上面创建的dnsutils的pod里面进行验证,确保pod容器中/etc/resolv.conf里的nameserver地址为"cluster_dns_svc_ip"变量值(即environment.sh脚本中定义的)
[root@k8s-master01 work]# kubectl -it exec dnsutils-ds-5sc4z bash
root@dnsutils-ds-5sc4z:/# cat /etc/resolv.conf
nameserver 10.254.0.2
search default.svc.cluster.local svc.cluster.local cluster.local localdomain
options ndots:5

[root@k8s-master01 work]# kubectl exec dnsutils-ds-5sc4z nslookup kubernetes
server:         10.254.0.2
address:        10.254.0.2#53

name:   kubernetes.default.svc.cluster.local
address: 10.254.0.1

[root@k8s-master01 work]# kubectl exec dnsutils-ds-5sc4z nslookup www.baidu.com
server:         10.254.0.2
address:        10.254.0.2#53

non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
www.a.shifen.com        canonical name = www.wshifen.com.
name:   www.wshifen.com
address: 103.235.46.39

发现可以将服务 my-nginx 解析到上面它对应的 cluster ip 10.254.170.246
[root@k8s-master01 work]# kubectl exec dnsutils-ds-5sc4z nslookup my-nginx
server:         10.254.0.2
address:        10.254.0.2#53

non-authoritative answer:
name:   my-nginx.default.svc.cluster.local
address: 10.254.170.246

[root@k8s-master01 work]# kubectl exec dnsutils-ds-5sc4z nslookup kube-dns.kube-system.svc.cluster
server:         10.254.0.2
address:        10.254.0.2#53

** server can't find kube-dns.kube-system.svc.cluster: nxdomain

command terminated with exit code 1

[root@k8s-master01 work]# kubectl exec dnsutils-ds-5sc4z nslookup kube-dns.kube-system.svc
server:         10.254.0.2
address:        10.254.0.2#53

name:   kube-dns.kube-system.svc.cluster.local
address: 10.254.0.2

[root@k8s-master01 work]# kubectl exec dnsutils-ds-5sc4z nslookup kube-dns.kube-system.svc.cluster.local
server:         10.254.0.2
address:        10.254.0.2#53

name:   kube-dns.kube-system.svc.cluster.local
address: 10.254.0.2

[root@k8s-master01 work]# kubectl exec dnsutils-ds-5sc4z nslookup kube-dns.kube-system.svc.cluster.local.
server:         10.254.0.2
address:        10.254.0.2#53

name:   kube-dns.kube-system.svc.cluster.local
address: 10.254.0.2

11.2 - kubernetes集群插件 - dashboard

可以从微软中国提供的 gcr.io 免费代理下载被墙的镜像;下面部署命令均在k8s-master01节点上执行。

1)修改配置文件
将下载的 kubernetes-server-linux-amd64.tar.gz 解压后,再解压其中的 kubernetes-src.tar.gz 文件 (上面在coredns部署阶段已经解压过了)
[root@k8s-master01 ~]# cd /opt/k8s/work/kubernetes/
[root@k8s-master01 kubernetes]# ls -d cluster/addons/dashboard
cluster/addons/dashboard

dashboard 对应的目录是:cluster/addons/dashboard
[root@k8s-master01 kubernetes]# cd /opt/k8s/work/kubernetes/cluster/addons/dashboard

修改 service 定义,指定端口类型为 nodeport,这样外界可以通过地址 nodeip:nodeport 访问 dashboard;
[root@k8s-master01 dashboard]# vim dashboard-service.yaml
apiversion: v1
kind: service
metadata:
  name: kubernetes-dashboard
  namespace: kube-system
  labels:
    k8s-app: kubernetes-dashboard
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: reconcile
spec:
  type: nodeport                    # 添加这一行内容
  selector:
    k8s-app: kubernetes-dashboard
  ports:
  - port: 443
    targetport: 8443

2) 执行所有定义文件
需要提前fq将k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1镜像下载下来,然后上传到node节点上,然后执行"docker load ......" 导入到node节点的images镜像里
或者从微软中国提供的gcr.io免费代理下载被墙的镜像,然后在修改yaml文件里更新dashboard的镜像下载地址

[root@k8s-master01 dashboard]# fgrep "image" ./*
./dashboard-controller.yaml:        image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1

[root@k8s-master01 dashboard]# ls *.yaml
dashboard-configmap.yaml  dashboard-controller.yaml  dashboard-rbac.yaml  dashboard-secret.yaml  dashboard-service.yaml

[root@k8s-master01 dashboard]# kubectl apply -f  .

3)查看分配的 nodeport
[root@k8s-master01 dashboard]# kubectl get deployment kubernetes-dashboard  -n kube-system
name                   ready   up-to-date   available   age
kubernetes-dashboard   1/1     1            1           48s

[root@k8s-master01 dashboard]# kubectl --namespace kube-system get pods -o wide
name                                    ready   status    restarts   age   ip            node         nominated node   readiness gates
coredns-5b969f4c88-pd5js                1/1     running   0          33m   172.30.72.3   k8s-node03   <none>           <none>
kubernetes-dashboard-85bcf5dbf8-8s7hm   1/1     running   0          63s   172.30.72.6   k8s-node03   <none>           <none>

[root@k8s-master01 dashboard]# kubectl get services kubernetes-dashboard -n kube-system
name                   type       cluster-ip       external-ip   port(s)         age
kubernetes-dashboard   nodeport   10.254.164.208   <none>        443:30284/tcp   104s

可以看出:nodeport 30284 映射到 dashboard pod 443 端口;

4)查看 dashboard 支持的命令行参数
[root@k8s-master01 dashboard]# kubectl exec --namespace kube-system -it kubernetes-dashboard-85bcf5dbf8-8s7hm -- /dashboard --help
2019/06/25 16:54:04 starting overwatch
usage of /dashboard:
      --alsologtostderr                  log to standard error as well as files
      --api-log-level string             level of api request logging. should be one of 'info|none|debug'. default: 'info'. (default "info")
      --apiserver-host string            the address of the kubernetes apiserver to connect to in the format of protocol://address:port, e.g., http://localhost:8080. if not specified, the assumption is that the binary runs inside a kubernetes cluster and local discovery is attempted.
      --authentication-mode strings      enables authentication options that will be reflected on login screen. supported values: token, basic. default: token.note that basic option should only be used if apiserver has '--authorization-mode=abac' and '--basic-auth-file' flags set. (default [token])
      --auto-generate-certificates       when set to true, dashboard will automatically generate certificates used to serve https. default: false.
      --bind-address ip                  the ip address on which to serve the --secure-port (set to 0.0.0.0 for all interfaces). (default 0.0.0.0)
      --default-cert-dir string          directory path containing '--tls-cert-file' and '--tls-key-file' files. used also when auto-generating certificates flag is set. (default "/certs")
      --disable-settings-authorizer      when enabled, dashboard settings page will not require user to be logged in and authorized to access settings page.
      --enable-insecure-login            when enabled, dashboard login view will also be shown when dashboard is not served over https. default: false.
      --enable-skip-login                when enabled, the skip button on the login page will be shown. default: false.
      --heapster-host string             the address of the heapster apiserver to connect to in the format of protocol://address:port, e.g., http://localhost:8082. if not specified, the assumption is that the binary runs inside a kubernetes cluster and service proxy will be used.
      --insecure-bind-address ip         the ip address on which to serve the --port (set to 0.0.0.0 for all interfaces). (default 127.0.0.1)
      --insecure-port int                the port to listen to for incoming http requests. (default 9090)
      --kubeconfig string                path to kubeconfig file with authorization and master location information.
      --log_backtrace_at tracelocation   when logging hits line file:n, emit a stack trace (default :0)
      --log_dir string                   if non-empty, write log files in this directory
      --logtostderr                      log to standard error instead of files
      --metric-client-check-period int   time in seconds that defines how often configured metric client health check should be run. default: 30 seconds. (default 30)
      --port int                         the secure port to listen to for incoming https requests. (default 8443)
      --stderrthreshold severity         logs at or above this threshold go to stderr (default 2)
      --system-banner string             when non-empty displays message to dashboard users. accepts simple html tags. default: ''.
      --system-banner-severity string    severity of system banner. should be one of 'info|warning|error'. default: 'info'. (default "info")
      --tls-cert-file string             file containing the default x509 certificate for https.
      --tls-key-file string              file containing the default x509 private key matching --tls-cert-file.
      --token-ttl int                    expiration time (in seconds) of jwe tokens generated by dashboard. default: 15 min. 0 - never expires (default 900)
  -v, --v level                          log level for v logs
      --vmodule modulespec               comma-separated list of pattern=n settings for file-filtered logging
pflag: help requested
command terminated with exit code 2

5)访问dashboard
从1.7版本开始,dashboard只允许通过https访问,如果使用kube proxy则必须监听localhost或127.0.0.1。
对于nodeport没有这个限制,但是仅建议在开发环境中使用。
对于不满足这些条件的登录访问,在登录成功后浏览器不跳转,始终停在登录界面。

有三种访问dashboard的方式:
-> kubernetes-dashboard 服务暴露了 nodeport,可以使用 https://nodeip:nodeport 地址访问 dashboard;
-> 通过 kube-apiserver 访问 dashboard;
-> 通过 kubectl proxy 访问 dashboard:

第一种方式:
kubernetes-dashboard 服务暴露了nodeport端口,可以通过https://nodeip+nodeport 来访问dashboard
[root@k8s-master01 dashboard]# kubectl get services kubernetes-dashboard -n kube-system
name                   type       cluster-ip       external-ip   port(s)         age
kubernetes-dashboard   nodeport   10.254.164.208   <none>        443:30284/tcp   14m

则可以通过访问https://172.16.60.244:30284,https://172.16.60.245:30284,https://172.16.60.246:30284 来打开dashboard界面

第二种方式:通过 kubectl proxy 访问 dashboard
启动代理(下面命令会一直在前台执行,可以选择使用tmux虚拟终端执行)
[root@k8s-master01 dashboard]# kubectl proxy --address='localhost' --port=8086 --accept-hosts='^*$'
starting to serve on 127.0.0.1:8086

需要注意:
--address 必须为 localhost 或 127.0.0.1;
需要指定 --accept-hosts 选项,否则浏览器访问 dashboard 页面时提示 “unauthorized”;
这样就可以在这个服务器的浏览器里访问 url:http://127.0.0.1:8086/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy

第三种方式:通过 kube-apiserver 访问 dashboard
获取集群服务地址列表:
[root@k8s-master01 dashboard]# kubectl cluster-info
kubernetes master is running at https://172.16.60.250:8443
coredns is running at https://172.16.60.250:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
kubernetes-dashboard is running at https://172.16.60.250:8443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy

to further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

需要注意:
必须通过 kube-apiserver 的安全端口(https)访问 dashbaord,访问时浏览器需要使用自定义证书,否则会被 kube-apiserver 拒绝访问。
创建和导入自定义证书的操作已经在前面"部署node工作节点"环节介绍过了,这里就略过了~~~

浏览器访问 url:https://172.16.60.250:8443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy 即可打开dashboard界面

6)创建登录 dashboard 的 token 和 kubeconfig 配置文件
dashboard 默认只支持 token 认证(不支持 client 证书认证),所以如果使用 kubeconfig 文件,需要将 token 写入到该文件。

方法一:创建登录 token
[root@k8s-master01 ~]# kubectl create sa dashboard-admin -n kube-system
serviceaccount/dashboard-admin created

[root@k8s-master01 ~]# kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
clusterrolebinding.rbac.authorization.k8s.io/dashboard-admin created

[root@k8s-master01 ~]# admin_secret=$(kubectl get secrets -n kube-system | grep dashboard-admin | awk '{print $1}')

[root@k8s-master01 ~]# dashboard_login_token=$(kubectl describe secret -n kube-system ${admin_secret} | grep -e '^token' | awk '{print $2}')

[root@k8s-master01 ~]# echo ${dashboard_login_token}
eyjhbgcioijsuzi1niisimtpzci6iij9.eyjpc3mioijrdwjlcm5ldgvzl3nlcnzpy2vhy2nvdw50iiwia3vizxjuzxrlcy5pby9zzxj2awnlywnjb3vudc9uyw1lc3bhy2uioijrdwjllxn5c3rlbsisimt1ymvybmv0zxmuaw8vc2vydmljzwfjy291bnqvc2vjcmv0lm5hbwuioijkyxnoym9hcmqtywrtaw4tdg9rzw4tcmnicnmilcjrdwjlcm5ldgvzlmlvl3nlcnzpy2vhy2nvdw50l3nlcnzpy2utywnjb3vudc5uyw1lijoizgfzagjvyxjklwfkbwluiiwia3vizxjuzxrlcy5pby9zzxj2awnlywnjb3vudc9zzxj2awnllwfjy291bnqudwlkijoizgq1njg0ogutotc2yi0xmwu5ltkwzdqtmda1mdu2ywm3yzgxiiwic3viijoic3lzdgvtonnlcnzpy2vhy2nvdw50omt1ymutc3lzdgvtomrhc2hib2fyzc1hzg1pbij9.kwh_zhi-da8kifs7drmnecs_pcxq3b2ujs_eoor-gvoaz29cjtzd_z67brds1qlj8oyiqjw2_m837ekucpj8lriontmjwbpmebphhomdgdsmdj37uec7yqa5amkvvwiyiukgthjjglaklk6eh7ihvcez3ibhwtfxlulu24mlmt9xp4j7m5fig7i5-ctflibv2nsvwlwiv6jaecocbgx1w0fjtmn9llheidqp1byxu_wavsfywoypeqduqbqcz7iovt1zuvyfugs5rxzshm86tck_pteinyo1dgljmrlrz3tb1oaow8_u-vnhqsnwkjbzjnuljfzcgy1yoi2xub7v4w

则可以使用上面输出的token 登录 dashboard。

方法二:创建使用 token 的 kubeconfig 文件 (推荐使用这种方式)
[root@k8s-master01 ~]# source /opt/k8s/bin/environment.sh

设置集群参数
[root@k8s-master01 ~]# kubectl config set-cluster kubernetes \
  --certificate-authority=/etc/kubernetes/cert/ca.pem \
  --embed-certs=true \
  --server=${kube_apiserver} \
  --kubeconfig=dashboard.kubeconfig

设置客户端认证参数,使用上面创建的 token
[root@k8s-master01 ~]# kubectl config set-credentials dashboard_user \
  --token=${dashboard_login_token} \
  --kubeconfig=dashboard.kubeconfig

设置上下文参数
[root@k8s-master01 ~]# kubectl config set-context default \
  --cluster=kubernetes \
  --user=dashboard_user \
  --kubeconfig=dashboard.kubeconfig

设置默认上下文
[root@k8s-master01 ~]# kubectl config use-context default --kubeconfig=dashboard.kubeconfig

将上面生成的 dashboard.kubeconfig文件拷贝到本地,然后使用这个文件登录 dashboard。
[root@k8s-master01 ~]# ll dashboard.kubeconfig
-rw------- 1 root root 3025 jun 26 01:14 dashboard.kubeconfig

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

这里由于缺少heapster或metrics-server插件,当前dashboard还不能展示 pod、nodes 的 cpu、内存等统计数据和图表。

11.3 - 部署 metrics-server 插件

metrics-server 通过 kube-apiserver 发现所有节点,然后调用 kubelet apis(通过 https 接口)获得各节点(node)和 pod 的 cpu、memory 等资源使用情况。从 kubernetes 1.12 开始,kubernetes 的安装脚本移除了 heapster,从 1.13 开始完全移除了对 heapster 的支持,heapster 不再被维护。替代方案如下:

-> 用于支持自动扩缩容的 cpu/memory hpa metrics:metrics-server;
-> 通用的监控方案:使用第三方可以获取 prometheus 格式监控指标的监控系统,如 prometheus operator;
-> 事件传输:使用第三方工具来传输、归档 kubernetes events;

从 kubernetes 1.8 开始,资源使用指标(如容器 cpu 和内存使用率)通过 metrics api 在 kubernetes 中获取, metrics-server 替代了heapster。metrics server 实现了resource metrics api,metrics server 是集群范围资源使用数据的聚合器。 metrics server 从每个节点上的 kubelet 公开的 summary api 中采集指标信息。

在了解metrics-server之前,必须要事先了解下metrics api的概念。metrics api相比于之前的监控采集方式(hepaster)是一种新的思路,官方希望核心指标的监控应该是稳定的,版本可控的,且可以直接被用户访问(例如通过使用 kubectl top 命令),或由集群中的控制器使用(如hpa),和其他的kubernetes apis一样。官方废弃heapster项目,就是为了将核心资源监控作为一等公民对待,即像pod、service那样直接通过api-server或者client直接访问,不再是安装一个hepater来汇聚且由heapster单独管理。

假设每个pod和node我们收集10个指标,从k8s的1.6开始,支持5000节点,每个节点30个pod,假设采集粒度为1分钟一次,则"10 x 5000 x 30 / 60 = 25000 平均每分钟2万多个采集指标"。因为k8s的api-server将所有的数据持久化到了etcd中,显然k8s本身不能处理这种频率的采集,而且这种监控数据变化快且都是临时数据,因此需要有一个组件单独处理他们,k8s版本只存放部分在内存中,于是metric-server的概念诞生了。其实hepaster已经有暴露了api,但是用户和kubernetes的其他组件必须通过master proxy的方式才能访问到,且heapster的接口不像api-server一样,有完整的鉴权以及client集成。

有了metrics server组件,也采集到了该有的数据,也暴露了api,但因为api要统一,如何将请求到api-server的/apis/metrics请求转发给metrics server呢,
解决方案就是:kube-aggregator,在k8s的1.7中已经完成,之前metrics server一直没有面世,就是耽误在了kube-aggregator这一步。

kube-aggregator(聚合api)主要提供:

-> provide an api for registering api servers;
-> summarize discovery information from all the servers;
-> proxy client requests to individual servers;

metric api的使用:

-> metrics api 只可以查询当前的度量数据,并不保存历史数据
-> metrics api uri 为 /apis/metrics.k8s.io/,在 k8s.io/metrics 维护
-> 必须部署 metrics-server 才能使用该 api,metrics-server 通过调用 kubelet summary api 获取数据

metrics server定时从kubelet的summary api(类似/ap1/v1/nodes/nodename/stats/summary)采集指标信息,这些聚合过的数据将存储在内存中,且以metric-api的形式暴露出去。metrics server复用了api-server的库来实现自己的功能,比如鉴权、版本等,为了实现将数据存放在内存中吗,去掉了默认的etcd存储,引入了内存存储(即实现storage interface)。因为存放在内存中,因此监控数据是没有持久化的,可以通过第三方存储来拓展,这个和heapster是一致的。

kubernetes dashboard 还不支持 metrics-server,如果使用 metrics-server 替代 heapster,将无法在 dashboard 中以图形展示 pod 的内存和 cpu 情况,需要通过 prometheus、grafana 等监控方案来弥补。kuberntes 自带插件的 manifests yaml 文件使用 gcr.io 的 docker registry,国内被墙,需要手动替换为其它 registry 地址(本文档未替换);可以从微软中国提供的 gcr.io 免费代理下载被墙的镜像;下面部署命令均在k8s-master01节点上执行。

监控架构

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

1)安装 metrics-server
从 github clone 源码:
[root@k8s-master01 ~]# cd /opt/k8s/work/
[root@k8s-master01 work]# git clone https://github.com/kubernetes-incubator/metrics-server.git
[root@k8s-master01 work]# cd metrics-server/deploy/1.8+/
[root@k8s-master01 1.8+]# ls
aggregated-metrics-reader.yaml  auth-reader.yaml         metrics-server-deployment.yaml  resource-reader.yaml
auth-delegator.yaml             metrics-apiservice.yaml  metrics-server-service.yaml

修改 metrics-server-deployment.yaml 文件,为 metrics-server 添加三个命令行参数(在"imagepullpolicy"行的下面添加):
[root@k8s-master01 1.8+]# cp metrics-server-deployment.yaml metrics-server-deployment.yaml.bak
[root@k8s-master01 1.8+]# vim metrics-server-deployment.yaml
.........
        args:
        - --metric-resolution=30s
        - --kubelet-preferred-address-types=internalip,hostname,internaldns,externaldns,externalip

这里需要注意:
--metric-resolution=30s:从 kubelet 采集数据的周期;
--kubelet-preferred-address-types:优先使用 internalip 来访问 kubelet,这样可以避免节点名称没有 dns 解析记录时,通过节点名称调用节点 kubelet api 失败的情况(未配置时默认的情况);

另外:
需要提前fq将k8s.gcr.io/metrics-server-amd64:v0.3.3镜像下载下来,然后上传到node节点上,然后执行"docker load ......" 导入到node节点的images镜像里
或者从微软中国提供的gcr.io免费代理下载被墙的镜像,然后在修改yaml文件里更新dashboard的镜像下载地址.

[root@k8s-master01 1.8+]# fgrep "image" metrics-server-deployment.yaml
      # mount in tmp so we can safely use from-scratch images and/or read-only containers
        image: k8s.gcr.io/metrics-server-amd64:v0.3.3
        imagepullpolicy: always

由于已经提前将相应镜像导入到各node节点的image里了,所以需要将metrics-server-deployment.yaml文件中的镜像拉取策略修改为"ifnotpresent".
即:本地有则使用本地镜像,不拉取

[root@k8s-master01 1.8+]# fgrep "image" metrics-server-deployment.yaml
      # mount in tmp so we can safely use from-scratch images and/or read-only containers
        image: k8s.gcr.io/metrics-server-amd64:v0.3.3
        imagepullpolicy: ifnotpresent

部署 metrics-server:
[root@k8s-master01 1.8+]# kubectl create -f .

2)查看运行情况
[root@k8s-master01 1.8+]# kubectl -n kube-system get pods -l k8s-app=metrics-server
name                              ready   status    restarts   age
metrics-server-54997795d9-4cv6h   1/1     running   0          50s

[root@k8s-master01 1.8+]# kubectl get svc -n kube-system  metrics-server
name             type        cluster-ip       external-ip   port(s)   age
metrics-server   clusterip   10.254.238.208   <none>        443/tcp   65s

3)metrics-server 的命令行参数 (在任意一个node节点上执行下面命令)
[root@k8s-node01 ~]# docker run -it --rm k8s.gcr.io/metrics-server-amd64:v0.3.3 --help

4)查看 metrics-server 输出的 metrics
-> 通过 kube-apiserver 或 kubectl proxy 访问:
https://172.16.60.250:8443/apis/metrics.k8s.io/v1beta1/nodes
https://172.16.60.250:8443/apis/metrics.k8s.io/v1beta1/nodes/
https://172.16.60.250:8443/apis/metrics.k8s.io/v1beta1/pods
https://172.16.60.250:8443/apis/metrics.k8s.io/v1beta1/namespace//pods/

-> 直接使用 kubectl 命令访问 :
# kubectl get --raw apis/metrics.k8s.io/v1beta1/nodes
# kubectl get --raw apis/metrics.k8s.io/v1beta1/pods kubectl
# get --raw apis/metrics.k8s.io/v1beta1/nodes/ kubectl
# get --raw apis/metrics.k8s.io/v1beta1/namespace//pods/

[root@k8s-master01 1.8+]# kubectl get --raw "/apis/metrics.k8s.io/v1beta1" | jq .
{
  "kind": "apiresourcelist",
  "apiversion": "v1",
  "groupversion": "metrics.k8s.io/v1beta1",
  "resources": [
    {
      "name": "nodes",
      "singularname": "",
      "namespaced": false,
      "kind": "nodemetrics",
      "verbs": [
        "get",
        "list"
      ]
    },
    {
      "name": "pods",
      "singularname": "",
      "namespaced": true,
      "kind": "podmetrics",
      "verbs": [
        "get",
        "list"
      ]
    }
  ]
}

[root@k8s-master01 1.8+]# kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq .
{
  "kind": "nodemetricslist",
  "apiversion": "metrics.k8s.io/v1beta1",
  "metadata": {
    "selflink": "/apis/metrics.k8s.io/v1beta1/nodes"
  },
  "items": [
    {
      "metadata": {
        "name": "k8s-node01",
        "selflink": "/apis/metrics.k8s.io/v1beta1/nodes/k8s-node01",
        "creationtimestamp": "2019-06-27t17:11:43z"
      },
      "timestamp": "2019-06-27t17:11:36z",
      "window": "30s",
      "usage": {
        "cpu": "47615396n",
        "memory": "2413536ki"
      }
    },
    {
      "metadata": {
        "name": "k8s-node02",
        "selflink": "/apis/metrics.k8s.io/v1beta1/nodes/k8s-node02",
        "creationtimestamp": "2019-06-27t17:11:43z"
      },
      "timestamp": "2019-06-27t17:11:38z",
      "window": "30s",
      "usage": {
        "cpu": "42000411n",
        "memory": "2496152ki"
      }
    },
    {
      "metadata": {
        "name": "k8s-node03",
        "selflink": "/apis/metrics.k8s.io/v1beta1/nodes/k8s-node03",
        "creationtimestamp": "2019-06-27t17:11:43z"
      },
      "timestamp": "2019-06-27t17:11:40z",
      "window": "30s",
      "usage": {
        "cpu": "54095172n",
        "memory": "3837404ki"
      }
    }
  ]
}

这里需要注意:/apis/metrics.k8s.io/v1beta1/nodes 和 /apis/metrics.k8s.io/v1beta1/pods 返回的 usage 包含 cpu 和 memory;

5)使用 kubectl top 命令查看集群节点资源使用情况
[root@k8s-master01 1.8+]# kubectl top node
name         cpu(cores)   cpu%   memory(bytes)   memory%  
k8s-node01   45m          1%     2357mi          61%      
k8s-node02   44m          1%     2437mi          63%      
k8s-node03   54m          1%     3747mi          47%

=======================================================================================================================================
报错解决:
[root@k8s-master01 1.8+]# kubectl top node
error from server (forbidden): nodes.metrics.k8s.io is forbidden: user "aggregator" cannot list resource "nodes" in api group "metrics.k8s.io" at the cluster scope

出现上述错误的原因主要是未对aggregator这个sa进行rbac授权!
偷懒的解决方案,直接将这个sa和cluster-admin进行绑定,但不符合最小权限原则。

[root@k8s-master01 1.8+]# kubectl create clusterrolebinding  custom-metric-with-cluster-admin --clusterrole=cluster-admin --user=aggregator

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

11.4 - 部署 kube-state-metrics 插件

上面已经部署了metric-server,几乎容器运行的大多数指标数据都能采集到了,但是下面这种情况的指标数据的采集却无能为力:

-> 调度了多少个replicas?现在可用的有几个?
-> 多少个pod是running/stopped/terminated状态?
-> pod重启了多少次?
-> 当前有多少job在运行中?

这些则是kube-state-metrics提供的内容,它是k8s的一个附加服务,基于client-go开发的。它会轮询kubernetes api,并将kubernetes的结构化信息转换为metrics。kube-state-metrics能够采集绝大多数k8s内置资源的相关数据,例如pod、deploy、service等等。同时它也提供自己的数据,主要是资源采集个数和采集发生的异常次数统计。

kube-state-metrics 指标类别包括:

cronjob metrics
daemonset metrics
deployment metrics
job metrics
limitrange metrics
node metrics
persistentvolume metrics
persistentvolumeclaim metrics
pod metrics
pod disruption budget metrics
replicaset metrics
replicationcontroller metrics
resourcequota metrics
service metrics
statefulset metrics
namespace metrics
horizontal pod autoscaler metrics
endpoint metrics
secret metrics
configmap metrics

以pod为例的指标有:

kube_pod_info
kube_pod_owner
kube_pod_status_running
kube_pod_status_ready
kube_pod_status_scheduled
kube_pod_container_status_waiting
kube_pod_container_status_terminated_reason
..............

kube-state-metrics与metric-server (或heapster)的对比

1)metric-server是从api-server中获取cpu,内存使用率这种监控指标,并把它们发送给存储后端,如influxdb或云厂商,它当前的核心作用是:为hpa等组件提供决策指标支持。

2)kube-state-metrics关注于获取k8s各种资源的最新状态,如deployment或者daemonset,之所以没有把kube-state-metrics纳入到metric-server的能力中,是因为它们的关注点本质上是不一样的。metric-server仅仅是获取、格式化现有数据,写入特定的存储,实质上是一个监控系统。而kube-state-metrics是将k8s的运行状况在内存中做了个快照,并且获取新的指标,但它没有能力导出这些指标

3)换个角度讲,kube-state-metrics本身是metric-server的一种数据来源,虽然现在没有这么做。

4)另外,像prometheus这种监控系统,并不会去用metric-server中的数据,它都是自己做指标收集、集成的(prometheus包含了metric-server的能力),但prometheus可以监控metric-server本身组件的监控状态并适时报警,这里的监控就可以通过kube-state-metrics来实现,如metric-serverpod的运行状态。

kube-state-metrics本质上是不断轮询api-server,其性能优化:

kube-state-metrics在之前的版本中暴露出两个问题:

1)/metrics接口响应慢(10-20s)
2)内存消耗太大,导致超出limit被杀掉

问题一的方案:就是基于client-go的cache tool实现本地缓存,具体结构为:var cache = map[uuid][]byte{}
问题二的的方案是:对于时间序列的字符串,是存在很多重复字符的(如namespace等前缀筛选),可以用指针或者结构化这些重复字符。

kube-state-metrics优化点和问题

1)因为kube-state-metrics是监听资源的add、delete、update事件,那么在kube-state-metrics部署之前已经运行的资源的数据是不是就拿不到了?其实kube-state-metric利用client-go可以初始化所有已经存在的资源对象,确保没有任何遗漏;

2)kube-state-metrics当前不会输出metadata信息(如help和description);

3)缓存实现是基于golang的map,解决并发读问题当期是用了一个简单的互斥锁,应该可以解决问题,后续会考虑golang的sync.map安全map;

4)kube-state-metrics通过比较resource version来保证event的顺序;

5)kube-state-metrics并不保证包含所有资源;

下面部署命令均在k8s-master01节点上执行。

1)修改配置文件
将下载的 kube-state-metrics.tar.gz 放到/opt/k8s/work目录下解压
[root@k8s-master01 ~]# cd /opt/k8s/work/
[root@k8s-master01 work]# tar -zvxf kube-state-metrics.tar.gz
[root@k8s-master01 work]# cd kube-state-metrics

kube-state-metrics目录下,有所需要的文件
[root@k8s-master01 kube-state-metrics]# ll
total 32
-rw-rw-r-- 1 root root  362 may  6 17:31 kube-state-metrics-cluster-role-binding.yaml
-rw-rw-r-- 1 root root 1076 may  6 17:31 kube-state-metrics-cluster-role.yaml
-rw-rw-r-- 1 root root 1657 jul  1 17:35 kube-state-metrics-deployment.yaml
-rw-rw-r-- 1 root root  381 may  6 17:31 kube-state-metrics-role-binding.yaml
-rw-rw-r-- 1 root root  508 may  6 17:31 kube-state-metrics-role.yaml
-rw-rw-r-- 1 root root   98 may  6 17:31 kube-state-metrics-service-account.yaml
-rw-rw-r-- 1 root root  404 may  6 17:31 kube-state-metrics-service.yaml

[root@k8s-master01 kube-state-metrics]# fgrep -r "image" ./*
./kube-state-metrics-deployment.yaml:        image: quay.io/coreos/kube-state-metrics:v1.5.0
./kube-state-metrics-deployment.yaml:        imagepullpolicy: ifnotpresent
./kube-state-metrics-deployment.yaml:        image: k8s.gcr.io/addon-resizer:1.8.3
./kube-state-metrics-deployment.yaml:        imagepullpolicy: ifnotpresent

[root@k8s-master01 kube-state-metrics]# cat kube-state-metrics-service.yaml
apiversion: v1
kind: service
metadata:
  name: kube-state-metrics
  namespace: kube-system
  labels:
    k8s-app: kube-state-metrics
  annotations:
    prometheus.io/scrape: 'true'
spec:
  ports:
  - name: http-metrics
    port: 8080
    targetport: http-metrics
    protocol: tcp
  - name: telemetry
    port: 8081
    targetport: telemetry
    protocol: tcp
  type: nodeport                                    #添加这一行
  selector:
    k8s-app: kube-state-metrics

注意两点:
其中有个是镜像是"k8s.gcr.io/addon-resizer:1.8.3"在国内因为某些原因无法拉取,可以更换为"ist0ne/addon-resizer"即可正常使用。或者通过fq下载。
service 如果需要集群外部访问,需要改为nodeport

2)执行所有定义文件
需要提前fq将quay.io/coreos/kube-state-metrics:v1.5.0 和 k8s.gcr.io/addon-resizer:1.8.3镜像下载下来,然后上传到node节点上,然后执行"docker load ......" 导入到node节点的images镜像里
或者从微软中国提供的gcr.io免费代理下载被墙的镜像,然后在修改yaml文件里更新dashboard的镜像下载地址。由于已经提前将相应镜像导入到各node节点的image里了,
所以需要将kube-state-metrics-deployment.yaml文件中的镜像拉取策略修改为"ifnotpresent".即本地有则使用本地镜像,不拉取。

[root@k8s-master01 kube-state-metrics]# kubectl create -f .

执行后检查一下:
[root@k8s-master01 kube-state-metrics]# kubectl get pod -n kube-system|grep kube-state-metrics     
kube-state-metrics-5dd55c764d-nnsdv     2/2     running   0          9m3s

[root@k8s-master01 kube-state-metrics]# kubectl get svc -n kube-system|grep kube-state-metrics    
kube-state-metrics     nodeport    10.254.228.212   <none>        8080:30978/tcp,8081:30872/tcp   9m14s

[root@k8s-master01 kube-state-metrics]# kubectl get pod,svc -n kube-system|grep kube-state-metrics
pod/kube-state-metrics-5dd55c764d-nnsdv     2/2     running   0          9m12s
service/kube-state-metrics     nodeport    10.254.228.212   <none>        8080:30978/tcp,8081:30872/tcp   9m18s

3)验证kube-state-metrics数据采集
通过上面的检查,可以得知映射到外部访问的nodeport端口是30978,通过任意一个node工作节点即可验证访问:
[root@k8s-master01 kube-state-metrics]# curl http://172.16.60.244:30978/metrics|head -10
  % total    % received % xferd  average speed   time    time     time  current
                                 dload  upload   total   spent    left  speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0# help kube_configmap_info information about configmap.
# type kube_configmap_info gauge
kube_configmap_info{namespace="kube-system",configmap="extension-apiserver-authentication"} 1
kube_configmap_info{namespace="kube-system",configmap="coredns"} 1
kube_configmap_info{namespace="kube-system",configmap="kubernetes-dashboard-settings"} 1
# help kube_configmap_created unix creation timestamp
# type kube_configmap_created gauge
kube_configmap_created{namespace="kube-system",configmap="extension-apiserver-authentication"} 1.560825764e+09
kube_configmap_created{namespace="kube-system",configmap="coredns"} 1.561479528e+09
kube_configmap_created{namespace="kube-system",configmap="kubernetes-dashboard-settings"} 1.56148146e+09
100 73353    0 73353    0     0   9.8m      0 --:--:-- --:--:-- --:--:-- 11.6m
curl: (23) failed writing body (0 != 2048)

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

11.5 - 部署 harbor 私有仓库

安装的话,可以参考docker私有仓库harbor介绍和部署方法详解,需要在两台节点机172.16.60.247、172.16.60.248上都安装harbor私有仓库环境。上层通过nginx+keepalived实现harbor的负载均衡+高可用,两个harbor相互同步(主主复制)。

harbor上远程同步的操作:

1)"仓库管理"创建目标,创建后可以测试是否正常连接目标。

2)"同步管理"创建规则,在规则中调用上面创建的目标。

3)手动同步或定时同步。

例如:已经在172.16.60.247这台harbor节点的私有仓库library和kevin_img的项目里各自存放了镜像,如下:

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

现在要把172.16.60.247的harbor私有仓库的这两个项目下的镜像同步到另一个节点172.16.60.248的harbor里。同步同步方式:147 -> 148 或 147 <- 148

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

上面是手动同步,也可以选择定时同步,分别填写的是"秒 分 时 日 月 周", 如下每两分钟同步一次!  则过了两分钟之后就会自动同步过来了~

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

11.6 - kubernetes集群管理测试

[root@k8s-master01 ~]# kubectl get cs
name                 status    message             error
scheduler            healthy   ok                
controller-manager   healthy   ok                
etcd-2               healthy   {"health":"true"} 
etcd-0               healthy   {"health":"true"} 
etcd-1               healthy   {"health":"true"} 
  
[root@k8s-master01 ~]# kubectl get nodes
name         status   roles    age   version
k8s-node01   ready    <none>   20d   v1.14.2
k8s-node02   ready    <none>   20d   v1.14.2
k8s-node03   ready    <none>   20d   v1.14.2
  
部署测试实例
[root@k8s-master01 ~]# kubectl run kevin-nginx --image=nginx --replicas=3
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/kevin-nginx created
  
[root@k8s-master01 ~]# kubectl run --generator=run-pod/v1 kevin-nginx --image=nginx --replicas=3
pod/kevin-nginx created
  
稍等一会儿,查看创建的kevin-nginx的pod(由于创建时要自动下载nginx镜像,所以需要等待一段时间)
[root@k8s-master01 ~]# kubectl get pods --all-namespaces|grep "kevin-nginx"
default       kevin-nginx                             1/1     running   0          98s
default       kevin-nginx-569dcd559b-6h4nn            1/1     running   0          106s
default       kevin-nginx-569dcd559b-7f2b4            1/1     running   0          106s
default       kevin-nginx-569dcd559b-7tds2            1/1     running   0          106s
  
查看具体详细事件
[root@k8s-master01 ~]# kubectl get pods --all-namespaces -o wide|grep "kevin-nginx"
default       kevin-nginx                             1/1     running   0          2m13s   172.30.72.12   k8s-node03   <none>           <none>
default       kevin-nginx-569dcd559b-6h4nn            1/1     running   0          2m21s   172.30.56.7    k8s-node02   <none>           <none>
default       kevin-nginx-569dcd559b-7f2b4            1/1     running   0          2m21s   172.30.72.11   k8s-node03   <none>           <none>
default       kevin-nginx-569dcd559b-7tds2            1/1     running   0          2m21s   172.30.88.8    k8s-node01   <none>           <none>
  
[root@k8s-master01 ~]# kubectl get deployment|grep kevin-nginx
kevin-nginx   3/3     3            3           2m57s
  
创建svc
[root@k8s-master01 ~]# kubectl expose deployment kevin-nginx --port=8080 --target-port=80 --type=nodeport
  
[root@k8s-master01 ~]# kubectl get svc|grep kevin-nginx
nginx         nodeport    10.254.111.50    <none>        8080:32177/tcp   33s
  
集群内部,各pod之间访问kevin-nginx
[root@k8s-master01 ~]# curl http://10.254.111.50:8080
  
外部访问kevin-nginx的地址为http://node_ip/32177
http://172.16.60.244:32177
http://172.16.60.245:32177
http://172.16.60.246:32177

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

11.7 - 清理kubernetes集群

1)清理 node 节点 (node节点同样操作)

停相关进程:
[root@k8s-node01 ~]# systemctl stop kubelet kube-proxy flanneld docker kube-proxy kube-nginx

清理文件:
[root@k8s-node01 ~]# source /opt/k8s/bin/environment.sh

umount kubelet 和 docker 挂载的目录
[root@k8s-node01 ~]# mount | grep "${k8s_dir}" | awk '{print $3}'|xargs sudo umount

删除 kubelet 工作目录
[root@k8s-node01 ~]# sudo rm -rf ${k8s_dir}/kubelet

删除 docker 工作目录
[root@k8s-node01 ~]# sudo rm -rf ${docker_dir}

删除 flanneld 写入的网络配置文件
[root@k8s-node01 ~]# sudo rm -rf /var/run/flannel/

删除 docker 的一些运行文件
[root@k8s-node01 ~]# sudo rm -rf /var/run/docker/

删除 systemd unit 文件
[root@k8s-node01 ~]# sudo rm -rf /etc/systemd/system/{kubelet,docker,flanneld,kube-nginx}.service

删除程序文件
[root@k8s-node01 ~]# sudo rm -rf /opt/k8s/bin/*

删除证书文件
[root@k8s-node01 ~]# sudo rm -rf /etc/flanneld/cert /etc/kubernetes/cert

清理 kube-proxy 和 docker 创建的 iptables
[root@k8s-node01 ~]# iptables -f && sudo iptables -x && sudo iptables -f -t nat && sudo iptables -x -t nat

删除 flanneld 和 docker 创建的网桥:
[root@k8s-node01 ~]# ip link del flannel.1
[root@k8s-node01 ~]# ip link del docker0

2)清理 master 节点 (master节点同样操作)

停相关进程:
[root@k8s-master01 ~]# systemctl stop kube-apiserver kube-controller-manager kube-scheduler kube-nginx

清理文件:
删除 systemd unit 文件
[root@k8s-master01 ~]# rm -rf /etc/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler,kube-nginx}.service

删除程序文件
[root@k8s-master01 ~]# rm -rf /opt/k8s/bin/{kube-apiserver,kube-controller-manager,kube-scheduler}

删除证书文件
[root@k8s-master01 ~]# rm -rf /etc/flanneld/cert /etc/kubernetes/cert

清理 etcd 集群
[root@k8s-master01 ~]# systemctl stop etcd

清理文件:
[root@k8s-master01 ~]# source /opt/k8s/bin/environment.sh

删除 etcd 的工作目录和数据目录
[root@k8s-master01 ~]# rm -rf ${etcd_data_dir} ${etcd_wal_dir}

删除 systemd unit 文件
[root@k8s-master01 ~]# rm -rf /etc/systemd/system/etcd.service

删除程序文件
[root@k8s-master01 ~]# rm -rf /opt/k8s/bin/etcd

删除 x509 证书文件
[root@k8s-master01 ~]# rm -rf /etc/etcd/cert/*

上面部署的dashboard是https证书方式,如果是http方式访问的kubernetes集群web-ui,操作如下:

1)配置kubernetes-dashboard.yaml (里面的"k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1"镜像已经提前在node节点上下载了)
[root@k8s-master01 ~]# cd /opt/k8s/work/
[root@k8s-master01 work]# cat kubernetes-dashboard.yaml
# ------------------- dashboard secret ------------------- #
  
apiversion: v1
kind: secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-certs
  namespace: kube-system
type: opaque
  
---
# ------------------- dashboard service account ------------------- #
  
apiversion: v1
kind: serviceaccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
  
---
# ------------------- dashboard role & role binding ------------------- #
  
kind: role
apiversion: rbac.authorization.k8s.io/v1
metadata:
  name: kubernetes-dashboard-minimal
  namespace: kube-system
rules:
  # allow dashboard to create 'kubernetes-dashboard-key-holder' secret.
- apigroups: [""]
  resources: ["secrets"]
  verbs: ["create"]
  # allow dashboard to create 'kubernetes-dashboard-settings' config map.
- apigroups: [""]
  resources: ["configmaps"]
  verbs: ["create"]
  # allow dashboard to get, update and delete dashboard exclusive secrets.
- apigroups: [""]
  resources: ["secrets"]
  resourcenames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs"]
  verbs: ["get", "update", "delete"]
  # allow dashboard to get and update 'kubernetes-dashboard-settings' config map.
- apigroups: [""]
  resources: ["configmaps"]
  resourcenames: ["kubernetes-dashboard-settings"]
  verbs: ["get", "update"]
  # allow dashboard to get metrics from heapster.
- apigroups: [""]
  resources: ["services"]
  resourcenames: ["heapster"]
  verbs: ["proxy"]
- apigroups: [""]
  resources: ["services/proxy"]
  resourcenames: ["heapster", "http:heapster:", "https:heapster:"]
  verbs: ["get"]
  
---
apiversion: rbac.authorization.k8s.io/v1
kind: rolebinding
metadata:
  name: kubernetes-dashboard-minimal
  namespace: kube-system
roleref:
  apigroup: rbac.authorization.k8s.io
  kind: role
  name: kubernetes-dashboard-minimal
subjects:
- kind: serviceaccount
  name: kubernetes-dashboard
  namespace: kube-system
  
---
kind: clusterrolebinding
apiversion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: kubernetes-dashboard
subjects:
  - kind: serviceaccount
    name: kubernetes-dashboard
    namespace: kube-system
roleref:
  kind: clusterrole
  name: cluster-admin
  apigroup: rbac.authorization.k8s.io
---
# ------------------- dashboard deployment ------------------- #
  
kind: deployment
apiversion: apps/v1beta2
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  replicas: 1
  revisionhistorylimit: 10
  selector:
    matchlabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
    spec:
      serviceaccountname: kubernetes-dashboard-admin
      containers:
      - name: kubernetes-dashboard
        image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
        ports:
        - containerport: 9090
          protocol: tcp
        args:
          #- --auto-generate-certificates
          # uncomment the following line to manually specify kubernetes api server host
          # if not specified, dashboard will attempt to auto discover the api server and connect
          # to it. uncomment only if the default does not work.
          #- --apiserver-host=http://10.0.1.168:8080
        volumemounts:
        - name: kubernetes-dashboard-certs
          mountpath: /certs
          # create on-disk volume to store exec logs
        - mountpath: /tmp
          name: tmp-volume
        livenessprobe:
          httpget:
            scheme: http
            path: /
            port: 9090
          initialdelayseconds: 30
          timeoutseconds: 30
      volumes:
      - name: kubernetes-dashboard-certs
        secret:
          secretname: kubernetes-dashboard-certs
      - name: tmp-volume
        emptydir: {}
      serviceaccountname: kubernetes-dashboard
      # comment the following tolerations if dashboard must not be deployed on master
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: noschedule
  
---
# ------------------- dashboard service ------------------- #
  
kind: service
apiversion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  ports:
    - port: 9090
      targetport: 9090
  selector:
    k8s-app: kubernetes-dashboard
  
# ------------------------------------------------------------
kind: service
apiversion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-external
  namespace: kube-system
spec:
  ports:
    - port: 9090
      targetport: 9090
      nodeport: 30090
  type: nodeport
  selector:
    k8s-app: kubernetes-dashboard
  
创建这个yaml文件
[root@k8s-master01 work]# kubectl create -f kubernetes-dashboard.yaml
  
稍微等一会儿,查看kubernetes-dashboard的pod创建情况(如下可知,该pod落在了k8s-node03节点上,即172.16.60.246)
[root@k8s-master01 work]# kubectl get pods -n kube-system -o wide|grep "kubernetes-dashboard"
kubernetes-dashboard-7976c5cb9c-q7z2w   1/1     running   0          10m     172.30.72.6   k8s-node03   <none>           <none>
  
[root@k8s-master01 work]# kubectl get svc -n kube-system|grep "kubernetes-dashboard"
kubernetes-dashboard-external   nodeport    10.254.227.142   <none>        9090:30090/tcp                  10m

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

Kubernetes(K8S)容器集群管理环境完整部署详细教程-下篇

相关标签: K8S