1. 服务器架构图

image.png

image.png

主机名 IP地址 角色
hdss-11.host.com 10.4.7.11 LB,DNS
hdss-12.host.com 10.4.7.12 LB, ETCD
hdss-21.host.com 10.4.7.21 K8s Master, K8s Node, ETCD
hdss-22.host.com 10.4.7.22 K8s Master, K8s Node, ETCD
hdss-200.host.com 10.4.7.200 Harbor, NFS

2. 基础环境准备

2.1. 基础安装

所有节点配置

  1. # 关闭selinux
  2. sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
  3. # 关闭firewalld防火墙
  4. systemctl stop firewalld ;systemctl disable firewalld
  5. # 关闭NetworkManager服务
  6. systemctl stop NetworkManager ;systemctl disable NetworkManager
  7. # 删除基础yum源 新增阿里云yum源
  8. rm -f /etc/yum.repos.d/*
  9. curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  10. yum -y install epel-release
  11. yum clean all ; yum makecache
  12. # 安装基础
  13. yum -y install vim wget net-tools telnet tree nmap sysstat lrzsz dos2unix bind-utils
  14. # kubectl命令行自动补全(bash-completion) (可选)
  15. yum install bash-completion -y

2.2. 安装bind9 (DNS)

在节点hdss7-11.host.com安装bind9

2.2.1. 安装bind9

  1. [root@hdss7-11 ~]# yum install -y bind
  2. [root@hdss7-11 ~]# rpm -qa bind
  3. bind-9.11.4-16.P2.el7_8.6.x86_64

2.2.2. 配置主配置文件

  1. # 新增或修改以下配置信息
  2. [root@hdss7-11 ~]# vim /etc/named.conf
  3. options {
  4. listen-on port 53 { 10.4.7.11; };
  5. # listen-on-v6 port 53 { ::1; };
  6. allow-query { any; };
  7. forwarders { 10.4.7.254; };
  8. recursion yes;
  9. dnssec-enable no;
  10. dnssec-validation no;
  11. };
  12. # 检查配置是否错误
  13. [root@hdss7-11 ~]# named-checkconf

2.2.3. 配置区域文件

增加两个zone配置, odl.com 为业务域 host,com.zone 为主机域

  1. cat > /etc/named.rfc1912.zones <<EOF
  2. zone "host.com" IN {
  3. type master;
  4. file "host.com.zone";
  5. allow-update { 10.4.7.11; };
  6. };
  7. zone "odl.com" IN{
  8. type master;
  9. file "odl.com.zone";
  10. allow-update { 10.4.7.11; };
  11. };
  12. EOF

2.2.4. 配置主机域文件

配置文件中分号后面都是注释, 其中 serial的2020091701, 当有新的A记录添加,需要增加一位值,即2020091702

  1. cat > /var/named/host.com.zone <<EOF
  2. $ORIGIN host.com.
  3. $TTL 600 ; 10 minutes
  4. @ IN SOA dns.host.com. dnsadmin.host.com. (
  5. 2020091701 ; serial
  6. 10800 ; refresh (3 hours)
  7. 900 ; retry (15 minutes)
  8. 604800 ; expire (1 week)
  9. 86400 ; minimum (1 day)
  10. )
  11. NS dns.host.com.
  12. $TTL 60 ; 1 minute
  13. dns A 10.4.7.11
  14. HDSS7-11 A 10.4.7.11
  15. HDSS7-12 A 10.4.7.12
  16. HDSS7-21 A 10.4.7.21
  17. HDSS7-22 A 10.4.7.22
  18. HDSS7-200 A 10.4.7.200
  19. EOF

2.2.5. 配置业务域文件

  1. cat >/var/named/odl.com.zone<<EOF
  2. $ORIGIN odl.com.
  3. $TTL 600 ; 10 minutes
  4. @ IN SOA dns.odl.com. dnsadmin.odl.com. (
  5. 2020091701 ; serial
  6. 10800 ; refresh (3 hours)
  7. 900 ; retry (15 minutes)
  8. 604800 ; expire (1 week)
  9. 86400 ; minimum (1 day)
  10. )
  11. NS dns.odl.com.
  12. $TTL 60 ; 1 minute
  13. dns A 10.4.7.11
  14. EOF
  15. # 检查配置是否正常
  16. [root@hdss7-11 ~]# named-checkconf

2.2.6. 启动bind并检查是否解析成功

  1. [root@hdss7-11 ~]# systemctl start named ; systemctl enable named
  2. # 检查是否解析成功
  3. [root@hdss7-11 !]# dig -t A hdss7-200.host.com @10.4.7.11 +short
  4. 10.4.7.200
  5. [root@hdss7-11 !]# dig -t A hdss7-11.host.com @10.4.7.11 +short
  6. 10.4.7.11

2.2.7. 修改各个主机DNS指向DNS

所有节点的操作
search 的设置可以将访问的后缀省略 即 访问hdss-7-200.host.com 可直接访问hdss-7-200

  1. [root@hdss7-12 ~]# cat /etc/resolv.conf
  2. # Generated by NetworkManager
  3. search host.com
  4. nameserver 10.4.7.11
  5. # 测试是否成功测试
  6. [root@hdss7-12 ~]# ping hdss7-200
  7. PING HDSS7-200.host.com (10.4.7.200) 56(84) bytes of data.
  8. 64 bytes from 10.4.7.200 (10.4.7.200): icmp_seq=1 ttl=64 time=0.578 ms
  9. 64 bytes from 10.4.7.200 (10.4.7.200): icmp_seq=2 ttl=64 time=0.327 ms
  10. 64 bytes from 10.4.7.200 (10.4.7.200): icmp_seq=3 ttl=64 time=0.345 ms

2.3 准备签发证书环境

在运维主机hdss7-200.host.com中部署

2.3.1 安装cfssl

  1. [root@hdss7-200 ~]# wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -O /usr/local/bin/cfssl
  2. [root@hdss7-200 ~]# wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -O /usr/local/bin/cfssl-json
  3. [root@hdss7-200 ~]# wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -O /usr/local/bin/cfssl-certinfo
  4. [root@hdss7-200 ~]# chmod u+x /usr/local/bin/cfssl*
  5. [root@hdss7-200 ~]# ll /usr/local/bin/
  6. 总用量 18808
  7. -rwxr--r--. 1 root root 10376657 3 30 2016 cfssl
  8. -rwxr--r--. 1 root root 6595195 3 30 2016 cfssl-certinfo
  9. -rwxr--r--. 1 root root 2277873 3 30 2016 cfssl-json

2.3.2. 创建CA证书请求文件(csr)的JSON配置文件

  1. [root@hdss7-200 ~]# mkdir /opt/certs ; cd /opt/certs
  2. [root@hdss7-200 certs]# vim ca-csr.json
  3. {
  4. "CN": "OldboyEdu",
  5. "hosts": [
  6. ],
  7. "key": {
  8. "algo": "rsa",
  9. "size": 2048
  10. },
  11. "names": [
  12. {
  13. "C": "CN",
  14. "ST": "beijing",
  15. "L": "beijing",
  16. "O": "odl",
  17. "OU": "ops"
  18. }
  19. ],
  20. "ca": {
  21. "expiry": "175200h"
  22. }
  23. }

CN 浏览器使用该字段验证网站是否合法,一般写域名

C: Country 国家

ST State 州,省

L Locality 地区 城市

O Oraganizetion Name 组织名称 公司名称

OU Oraganizetion Unit Name 组织单位名称 公司部门

expiry 过期时间

2.3.3. 生成CA证书和私钥

  1. [root@hdss7-200 certs]# cfssl gencert -initca ca-csr.json | cfssl-json -bare ca
  2. 2020/09/17 20:51:52 [INFO] generating a new CA key and certificate from CSR
  3. 2020/09/17 20:51:52 [INFO] generate received request
  4. 2020/09/17 20:51:52 [INFO] received CSR
  5. 2020/09/17 20:51:52 [INFO] generating key: rsa-2048
  6. 2020/09/17 20:51:52 [INFO] encoded CSR
  7. 2020/09/17 20:51:52 [INFO] signed certificate with serial number 295886028821387664475820071483087242159712639122
  8. [root@hdss7-200 certs]# ll
  9. 总用量 16
  10. -rw-r--r--. 1 root root 993 9 17 20:51 ca.csr
  11. -rw-r--r--. 1 root root 328 9 17 20:51 ca-csr.json
  12. -rw-------. 1 root root 1679 9 17 20:51 ca-key.pem #根证书私钥
  13. -rw-r--r--. 1 root root 1346 9 17 20:51 ca.pem # 根证书

2.3.4. 查看证书时间

当接触一套新的K8s集群时候,使用cfssl-certinfo查看证书有效期还有多久,以免过期

  1. [root@hdss7-200 certs]# cfssl-certinfo -cert ca.pem
  2. "serial_number": "174577544452067437880255882036172381106489948922",
  3. "not_before": "2020-09-18T01:23:00Z",
  4. "not_after": "2040-09-13T01:23:00Z",

2.4. 安装docker环境

hdss-200.host.com hdss-21.host.com hdss-22.host.com 注意一些配置上的不同

  1. # 通过脚本下载yum源,安装docker
  2. [root@hdss7-200 ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
  3. [root@hdss7-21 ~]# mkdir /etc/docker/
  4. # 不安全的registry中增加了harbor地址
  5. # 各个机器上bip网段不一致,bip中间两段与宿主机最后两段相同,目的是方便定位问题
  6. # 不同主机需要修改bip
  7. # 比如 hdss-200.host.com 为 172.7.200.1/24 方便问题出现可以排查问题
  8. [root@hdss7-21 ~]# vim /etc/docker/daemon.json
  9. {
  10. "graph": "/data/docker",
  11. "storage-driver": "overlay2",
  12. "insecure-registries": ["registry.access.redhat.com","quay.io","harbor.odl.com"],
  13. "registry-mirrors": ["https://4nxg9p7s.mirror.aliyuncs.com"],
  14. "bip": "172.7.21.1/24",
  15. "exec-opts": ["native.cgroupdriver=systemd"],
  16. "live-restore": true
  17. }
  18. [root@hdss7-21 ~]# mkdir -p /data/docker
  19. [root@hdss7-21 ~]# systemctl start docker ; systemctl enable docker

2.5. 安装私有仓库harbor


hdss-200.host.com

2.5.1 准备harbor环境

  1. [root@hdss7-200 ~]# tar -xf harbor-offline-installer-v2.0.2-rc1.tgz -C /opt/
  2. [root@hdss7-200 ~]# cd /opt
  3. # 做软链接方便以后进行升级
  4. [root@hdss7-200 opt]# mv harbor harbor-v2.0.2
  5. [root@hdss7-200 opt]# ln -s harbor-v2.0.2 harbor
  6. [root@hdss7-200 opt]# cd harbor
  7. [root@hdss7-200 harbor]# cp harbor.yml.tmpl harbor.yml
  8. # 注释https相关配置,否则安装时候报错
  9. # external_url必须,因为后面使用nginx反向代理harbor,需要配置该参数,否则在访问时候报错
  10. [root@hdss7-200 harbor]# vim harbor.yml
  11. hostname: harbor.odl.com
  12. http:
  13. port: 180
  14. external_url: http://harbor.odl.com:80
  15. harbor_admin_password: Harbor12345
  16. data_volume: /data/harbor
  17. log:
  18. location: /data/harbor/logs
  19. # 创建harbor数据目录和日志目录
  20. [root@hdss7-200 harbor]# mkdir -p /data/harbor /data/harbor/logs

2.5.2. 安装docker-compose并安装harbor

在安装harbor的时候可—with-chartmuseum参数, 该参数会添加Helm-Charts仓库,详情可查看Helm章节 ./install.sh —with-chartmuseum

  1. # daocloud 安装源
  2. [root@hdss7-200 harbor]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
  3. # 查看是否正常安装并赋予执行权限
  4. [root@hdss7-200 harbor]# ls -l /usr/local/bin/docker-compose
  5. -rw-r--r-- 1 root root 17031320 8 2 16:25 /usr/local/bin/docker-compose
  6. [root@real-server-2 harbor]# chmod +x /usr/local/bin/docker-compose
  7. [root@hdss7-200 harbor]# docker-compose version
  8. docker-compose version 1.25.0, build 0a186604
  9. docker-py version: 4.1.0
  10. CPython version: 3.7.4
  11. OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
  12. [root@hdss7-200 harbor]# ./install.sh
  13. [Step 0]: checking if docker is installed ...
  14. Note: docker version: 19.03.12
  15. [Step 1]: checking docker-compose is installed ...
  16. Note: docker-compose version: 1.25.0
  17. [Step 2]: loading Harbor images ...
  18. Loaded image: goharbor/prepare:v2.0.2
  19. Loaded image: goharbor/harbor-jobservice:v2.0.2
  20. Loaded image: goharbor/harbor-registryctl:v2.0.2
  21. Loaded image: goharbor/registry-photon:v2.0.2
  22. Loaded image: goharbor/harbor-core:v2.0.2
  23. Loaded image: goharbor/notary-signer-photon:v2.0.2
  24. Loaded image: goharbor/clair-photon:v2.0.2
  25. Loaded image: goharbor/trivy-adapter-photon:v2.0.2
  26. Loaded image: goharbor/harbor-log:v2.0.2
  27. Loaded image: goharbor/nginx-photon:v2.0.2
  28. Loaded image: goharbor/clair-adapter-photon:v2.0.2
  29. Loaded image: goharbor/chartmuseum-photon:v2.0.2
  30. Loaded image: goharbor/harbor-portal:v2.0.2
  31. Loaded image: goharbor/harbor-db:v2.0.2
  32. Loaded image: goharbor/redis-photon:v2.0.2
  33. Loaded image: goharbor/notary-server-photon:v2.0.2
  34. [Step 3]: preparing environment ...
  35. [Step 4]: preparing harbor configs ...
  36. prepare base dir is set to /opt/harbor-v2.0.2
  37. WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https
  38. Generated configuration file: /config/log/logrotate.conf
  39. Generated configuration file: /config/log/rsyslog_docker.conf
  40. Generated configuration file: /config/nginx/nginx.conf
  41. Generated configuration file: /config/core/env
  42. Generated configuration file: /config/core/app.conf
  43. Generated configuration file: /config/registry/config.yml
  44. Generated configuration file: /config/registryctl/env
  45. Generated configuration file: /config/registryctl/config.yml
  46. Generated configuration file: /config/db/env
  47. Generated configuration file: /config/jobservice/env
  48. Generated configuration file: /config/jobservice/config.yml
  49. Generated and saved secret to file: /data/secret/keys/secretkey
  50. Successfully called func: create_root_cert
  51. Generated configuration file: /compose_location/docker-compose.yml
  52. Clean up the input dir
  53. [Step 5]: starting Harbor ...
  54. Creating network "harbor-v202_harbor" with the default driver
  55. Creating harbor-log ... done
  56. Creating registry ... done
  57. Creating harbor-db ... done
  58. Creating redis ... done
  59. Creating harbor-portal ... done
  60. Creating registryctl ... done
  61. Creating harbor-core ... done
  62. Creating harbor-jobservice ... done
  63. Creating nginx ... done
  64. ----Harbor has been installed and started successfully.----
  65. [root@hdss7-200 harbor]# docker-compose ps
  66. Name Command State Ports
  67. ---------------------------------------------------------------------------------------------
  68. harbor-core /harbor/entrypoint.sh Up (healthy)
  69. harbor-db /docker-entrypoint.sh Up (healthy) 5432/tcp
  70. harbor-jobservice /harbor/entrypoint.sh Up (healthy)
  71. harbor-log /bin/sh -c /usr/local/bin/ ... Up (healthy) 127.0.0.1:1514->10514/tcp
  72. harbor-portal nginx -g daemon off; Up (healthy) 8080/tcp
  73. nginx nginx -g daemon off; Up (healthy) 0.0.0.0:180->8080/tcp
  74. redis redis-server /etc/redis.conf Up (healthy) 6379/tcp
  75. registry /home/harbor/entrypoint.sh Up (healthy) 5000/tcp
  76. registryctl /home/harbor/start.sh Up (healthy)
  77. # 设置开机启动
  78. [root@hdss7-200 ~]# vim /etc/rc.d/rc.local
  79. # start harbor
  80. cd /opt/harbor/
  81. /usr/bin/docker-compose stop
  82. /usr/bin/docker-compose start

2.5.3. 安装nginx

  1. [root@hdss7-200 ~]# yum install nginx -y
  2. # 或者使用源码安装.当前源码安装
  3. [root@hdss7-200 ~]# vim /opt/nginx/conf/conf.d/harbor.odl.com.conf
  4. server {
  5. listen 80;
  6. server_name harbor.odl.com;
  7. # 避免出现上传失败的情况
  8. client_max_body_size 1000m;
  9. location / {
  10. proxy_pass http://127.0.0.1:180;
  11. }
  12. }
  13. [root@hdss7-200 ~]# /opt/nginx/sbin/nginx -t
  14. [root@hdss7-200 ~]# /opt/nginx/sbin/nginx

不添加client_max_body_size参数, 上传镜像时候会报错 error parsing HTTP 413 response body: invalid character ‘<’ looking for beginning of value: “\r\n\r\n\r\n

413 Request Entity Too Large

\r\n
nginx/1.16.1
\r\n\r\n\r\n”

2.5.4. 配置hdss7-11的DNS配置

serial 序列号需要滚动一位,即 2020091701 —> 2020091702 新增harbor.odl.com 的A记录

  1. [root@hdss7-11 ~]# vim /var/named/odl.com.zone
  2. $ORIGIN odl.com.
  3. $TTL 600 ; 10 minutes
  4. @ IN SOA dns.odl.com. dnsadmin.odl.com. (
  5. 2020091702 ; serial
  6. 10800 ; refresh (3 hours)
  7. 900 ; retry (15 minutes)
  8. 604800 ; expire (1 week)
  9. 86400 ; minimum (1 day)
  10. )
  11. NS dns.odl.com.
  12. $TTL 60 ; 1 minute
  13. dns A 10.4.7.11
  14. harbor A 10.4.7.200
  15. [root@hdss7-11 ~]# systemctl restart named
  16. [root@hdss7-11 ~]# dig -t A harbor.odl.com +short
  17. 10.4.7.200


2.5.5. 下载nginx镜像上传至harbor

前提需要: 需要在harbor目录上创建public创建项目, 否则上传会失败 harbor初始登录账号 admin Harbor12345

  1. [root@hdss7-200 ~]# docker pull nginx:1.7.9
  2. [root@hdss7-200 ~]# docker tag nginx:1.7.9 harbor.odl.com/public/nginx:v1.7.9
  3. [root@hdss7-200 ~]# docker login harbor.odl.com
  4. Username: admin
  5. Password: Harbor12345
  6. [root@hdss7-200 ~]# docker push harbor.odl.com/public/nginx:v1.7.9

image.png

2.6. 配置yaml文件目录树

在hdss7-200的nginx中配置yaml数目录, 后期通过http方式去使用yaml清单文件

2.6.1. 配置nginx虚拟主机

  1. [root@hdss7-200 ~]# vim /etc/nginx/conf.d/k8s-yaml.odl.com.comf
  2. server {
  3. listen 80;
  4. server_name k8s-yaml.odl.com;
  5. location / {
  6. autoindex on;
  7. default_type text/plain;
  8. root /data/k8s-yaml;
  9. }
  10. }
  11. [root@hdss7-200 ~]# mkdir /data/k8s-yaml

2.6.2. 配置DNS解析

  1. [root@hdss7-11 ~]# vim /var/named/odl.com.zone
  2. $ORIGIN odl.com.
  3. $TTL 600 ; 10 minutes
  4. @ IN SOA dns.odl.com. dnsadmin.odl.com. (
  5. 2020091705 ; serial
  6. 10800 ; refresh (3 hours)
  7. 900 ; retry (15 minutes)
  8. 604800 ; expire (1 week)
  9. 86400 ; minimum (1 day)
  10. )
  11. NS dns.odl.com.
  12. $TTL 60 ; 1 minute
  13. dns A 10.4.7.11
  14. harbor A 10.4.7.200
  15. k8s-yaml A 10.4.7.200
  16. [root@hdss7-11 ~]# systemctl restart named

image.png

3. master节点组件部署

主机名 角色 IP
hdss7-12.host.com etcd lead 10.4.7.12
hdss7-21.host.com etcd follow 10.4.7.21
hdss7-22.host.com etcd follow 10.4.7.22
  • server 表示服务端连接客户端时携带的证书,用于客户端验证服务端身份
  • client 表示客户端连接服务端时携带的证书,用于服务端验证客户端身份
  • peer 表示相互之间连接时使用的证书,如etcd节点之间验证

    3.1 创建所需证书

    3.1.1. 创建基于根证书的config配置文件

在hdss7-200 操作

  1. [root@hdss7-200 ~]# vim /opt/certs/ca-config.json
  2. {
  3. "signing": {
  4. "default": {
  5. "expiry": "175200h"
  6. },
  7. "profiles": {
  8. "server": {
  9. "expiry": "175200h",
  10. "usages": [
  11. "signing",
  12. "key encipherment",
  13. "server auth"
  14. ]
  15. },
  16. "client": {
  17. "expiry": "175200h",
  18. "usages": [
  19. "signing",
  20. "key encipherment",
  21. "client auth"
  22. ]
  23. },
  24. "peer": {
  25. "expiry": "175200h",
  26. "usages": [
  27. "signing",
  28. "key encipherment",
  29. "server auth",
  30. "client auth"
  31. ]
  32. }
  33. }
  34. }
  35. }

3.1.2. 创建etcd证书配置:/opt/certs/etcd-peer-csr.json

  1. [root@hdss7-200 ~]# vim /opt/certs/etcd-peer-csr.json
  2. {
  3. "CN": "k8s-etcd",
  4. "hosts": [
  5. "10.4.7.11",
  6. "10.4.7.12",
  7. "10.4.7.21",
  8. "10.4.7.22"
  9. ],
  10. "key": {
  11. "algo": "rsa",
  12. "size": 2048
  13. },
  14. "names": [
  15. {
  16. "C": "CN",
  17. "ST": "beijing",
  18. "L": "beijing",
  19. "O": "odl",
  20. "OU": "ops"
  21. }
  22. ]
  23. }

3.1.3. 签发证书

  1. [root@hdss7-200 ~]# cd /opt/certs/
  2. [root@hdss7-200 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=peer etcd-peer-csr.json |cfssl-json -bare etcd-peer
  3. 2020/09/18 21:35:54 [INFO] generate received request
  4. 2020/09/18 21:35:54 [INFO] received CSR
  5. 2020/09/18 21:35:54 [INFO] generating key: rsa-2048
  6. 2020/09/18 21:35:54 [INFO] encoded CSR
  7. 2020/09/18 21:35:54 [INFO] signed certificate with serial number 337386994541440179594156263415059535813502948433
  8. 2020/09/18 21:35:54 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
  9. websites. For more information see the Baseline Requirements for the Issuance and Management
  10. of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
  11. specifically, section 10.2.3 ("Information Requirements").
  12. [root@hdss7-200 certs]# ll
  13. 总用量 36
  14. -rw-r--r--. 1 root root 836 9 18 17:59 ca-config.json
  15. -rw-r--r--. 1 root root 997 9 18 09:28 ca.csr
  16. -rw-r--r--. 1 root root 329 9 18 09:28 ca-csr.json
  17. -rw-------. 1 root root 1679 9 18 09:28 ca-key.pem
  18. -rw-r--r--. 1 root root 1346 9 18 09:28 ca.pem
  19. -rw-r--r--. 1 root root 1066 9 18 21:35 etcd-peer.csr
  20. -rw-r--r--. 1 root root 364 9 18 21:34 etcd-peer-csr.json
  21. -rw-------. 1 root root 1675 9 18 21:35 etcd-peer-key.pem
  22. -rw-r--r--. 1 root root 1432 9 18 21:35 etcd-peer.pem

3.2. 安装etcd组件


涉及:hdss7-12,hdss7-21,hdss7-22 , etcd启动

下载地址: https://github.com/etcd-io/etcd/releases/tag/v3.1.20

当前ectd版本: etcd-v3.1.20-linux-amd64.tar.gz

3.2.1. 准备etcd组件

  1. # 创建etcd用户
  2. [root@hdss7-12 ~]# useradd -M -s /sbin/nologin etcd
  3. # 解压etcd
  4. [root@hdss7-12 ~]# cd /opt; tar -xf etcd-v3.1.20-linux-amd64.tar.gz
  5. [root@hdss7-12 opt]# mv etcd-v3.1.20-linux-amd64 etcd-v3.1.20
  6. [root@hdss7-12 opt]# ln -s etcd-v3.1.20 etcd
  7. [root@hdss7-12 opt]# mkdir -p /opt/etcd/certs /data/etcd /data/logs/etcd-server
  8. [root@hdss7-12 opt]# scp hdss7-200:/opt/certs/{ca.pem,etcd-peer.pem,etcd-peer-key.pem} /opt/etcd/certs/
  9. [root@hdss7-12 opt]# ll /opt/etcd/certs/
  10. 总用量 12
  11. -rw-r--r--. 1 root root 1346 9 18 21:49 ca.pem
  12. -rw-------. 1 root root 1675 9 18 21:49 etcd-peer-key.pem
  13. -rw-r--r--. 1 root root 1432 9 18 21:49 etcd-peer.pem

3.2.2. 设置etcd启动脚本

需要修改的参数:

name listen-peer-urls listen-client-urls initial-advertise-peer-urls advertise-client-urls

  1. [root@hdss7-12 ~ ]# vim /opt/etcd/etcd-server-startup.sh
  2. #!/bin/sh
  3. # listen-peer-urls etcd节点之间通信端口
  4. # listen-client-urls 客户端与etcd通信端口
  5. # quota-backend-bytes 配额大小
  6. WORK_DIR=$(dirname $(readlink -f $0))
  7. [ $? -eq 0 ] && cd $WORK_DIR || exit
  8. /opt/etcd/etcd --name etcd-server-7-12 \
  9. --data-dir /data/etcd/etcd-server \
  10. --listen-peer-urls https://10.4.7.12:2380 \
  11. --listen-client-urls https://10.4.7.12:2379,http://127.0.0.1:2379 \
  12. --quota-backend-bytes 8000000000 \
  13. --initial-advertise-peer-urls https://10.4.7.12:2380 \
  14. --advertise-client-urls https://10.4.7.12:2379,http://127.0.0.1:2379 \
  15. --initial-cluster etcd-server-7-12=https://10.4.7.12:2380,etcd-server-7-21=https://10.4.7.21:2380,etcd-server-7-22=https://10.4.7.22:2380 \
  16. --ca-file ./certs/ca.pem \
  17. --cert-file ./certs/etcd-peer.pem \
  18. --key-file ./certs/etcd-peer-key.pem \
  19. --client-cert-auth \
  20. --trusted-ca-file ./certs/ca.pem \
  21. --peer-ca-file ./certs/ca.pem \
  22. --peer-cert-file ./certs/etcd-peer.pem \
  23. --peer-key-file ./certs/etcd-peer-key.pem \
  24. --peer-client-cert-auth \
  25. --peer-trusted-ca-file ./certs/ca.pem \
  26. --log-output stdout

3.2.3. 启动etcd

采用后台管理工具 supervisor
当没指定下列两个参数,在停止服务的时候并不会杀死子进程,只会杀死父进程
stopasgroup=true ; 停止父进程和子进程
killasgroup=true ; 杀死父进程和子进程

  1. # 修改目录权限
  2. [root@hdss7-12 ~]# chmod u+x /opt/etcd/etcd-server-startup.sh
  3. [root@hdss7-12 ~]# chown -R etcd:etcd /opt/etcd /opt/etcd-v3.1.20 /data/etcd/ /data/logs/etcd-server/
  4. [root@hdss7-12 ~]# yum install -y supervisor
  5. [root@hdss7-12 ~]# systemctl start supervisord ; systemctl enable supervisord
  6. [root@hdss7-12 ~]# vim /etc/supervisord.d/etcd-server.ini
  7. [program:etcd-server-7-12]
  8. command=/opt/etcd/etcd-server-startup.sh ; the program (relative uses PATH, can take args)
  9. numprocs=1 ; number of processes copies to start (def 1)
  10. directory=/opt/etcd ; directory to cwd to before exec (def no cwd)
  11. autostart=true ; start at supervisord start (default: true)
  12. autorestart=true ; retstart at unexpected quit (default: true)
  13. startsecs=30 ; number of secs prog must stay running (def. 1)
  14. startretries=3 ; max # of serial start failures (default 3)
  15. exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
  16. stopsignal=QUIT ; signal used to kill process (default TERM)
  17. stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
  18. user=etcd ; setuid to this UNIX account to run the program
  19. redirect_stderr=true ; redirect proc stderr to stdout (default false)
  20. stdout_logfile=/data/logs/etcd-server/etcd.stdout.log ; stdout log path, NONE for none; default AUTO
  21. stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB)
  22. stdout_logfile_backups=5 ; # of stdout logfile backups (default 10)
  23. stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
  24. stdout_events_enabled=false ; emit events on stdout writes (default false)
  25. stopasgroup=true
  26. killasgroup=true
  27. # 加入到supervisorctl后会自动启动
  28. [root@hdss7-12 ~]# supervisorctl update
  29. etcd-server-7-12: added process group
  30. [root@hdss7-12 ~]# supervisorctl start etcd-server-7-12
  31. [root@hdss7-12 ~]# supervisorctl status
  32. etcd-server-7-12 RUNNING pid 30305, uptime 0:00:41

3.2.4. 检查etcd集群情况

当只部署了一个etcd节点的时候会报错,因无法连接其他节点

  1. # 查看节点成员
  2. [root@hdss7-12 ~]# /opt/etcd/etcdctl member list
  3. client: etcd cluster is unavailable or misconfigured; error #0: client: endpoint http://127.0.0.1:2379 exceeded header timeout
  4. ; error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refused
  5. # 查看集群健康
  6. [root@hdss7-12 ~]# /opt/etcd/etcdctl cluster-health
  7. cluster may be unhealthy: failed to list members
  8. Error: client: etcd cluster is unavailable or misconfigured; error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused
  9. ; error #1: client: endpoint http://127.0.0.1:2379 exceeded header timeout
  10. error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused
  11. error #1: client: endpoint http://127.0.0.1:2379 exceeded header timeout

当正常部署两台以上节点时,(此处已将hdss7-12 hdss7-21 hdss-22 的etcd部署完毕)

  1. # 查看节点成员
  2. [root@hdss7-12 ~]# /opt/etcd/etcdctl member list
  3. 988139385f78284: name=etcd-server-7-22 peerURLs=https://10.4.7.22:2380 clientURLs=http://127.0.0.1:2379,https://10.4.7.22:2379 isLeader=false
  4. 5a0ef2a004fc4349: name=etcd-server-7-21 peerURLs=https://10.4.7.21:2380 clientURLs=http://127.0.0.1:2379,https://10.4.7.21:2379 isLeader=false
  5. f4a0cb0a765574a8: name=etcd-server-7-12 peerURLs=https://10.4.7.12:2380 clientURLs=http://127.0.0.1:2379,https://10.4.7.12:2379 isLeader=true
  6. # 查看集群健康
  7. [root@hdss7-12 ~]# /opt/etcd/etcdctl cluster-health
  8. member 988139385f78284 is healthy: got healthy result from http://127.0.0.1:2379
  9. member 5a0ef2a004fc4349 is healthy: got healthy result from http://127.0.0.1:2379
  10. member f4a0cb0a765574a8 is healthy: got healthy result from http://127.0.0.1:2379
  11. cluster is healthy

关停一个节点,但无法显示正确的健康状态,可使用以下命令

  1. - 当关停10.4.7.12后, 常用命令还是显示healthy
  2. # ./etcdctl cluster-health
  3. member 988139385f78284 is healthy: got healthy result from http://127.0.0.1:2379
  4. member 5a0ef2a004fc4349 is healthy: got healthy result from http://127.0.0.1:2379
  5. member f4a0cb0a765574a8 is healthy: got healthy result from http://127.0.0.1:2379
  6. cluster is healthy
  7. - 解决办法
  8. # ETCDCTL_API=3 /opt/etcd/etcdctl --endpoints=https://10.4.7.12:2379,https://10.4.7.21:2379,https://10.4.7.22:2379 --cacert=/opt/etcd/certs/ca.pem --cert=/opt/etcd/certs/etcd-peer.pem --key=/opt/etcd/certs/etcd-peer-key.pem endpoint health --write-out=table
  9. 2020-11-16 09:45:07.452263 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
  10. 2020-11-16 09:45:07.452773 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
  11. 2020-11-16 09:45:07.453196 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
  12. https://10.4.7.21:2379 is healthy: successfully committed proposal: took = 2.849422ms
  13. https://10.4.7.22:2379 is healthy: successfully committed proposal: took = 3.636646ms
  14. https://10.4.7.12:2379 is unhealthy: failed to connect: grpc: timed out when dialing
  15. - 警告信息warning将在 etcd 3.2版本取消, 但在apiserver日志中显示

3.3. 安装apiserver组件

涉及到主机 hdss-21 hdss-22 root启动

kubernetes当前版本为1.15.3
科学上网下载: https://dl.k8s.io/v1.15.3/kubernetes-server-linux-amd64.tar.gz
当前压缩包包含了kubernetes集群所需的所有必要组件

3.3.1. 准备apiserver环境

  • 创建kubernetes目录,方便升级

    1. [root@hdss7-21 opt]# tar -xf kubernetes-server-linux-amd64.tar.gz -C /opt/
    2. [root@hdss7-21 opt]# mv kubernetes/ kubernetes-1.15.3
    3. [root@hdss7-21 opt]# ln -s kubernetes-1.15.3 kubernetes
  • 解压后kubernetes目录下有kubernetes的源码包和docker的压缩包,可删除 ```bash [root@hdss7-21 opt]# rm -f /opt/kubernetes/kubernetes-src.tar.gz [root@hdss7-21 opt]# rm -f /opt/kubernetes/server/bin/.tar /opt/kubernetes/server/bin/_tag

[root@hdss7-21 opt]# ll /opt/kubernetes/server/bin/ 总用量 884764 -rwxr-xr-x. 1 root root 43534816 8月 19 2019 apiextensions-apiserver -rwxr-xr-x. 1 root root 100569120 8月 19 2019 cloud-controller-manager -rwxr-xr-x. 1 root root 200681104 8月 19 2019 hyperkube -rwxr-xr-x. 1 root root 40186304 8月 19 2019 kubeadm -rwxr-xr-x. 1 root root 164522400 8月 19 2019 kube-apiserver -rwxr-xr-x. 1 root root 116421664 8月 19 2019 kube-controller-manager -rwxr-xr-x. 1 root root 42985504 8月 19 2019 kubectl -rwxr-xr-x. 1 root root 119645232 8月 19 2019 kubelet -rwxr-xr-x. 1 root root 36987488 8月 19 2019 kube-proxy -rwxr-xr-x. 1 root root 38786144 8月 19 2019 kube-scheduler -rwxr-xr-x. 1 root root 1648224 8月 19 2019 mounter

  1. <a name="96JUl"></a>
  2. ### 3.3.2. 签发证书
  3. > 签发证书涉及的服务器: hdss7-200
  4. <a name="7dlwu"></a>
  5. #### 3.3.2.1. 签发client证书(apiserver和etcd通信证书)
  6. ```bash
  7. [root@hdss7-200 ~]# cd /opt/certs/
  8. [root@hdss7-200 certs]# vim /opt/certs/client-csr.json
  9. {
  10. "CN": "k8s-node",
  11. "hosts": [
  12. ],
  13. "key": {
  14. "algo": "rsa",
  15. "size": 2048
  16. },
  17. "names": [
  18. {
  19. "C": "CN",
  20. "ST": "beijing",
  21. "L": "beijing",
  22. "O": "od",
  23. "OU": "ops"
  24. }
  25. ]
  26. }
  27. [root@hdss7-200 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=client client-csr.json |cfssl-json -bare client
  28. 2020/01/06 13:42:47 [INFO] generate received request
  29. 2020/01/06 13:42:47 [INFO] received CSR
  30. 2020/01/06 13:42:47 [INFO] generating key: rsa-2048
  31. 2020/01/06 13:42:47 [INFO] encoded CSR
  32. 2020/01/06 13:42:47 [INFO] signed certificate with serial number 268276380983442021656020268926931973684313260543
  33. 2020/01/06 13:42:47 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
  34. websites. For more information see the Baseline Requirements for the Issuance and Management
  35. of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
  36. specifically, section 10.2.3 ("Information Requirements").
  37. [root@hdss7-200 certs]# ls client* -l
  38. -rw-r--r-- 1 root root 993 Jan 6 13:42 client.csr
  39. -rw-r--r-- 1 root root 280 Jan 6 13:42 client-csr.json
  40. -rw------- 1 root root 1679 Jan 6 13:42 client-key.pem
  41. -rw-r--r-- 1 root root 1363 Jan 6 13:42 client.pem

3.3.2.2. 签发server证书(apiserver和其他k8s组件通信使用)

hosts中将所有可能作为apiserver的ip添加进去,VIP 10.4.7.10 也要加入

  1. [root@hdss7-200 certs]# vim /opt/certs/apiserver-csr.json
  2. {
  3. "CN": "k8s-apiserver",
  4. "hosts": [
  5. "127.0.0.1",
  6. "192.168.0.1",
  7. "kubernetes.default",
  8. "kubernetes.default.svc",
  9. "kubernetes.default.svc.cluster",
  10. "kubernetes.default.svc.cluster.local",
  11. "10.4.7.10",
  12. "10.4.7.21",
  13. "10.4.7.22",
  14. "10.4.7.23"
  15. ],
  16. "key": {
  17. "algo": "rsa",
  18. "size": 2048
  19. },
  20. "names": [
  21. {
  22. "C": "CN",
  23. "ST": "beijing",
  24. "L": "beijing",
  25. "O": "od",
  26. "OU": "ops"
  27. }
  28. ]
  29. }
  30. [root@hdss7-200 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server apiserver-csr.json |cfssl-json -bare apiserver
  31. 2020/01/06 13:46:56 [INFO] generate received request
  32. 2020/01/06 13:46:56 [INFO] received CSR
  33. 2020/01/06 13:46:56 [INFO] generating key: rsa-2048
  34. 2020/01/06 13:46:56 [INFO] encoded CSR
  35. 2020/01/06 13:46:56 [INFO] signed certificate with serial number 573076691386375893093727554861295529219004473872
  36. 2020/01/06 13:46:56 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
  37. websites. For more information see the Baseline Requirements for the Issuance and Management
  38. of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
  39. specifically, section 10.2.3 ("Information Requirements").
  40. [root@hdss7-200 certs]# ls apiserver* -l
  41. -rw-r--r-- 1 root root 1249 Jan 6 13:46 apiserver.csr
  42. -rw-r--r-- 1 root root 566 Jan 6 13:45 apiserver-csr.json
  43. -rw------- 1 root root 1675 Jan 6 13:46 apiserver-key.pem
  44. -rw-r--r-- 1 root root 1598 Jan 6 13:46 apiserver.pem

3.3.2.3. 证书下发至对应主机目录

前提已免密钥登录, 涉及到hdss7-21 hdss7-22 hdss7-200

  1. [root@hdss7-21 ~]# mkdir /opt/kubernetes/server/bin/certs
  2. [root@hdss7-21 ~]# scp hdss7-200:/opt/certs/{apiserver-key.pem,apiserver.pem,ca-key.pem,ca.pem,client-key.pem,client.pem} /opt/kubernetes/server/bin/certs/
  3. [root@hdss7-22 ~]# mkdir /opt/kubernetes/server/bin/certs
  4. [root@hdss7-22 ~]# scp hdss7-200:/opt/certs/{apiserver-key.pem,apiserver.pem,ca-key.pem,ca.pem,client-key.pem,client.pem} /opt/kubernetes/server/bin/certs/

3.3.3. 配置apiserver日志审计

aipserver 涉及的服务器:hdss7-21,hdss7-22

  1. [root@hdss7-21 bin]# mkdir /opt/kubernetes/conf
  2. [root@hdss7-21 bin]# vi /opt/kubernetes/conf/audit.yaml # 打开文件后,设置 :set paste,避免自动缩进
  3. apiVersion: audit.k8s.io/v1beta1 # This is required.
  4. kind: Policy
  5. # Don't generate audit events for all requests in RequestReceived stage.
  6. omitStages:
  7. - "RequestReceived"
  8. rules:
  9. # Log pod changes at RequestResponse level
  10. - level: RequestResponse
  11. resources:
  12. - group: ""
  13. # Resource "pods" doesn't match requests to any subresource of pods,
  14. # which is consistent with the RBAC policy.
  15. resources: ["pods"]
  16. # Log "pods/log", "pods/status" at Metadata level
  17. - level: Metadata
  18. resources:
  19. - group: ""
  20. resources: ["pods/log", "pods/status"]
  21. # Don't log requests to a configmap called "controller-leader"
  22. - level: None
  23. resources:
  24. - group: ""
  25. resources: ["configmaps"]
  26. resourceNames: ["controller-leader"]
  27. # Don't log watch requests by the "system:kube-proxy" on endpoints or services
  28. - level: None
  29. users: ["system:kube-proxy"]
  30. verbs: ["watch"]
  31. resources:
  32. - group: "" # core API group
  33. resources: ["endpoints", "services"]
  34. # Don't log authenticated requests to certain non-resource URL paths.
  35. - level: None
  36. userGroups: ["system:authenticated"]
  37. nonResourceURLs:
  38. - "/api*" # Wildcard matching.
  39. - "/version"
  40. # Log the request body of configmap changes in kube-system.
  41. - level: Request
  42. resources:
  43. - group: "" # core API group
  44. resources: ["configmaps"]
  45. # This rule only applies to resources in the "kube-system" namespace.
  46. # The empty string "" can be used to select non-namespaced resources.
  47. namespaces: ["kube-system"]
  48. # Log configmap and secret changes in all other namespaces at the Metadata level.
  49. - level: Metadata
  50. resources:
  51. - group: "" # core API group
  52. resources: ["secrets", "configmaps"]
  53. # Log all other resources in core and extensions at the Request level.
  54. - level: Request
  55. resources:
  56. - group: "" # core API group
  57. - group: "extensions" # Version of group should NOT be included.
  58. # A catch-all rule to log all other requests at the Metadata level.
  59. - level: Metadata
  60. # Long-running requests like watches that fall under this rule will not
  61. # generate an audit event in RequestReceived.
  62. omitStages:
  63. - "RequestReceived"

3.3.4. 配置apiserver启动脚本

aipserver 涉及的服务器:hdss7-21,hdss7-22

配置解析 https://kubernetes.io/zh/docs/reference/command-line-tools-reference/kube-apiserver/

  1. [root@hdss7-21 bin]# vim /opt/kubernetes/server/bin/kube-apiserver-startup.sh
  2. #!/bin/bash
  3. WORK_DIR=$(dirname $(readlink -f $0))
  4. [ $? -eq 0 ] && cd $WORK_DIR || exit
  5. /opt/kubernetes/server/bin/kube-apiserver \
  6. --apiserver-count 2 \
  7. --audit-log-path /data/logs/kubernetes/kube-apiserver/audit-log \
  8. --audit-policy-file ../../conf/audit.yaml \
  9. --authorization-mode RBAC \
  10. --client-ca-file ./certs/ca.pem \
  11. --requestheader-client-ca-file ./certs/ca.pem \
  12. --enable-admission-plugins NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota \
  13. --etcd-cafile ./certs/ca.pem \
  14. --etcd-certfile ./certs/client.pem \
  15. --etcd-keyfile ./certs/client-key.pem \
  16. --etcd-servers https://10.4.7.12:2379,https://10.4.7.21:2379,https://10.4.7.22:2379 \
  17. --service-account-key-file ./certs/ca-key.pem \
  18. --service-cluster-ip-range 192.168.0.0/16 \
  19. --service-node-port-range 3000-29999 \
  20. --target-ram-mb=1024 \
  21. --kubelet-client-certificate ./certs/client.pem \
  22. --kubelet-client-key ./certs/client-key.pem \
  23. --log-dir /data/logs/kubernetes/kube-apiserver \
  24. --tls-cert-file ./certs/apiserver.pem \
  25. --tls-private-key-file ./certs/apiserver-key.pem \
  26. --v 2
  27. # 创建apiserver日志目录
  28. [root@hdss7-21 bin]# mkdir -p /data/logs/kubernetes/kube-apiserver/
  29. [root@hdss7-21 bin]# chmod u+x /opt/kubernetes/server/bin/kube-apiserver-startup.sh

3.3.5. apiserver配置supervisor启动

涉及的服务器:hdss7-21,hdss7-22

  • 注意名称需要变更
    1. [root@hdss7-21 bin]# vim /etc/supervisord.d/kube-apiserver.ini
    2. [program:kube-apiserver-7-21]
    3. command=/opt/kubernetes/server/bin/kube-apiserver-startup.sh
    4. numprocs=1
    5. directory=/opt/kubernetes/server/bin
    6. autostart=true
    7. autorestart=true
    8. startsecs=30
    9. startretries=3
    10. exitcodes=0,2
    11. stopsignal=QUIT
    12. stopwaitsecs=10
    13. user=root
    14. redirect_stderr=true
    15. stdout_logfile=/data/logs/kubernetes/kube-apiserver/apiserver.stdout.log
    16. stdout_logfile_maxbytes=64MB
    17. stdout_logfile_backups=5
    18. stdout_capture_maxbytes=1MB
    19. stdout_events_enabled=false
    20. stopasgroup=true
    21. killasgroup=true
    22. [root@hdss7-21 bin]# supervisorctl update
    23. [root@hdss7-21 bin]# supervisorctl status
    24. etcd-server-7-21 RUNNING pid 35582, uptime 1 day, 0:57:12
    25. kube-apiserver-7-21 RUNNING pid 37301, uptime 0:00:34
    26. tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 37305/kube-apiserve
    27. tcp6 0 0 :::6443 :::* LISTEN 37305/kube-apiserve

3.3.6. apiserver配置四层代理

代理涉及的服务器:hdss7-11,hdss7-12

3.3.6.1. 安装nginx并配置

  • 可以yum安装nginx, 也可以源码安装nginx (需要添加—with-stream模块)
  1. [root@hdss7-11 ~]# yum install -y nginx
  2. [root@hdss7-11 ~]# vim /etc/nginx/nginx.conf
  3. # 末尾加上以下内容,stream 只能加在 main 中
  4. # 此处只是简单配置下nginx,实际生产中,建议进行更合理的配置
  5. # 配置时候可修改log地址
  6. stream {
  7. log_format proxy '$time_local|$remote_addr|$upstream_addr|$protocol|$status|'
  8. '$session_time|$upstream_connect_time|$bytes_sent|$bytes_received|'
  9. '$upstream_bytes_sent|$upstream_bytes_received' ;
  10. upstream kube-apiserver {
  11. server 10.4.7.21:6443 max_fails=3 fail_timeout=30s;
  12. server 10.4.7.22:6443 max_fails=3 fail_timeout=30s;
  13. }
  14. server {
  15. listen 7443;
  16. proxy_connect_timeout 2s;
  17. proxy_timeout 900s;
  18. proxy_pass kube-apiserver;
  19. access_log /var/log/nginx/proxy.log proxy;
  20. }
  21. }
  22. [root@hdss7-11 ~]# systemctl start nginx; systemctl enable nginx
  23. [root@hdss7-11 ~]# curl 127.0.0.1:7443 # 测试几次
  24. Client sent an HTTP request to an HTTPS server.
  25. [root@hdss7-11 ~]# cat /var/log/nginx/proxy.log
  26. 20/Sep/2020:18:43:03 +0800|127.0.0.1|10.4.7.21:6443|TCP|200|0.002|0.001|76|78|78|76
  27. 20/Sep/2020:18:43:04 +0800|127.0.0.1|10.4.7.22:6443|TCP|200|0.001|0.000|76|78|78|76
  28. 20/Sep/2020:18:43:05 +0800|127.0.0.1|10.4.7.21:6443|TCP|200|0.003|0.001|76|78|78|76
  29. 20/Sep/2020:18:43:05 +0800|127.0.0.1|10.4.7.22:6443|TCP|200|0.001|0.001|76|78|78|76
  30. 20/Sep/2020:18:43:05 +0800|127.0.0.1|10.4.7.21:6443|TCP|200|0.001|0.001|76|78|78|76

3.3.6.2. 安装keepalived保证nginx高可用

涉及的服务器:hdss7-11,hdss7-12

  • 安装keepalived,并配置nginx高可用脚本

    1. [root@hdss7-11 ~]# yum install -y keepalived
    2. [root@hdss7-11 ~]# vim /etc/keepalived/check_port.sh # 配置检查脚本
    3. #!/bin/bash
    4. if [ $# -eq 1 ] && [[ $1 =~ ^[0-9]+ ]];then
    5. [ $(netstat -lntp|grep ":$1 " |wc -l) -eq 0 ] && echo "[ERROR] nginx may be not running!" && exit 1 || exit 0
    6. else
    7. echo "[ERROR] need one port!"
    8. exit 1
    9. fi
    10. [root@hdss7-11 ~]# chmod +x /etc/keepalived/check_port.sh
  • 配置主节点

该主节点必须加入nopreempt参数, 不能让VIP飘逸

  1. [root@hdss7-11 ~]# vim /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. router_id 10.4.7.11
  5. }
  6. vrrp_script chk_nginx {
  7. script "/etc/keepalived/check_port.sh 7443"
  8. interval 2
  9. weight -20
  10. }
  11. vrrp_instance VI_1 {
  12. state MASTER
  13. interface ens33
  14. virtual_router_id 251
  15. priority 100
  16. advert_int 1
  17. mcast_src_ip 10.4.7.11
  18. nopreempt
  19. authentication {
  20. auth_type PASS
  21. auth_pass 11111111
  22. }
  23. track_script {
  24. chk_nginx
  25. }
  26. virtual_ipaddress {
  27. 10.4.7.10
  28. }
  29. }
  • 配置备用节点

    1. [root@hdss7-12 ~]# vim /etc/keepalived/keepalived.conf
    2. ! Configuration File for keepalived
    3. global_defs {
    4. router_id 10.4.7.12
    5. }
    6. vrrp_script chk_nginx {
    7. script "/etc/keepalived/check_port.sh 7443"
    8. interval 2
    9. weight -20
    10. }
    11. vrrp_instance VI_1 {
    12. state BACKUP
    13. interface ens33
    14. virtual_router_id 251
    15. mcast_src_ip 10.4.7.12
    16. priority 90
    17. advert_int 1
    18. authentication {
    19. auth_type PASS
    20. auth_pass 11111111
    21. }
    22. track_script {
    23. chk_nginx
    24. }
    25. virtual_ipaddress {
    26. 10.4.7.10
    27. }
    28. }
  • 测试keepalived是否正常

    3.4. 安装controller-manager组件

    涉及的服务器:hdss7-21,hdss7-2

只要有一个节点存活, controller-manager的状态仍然为”OK”, 仍然会为集群提供服务
controller-manager 设置为只调用当前机器的 apiserver,走127.0.0.1网卡,因此不配制SSL证书

3.4.1. 配置controller-manager启动脚本

  1. [root@hdss7-21 ~]# vim /opt/kubernetes/server/bin/kube-controller-manager-startup.sh
  2. #!/bin/sh
  3. WORK_DIR=$(dirname $(readlink -f $0))
  4. [ $? -eq 0 ] && cd $WORK_DIR || exit
  5. /opt/kubernetes/server/bin/kube-controller-manager \
  6. --cluster-cidr 172.7.0.0/16 \
  7. --leader-elect true \
  8. --log-dir /data/logs/kubernetes/kube-controller-manager \
  9. --master http://127.0.0.1:8080 \
  10. --service-account-private-key-file ./certs/ca-key.pem \
  11. --service-cluster-ip-range 192.168.0.0/16 \
  12. --root-ca-file ./certs/ca.pem \
  13. --v 2
  14. [root@hdss7-21 ~]# mkdir -p /data/logs/kubernetes/kube-controller-manager
  15. [root@hdss7-21 ~]# chmod u+x /opt/kubernetes/server/bin/kube-controller-manager-startup.sh

3.4.2. 配置supervisor启动

注意名字的修改

  1. [root@hdss7-21 ~]# vim /etc/supervisord.d/kube-controller-manager.ini
  2. [program:kube-controller-manager-7-21]
  3. command=/opt/kubernetes/server/bin/kube-controller-manager-startup.sh ; the program (relative uses PATH, can take args)
  4. numprocs=1 ; number of processes copies to start (def 1)
  5. directory=/opt/kubernetes/server/bin ; directory to cwd to before exec (def no cwd)
  6. autostart=true ; start at supervisord start (default: true)
  7. autorestart=true ; retstart at unexpected quit (default: true)
  8. startsecs=30 ; number of secs prog must stay running (def. 1)
  9. startretries=3 ; max # of serial start failures (default 3)
  10. exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
  11. stopsignal=QUIT ; signal used to kill process (default TERM)
  12. stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
  13. user=root ; setuid to this UNIX account to run the program
  14. redirect_stderr=true ; redirect proc stderr to stdout (default false)
  15. stdout_logfile=/data/logs/kubernetes/kube-controller-manager/controller.stdout.log ; stderr log path, NONE for none; default AUTO
  16. stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB)
  17. stdout_logfile_backups=4 ; # of stdout logfile backups (default 10)
  18. stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
  19. stdout_events_enabled=false ; emit events on stdout writes (default false)
  20. stopasgroup=true
  21. killasgroup=true
  1. [root@hdss7-21 ~]# supervisorctl update
  2. [root@hdss7-21 ~]# supervisorctl status
  3. etcd-server-7-21 RUNNING pid 35582, uptime 1 day, 3:45:02
  4. kube-apiserver-7-21 RUNNING pid 37301, uptime 2:48:24
  5. kube-controller-manager-7-21 RUNNING pid 37496, uptime 0:01:06

3.5. 安装kube-scheduler组件

kube-scheduler 涉及的服务器:hdss7-21,hdss7-22

kube-scheduler 设置为只调用当前机器的 apiserver,走127.0.0.1网卡,因此不配制SSL证书

3.5.1. 配置kube-scheduler启动脚本

  1. [root@hdss7-21 ~]# vim /opt/kubernetes/server/bin/kube-scheduler-startup.sh
  2. #!/bin/sh
  3. WORK_DIR=$(dirname $(readlink -f $0))
  4. [ $? -eq 0 ] && cd $WORK_DIR || exit
  5. /opt/kubernetes/server/bin/kube-scheduler \
  6. --leader-elect \
  7. --log-dir /data/logs/kubernetes/kube-scheduler \
  8. --master http://127.0.0.1:8080 \
  9. --v 2
  10. [root@hdss7-21 ~]# chmod u+x /opt/kubernetes/server/bin/kube-scheduler-startup.sh
  11. [root@hdss7-21 ~]# mkdir -p /data/logs/kubernetes/kube-scheduler

3.5.2. 配置supervisor启动配置

注意名字的修改

  1. [root@hdss7-21 ~]# vim /etc/supervisord.d/kube-scheduler.ini
  2. [program:kube-scheduler-7-21]
  3. command=/opt/kubernetes/server/bin/kube-scheduler-startup.sh
  4. numprocs=1
  5. directory=/opt/kubernetes/server/bin
  6. autostart=true
  7. autorestart=true
  8. startsecs=30
  9. startretries=3
  10. exitcodes=0,2
  11. stopsignal=QUIT
  12. stopwaitsecs=10
  13. user=root
  14. redirect_stderr=true
  15. stdout_logfile=/data/logs/kubernetes/kube-scheduler/scheduler.stdout.log
  16. stdout_logfile_maxbytes=64MB
  17. stdout_logfile_backups=4
  18. stdout_capture_maxbytes=1MB
  19. stdout_events_enabled=false
  20. stopasgroup=true
  21. killasgroup=true
  1. [root@hdss7-21 ~]# supervisorctl update
  2. [root@hdss7-21 ~]# supervisorctl status
  3. etcd-server-7-21 RUNNING pid 35582, uptime 1 day, 3:46:44
  4. kube-apiserver-7-21 RUNNING pid 37301, uptime 2:50:06
  5. kube-controller-manager-7-21 RUNNING pid 37496, uptime 0:02:48
  6. kube-scheduler-7-21 RUNNING pid 37522, uptime 0:00:43

3.6. 检查节点组件的健康

  1. [root@hdss7-21 ~]# ln -s /opt/kubernetes/server/bin/kubectl /usr/local/bin/
  2. [root@hdss7-21 ~]# kubectl get cs
  3. NAME STATUS MESSAGE ERROR
  4. controller-manager Healthy ok
  5. scheduler Healthy ok
  6. etcd-0 Healthy {"health": "true"}
  7. etcd-1 Healthy {"health": "true"}
  8. etcd-2 Healthy {"health": "true"}

4. node节点组件部署

4.1. kubelet组件安装

4.1.1. 签发证书

证书签发在 hdss7-200 操作

  1. [root@hdss7-200 ~]# cd /opt/certs/
  2. [root@hdss7-200 certs]# vim kubelet-csr.json # 将所有可能的kubelet机器IP添加到hosts中
  3. {
  4. "CN": "k8s-kubelet",
  5. "hosts": [
  6. "127.0.0.1",
  7. "10.4.7.10",
  8. "10.4.7.21",
  9. "10.4.7.22",
  10. "10.4.7.23",
  11. "10.4.7.24",
  12. "10.4.7.25",
  13. "10.4.7.26",
  14. "10.4.7.27",
  15. "10.4.7.28"
  16. ],
  17. "key": {
  18. "algo": "rsa",
  19. "size": 2048
  20. },
  21. "names": [
  22. {
  23. "C": "CN",
  24. "ST": "beijing",
  25. "L": "beijing",
  26. "O": "odl",
  27. "OU": "ops"
  28. }
  29. ]
  30. }
  31. [root@hdss7-200 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server kubelet-csr.json | cfssl-json -bare kubelet
  32. 2020/01/06 23:10:56 [INFO] generate received request
  33. 2020/01/06 23:10:56 [INFO] received CSR
  34. 2020/01/06 23:10:56 [INFO] generating key: rsa-2048
  35. 2020/01/06 23:10:56 [INFO] encoded CSR
  36. 2020/01/06 23:10:56 [INFO] signed certificate with serial number 61221942784856969738771370531559555767101820379
  37. 2020/01/06 23:10:56 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
  38. websites. For more information see the Baseline Requirements for the Issuance and Management
  39. of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
  40. specifically, section 10.2.3 ("Information Requirements").
  41. [root@hdss7-200 certs]# ls kubelet* -l
  42. -rw-r--r-- 1 root root 1115 Jan 6 23:10 kubelet.csr
  43. -rw-r--r-- 1 root root 452 Jan 6 23:10 kubelet-csr.json
  44. -rw------- 1 root root 1675 Jan 6 23:10 kubelet-key.pem
  45. -rw-r--r-- 1 root root 1468 Jan 6 23:10 kubelet.pem
  46. [root@hdss7-200 certs]# scp kubelet.pem kubelet-key.pem hdss7-21:/opt/kubernetes/server/bin/certs/
  47. [root@hdss7-200 certs]# scp kubelet.pem kubelet-key.pem hdss7-22:/opt/kubernetes/server/bin/certs/

4.1.2. 创建kubelet配置

配置在 hdss7-21 hdss7-22 操作

  • set-cluster # 创建需要连接的集群信息,可以创建多个k8s集群信息

    1. [root@hdss7-21 ~]# kubectl config set-cluster myk8s \
    2. --certificate-authority=/opt/kubernetes/server/bin/certs/ca.pem \
    3. --embed-certs=true \
    4. --server=https://10.4.7.10:7443 \
    5. --kubeconfig=/opt/kubernetes/conf/kubelet.kubeconfig
    6. #-----------------------
    7. Cluster "myk8s" set.
  • set-credentials # 创建用户账号,即用户登陆使用的客户端私有和证书,可以创建多个证书

    1. [root@hdss7-21 ~]# kubectl config set-credentials k8s-node \
    2. --client-certificate=/opt/kubernetes/server/bin/certs/client.pem \
    3. --client-key=/opt/kubernetes/server/bin/certs/client-key.pem \
    4. --embed-certs=true \
    5. --kubeconfig=/opt/kubernetes/conf/kubelet.kubeconfig
    6. #--------
    7. User "k8s-node" set.
  • set-context # 设置context,即确定账号和集群对应关系

  • use-context # 设置当前使用哪个context

    1. [root@hdss7-21 ~]# kubectl config use-context myk8s-context --kubeconfig=/opt/kubernetes/conf/kubelet.kubeconfig
    2. #------------------------
    3. Switched to context "myk8s-context".
    4. [root@hdss7-21 ~]# ll /opt/kubernetes/conf/kubelet.kubeconfig
    5. -rw-------. 1 root root 6203 9 20 20:57 /opt/kubernetes/conf/kubelet.kubeconfig
  • hdss7-22节点可复制hdss-21的kubelet.kubeconfig文件至对应目录,无需重复执行上门面的操作

    1. [root@hdss7-22 ~]# scp hdss7-21:/opt/kubernetes/conf/kubelet.kubeconfig /opt/kubernetes/conf/

    4.1.3. 授权k8s-node用户

    此步骤只需要在一台master节点执行 hdss7-21

授权 k8s-node 用户绑定集群角色 system:node ,让 k8s-node 成为具备运算节点的权限
k8s-node 为 用户账户

  1. [root@hdss7-21 ~]# vim k8s-node.yaml
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. kind: ClusterRoleBinding
  4. metadata:
  5. name: k8s-node
  6. roleRef:
  7. apiGroup: rbac.authorization.k8s.io
  8. kind: ClusterRole
  9. name: system:node
  10. subjects:
  11. - apiGroup: rbac.authorization.k8s.io
  12. kind: User
  13. name: k8s-node
  14. [root@hdss7-21 ~]# kubectl create -f k8s-node.yaml
  15. clusterrolebinding.rbac.authorization.k8s.io/k8s-node created
  16. [root@hdss7-21 ~]# kubectl get clusterrolebinding k8s-node
  17. NAME AGE
  18. k8s-node 36s

4.1.4. 准备pause镜像

将pause镜像放入到harbor私有仓库中,仅在 hdss7-200 操作

  1. [root@hdss7-200 ~]# docker image pull kubernetes/pause
  2. [root@hdss7-200 ~]# docker image tag kubernetes/pause:latest harbor.odl.com/public/pause:latest
  3. [root@hdss7-200 ~]# docker login -u admin harbor.odl.com
  4. [root@hdss7-200 ~]# docker push harbor.odl.com/public/pause:latest
  5. The push refers to repository [harbor.odl.com/public/pause]
  6. 5f70bf18a086: Mounted from public/nginx
  7. e16a89738269: Pushed
  8. latest: digest: sha256:b31bfb4d0213f254d361e0079deaaebefa4f82ba7aa76ef82e90b4935ad5b105 size: 93

4.1.5. 创建kubelet启动脚本

在node节点创建脚本并启动kubelet,涉及服务器: hdss7-21 hdss7-22

—hostname-override 需修改

  1. [root@hdss7-21 ~]# vim /opt/kubernetes/server/bin/kubelet-startup.sh
  2. #!/bin/sh
  3. WORK_DIR=$(dirname $(readlink -f $0))
  4. [ $? -eq 0 ] && cd $WORK_DIR || exit
  5. /opt/kubernetes/server/bin/kubelet \
  6. --anonymous-auth=false \
  7. --cgroup-driver systemd \
  8. --cluster-dns 192.168.0.2 \
  9. --cluster-domain cluster.local \
  10. --runtime-cgroups=/systemd/system.slice \
  11. --kubelet-cgroups=/systemd/system.slice \
  12. --fail-swap-on="false" \
  13. --client-ca-file ./certs/ca.pem \
  14. --tls-cert-file ./certs/kubelet.pem \
  15. --tls-private-key-file ./certs/kubelet-key.pem \
  16. --hostname-override hdss7-21.host.com \
  17. --image-gc-high-threshold 20 \
  18. --image-gc-low-threshold 10 \
  19. --kubeconfig ../../conf/kubelet.kubeconfig \
  20. --log-dir /data/logs/kubernetes/kube-kubelet \
  21. --pod-infra-container-image harbor.odl.com/public/pause:latest \
  22. --root-dir /data/kubelet
  23. [root@hdss7-21 ~]# chmod u+x /opt/kubernetes/server/bin/kubelet-startup.sh
  24. [root@hdss7-21 ~]# mkdir -p /data/logs/kubernetes/kube-kubelet /data/kubelet
  25. # 注意名字
  26. [root@hdss7-21 ~]# vim /etc/supervisord.d/kube-kubelet.ini
  27. [program:kube-kubelet-7-21]
  28. command=/opt/kubernetes/server/bin/kubelet-startup.sh
  29. numprocs=1
  30. directory=/opt/kubernetes/server/bin
  31. autostart=true
  32. autorestart=true
  33. startsecs=30
  34. startretries=3
  35. exitcodes=0,2
  36. stopsignal=QUIT
  37. stopwaitsecs=10
  38. user=root
  39. redirect_stderr=true
  40. stdout_logfile=/data/logs/kubernetes/kube-kubelet/kubelet.stdout.log
  41. stdout_logfile_maxbytes=64MB
  42. stdout_logfile_backups=5
  43. stdout_capture_maxbytes=1MB
  44. stdout_events_enabled=false
  45. stopasgroup=true
  46. killasgroup=true
  47. [root@hdss7-21 ~]# supervisorctl update
  48. [root@hdss7-21 ~]# supervisorctl status
  49. etcd-server-7-21 RUNNING pid 35582, uptime 1 day, 4:02:17
  50. kube-apiserver-7-21 RUNNING pid 37301, uptime 3:05:39
  51. kube-controller-manager-7-21 RUNNING pid 37496, uptime 0:18:21
  52. kube-kubelet-7-21 RUNNING pid 37650, uptime 0:00:32
  53. kube-scheduler-7-21 RUNNING pid 37522, uptime 0:16:16

4.1.6. 修改节点角色

  1. [root@hdss7-21 ~]# kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. hdss7-21.host.com Ready <none> 2m9s v1.15.3
  4. hdss7-22.host.com Ready <none> 13s v1.15.3
  5. [root@hdss7-21 ~]# kubectl label node hdss7-21.host.com node-role.kubernetes.io/node=
  6. node/hdss7-21.host.com labeled
  7. [root@hdss7-21 ~]# kubectl label node hdss7-21.host.com node-role.kubernetes.io/master=
  8. node/hdss7-21.host.com labeled
  9. [root@hdss7-21 ~]# kubectl label node hdss7-22.host.com node-role.kubernetes.io/master=
  10. node/hdss7-22.host.com labeled
  11. [root@hdss7-21 ~]# kubectl label node hdss7-22.host.com node-role.kubernetes.io/node=
  12. node/hdss7-22.host.com labeled
  13. [root@hdss7-21 ~]# kubectl get node
  14. NAME STATUS ROLES AGE VERSION
  15. hdss7-21.host.com Ready master,node 3m8s v1.15.3
  16. hdss7-22.host.com Ready master,node 72s v1.15.3

4.2. 安装kube-proxy组件

4.2.1. 签发证书

签发证书在hdss7-200操作

  1. [root@hdss7-200 ~]# cd /opt/certs/
  2. [root@hdss7-200 certs]# vim kube-proxy-csr.json # CN 其实是k8s中的角色
  3. {
  4. "CN": "system:kube-proxy",
  5. "key": {
  6. "algo": "rsa",
  7. "size": 2048
  8. },
  9. "names": [
  10. {
  11. "C": "CN",
  12. "ST": "beijing",
  13. "L": "beijing",
  14. "O": "odl",
  15. "OU": "ops"
  16. }
  17. ]
  18. }
  19. [root@hdss7-200 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=client kube-proxy-csr.json |cfssl-json -bare kube-proxy-client
  20. 2020/01/07 21:45:53 [INFO] generate received request
  21. 2020/01/07 21:45:53 [INFO] received CSR
  22. 2020/01/07 21:45:53 [INFO] generating key: rsa-2048
  23. 2020/01/07 21:45:53 [INFO] encoded CSR
  24. 2020/01/07 21:45:53 [INFO] signed certificate with serial number 620191685968917036075463174423999296907693104226
  25. 2020/01/07 21:45:53 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
  26. websites. For more information see the Baseline Requirements for the Issuance and Management
  27. of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
  28. [root@hdss7-200 certs]# ls kube-proxy-c* -l # 因为kube-proxy使用的用户是kube-proxy,不能使用client证书,必须要重新签发自己的证书
  29. -rw-r--r-- 1 root root 1005 Jan 7 21:45 kube-proxy-client.csr
  30. -rw------- 1 root root 1675 Jan 7 21:45 kube-proxy-client-key.pem
  31. -rw-r--r-- 1 root root 1375 Jan 7 21:45 kube-proxy-client.pem
  32. -rw-r--r-- 1 root root 267 Jan 7 21:45 kube-proxy-csr.json
  33. [root@hdss7-200 certs]# scp kube-proxy-client-key.pem kube-proxy-client.pem hdss7-21:/opt/kubernetes/server/bin/certs/
  34. [root@hdss7-200 certs]# scp kube-proxy-client-key.pem kube-proxy-client.pem hdss7-22:/opt/kubernetes/server/bin/certs/

4.2.2. 创建kube-proxy配置

在所有node节点创建,涉及服务器:hdss7-21 ,hdss7-22

  1. [root@hdss7-21 ~]# kubectl config set-cluster myk8s \
  2. --certificate-authority=/opt/kubernetes/server/bin/certs/ca.pem \
  3. --embed-certs=true \
  4. --server=https://10.4.7.10:7443 \
  5. --kubeconfig=/opt/kubernetes/conf/kube-proxy.kubeconfig
  6. [root@hdss7-21 ~]# kubectl config set-credentials kube-proxy \
  7. --client-certificate=/opt/kubernetes/server/bin/certs/kube-proxy-client.pem \
  8. --client-key=/opt/kubernetes/server/bin/certs/kube-proxy-client-key.pem \
  9. --embed-certs=true \
  10. --kubeconfig=/opt/kubernetes/conf/kube-proxy.kubeconfig
  11. [root@hdss7-21 ~]# kubectl config set-context myk8s-context \
  12. --cluster=myk8s \
  13. --user=kube-proxy \
  14. --kubeconfig=/opt/kubernetes/conf/kube-proxy.kubeconfig
  15. [root@hdss7-21 ~]# kubectl config use-context myk8s-context --kubeconfig=/opt/kubernetes/conf/kube-proxy.kubeconfig
  16. [root@hdss7-21 ~]# ll /opt/kubernetes/conf/kube-proxy.kubeconfig
  17. -rw-------. 1 root root 6219 9 20 21:14 /opt/kubernetes/conf/kube-proxy.kubeconfig
  • hdss7-22节点可复制hdss-21的kube-proxy.kubeconfig 文件至对应目录,无需重复执行上门面的操作
    1. [root@hdss7-22 ~]# scp hdss7-21:/opt/kubernetes/conf/kube-proxy.kubeconfig /opt/kubernetes/conf/

4.2.3. 加载jpvs模块

kube-proxy 共有3种流量调度模式,分别是 namespace,iptables,ipvs,其中ipvs性能最好。

  1. [root@hdss7-21 ~]# for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done
  2. [root@hdss7-21 ~]# lsmod | grep ip_vs # 查看ipvs模块

4.2.4. 创建启动脚本

  1. # 修改--hostname-override
  2. [root@hdss7-21 ~]# vim /opt/kubernetes/server/bin/kube-proxy-startup.sh
  3. #!/bin/sh
  4. WORK_DIR=$(dirname $(readlink -f $0))
  5. [ $? -eq 0 ] && cd $WORK_DIR || exit
  6. /opt/kubernetes/server/bin/kube-proxy \
  7. --cluster-cidr 172.7.0.0/16 \
  8. --hostname-override hdss7-21.host.com \
  9. --proxy-mode=ipvs \
  10. --ipvs-scheduler=nq \
  11. --kubeconfig ../../conf/kube-proxy.kubeconfig
  12. [root@hdss7-21 ~]# chmod u+x /opt/kubernetes/server/bin/kube-proxy-startup.sh
  13. [root@hdss7-21 ~]# mkdir -p /data/logs/kubernetes/kube-proxy
  14. [root@hdss7-21 ~]# vim /etc/supervisord.d/kube-proxy.ini
  15. [program:kube-proxy-7-21]
  16. command=/opt/kubernetes/server/bin/kube-proxy-startup.sh
  17. numprocs=1
  18. directory=/opt/kubernetes/server/bin
  19. autostart=true
  20. autorestart=true
  21. startsecs=30
  22. startretries=3
  23. exitcodes=0,2
  24. stopsignal=QUIT
  25. stopwaitsecs=10
  26. user=root
  27. redirect_stderr=true
  28. stdout_logfile=/data/logs/kubernetes/kube-proxy/proxy.stdout.log
  29. stdout_logfile_maxbytes=64MB
  30. stdout_logfile_backups=5
  31. stdout_capture_maxbytes=1MB
  32. stdout_events_enabled=false
  33. stopasgroup=true
  34. killasgroup=true
  35. [root@hdss7-21 ~]# supervisorctl update
  36. [root@hdss7-21 ~]# supervisorctl status
  37. etcd-server-7-21 RUNNING pid 35582, uptime 1 day, 4:14:19
  38. kube-apiserver-7-21 RUNNING pid 37301, uptime 3:17:41
  39. kube-controller-manager-7-21 RUNNING pid 37496, uptime 0:30:23
  40. kube-kubelet-7-21 RUNNING pid 37650, uptime 0:12:34
  41. kube-proxy-7-21 RUNNING pid 40526, uptime 0:00:30
  42. kube-scheduler-7-21 RUNNING pid 37522, uptime 0:28:18

3.2.5. 验证集群

  1. [root@hdss7-21 ~]# supervisorctl status
  2. etcd-server-7-21 RUNNING pid 23637, uptime 2 days, 0:27:18
  3. kube-apiserver-7-21 RUNNING pid 32591, uptime 1 day, 2:06:47
  4. kube-controller-manager-7-21 RUNNING pid 33357, uptime 1 day, 0:11:02
  5. kube-kubelet-7-21 RUNNING pid 37232, uptime 9:32:01
  6. kube-proxy-7-21 RUNNING pid 47088, uptime 0:06:19
  7. kube-scheduler-7-21 RUNNING pid 33450, uptime 1 day, 0:01:43
  8. [root@hdss7-21 ~]# yum install -y ipvsadm
  9. [root@hdss7-21 ~]# ipvsadm -Ln
  10. IP Virtual Server version 1.2.1 (size=4096)
  11. Prot LocalAddress:Port Scheduler Flags
  12. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  13. TCP 192.168.0.1:443 nq
  14. -> 10.4.7.21:6443 Masq 1 0 0
  15. -> 10.4.7.22:6443 Masq 1 0 0
  • 创建一个nginx的deployment资源验证 ```bash [root@hdss7-21 ~]# vim nginx-ds.yaml apiVersion: extensions/v1beta1 kind: DaemonSet metadata: name: nginx-ds spec: template: metadata:
    1. labels:
    2. app: nginx-ds
    spec:
    1. containers:
    2. - name: my-nginx
    3. image: harbor.odl.com/public/nginx:v1.7.9
    4. ports:
    5. - containerPort: 80

[root@hdss7-21 ~]# kubectl create -f nginx-ds.yaml daemonset.extensions/nginx-ds created [root@hdss7-21 ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-ds-8flhn 1/1 Running 0 2m1s 172.7.22.2 hdss7-22.host.com nginx-ds-w4h22 1/1 Running 0 2m2s 172.7.21.2 hdss7-21.host.com

[root@hdss7-21 ~]# curl -I 172.7.21.2 HTTP/1.1 200 OK Server: nginx/1.7.9 Date: Sun, 20 Sep 2020 13:29:51 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 23 Dec 2014 16:25:09 GMT Connection: keep-alive ETag: “54999765-264” Accept-Ranges: bytes

[root@hdss7-21 ~]# curl -I 172.7.22.2 # 缺少网络插件,无法跨节点通信

  1. <a name="nNX83"></a>
  2. # 5. kubernetes插件安装
  3. <a name="HEIjv"></a>
  4. ## 5.1. flannel插件
  5. <a name="aZllG"></a>
  6. ### 5.1.1. flannel作用和模型
  7. kubernetes设计了网络模型,但是pod之间通信的具体实现交给了CNI往插件。 常用的CNI网络插件有:Flannel 、Calico、Canal、Contiv等,其中Flannel和Calico占比接近80%,Flannel占比略多于Calico<br /> Flannel的设计目的就是为集群中的所有节点重新规划IP地址的使用规则,从而使得不同节点上的容器能够获得“同属一个内网”且”不重复的”IP地址,并让属于不同节点上的容器能够直接通过内网IP通信。
  8. <a name="e518cd2a"></a>
  9. #### 5.1.1.1. host-gw模型 (当前教程使用)
  10. 只在节点中添加了一条路由规则实现pod之间的通信<br />节点需要在二层网络中, 指向同一个网关地址<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/2877637/1608020340006-dc8b33de-c7bf-4f1a-b503-2685ae71a9f1.png#align=left&display=inline&height=614&margin=%5Bobject%20Object%5D&name=image.png&originHeight=614&originWidth=1161&size=237040&status=done&style=none&width=1161)
  11. <a name="8wFWI"></a>
  12. #### 5.1.1.2. VxLAN模型
  13. 通过虚拟的网络隧道通信,会在节点中添加flannel.1虚拟网卡
  14. ```bash
  15. ./etcdctl set /coreos.com/network/config/ '{"Network": "172.7.0.0/16", "Backend": {"Type": "VxLAN"}}''

image.png

5.1.1.3. 直接路由模型

host-gw和VxLAN结合模型, flannel自动判断互相通信的节点所需模型路由设置

5.1.2. 准备flannel文件

在 hdss7-21 hdss7-22 操作

  1. [root@hdss7-21 ~]# wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz
  2. # flannel压缩包没有目录 需要创建一个主目录
  3. [root@hdss7-21 ~]# mkdir /opt/flannel-v0.11.0
  4. [root@hdss7-21 ~]# tar-xf flannel-v0.11.0-linux-amd64.tar.gz -C /opt/flannel-v0.11.0
  5. [root@hdss7-21 ~]# cd /opt/ ; ln -s flannel-v0.11.0 flannel
  6. # 设置ip_forward,ip_forward等于0,则在重启的时候会网络不通
  7. echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
  8. sysctl -p

5.1.3. 拷贝证书

  1. # flannel需要以客户端的身份访问etcd, 需要相关的证书
  2. [root@hdss7-21 opt]# mkdir /opt/flannel-v0.11.0/certs
  3. # 切换至hdss7-200节点
  4. [root@hdss7-200 ~]# cd /opt/certs
  5. [root@hdss7-200 certs]# scp ca.pem client-key.pem client.pem hdss7-21:/opt/flannel/certs/

5.1.4. 配置文件

  1. # 创建子网信息
  2. [root@hdss7-21 ~]# cat /opt/flannel/subnet.env
  3. FLANNEL_NETWORK=172.7.0.0/16
  4. FLANNEL_SUBNET=172.7.21.1/24
  5. FLANNEL_MTU=1500
  6. FLANNEL_IPMASQ=false
  7. # etcd配置, 只需要一台etcd节点运行即可
  8. [root@hdss7-21 ~]# /opt/etcd/etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}'
  9. # 查看配置是否成功
  10. [root@hdss7-21 ~]# /opt/etcd/etcdctl get /coreos.com/network/config
  11. {"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}

[root@hdss7-21 ~]# /opt/etcd/etcdctl get /coreos.com/network/config Error: 100: Key not found (/coreos.com) [10] 该报错为etcd没有记录到flannel信息 该报错在etcd恢复集群数据时候丢失了配置信息

5.1.5. 创建flannel启动脚本

在 hdss7-21 hdss7-22 操作 —public-ip 为本机IP —iface 为当前宿主机的对外网卡

  1. [root@hdss7-21 ~]# vim /opt/flannel/flannel-startup.sh
  2. #!/bin/sh
  3. WORK_DIR=$(dirname $(readlink -f $0))
  4. [ $? -eq 0 ] && cd $WORK_DIR || exit
  5. /opt/flannel/flanneld \
  6. --public-ip=10.4.7.21 \
  7. --etcd-endpoints=https://10.4.7.12:2379,https://10.4.7.21:2379,https://10.4.7.22:2379 \
  8. --etcd-keyfile=./certs/client-key.pem \
  9. --etcd-certfile=./certs/client.pem \
  10. --etcd-cafile=./certs/ca.pem \
  11. --iface=ens33 \
  12. --subnet-file=./subnet.env \
  13. --healthz-port=2401
  14. [root@hdss7-21 ~]# chmod u+x /opt/flannel/flannel-startup.sh

5.1.6. 编写flannel supervisord启动文件

  1. [root@hdss7-21 ~]# vim /etc/supervisord.d/flannel.ini
  2. [program:flanneld-7-21]
  3. command=/opt/flannel/flannel-startup.sh
  4. numprocs=1
  5. directory=/opt/apps/flannel
  6. autostart=true
  7. autorestart=true
  8. startsecs=30
  9. startretries=3
  10. exitcodes=0,2
  11. stopsignal=QUIT
  12. stopwaitsecs=10
  13. user=root
  14. redirect_stderr=true
  15. stdout_logfile=/data/logs/flanneld/flanneld.stdout.log
  16. stdout_logfile_maxbytes=64MB
  17. stdout_logfile_backups=5
  18. stdout_capture_maxbytes=1MB
  19. stdout_events_enabled=false
  20. stopasgroup=true
  21. killasgroup=true
  22. [root@hdss7-21 ~]# mkdir -p /data/logs/flanneld/
  23. [root@hdss7-21 ~]# supervisorctl update
  24. flanneld-7-21: added process group
  25. [root@hdss7-21 ~]# supervisorctl status

image.png

5.1.7. 验证网络访问情况

  1. [root@hdss7-21 ~]# kubectl get pods -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. nginx-ds-br65n 1/1 Running 0 42h 172.7.21.2 hdss7-21.host.com <none> <none>
  4. nginx-ds-hpbbt 1/1 Running 0 42h 172.7.22.2 hdss7-22.host.com <none> <none>
  5. [root@hdss7-21 ~]# curl -I 172.7.22.2
  6. HTTP/1.1 200 OK
  7. Server: nginx/1.18.0
  8. Date: Mon, 16 Nov 2020 06:47:48 GMT
  9. Content-Type: text/html
  10. Content-Length: 988
  11. Last-Modified: Sat, 14 Nov 2020 12:09:29 GMT
  12. Connection: keep-alive
  13. ETag: "5fafc8f9-3dc"
  14. Accept-Ranges: byte

5.1.8. 解决pod之间IP透传问题

  1. 需所有node节点都要执行,优化NAT网络<br /> nginx pod A跨宿主机访问 nginx pod b时候, pod b看到的地址为pod a的宿主机地址<br /> 问题: 宿主机重启后, 当前节点无效<br /> 报错 Applying firewall rules: iptables-restore v1.4.21: Set KUBE-CLUSTER-IP doesn't exist.
  1. # 多次执行,
  2. [root@hdss7-21 ~]# kubectl exec -it nginx-ds-br65n -- /usr/bin/curl -I 172.7.22.2
  3. [root@hdss7-21 ~]# kubectl exec -it nginx-ds-hpbbt -- tail -f /config/log/nginx/access.log
  4. 10.4.7.21 - - [18/Nov/2020:01:49:39 +0000] "GET / HTTP/1.1" 200 988 "-" "curl/7.69.1"
  5. 10.4.7.21 - - [18/Nov/2020:01:52:47 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.69.1"
  6. 10.4.7.21 - - [18/Nov/2020:01:56:18 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.69.1"
  7. 10.4.7.21 - - [18/Nov/2020:02:00:28 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.69.1"
  8. [root@hdss7-21 ~]# iptables-save |grep POSTROUTING|grep docker # 引发问题的规则
  9. -A POSTROUTING -s 172.7.21.0/24 ! -o docker0 -j MASQUERADE
  • 需所有node节点都要执行,优化NAT网络
  1. [root@hdss7-21 ~]# yum install -y iptables-services
  2. # 启动
  3. [root@hdss7-21 ~]# systemctl start iptables.service ; systemctl enable iptables.service
  4. # 需要处理的规则:
  5. [root@hdss7-21 ~]# iptables-save |grep POSTROUTING|grep docker
  6. -A POSTROUTING -s 172.7.21.0/24 ! -o docker0 -j MASQUERADE
  7. [root@hdss7-21 ~]# iptables-save | grep -i reject
  8. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  9. -A FORWARD -j REJECT --reject-with icmp-host-prohibited
  10. # 处理方式:
  11. # 删除, 该规则在docker服务重启后会重新加入 注意-s参数IP
  12. [root@hdss7-21 ~]# iptables -t nat -D POSTROUTING -s 172.7.21.0/24 ! -o docker0 -j MASQUERADE
  13. # 新增,在10.4.7.21主机上,来源是172.7.21.0/24段的docker的IP,目标IP不是172.7.0.0/16段的,
  14. # 网络发包不是从docker0设备出站的, 才进行SNAT转换
  15. # 注意此处 host7-22节点 为 -s 172.7.22.0/24
  16. [root@hdss7-21 ~]# iptables -t nat -I POSTROUTING -s 172.7.21.0/24 ! -d 172.7.0.0/16 ! -o docker0 -j MASQUERADE
  17. # 删除规则,否则通信会失败
  18. [root@hdss7-21 ~]# iptables -t filter -D INPUT -j REJECT --reject-with icmp-host-prohibited
  19. [root@hdss7-21 ~]# iptables -t filter -D FORWARD -j REJECT --reject-with icmp-host-prohibited
  20. [root@hdss7-21 ~]# iptables-save > /etc/sysconfig/iptables
  • 多次执行, 此时显示的是pod的IP

    1. [root@hdss7-21 ~]# kubectl exec -it nginx-ds-br65n -- /usr/bin/curl -I 172.7.22.2
    2. [root@hdss7-21 ~]# kubectl exec -it nginx-ds-hpbbt -- tail -f /config/log/nginx/access.log
    3. 172.7.21.2 - - [18/Nov/2020:02:21:09 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.69.1"
    4. 172.7.21.2 - - [18/Nov/2020:02:37:47 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.69.1"
    5. 172.7.21.2 - - [18/Nov/2020:02:37:50 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.69.1"
    6. 172.7.21.2 - - [18/Nov/2020:02:37:52 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.69.1"
    7. 172.7.21.2 - - [18/Nov/2020:02:38:37 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.69.1"
  • docker重启后会重新出现规则,需重新配置,或者删除 ```bash [root@hdss7-21 ~]# systemctl restart docker [root@hdss7-21 ~]# iptables-save |grep -i postrouting|grep docker0 -A POSTROUTING -s 172.7.22.0/24 ! -o docker0 -j MASQUERADE

可以用iptables-restore重新应用iptables规则,也可以直接再删

[root@hdss7-21 ~]# iptables-save |grep -i postrouting|grep docker0 -A POSTROUTING -s 172.7.22.0/24 ! -d 172.7.0.0/16 ! -o docker0 -j MASQUERADE

  1. <a name="Wo4an"></a>
  2. ## 5.2. CoreDns
  3. k8s-coredns 实现了集群内部通过服务名进行互相定位的过程。<br /> Coredns从kubernetes-v1.11版本后取代了kube-dns<br /> 应用场景:
  4. - 服务(应用)的动态性强<br />
  5. - 服务(应用)更新发布频繁<br />
  6. - 服务(应用)支持自动伸缩<br />Pod的IP在不断的变化,就需要<br />
  7. - 抽象出service资源, 通过标签选择器,关联一组Pod<br />
  8. - 抽象出了集群网络, 通过相对固定的"集群IP",使服务接入点固定<br />
  9. Kubernetes内部域名解析原理、弊端及优化方式<br />[http://ccnuo.com/2019/08/25/CoreDNS%EF%BC%9AKubernetes%E5%86%85%E9%83%A8%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90%E5%8E%9F%E7%90%86%E3%80%81%E5%BC%8A%E7%AB%AF%E5%8F%8A%E4%BC%98%E5%8C%96%E6%96%B9%E5%BC%8F/](http://ccnuo.com/2019/08/25/CoreDNS%EF%BC%9AKubernetes%E5%86%85%E9%83%A8%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90%E5%8E%9F%E7%90%86%E3%80%81%E5%BC%8A%E7%AB%AF%E5%8F%8A%E4%BC%98%E5%8C%96%E6%96%B9%E5%BC%8F/)
  10. <a name="8Cafz"></a>
  11. ### 5.2.1. 准备资源配置清单
  12. 将清单文件放置在hdss7-200:/data/k8s-yaml/coredns_1.6.1中<br />文件参考地址: [https://github.com/kubernetes/kubernetes/blob/master/cluster/addons/dns/coredns/coredns.yaml.base](https://github.com/kubernetes/kubernetes/blob/master/cluster/addons/dns/coredns/coredns.yaml.base)
  13. <a name="NWQtk"></a>
  14. #### 5.2.1.1. rbac.yaml
  15. ```yaml
  16. apiVersion: v1
  17. kind: ServiceAccount
  18. metadata:
  19. name: coredns
  20. namespace: kube-system
  21. labels:
  22. kubernetes.io/cluster-service: "true"
  23. addonmanager.kubernetes.io/mode: Reconcile
  24. ---
  25. apiVersion: rbac.authorization.k8s.io/v1
  26. kind: ClusterRole
  27. metadata:
  28. labels:
  29. kubernetes.io/bootstrapping: rbac-defaults
  30. addonmanager.kubernetes.io/mode: Reconcile
  31. name: system:coredns
  32. rules:
  33. - apiGroups:
  34. - ""
  35. resources:
  36. - endpoints
  37. - services
  38. - pods
  39. - namespaces
  40. verbs:
  41. - list
  42. - watch
  43. ---
  44. apiVersion: rbac.authorization.k8s.io/v1
  45. kind: ClusterRoleBinding
  46. metadata:
  47. annotations:
  48. rbac.authorization.kubernetes.io/autoupdate: "true"
  49. labels:
  50. kubernetes.io/bootstrapping: rbac-defaults
  51. addonmanager.kubernetes.io/mode: EnsureExists
  52. name: system:coredns
  53. roleRef:
  54. apiGroup: rbac.authorization.k8s.io
  55. kind: ClusterRole
  56. name: system:coredns
  57. subjects:
  58. - kind: ServiceAccount
  59. name: coredns
  60. namespace: kube-system

5.2.1.2. configmap.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: coredns
  5. namespace: kube-system
  6. data:
  7. Corefile: |
  8. .:53 {
  9. errors
  10. log
  11. health
  12. ready
  13. kubernetes cluster.local 192.168.0.0/16
  14. forward . 10.4.7.11
  15. cache 30
  16. loop
  17. reload
  18. loadbalance
  19. }
  • errors : 将错误记录到stdout
  • health : CoreDNS的运行状况报告为http://localhost:8080/health
  • ready: 全部插件已经加载完成时,将通过 endpoints 在 8081 端口返回 HTTP 状态 200。
  • kubernetes : coreDNS将根据kubernetes服务和pod的IP回复DNS查询
  • forward : 转发上游DNS
  • cache 启动前端缓存
  • loop : 检测简单的转发循环,如果找到循环则停止CoreDNS进程
  • reload : 允许自动重新加载已更改的CoreFile, 编辑ConfigMap配置后,请等待两分钟以使更改生效
  • loadbalance : 这是一个循环DNS负载均衡器,可在答案中随机化A,AAAA和MX记录的顺序
  • prometheus:是否开启 CoreDNS Metrics 信息接口,如果配置则开启,接口地址为 http://localhost:9153/metrics

5.2.1.3. deployment.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: coredns
  5. namespace: kube-system
  6. labels:
  7. k8s-app: coredns
  8. kubernetes.io/name: "CoreDNS"
  9. spec:
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. k8s-app: coredns
  14. template:
  15. metadata:
  16. labels:
  17. k8s-app: coredns
  18. spec:
  19. priorityClassName: system-cluster-critical
  20. serviceAccountName: coredns
  21. containers:
  22. - name: coredns
  23. image: harbor.odl.com/public/coredns:v1.6.1
  24. args:
  25. - -conf
  26. - /etc/coredns/Corefile
  27. volumeMounts:
  28. - name: config-volume
  29. mountPath: /etc/coredns
  30. ports:
  31. - containerPort: 53
  32. name: dns
  33. protocol: UDP
  34. - containerPort: 53
  35. name: dns-tcp
  36. protocol: TCP
  37. - containerPort: 9153
  38. name: metrics
  39. protocol: TCP
  40. livenessProbe:
  41. httpGet:
  42. path: /health
  43. port: 8080
  44. scheme: HTTP
  45. initialDelaySeconds: 60
  46. timeoutSeconds: 5
  47. successThreshold: 1
  48. failureThreshold: 5
  49. dnsPolicy: Default
  50. volumes:
  51. - name: config-volume
  52. configMap:
  53. name: coredns
  54. items:
  55. - key: Corefile
  56. path: Corefile

5.2.1.4. service.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: coredns
  5. namespace: kube-system
  6. labels:
  7. k8s-app: coredns
  8. kubernetes.io/cluster-service: "true"
  9. kubernetes.io/name: "CoreDNS"
  10. spec:
  11. selector:
  12. k8s-app: coredns
  13. clusterIP: 192.168.0.2
  14. ports:
  15. - name: dns
  16. port: 53
  17. protocol: UDP
  18. - name: dns-tcp
  19. port: 53
  20. - name: metrics
  21. port: 9153
  22. protocol: TCP

5.2.2. 交付coredns到k8s

  • 准备镜像

    1. [root@hdss7-200 ~]# docker pull coredns/coredns:1.6.1
    2. [root@hdss7-200 ~]# docker image tag coredns/coredns:1.6.1 harbor.odl.com/public/coredns:v1.6.1
    3. [root@hdss7-200 ~]# docker image push harbor.odl.com/public/coredns:v1.6.1
  • 交付coredns

Coredns的cluster-ip为192.168.0.2, 在kubelet启动的时候参数cluster-dns 192.168.0.2

  1. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/coredns/coredns_1.6.1/rbac.yaml
  2. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/coredns/coredns_1.6.1/configmap.yaml
  3. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/coredns/coredns_1.6.1/deployment.yaml
  4. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/coredns/coredns_1.6.1/service.yaml
  5. [root@hdss7-21 ~]# kubectl get all -n kube-system
  6. NAME READY STATUS RESTARTS AGE
  7. pod/coredns-7674f74c44-9n298 1/1 Running 0 23h
  8. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  9. service/coredns ClusterIP 192.168.0.2 <none> 53/UDP,53/TCP,9153/TCP 27d
  10. NAME READY UP-TO-DATE AVAILABLE AGE
  11. deployment.apps/coredns 1/1 1 1 27d
  12. NAME DESIRED CURRENT READY AGE
  13. replicaset.apps/coredns-7674f74c44 1 1 1 27d

5.2.3. 集群内测试DNS

  • FQDN格式为

service服务名 . + 命名空间 . + 服务 . + cluster.local
nginx-dp.kube-public.svc.cluster.local
Pod内 可以使用短域名 nginx-dp.kube-public , 为什么?
查看Pod容器里面的/etc/resolv.conf文件定义了
nameserver 192.168.0.2
search default.svc.cluster.local svc.cluster.local cluster.local host.com

  1. # 创建service
  2. [root@hdss7-21 ~]# kubectl create deployment nginx-web --image=harbor.od.com/public/nginx:v1.18.0
  3. [root@hdss7-21 ~]# kubectl expose deployment nginx-web --port=80 --target-port=80
  4. [root@hdss7-21 ~]# kubectl get svc
  5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  6. kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 44h
  7. nginx-web ClusterIP 192.168.126.189 <none> 80/TCP 42h
  8. # 测试DNS,集群外必须使用FQDN(Fully Qualified Domain Name),全域名
  9. [root@hdss7-21 ~]# dig -t A nginx-web.default.svc.cluster.local @192.168.0.2 +short # 内网解析OK
  10. 192.168.164.230
  11. # # 外网解析OK
  12. [root@hdss7-21 ~]# dig -t A www.baidu.com @192.168.0.2 +short
  13. www.a.shifen.com.
  14. 180.101.49.11
  15. 180.101.49.12

5.3. Ingress-Controller

5.3.1. 描述

将services服务暴露至外部访问
service是将一组pod管理起来,提供了一个cluster ip和service name的统一访问入口,屏蔽了pod的ip变化。
ingress是K8s API的标准资源类型之一, 也是一种核心资源, 它其实就是一组基于域名和URL路径,,吧用户的请求转发至指定Service资源的规则
可以将集群外部的请求流量,转发至集群内部,从而实现”服务暴露”
ingress-controller 是一个代理服务器,将ingress的规则能真正实现的方式,常用的有 nginx,traefik,haproxy。但是在k8s集群中,建议使用traefik,性能比haroxy强大,更新配置不需要重载服务,是首选的ingress-controller。
注意:

  • 设置成ipvs就需要此ingress插件 (不推荐代理https,证书要设置在集群内比较麻烦)

5.3.2. 配置traefik资源清单

清单存放到hdss7-200:/data/k8s-yaml/traefik_1.7.2 中

参考文件: https://github.com/traefik/traefik/blob/v1.7/examples/k8s/

5.3.2.1. rbac.yaml

  1. # 创建了一个服务用户
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: traefik-ingress-controller
  6. namespace: kube-system
  7. ---
  8. # 绑定角色的集群权限
  9. apiVersion: rbac.authorization.k8s.io/v1beta1
  10. kind: ClusterRole
  11. metadata:
  12. name: traefik-ingress-controller
  13. rules:
  14. - apiGroups:
  15. - ""
  16. resources:
  17. - services
  18. - endpoints
  19. - secrets
  20. verbs:
  21. - get
  22. - list
  23. - watch
  24. - apiGroups:
  25. - extensions
  26. resources:
  27. - ingresses
  28. verbs:
  29. - get
  30. - list
  31. - watch
  32. ---
  33. # 绑定服务角色(ServiceAccount)和 集群权限 (ClusterRole)
  34. kind: ClusterRoleBinding
  35. apiVersion: rbac.authorization.k8s.io/v1beta1
  36. metadata:
  37. name: traefik-ingress-controller
  38. roleRef:
  39. apiGroup: rbac.authorization.k8s.io
  40. kind: ClusterRole
  41. name: traefik-ingress-controller
  42. subjects:
  43. - kind: ServiceAccount
  44. name: traefik-ingress-controller
  45. namespace: kube-system

5.3.2.2. daemonset.yaml

  1. 暴露了两个端口 controller( 80-->81对外端口) admin-web(8080 traefik管理页面 serivce服务端口)<br /> name: controller 名字指定不能超过15个字符,否则报错<br /> --kubernetes.endpoint
  1. apiVersion: extensions/v1beta1
  2. kind: DaemonSet
  3. metadata:
  4. name: traefik-ingress
  5. namespace: kube-system
  6. labels:
  7. k8s-app: traefik-ingress
  8. spec:
  9. template:
  10. metadata:
  11. labels:
  12. k8s-app: traefik-ingress
  13. name: traefik-ingress
  14. spec:
  15. serviceAccountName: traefik-ingress-controller
  16. terminationGracePeriodSeconds: 60
  17. containers:
  18. - image: harbor.odl.com/public/traefik:v1.7.2
  19. name: traefik-ingress
  20. ports:
  21. - name: controller
  22. containerPort: 80
  23. hostPort: 81
  24. - name: admin-web
  25. containerPort: 8080
  26. securityContext:
  27. capabilities:
  28. drop:
  29. - ALL
  30. add:
  31. - NET_BIND_SERVICE
  32. args:
  33. - --api
  34. - --kubernetes
  35. - --logLevel=INFO
  36. - --insecureskipverify=true
  37. - --kubernetes.endpoint=https://10.4.7.10:7443
  38. - --accesslog
  39. - --accesslog.filepath=/var/log/traefik_access.log
  40. - --traefiklog
  41. - --traefiklog.filepath=/var/log/traefik.log
  42. - --metrics.prometheus

5.3.2.3. service.yaml

  1. kind: Service
  2. apiVersion: v1
  3. metadata:
  4. name: traefik-ingress-service
  5. namespace: kube-system
  6. spec:
  7. selector:
  8. k8s-app: traefik-ingress
  9. ports:
  10. - protocol: TCP
  11. port: 80
  12. name: controller
  13. - protocol: TCP
  14. port: 8080
  15. name: admin-web

5.3.2.4. ingress.yaml

修改

    • host: traefik.odl.com 指定域名
    • path: / 指定uri
  • erviceName: traefik-ingress-service Service服务名
  • servicePort: 8080 Service服务对外提供服务端口
  1. apiVersion: extensions/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: traefik-web-ui
  5. namespace: kube-system
  6. annotations:
  7. kubernetes.io/ingress.class: traefik
  8. spec:
  9. rules:
  10. - host: traefik.odl.com
  11. http:
  12. paths:
  13. - path: /
  14. backend:
  15. serviceName: traefik-ingress-service
  16. servicePort: 8080

5.3.3. 准备镜像并交付

  1. [root@hdss7-200 ~]# docker pull traefik:v1.7.2-alpine
  2. [root@hdss7-200 ~]# docker image tag traefik:v1.7.2-alpine harbor.odl.com/public/traefik:v1.7.2
  3. [root@hdss7-200 ~]# docker push harbor.odl.com/public/traefik:v1.7.2

5.3.4. 交付traefik到k8s

  1. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/traefik/traefik_1.7.2/rbac.yaml
  2. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/traefik/traefik_1.7.2/daemonset.yaml
  3. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/traefik/traefik_1.7.2/service.yaml
  4. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/traefik/traefik_1.7.2/ingress.yaml
  5. [root@hdss7-21 ~]# kubectl get pods -n kube-system -o wide
  6. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  7. coredns-7674f74c44-4jvm7 1/1 Running 1 2d14h 172.7.21.2 hdss7-21.host.com <none> <none>
  8. traefik-ingress-56n56 1/1 Running 1 2d13h 172.7.22.7 hdss7-22.host.com <none> <none>
  9. traefik-ingress-lz4cv 1/1 Running 1 2d13h 172.7.21.3 hdss7-21.host.com <none> <none>

5.3.5. 配置外部nginx负载均衡

在hdss7-11,hdss7-12 配置nginx L7转发, 将服务暴露给外部客户端访问

  • 注意

当K8s一个节点需要维护的时候,需要提前在配置文件中剔除,避免流量转发至该维护的节点

  1. [root@hdss7-11 ~]# vim /etc/nginx/conf.d/odl.conf.conf
  2. server {
  3. server_name *.odl.com;
  4. location / {
  5. proxy_pass http://default_backend_traefik;
  6. proxy_set_header Host $http_host;
  7. proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
  8. }
  9. }
  10. upstream default_backend_traefik {
  11. # 所有的nodes都放到upstream中
  12. server 10.4.7.21:81 max_fails=3 fail_timeout=10s;
  13. server 10.4.7.22:81 max_fails=3 fail_timeout=10s;
  14. }

5.3.6. 配置DNS解析

  1. [root@hdss7-11 ~]# vim /var/named/od.com.zone
  2. $ORIGIN od.com.
  3. $TTL 600 ; 10 minutes
  4. @ IN SOA dns.od.com. dnsadmin.od.com. (
  5. 2020011302 ; serial
  6. 10800 ; refresh (3 hours)
  7. 900 ; retry (15 minutes)
  8. 604800 ; expire (1 week)
  9. 86400 ; minimum (1 day)
  10. )
  11. NS dns.od.com.
  12. $TTL 60 ; 1 minute
  13. dns A 10.4.7.11
  14. harbor A 10.4.7.200
  15. k8s-yaml A 10.4.7.200
  16. traefik A 10.4.7.10
  17. [root@hdss7-11 ~]# systemctl restart named

5.3.7. 查看traefik页面

网页地址: http://traefik.odl.com
image.png

5.3.8. 新建一个deployment测试是否能暴露端口

  • tomcat-test1.yaml ```yaml

    ——-Deployment————————

    apiVersion: apps/v1 kind: Deployment metadata: name: tomcat-test1 labels: app: tomcat-test1 spec: replicas: 1 selector: matchLabels:
    1. app: tomcat-test1
    template: metadata:
    1. labels:
    2. app: tomcat-test1
    spec:
    1. containers:
    2. - name: tomcat-test1
    3. image: harbor.odl.com/public/tomcat:v1.0
    4. imagePullPolicy: IfNotPresent
    5. ports:
    6. - containerPort: 8443
    7. - containerPort: 8080

———service———————-

apiVersion: v1 kind: Service metadata: name: tomcat-test1 labels: name: tomcat-test1 spec: ports:

  • port: 8443 targetPort: 8443 selector: app: tomcat-test1 ports:
  • port: 8080 targetPort: 8080 selector: app: tomcat-test1 ```
  • ingress-tomcat1.yaml

    1. apiVersion: extensions/v1beta1
    2. kind: Ingress
    3. metadata:
    4. name: tomcat-test1-web
    5. namespace: default
    6. annotations:
    7. kubernetes.io/ingress.class: traefik
    8. spec:
    9. rules:
    10. - host: tomcat.odl.com
    11. http:
    12. paths:
    13. - path: /
    14. backend:
    15. serviceName: tomcat-test1
    16. servicePort: 8080
  • 交付至k8s

    1. [root@hdss7-21 ~]# kubectl apply -f tomcat-test1.yaml
    2. [root@hdss7-21 ~]# kubectl apply -f ingress-tomcat1.yaml
  • 配置DNS解析

    因hdss7-11和12配置了负载均衡,反向代理traefik,且两台主机为高可用, 需配置浮动IP

  1. [root@hdss7-11 ~]# vim /var/named/odl.com.zone
  2. $ORIGIN odl.com.
  3. $TTL 600 ; 10 minutes
  4. @ IN SOA dns.odl.com. dnsadmin.odl.com. (
  5. 2020091709 ; serial
  6. 10800 ; refresh (3 hours)
  7. 900 ; retry (15 minutes)
  8. 604800 ; expire (1 week)
  9. 86400 ; minimum (1 day)
  10. )
  11. NS dns.odl.com.
  12. $TTL 60 ; 1 minute
  13. dns A 10.4.7.11
  14. harbor A 10.4.7.200
  15. k8s-yaml A 10.4.7.200
  16. traefik A 10.4.7.10
  17. tomcat A 10.4.7.10
  18. [root@hdss7-11 ~]# systemctl restart named
  • 测试网页是否打开正常

    5.3.9. nginx无法获取客户端IP地址

    默认的nginx配置是无法显示真实的客户端IP

需要修改nginx的日志配置信息 其中$http_x_forwarded_for显示了客户端的IP地址和代理IP

  1. log_format access '$remote_addr - $remote_user [$time_local] "$request" '
  2. '$status $body_bytes_sent "$http_referer" '
  3. '"$http_user_agent" $http_x_forwarded_for '
  4. '"$upstream_addr" "$upstream_status" "$upstream_response_time" "$request_time"';
  5. access_log /config/log/nginx/access.log access;
  1. 172.7.21.3 - - [25/Dec/2020:16:30:51 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" 192.168.1.147, 10.4.7.200 "-" "-" "-" "0.000"

5.4. dashboard图形化

当前dashboard为2.0.1版本, 从1.9.0+版本开始集成了metrics-scraper来获取metrics-server采集的数据展示在dashboard的UI界面中

[root@hdss7-200 ~]# docker pull kubernetesui/metrics-scraper:v1.0.4 [root@hdss7-200 ~]# docker tag kubernetesui/metrics-scraper:v1.0.4 harbor.odl.com/public/metrics-scraper:v1.0.4 [root@hdss7-200 ~]# docker push harbor.odl.com/public/metrics-scraper:v1.0.4

  1. <a name="nO5Qw"></a>
  2. #### 5.4.1.1. dashboard.yaml
  3. ```yaml
  4. apiVersion: v1
  5. kind: Namespace
  6. metadata:
  7. name: kubernetes-dashboard
  8. labels:
  9. k8s-app: kubernetes-dashboard
  10. addonmanager.kubernetes.io/mode: Reconcile
  11. ---
  12. apiVersion: v1
  13. kind: ServiceAccount
  14. metadata:
  15. labels:
  16. k8s-app: kubernetes-dashboard
  17. addonmanager.kubernetes.io/mode: Reconcile
  18. name: kubernetes-dashboard
  19. namespace: kubernetes-dashboard
  20. ---
  21. kind: Service
  22. apiVersion: v1
  23. metadata:
  24. labels:
  25. k8s-app: kubernetes-dashboard
  26. kubernetes.io/cluster-service: "true"
  27. addonmanager.kubernetes.io/mode: Reconcile
  28. name: kubernetes-dashboard
  29. namespace: kubernetes-dashboard
  30. spec:
  31. ports:
  32. - port: 443
  33. targetPort: 8443
  34. selector:
  35. k8s-app: kubernetes-dashboard
  36. ---
  37. apiVersion: v1
  38. kind: Secret
  39. metadata:
  40. labels:
  41. k8s-app: kubernetes-dashboard
  42. addonmanager.kubernetes.io/mode: EnsureExists
  43. name: kubernetes-dashboard-certs
  44. namespace: kubernetes-dashboard
  45. type: Opaque
  46. ---
  47. apiVersion: v1
  48. kind: Secret
  49. metadata:
  50. labels:
  51. k8s-app: kubernetes-dashboard
  52. addonmanager.kubernetes.io/mode: EnsureExists
  53. name: kubernetes-dashboard-csrf
  54. namespace: kubernetes-dashboard
  55. type: Opaque
  56. data:
  57. csrf: ""
  58. ---
  59. apiVersion: v1
  60. kind: Secret
  61. metadata:
  62. labels:
  63. k8s-app: kubernetes-dashboard
  64. addonmanager.kubernetes.io/mode: EnsureExists
  65. name: kubernetes-dashboard-key-holder
  66. namespace: kubernetes-dashboard
  67. type: Opaque
  68. ---
  69. kind: ConfigMap
  70. apiVersion: v1
  71. metadata:
  72. labels:
  73. k8s-app: kubernetes-dashboard
  74. addonmanager.kubernetes.io/mode: EnsureExists
  75. name: kubernetes-dashboard-settings
  76. namespace: kubernetes-dashboard
  77. ---
  78. kind: Role
  79. apiVersion: rbac.authorization.k8s.io/v1
  80. metadata:
  81. labels:
  82. k8s-app: kubernetes-dashboard
  83. addonmanager.kubernetes.io/mode: Reconcile
  84. name: kubernetes-dashboard
  85. namespace: kubernetes-dashboard
  86. rules:
  87. - apiGroups: [""]
  88. resources: ["secrets"]
  89. resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs", "kubernetes-dashboard-csrf"]
  90. verbs: ["get", "update", "delete"]
  91. - apiGroups: [""]
  92. resources: ["configmaps"]
  93. resourceNames: ["kubernetes-dashboard-settings"]
  94. verbs: ["get", "update"]
  95. - apiGroups: [""]
  96. resources: ["services"]
  97. resourceNames: ["heapster", "dashboard-metrics-scraper"]
  98. verbs: ["proxy"]
  99. - apiGroups: [""]
  100. resources: ["services/proxy"]
  101. resourceNames: ["heapster", "http:heapster:", "https:heapster:", "dashboard-metrics-scraper", "http:dashboard-metrics-scraper"]
  102. verbs: ["get"]
  103. ---
  104. kind: ClusterRole
  105. apiVersion: rbac.authorization.k8s.io/v1
  106. metadata:
  107. labels:
  108. k8s-app: kubernetes-dashboard
  109. addonmanager.kubernetes.io/mode: Reconcile
  110. name: kubernetes-dashboard
  111. rules:
  112. - apiGroups: ["metrics.k8s.io"]
  113. resources: ["pods", "nodes"]
  114. verbs: ["get", "list", "watch"]
  115. ---
  116. apiVersion: rbac.authorization.k8s.io/v1
  117. kind: RoleBinding
  118. metadata:
  119. labels:
  120. k8s-app: kubernetes-dashboard
  121. addonmanager.kubernetes.io/mode: Reconcile
  122. name: kubernetes-dashboard
  123. namespace: kubernetes-dashboard
  124. roleRef:
  125. apiGroup: rbac.authorization.k8s.io
  126. kind: Role
  127. name: kubernetes-dashboard
  128. subjects:
  129. - kind: ServiceAccount
  130. name: kubernetes-dashboard
  131. namespace: kubernetes-dashboard
  132. ---
  133. apiVersion: rbac.authorization.k8s.io/v1
  134. kind: ClusterRoleBinding
  135. metadata:
  136. name: kubernetes-dashboard
  137. labels:
  138. k8s-app: kubernetes-dashboard
  139. addonmanager.kubernetes.io/mode: Reconcile
  140. roleRef:
  141. apiGroup: rbac.authorization.k8s.io
  142. kind: ClusterRole
  143. name: kubernetes-dashboard
  144. subjects:
  145. - kind: ServiceAccount
  146. name: kubernetes-dashboard
  147. namespace: kubernetes-dashboard
  148. ---
  149. kind: Deployment
  150. apiVersion: apps/v1
  151. metadata:
  152. labels:
  153. k8s-app: kubernetes-dashboard
  154. name: kubernetes-dashboard
  155. namespace: kubernetes-dashboard
  156. spec:
  157. replicas: 1
  158. revisionHistoryLimit: 10
  159. selector:
  160. matchLabels:
  161. k8s-app: kubernetes-dashboard
  162. template:
  163. metadata:
  164. labels:
  165. k8s-app: kubernetes-dashboard
  166. spec:
  167. containers:
  168. - name: kubernetes-dashboard
  169. image: harbor.odl.com/public/kubernetes-dashboard:v2.0.1
  170. imagePullPolicy: Always
  171. ports:
  172. - containerPort: 8443
  173. protocol: TCP
  174. args:
  175. - --auto-generate-certificates
  176. - --namespace=kubernetes-dashboard
  177. volumeMounts:
  178. - name: kubernetes-dashboard-certs
  179. mountPath: /certs
  180. - mountPath: /tmp
  181. name: tmp-volume
  182. livenessProbe:
  183. httpGet:
  184. scheme: HTTPS
  185. path: /
  186. port: 8443
  187. initialDelaySeconds: 30
  188. timeoutSeconds: 30
  189. securityContext:
  190. allowPrivilegeEscalation: false
  191. readOnlyRootFilesystem: true
  192. runAsUser: 1001
  193. runAsGroup: 2001
  194. volumes:
  195. - name: kubernetes-dashboard-certs
  196. secret:
  197. secretName: kubernetes-dashboard-certs
  198. - name: tmp-volume
  199. emptyDir: {}
  200. serviceAccountName: kubernetes-dashboard
  201. nodeSelector:
  202. "kubernetes.io/os": linux
  203. tolerations:
  204. - key: "CriticalAddonsOnly"
  205. operator: "Exists"
  206. - key: node-role.kubernetes.io/master
  207. effect: NoSchedule
  208. ---
  209. kind: Service
  210. apiVersion: v1
  211. metadata:
  212. labels:
  213. k8s-app: dashboard-metrics-scraper
  214. name: dashboard-metrics-scraper
  215. namespace: kubernetes-dashboard
  216. spec:
  217. ports:
  218. - port: 8000
  219. targetPort: 8000
  220. selector:
  221. k8s-app: dashboard-metrics-scraper
  222. ---
  223. kind: Deployment
  224. apiVersion: apps/v1
  225. metadata:
  226. labels:
  227. k8s-app: dashboard-metrics-scraper
  228. name: dashboard-metrics-scraper
  229. namespace: kubernetes-dashboard
  230. spec:
  231. replicas: 1
  232. revisionHistoryLimit: 10
  233. selector:
  234. matchLabels:
  235. k8s-app: dashboard-metrics-scraper
  236. template:
  237. metadata:
  238. labels:
  239. k8s-app: dashboard-metrics-scraper
  240. annotations:
  241. seccomp.security.alpha.kubernetes.io/pod: 'runtime/default'
  242. spec:
  243. containers:
  244. - name: dashboard-metrics-scraper
  245. image: harbor.odl.com/public/metrics-scraper:v1.0.4
  246. ports:
  247. - containerPort: 8000
  248. protocol: TCP
  249. livenessProbe:
  250. httpGet:
  251. scheme: HTTP
  252. path: /
  253. port: 8000
  254. initialDelaySeconds: 30
  255. timeoutSeconds: 30
  256. volumeMounts:
  257. - mountPath: /tmp
  258. name: tmp-volume
  259. securityContext:
  260. allowPrivilegeEscalation: false
  261. readOnlyRootFilesystem: true
  262. runAsUser: 1001
  263. runAsGroup: 2001
  264. serviceAccountName: kubernetes-dashboard
  265. nodeSelector:
  266. "kubernetes.io/os": linux
  267. tolerations:
  268. - key: node-role.kubernetes.io/master
  269. effect: NoSchedule
  270. volumes:
  271. - name: tmp-volume
  272. emptyDir: {}

5.4.1.2. rbac.yaml

创建管理员用户资源, 官方创建的用户没有权限

  1. ---
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: dashboard-admin-sa
  6. namespace: kubernetes-dashboard
  7. labels:
  8. kubernetes.io/cluster-service: "true"
  9. addonmanager.kubernetes.io/mode: Reconcile
  10. ---
  11. apiVersion: rbac.authorization.k8s.io/v1
  12. kind: ClusterRoleBinding
  13. metadata:
  14. name: dashboard-admin-sa
  15. namespace: kubernetes-dashboard
  16. annotations:
  17. rbac.authorization.kubernetes.io/autoupdate: "true"
  18. roleRef:
  19. apiGroup: rbac.authorization.k8s.io
  20. kind: ClusterRole
  21. name: cluster-admin
  22. subjects:
  23. - kind: ServiceAccount
  24. name: dashboard-admin-sa
  25. namespace: kubernetes-dashboard

5.4.1.3. ingress.yaml

如果当前没有使用ingress来提供服务, 可在dashboard的资源清单service资源指定NodePort提供服务

  1. apiVersion: extensions/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: kubernetes-dashboard
  5. namespace: kubernetes-dashboard
  6. annotations:
  7. kubernetes.io/ingress.class: traefik
  8. spec:
  9. rules:
  10. - host: dashboard.odl.com
  11. http:
  12. paths:
  13. - backend:
  14. serviceName: kubernetes-dashboard
  15. servicePort: 443
  1. apiVersion: extensions/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: kubernetes-dashboard
  5. namespace: kubernetes-dashboard
  6. annotations:
  7. kubernetes.io/ingress.class: traefik
  8. spec:
  9. rules:
  10. - host: dashboard.odl.com
  11. http:
  12. paths:
  13. - backend:
  14. serviceName: kubernetes-dashboard
  15. servicePort: 443

5.4.2. dashboard交付至k8s

  1. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/dashboard_2.0.1/dashboard.yaml
  2. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/dashboard_2.0.1/rbac.yaml
  3. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.odl.com/dashboard_2.0.1/ingress.yaml

5.4.3. 配置 DNS

操作在hdss7-11 如果使用NodePort映射端口, 可忽略此步骤

  1. [root@hdss7-11 ~]# vim /var/named/odl.com.zone
  2. $ORIGIN odl.com.
  3. $TTL 600 ; 10 minutes
  4. @ IN SOA dns.odl.com. dnsadmin.odl.com. (
  5. 2020091712 ; serial
  6. 10800 ; refresh (3 hours)
  7. 900 ; retry (15 minutes)
  8. 604800 ; expire (1 week)
  9. 86400 ; minimum (1 day)
  10. )
  11. NS dns.odl.com.
  12. $TTL 60 ; 1 minute
  13. dns A 10.4.7.11
  14. harbor A 10.4.7.200
  15. k8s-yaml A 10.4.7.200
  16. traefik A 10.4.7.10
  17. dashboard A 10.4.7.10
  18. [root@hdss7-11 ~]# systemctl restart named

5.4.4. 登录dashboard界面

  • 查看用户token

    1. [root@hdss7-21 ~]# kubectl get secret -n kubernetes-dashboard
    2. NAME TYPE DATA AGE
    3. dashboard-admin-sa-token-qrkdl kubernetes.io/service-account-token 3 30m
    4. default-token-h4p79 kubernetes.io/service-account-token 3 37m
    5. kubernetes-dashboard-certs Opaque 0 37m
    6. kubernetes-dashboard-csrf Opaque 1 37m
    7. kubernetes-dashboard-key-holder Opaque 2 37m
    8. kubernetes-dashboard-token-n8t4c kubernetes.io/service-account-token 3 37m
    9. [root@hdss7-21 ~]# kubectl describe secret dashboard-admin-sa-token-qrkdl -n kubernetes-dashboard
    10. Name: dashboard-admin-sa-token-qrkdl
    11. Namespace: kubernetes-dashboard
    12. Labels: <none>
    13. Annotations: kubernetes.io/service-account.name: dashboard-admin-sa
    14. kubernetes.io/service-account.uid: 661f4adb-b51b-46d5-b9f8-966c91161f20
    15. Type: kubernetes.io/service-account-token
    16. Data
    17. ====
    18. ca.crt: 1346 bytes
    19. namespace: 20 bytes
    20. token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9......(省略)
  • 使用token登录

image.png

image.png

image.png