集群组件

角色 组成
Etcd Cluster etcd 1、etcd 2、etcd 3
Master kube-scheduler、kube-apiserver、kube-controller-manager
Node Kuberlet、kube-proxy

服务器划分

主机名 IP地址 部署组件
k8s-20.host.com 192.168.10.20 nginx、keepalive、bind、L4、L7、etcd1
k8s-30.host.com 192.168.10.30 nginx、keepalive、L4、L7、etcd2
k8s-40.host.com 192.168.10.40 etcd3、docker、kuber-apiserver、kube-scheduler、kube-controller-manager、kubelet、kube-proxy
k8s-50.host.com 192.168.10.50 docker、kuber-apiserver、kube-scheduler、kube-controller-manager、kubelet、kube-proxy
k8s-100.host.com 192.168.10.100 docker、docker-compose、nginx、harbor、CFSSL

2.基础环境准备

2.1.系统设置

2.1.1.设置主机名

  1. hostnamectl set-hostname k8s-xx.bolou.com

主机名=短域名+后缀

主机名后缀跟等会配置的DNS主域名一样,这样后面可以使用短域名

出现问题:

我原先主机名是:k8s-xx.locathost

每次重启服务器,/etc/resolv.conf文件的search都会自动配置主机名,导致DNS设置不生效,使用不了短域名访问别的服务器

search配置的主机名要跟DNS中一样

所以要主机名后缀设置成DNS域名

2.1.2.关闭防火墙和selinux**

  1. systemctl stop firewalld
  2. systemctl disable firewalld
  3. setenforce 0
  4. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

2.1.3.设置网卡

  1. cat /etc/sysconfig/network-scripts/ifcfg-ens33
  2. TYPE=Ethernet
  3. BOOTPROTO=none
  4. NAME=ens33
  5. DEVICE=ens33
  6. ONBOOT=yes
  7. IPADDR=192.168.10.xx
  8. NETMASK=255.255.255.0
  9. GATEWAY=192.168.10.1
  10. DNS1=114.114.114.114

2.1.4.设置yum源

  1. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  2. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  3. yum clean all
  4. yum makecache

2.1.5.安装常用工具

  1. yum install wget net-tools telnet tree nmap sysstat lrzsz dos2unix bind-utils -y

2.2.安装bind服务

k8s-20.bolou.com 上

NDS两种查询算法:递归、迭代ks-

2.2.1.安装bind 9

  1. yum install bind -y

2.2.2.配置bind 9

  1. vi /etc/named.conf
  2. listen-on port 53 { 192.168.10.20; };
  3. allow-query { any; };
  4. forwarders { 192.168.10.254; };
  5. recursion yes;
  6. dnssec-enable no;
  7. dnssec-validation no;
  8. ##########
  9. named-checkconf
  10. vi /etc/named.rfc1912.zones
  11. zone "bolou.com" IN {
  12. type master;
  13. file "bolou.com.zone";
  14. allow-update { 192.168.10.20; };
  15. };
  16. zone "daa.com" IN {
  17. type master;
  18. file "daa.com.zone";
  19. allow-update { 192.168.10.20; };
  20. };
  21. ##########
  22. vi /var/named/bolou.com.zone
  23. $ORIGIN bolou.com.
  24. $TTL 600 ; 10 minutes
  25. @ IN SOA dns.bolou.com. dnsadmin.bolou.com. (
  26. 2020032001 ; serial
  27. 10800 ; refresh (3 hours)
  28. 900 ; retry (15 minutes)
  29. 604800 ; expire (1 week)
  30. 86400 ; minimum (1 day)
  31. )
  32. NS dns.bolou.com.
  33. $TTL 60 ; 1 minute
  34. dns A 192.168.10.20
  35. k8s-20 A 192.168.10.20
  36. k8s-30 A 192.168.10.30
  37. k8s-40 A 192.168.10.40
  38. k8s-50 A 192.168.10.50
  39. k8s-60 A 192.168.10.60
  40. ##########
  41. vi /var/named/daa.com.zone
  42. $ORIGIN daa.com.
  43. $TTL 600 ; 10 minutes
  44. @ IN SOA dns.daa.com. dnsadmin.daa.com. (
  45. 2020032001 ; serial
  46. 10800 ; refresh (3 hours)
  47. 900 ; retry (15 minutes)
  48. 604800 ; expire (1 week)
  49. 86400 ; minimum (1 day)
  50. )
  51. NS dns.daa.com.
  52. $TTL 60 ; 1 minute
  53. dns A 192.168.10.20

2.2.3.检查配置并启动bind 9

  1. named-checkconf
  2. systemctl start named
  3. systemctl enable named
  4. netstat -lntup|grep 53

2.2.4.检查

  1. 安装命令:dignslookup
  2. [root@k8s-20 ~]# yum -y install bind-utils
  3. [root@k8s-20 ~]# dig -t A k8s-20.bolou.com @192.168.10.20 +short
  4. 192.168.10.20
  5. [root@k8s-20 ~]# dig -t A k8s-30.bolou.com @192.168.10.20 +short
  6. 192.168.10.30
  7. [root@k8s-20 ~]# dig -t A k8s-40.bolou.com @192.168.10.20 +short
  8. 192.168.10.40
  9. [root@k8s-20 ~]# dig -t A k8s-50.bolou.com @192.168.10.20 +short
  10. 192.168.10.50
  11. [root@k8s-20 ~]# dig -t A k8s-60.bolou.com @192.168.10.20 +short
  12. 192.168.10.60

2.2.5.配置DNS客户端

Linux所有主机

  1. vi /etc/sysconfig/network-scripts/ifcfg-ens33
  2. GATEWAY=192.168.10.1
  3. DNS1=192.168.10.20
  4. ##########
  5. vi /etc/resolv.conf
  6. search host.com
  7. nameserver 192.168.10.20
  8. ##########
  9. systemctl restart network

Windows主机

  1. wmnet2网卡更改DNS192.168.10.20

2.2.6.检查

Linux

  1. ping www.baidu.com
  2. ping k8s-100
  3. 主机域和业务域要分开用
  4. 比如:www.baidu.com 是百度的业务,用的业务域
  5. 主机域是内部使用,假的域,随便定义
  6. 比如,目前使用的主机域,host,也是主机的意思,好记

Windows

  1. ping k8s-100.bolou.com

2.3.准备签发证书环境

k8s-60.bolou.com 上

2.3.1.安装cfssl

  1. wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -O /usr/bin/cfssl
  2. wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -O /usr/bin/cfssl-json
  3. wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -O /usr/bin/cfssl-certinfo
  4. chmod +x /usr/bin/cfssl*
  5. wget https://github.com/cloudflare/cfssl/releases/download/v1.6.0/cfssljson_1.6.0_linux_amd64 -O /usr/bin/cfssl-json
  6. wget https://github.com/cloudflare/cfssl/releases/download/v1.6.0/cfssl_1.6.0_linux_amd64 -O /usr/bin/cfssl
  7. wget https://github.com/cloudflare/cfssl/releases/download/v1.6.0/cfssl-certinfo_1.6.0_linux_amd64 -O /usr/bin/cfssl-certinfo

2.3.2.创建生成ca证书csr的json配置文件

  1. mkdir /opt/certs
  2. vi /opt/certs/ca-csr.json
  3. {
  4. "CN": "Xingqiwu",
  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": "daa",
  17. "OU": "ops"
  18. }
  19. ],
  20. "ca": {
  21. "expiry": "175200h" # 证书到期时间
  22. }
  23. }

2.3.3.生成ca证书文件

  1. cd /opt/certs
  2. cfssl gencert -initca ca-csr.json | cfssl-json -bare ca
  3. ll
  4. ca.csr
  5. ca-csr.json
  6. ca-key.pem
  7. ca.pem

2.4.部署docker

k8s-40.bolou.com,k8s-50.bolou.com,k8s-100.bolou.com上

2.4.1.安装

  1. [root@k8s-40 ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

2.4.2.配置

  1. mkdir /etc/docker
  2. vi /etc/docker/daemon.json
  3. {
  4. "graph": "/data/docker",
  5. "storage-driver": "overlay2",
  6. "insecure-registries": ["registry.access.redhat.com","quay.io","harbor.od.com"],
  7. "registry-mirrors": ["https://9h2e4dkd.mirror.aliyuncs.com"], # 换自己阿里云镜像加速
  8. "bip": "172.7.40.1/24",
  9. "exec-opts": ["native.cgroupdriver=systemd"],
  10. "live-restore": true
  11. }
  12. ##########
  13. bip要根据宿主机ip变化
  14. 注意:k8s-40.bolou.com bip 172.7.40.1/24
  15. k8s-50.bolou.com bip 172.7.50.1/24
  16. k8s-100.bolou.com bip 172.7.100.1/24

2.4.3.启动

  1. mkdir -p /data/docker
  2. systemctl start docker
  3. systemctl enable docker
  4. docker --version

2.5.部署docker镜像私有仓库harbor

k8s-100.bolou.com 上

2.5.1.下载软件并解压

  1. harbor官网github地址
  2. https://github.com/goharbor/harbor
  3. 1.7.5以上的版本,1.7.5以下有漏洞
  4. wget https://github.com/goharbor/harbor/releases/download/v2.0.0/harbor-offline-installer-v2.0.0.tgz
  5. [root@k8s-100 src]# tar -xvf harbor-offline-installer-v2.0.0.tgz -C /opt/
  6. [root@k8s-100 src]# cd /opt/
  7. [root@k8s-100 opt]# mv harbor harbor-v2.0.0
  8. [root@k8s-100 opt]# ln -s harbor-v2.0.0 harbor

2.5.2.配置

  1. [root@k8s-100 opt]# cd harbor
  2. [root@k8s-100 harbor]# cp harbor.yml.tmpl harbor.yml
  3. [root@k8s-100 opt]# vi /opt/harbor/harbor.yml
  4. hostname: harbor.daa.com
  5. http:
  6. port: 180
  7. # 没用https,注销
  8. # https:
  9. # https port for harbor, default is 443
  10. # port: 443
  11. # The path of cert and key files for nginx
  12. # certificate: /your/certificate/path
  13. # private_key: /your/private/key/path
  14. harbor_admin_password:12345
  15. data_volume: /data/harbor
  16. log:
  17. level: info
  18. rotate_count: 50
  19. rotate_size:200M
  20. location: /data/harbor/logs
  21. [root@k8s-100 opt]# mkdir -p /data/harbor/logs

2.5.3.安装docker-compose

  1. [root@k8s-100 opt]# yum install docker-compose -y

2.5.4.安装harbor

  1. [root@k8s-100 harbor]# ./install.sh

2.5.5.检查harbor启动情况

  1. [root@k8s-100 harbor]# docker-compose ps
  2. Name Command State Ports
  3. ---------------------------------------------------------------------------------------------
  4. harbor-core /harbor/entrypoint.sh Up (healthy)
  5. harbor-db /docker-entrypoint.sh Up (healthy) 5432/tcp
  6. harbor-jobservice /harbor/entrypoint.sh Up (healthy)
  7. harbor-log /bin/sh -c /usr/local/bin/ ... Up (healthy) 127.0.0.1:1514->10514/tcp
  8. harbor-portal nginx -g daemon off; Up (healthy) 8080/tcp
  9. nginx nginx -g daemon off; Up (healthy) 0.0.0.0:180->8080/tcp
  10. redis redis-server /etc/redis.conf Up (healthy) 6379/tcp
  11. registry /home/harbor/entrypoint.sh Up (healthy) 5000/tcp
  12. registryctl /home/harbor/start.sh Up (healthy)

2.5.6.配置harbor的dns内网解析

  1. [root@k8s-20 ~]# vi /var/named/daa.com.zone
  2. 2020032002 ; serial
  3. harbor A 192.168.10.100
  4. [root@k8s-20 ~]# systemctl restart named
  5. [root@k8s-20 ~]# dig -t A harbor.daa.com +short
  6. 192.168.10.100
  7. dig -t A harbor.daa.com @192.168.10.10 +short

2.5.7.安装NGINX并配置

  1. [root@k8s-100 harbor]# yum install nginx -y
  2. [root@k8s-100 harbor]# vi /etc/nginx/conf.d/harbor.daa.com.conf
  3. server {
  4. listen 80;
  5. server_name harbor.daa.com;
  6. client_max_body_size 1000m;
  7. location / {
  8. proxy_pass http://127.0.0.1:180;
  9. }
  10. }
  11. [root@k8s-100 harbor]# nginx -t
  12. [root@k8s-100 harbor]# systemctl start nginx
  13. [root@k8s-100 harbor]# systemctl enable nginx

2.5.8.浏览器打开harbor.daa.com并测试

  1. [root@k8s-20 ~]# curl harbor.daa.com

1、浏览器输入:harbor.daa.com 用户名:admin 密码:12345

2、新建项目:public 访问级别:公开

3、下载镜像并给镜像打tag

  1. [root@k8s-100 harbor]# docker pull nginx:1.7.9
  2. [root@k8s-100 harbor]# docker images |grep 1.7.9
  3. [root@k8s-100 harbor]# docker tag 84581e99d807 harbor.daa.com/public/nginx:v1.7.9

4、登录harbor并上传到仓库

  1. [root@k8s-100 harbor]# docker login harbor.daa.com
  2. [root@k8s-100 harbor]# docker push harbor.daa.com/public/nginx:v1.7.9
  3. ## 登录harbor报错
  4. Error response from daemon: Get https://harbor.daa.com/v2/: dial tcp 192.168.10.100:443: connect: connection refused
  5. 解决方法:
  6. /etc/docker/daemon.json中添加
  7. "insecure-registries": ["registry.access.redhat.com","quay.io","harbor.daa.com"],
  8. 报错:
  9. Error response from daemon: Get "http://harbor.daa.com/v2/": Get "http://harbor.daa.com:180/service/token?account=admin&client_id=docker&offline_token=true&service=harbor-registry": dial tcp 192.168.10.50:180: connect: connection timed out
  10. 原因:这是180端口不通
  11. harbor本机 curl ip:180 telnet ip 180 是通的,到别的服务器就不通了
  12. 解决方法:把原先harbor的所有容器全删除,再重新./install.sh 起服务

2.5.9.检查

可以看到NGINX镜像已经上传到public下

设置harbor开机自启

  1. [root@k8s-100 ~]# vim /etc/rc.d/rc.local
  2. cd /opt/harbor
  3. /usr/bin/docker-compose stop
  4. /usr/bin/docker-compose start
  5. [root@k8s-100 ~]# chmod +x /etc/rc.d/rc.local

3.部署master节点

3.1.部署etcd集群

3.1.1.集群架构

主机名 角色 ip地址
k8s-30.bolou.com lead 192.168.10.30
k8s-40.bolou.com follow 192.168.10.40
k8s-50.bolou.com follow 192.168.10.50

部署方法以k8s-30.bolou.com为例

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

k8s-60上

  1. [root@k8s-100 ~]# vi /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.3.创建生成自签发证书的csr的json配置文件

  1. [root@k8s-100 ~]# vi /opt/certs/etcd-peer-csr.json
  2. {
  3. "CN": "k8s-etcd",
  4. "hosts": [
  5. "192.168.10.20",
  6. "192.168.10.30",
  7. "192.168.10.40",
  8. "192.168.10.50"
  9. ],
  10. "key": {
  11. "algo": "rsa",
  12. "size": 2048
  13. },
  14. "names": [
  15. {
  16. "C": "CN",
  17. "ST": "beijing",
  18. "L": "beijing",
  19. "O": "daa",
  20. "OU": "ops"
  21. }
  22. ]
  23. }

3.1.4.生成etcd证书文件

  1. [root@k8s-100 ~]# cd /opt/certs/
  2. [root@k8s-100 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.1.5.检查生成的证书文件

  1. [root@k8s-100 certs]# ll etcd-peer*
  2. etcd-peer.csr
  3. etcd-peer-csr.json
  4. etcd-peer-key.pem
  5. etcd-peer.pem

3.1.6.创建etcd用户

k8s-30、k8s-40、k8s-50上

  1. [root@k8s-30 opt]# useradd -s /sbin/nologin -M etcd

3.1.7.下载软件,解压,做软连接

  1. https://github.com/etcd-io/etcd/tags
  2. wget https://github.com/etcd-io/etcd/releases/download/v3.3.20/etcd-v3.3.20-linux-amd64.tar.gz
  3. [root@k8s-30 src]# tar -xvf etcd-v3.4.7-linux-amd64.tar.gz -C /opt/
  4. [root@k8s-30 src]# cd /opt/
  5. [root@k8s-30 opt]# ln -s etcd-v3.4.7-linux-amd64/ etcd

3.1.8.创建目录,拷贝证书文件

创建证书目录、数据目录、日志目录

  1. [root@k8s-30 opt]# mkdir -p /opt/etcd/certs /data/etcd /data/etcd/etcd-server /data/logs/etcd-server

拷贝生成的证书文件

  1. 免密:
  2. ssh-keygen
  3. ssh-copy-id 目的主机ip
  4. 到运维主机上去拷贝
  5. [root@k8s-60 certs]# scp ca.pem etcd-peer.pem etcd-peer-key.pem k8s-30:/opt/etcd/certs
  6. 注意私钥文件权限600
  7. [root@k8s-30 certs]# chmod 600 etcd-peer-key.pem

3.1.9.创建etcd服务启动脚本

  1. [root@k8s-30 ~]# vi /opt/etcd/etcd-server-startup.sh
  2. #!/bin/sh
  3. ./etcd --name etcd-server-10-30 \
  4. --data-dir /data/etcd/etcd-server \
  5. --listen-peer-urls https://192.168.10.30:2380 \
  6. --listen-client-urls https://192.168.10.30:2379,http://127.0.0.1:2379 \
  7. --quota-backend-bytes 8000000000 \
  8. --initial-advertise-peer-urls https://192.168.10.30:2380 \
  9. --advertise-client-urls https://192.168.10.30:2379,http://127.0.0.1:2379 \
  10. --initial-cluster etcd-server-10-30=https://192.168.10.30:2380,etcd-server-10-40=https://192.168.10.40:2380,etcd-server-10-50=https://192.168.10.50:2380 \
  11. --ca-file ./certs/ca.pem \
  12. --cert-file ./certs/etcd-peer.pem \
  13. --key-file ./certs/etcd-peer-key.pem \
  14. --client-cert-auth \
  15. --trusted-ca-file ./certs/ca.pem \
  16. --peer-ca-file ./certs/ca.pem \
  17. --peer-cert-file ./certs/etcd-peer.pem \
  18. --peer-key-file ./certs/etcd-peer-key.pem \
  19. --peer-client-cert-auth \
  20. --peer-trusted-ca-file ./certs/ca.pem \
  21. --log-output stdout
  22. [root@k8s-30 ~]# chmod +x /opt/etcd/etcd-server-startup.sh
  23. 高版本的etcd已经没有--ca-file、--peer-ca-file,需要删掉这两个配置

3.1.10.授权目录权限

  1. [root@k8s-30 certs]# chown -R etcd.etcd /opt/etcd-v3.4.7-linux-amd64/ /data/etcd/ /data/logs/etcd-server/

3.1.11.安装supervisor软件

  1. [root@k8s-30 ~]# yum install supervisor -y
  2. [root@k8s-30 ~]# systemctl start supervisord
  3. [root@k8s-30 ~]# systemctl enable supervisord

3.1.12.创建supervisor配置

  1. [root@k8s-30 ~]# vi /etc/supervisord.d/etcd-server.ini
  2. [program:etcd-server-10-30]
  3. command=/opt/etcd/etcd-server-startup.sh ; the program (relative uses PATH, can take args)
  4. numprocs=1 ; number of processes copies to start (def 1)
  5. directory=/opt/etcd ; 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=etcd ; 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/etcd-server/etcd.stdout.log ; stdout 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)

3.1.13.启动etcd服务并检查

  1. [root@k8s-30 ~]# supervisorctl update
  2. [root@k8s-30 ~]# supervisorctl status
  3. [root@k8s-30 ~]# netstat -lntup|grep etcd
  4. 排错:
  5. # 查看某进程错误日志
  6. supervisorctl tail program_name

3.1.14.部署启动所有集群

不同的地方

  1. /opt/etcd/etcd-server-startup.sh
  2. --name
  3. --listen-peer-urls
  4. --listen-client-urls
  5. --initial-advertise-peer-urls
  6. --advertise-client-urls
  7. ##########
  8. /etc/supervisord.d/etcd-server.ini
  9. [program:etcd-server-10-30]

3.1.15.检查集群状态

  1. [root@k8s-30 etcd]# vim ~/.bashrc # 加入下面一行内容,另外两台etcd节点服务也加
  2. export PATH=$PATH:/opt/etcd
  3. [root@k8s-30 etcd]# source ~/.bashrc # 加载生效
  4. [root@k8s-30 ~]# etcdctl cluster-health
  5. [root@k8s-30 ~]# etcdctl member list

3.2.部署kube-apiserver集群

3.2.1.集群架构

主机名 角色 ip地址
k8s-40.bolou.com kube-apiserver 192.168.10.40
k8s-50.bolou.com kube-apiserver 192.168.10.50

部署方法以k8s-40.bolou.com为例

3.2.2.下载软件,解压,做软连接

k8s-40.bolou.com上

下载地址:https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.20.md#source-code-2

https://dl.k8s.io/v1.15.10/kubernetes-server-linux-amd64.tar.gz
https://dl.k8s.io/v1.20.10/kubernetes-server-linux-amd64.tar.gz

  1. https://github.com/kubernetes/kubernetes/releases/tag/v1.15.10
  2. CHANGELOG-1.15.md--→server binaries--→kubernetes-server-linux-amd64.tar.gz
  3. [root@k8s-40 src]# tar -xf kubernetes-server-linux-amd64-1.15.10.tar.gz -C /opt/
  4. [root@k8s-40 opt]# mv kubernetes/ kubernetes-v1.15.10
  5. [root@k8s-40 opt]# ln -s /opt/kubernetes-v1.15.10/ /opt/kubernetes
  6. [root@k8s-40 opt]# cd kubernetes
  7. [root@k8s-40 kubernetes]# rm -rf kubernetes-src.tar.gz # go语言写的kubernetes源码
  8. [root@k8s-40 kubernetes]# cd server/bin
  9. [root@k8s-40 bin]# rm -f *.tar # kubernetes镜像包
  10. [root@k8s-40 bin]# rm -f *_tag

3.2.3.签发client证书

k8s-100.bolou.com上

1、创建生成证书csr的json配置文件

  1. [root@k8s-100 certs]# vi /opt/certs/client-csr.json
  2. {
  3. "CN": "k8s-node",
  4. "hosts": [
  5. ],
  6. "key": {
  7. "algo": "rsa",
  8. "size": 2048
  9. },
  10. "names": [
  11. {
  12. "C": "CN",
  13. "ST": "beijing",
  14. "L": "beijing",
  15. "O": "daa",
  16. "OU": "ops"
  17. }
  18. ]
  19. }

2、生成client证书文件

  1. [root@k8s-100 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=client client-csr.json |cfssl-json -bare client

3、检查生成的证书文件

  1. [root@k8s-100 certs]# ll
  2. client.csr
  3. client-csr.json
  4. client-key.pem
  5. client.pem

3.2.4.签发kube-apiserver证书

k8s-100.bolou.com上

1、创建生成证书csr的json配置文件

  1. [root@k8s-100 certs]# vi /opt/certs/apiserver-csr.json
  2. {
  3. "CN": "k8s-apiserver",
  4. "hosts": [
  5. "127.0.0.1",
  6. "10.96.0.1", # Service ClusterIP
  7. "kubernetes.default",
  8. "kubernetes.default.svc",
  9. "kubernetes.default.svc.cluster",
  10. "kubernetes.default.svc.cluster.local",
  11. "192.168.10.10",
  12. "192.168.10.40",
  13. "192.168.10.50",
  14. "192.168.10.51"
  15. ],
  16. "key": {
  17. "algo": "rsa",
  18. "size": 2048
  19. },
  20. "names": [
  21. {
  22. "C": "CN",
  23. "ST": "beijing",
  24. "L": "beijing",
  25. "O": "daa",
  26. "OU": "ops"
  27. }
  28. ]
  29. }

2、生成kube-apiserver证书文件

  1. [root@k8s-100 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server apiserver-csr.json |cfssl-json -bare apiserver

3、检查生成的证书文件

  1. [root@k8s-100 certs]# ll
  2. apiserver.csr
  3. apiserver-csr.json
  4. apiserver-key.pem
  5. apiserver.pem

3.2.5.拷贝证书文件至各节点,并创建配置

1、拷贝证书文件到/opt/kubernetes/server/bin/cert目录下

  1. 两套master节点创建证书目录
  2. [root@k8s-40 bin]# mkdir cert
  3. [root@k8s-50 bin]# mkdir cert
  4. [root@k8s-100 certs]# scp ca.pem ca-key.pem client.pem client-key.pem apiserver.pem apiserver-key.pem k8s-40:/opt/kubernetes/server/bin/cert

2、创建配置

  1. [root@k8s-40 bin]# mkdir conf
  2. [root@k8s-40 bin]# cd conf
  3. [root@k8s-40 conf]# vi audit.yaml
  4. apiVersion: audit.k8s.io/v1beta1 # This is required.
  5. kind: Policy
  6. omitStages:
  7. - "RequestReceived"
  8. rules:
  9. - level: RequestResponse
  10. resources:
  11. - group: ""
  12. resources: ["pods"]
  13. - level: Metadata
  14. resources:
  15. - group: ""
  16. resources: ["pods/log", "pods/status"]
  17. - level: None
  18. resources:
  19. - group: ""
  20. resources: ["configmaps"]
  21. resourceNames: ["controller-leader"]
  22. - level: None
  23. users: ["system:kube-proxy"]
  24. verbs: ["watch"]
  25. resources:
  26. - group: "" # core API group
  27. resources: ["endpoints", "services"]
  28. - level: None
  29. userGroups: ["system:authenticated"]
  30. nonResourceURLs:
  31. - "/api*" # Wildcard matching.
  32. - "/version"
  33. - level: Request
  34. resources:
  35. - group: "" # core API group
  36. resources: ["configmaps"]
  37. namespaces: ["kube-system"]
  38. - level: Metadata
  39. resources:
  40. - group: "" # core API group
  41. resources: ["secrets", "configmaps"]
  42. - level: Request
  43. resources:
  44. - group: "" # core API group
  45. - group: "extensions" # Version of group should NOT be included.
  46. - level: Metadata
  47. mitStages:
  48. - "RequestReceived"

3.2.6.创建apiserver启动脚本

  1. [root@k8s-40 bin]# vi /opt/kubernetes/server/bin/kube-apiserver.sh
  2. #!/bin/bash
  3. ./kube-apiserver \
  4. --apiserver-count 2 \
  5. --audit-log-path /data/logs/kubernetes/kube-apiserver/audit-log \
  6. --audit-policy-file ./conf/audit.yaml \
  7. --authorization-mode RBAC \
  8. --client-ca-file ./cert/ca.pem \
  9. --requestheader-client-ca-file ./cert/ca.pem \
  10. --enable-admission-plugins NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota \
  11. --etcd-cafile ./cert/ca.pem \
  12. --etcd-certfile ./cert/client.pem \
  13. --etcd-keyfile ./cert/client-key.pem \
  14. --etcd-servers https://192.168.10.30:2379,https://192.168.10.40:2379,https://192.168.10.50:2379 \
  15. --service-account-key-file ./cert/ca-key.pem \
  16. --service-cluster-ip-range 10.96.0.0/22 \
  17. --service-node-port-range 3000-29999 \
  18. --target-ram-mb=1024 \
  19. --kubelet-client-certificate ./cert/client.pem \
  20. --kubelet-client-key ./cert/client-key.pem \
  21. --log-dir /data/logs/kubernetes/kube-apiserver \
  22. --tls-cert-file ./cert/apiserver.pem \
  23. --tls-private-key-file ./cert/apiserver-key.pem \
  24. --service-account-signing-key-file ./cert/ca-key.pem \
  25. --service-account-issuer https://kubernetes.default.svc.cluster.local \
  26. --v 2
  27. ### 说明1.20版本之后,为了启用令牌请求投射,你必须为 kube-apiserver 设置以下命令行参数:
  28. --service-account-issuer
  29. --service-account-key-file
  30. --service-account-signing-key-file
  31. --api-audiences
  32. 官方文档:https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-service-account/
  • RBAC 基于角色的访问控制
  • target-ram-mb参数:./kube-apiserver —help | grep -A 5 target-ram-mb

3.2.7.授权和创建目录

  1. [root@k8s-40 bin]# cd /opt/kubernetes/server/bin
  2. [root@k8s-40 bin]# chmod +x kube-apiserver.sh
  3. [root@k8s-40 bin]# mkdir -p /data/logs/kubernetes/kube-apiserver

3.2.8.创建supervisor配置

  1. [root@k8s-40 bin]# vi /etc/supervisord.d/kube-apiserver.ini
  2. [program:kube-apiserver-10-40]
  3. command=/opt/kubernetes/server/bin/kube-apiserver.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-apiserver/apiserver.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)

3.2.9.启动服务并检查

  1. [root@k8s-40 bin]# supervisorctl update
  2. [root@k8s-40 bin]# supervisorctl status
  3. [root@k8s-40 bin]# netstat -nltup|grep kube-api

3.2.10.部署启动所有集群

不同的地方

  1. /etc/supervisord.d/kube-apiserver.ini
  2. [program:kube-apiserver-10-40]

3.3.部署四层反向代理

3.3.1.集群架构

主机名 角色 IP地址 VIP地址
k8s-20.bolou.com L4 192.168.10.20 192.168.10.10
k8s-30.bolou.com L4 192.168.10.30 192.168.10.10

3.3.2.安装NGINX和keepalived

1、k8s-20.bolou.com和k8s-30.bolou.com都安装NGINX和keepalived

  1. [root@k8s-20 etcd]# yum install nginx keepalived -y

2、k8s-20.host.com和k8s-30.host.com配置NGINX

  1. [root@k8s-20 conf.d]# vi /etc/nginx/nginx.conf #以下内容添加到http之外
  2. stream {
  3. upstream kube-apiserver {
  4. server 192.168.10.40:6443 max_fails=3 fail_timeout=30s;
  5. server 192.168.10.50:6443 max_fails=3 fail_timeout=30s;
  6. }
  7. server {
  8. listen 7443;
  9. proxy_connect_timeout 2s;
  10. proxy_timeout 900s;
  11. proxy_pass kube-apiserver;
  12. }
  13. }
  14. [root@k8s-20 etcd]# nginx -t

3、k8s-20.host.com和k8s-30.host.com配置keepalived

  1. 检查脚本
  2. [root@k8s-20 ~]# vi /etc/keepalived/check_port.sh
  3. #!/bin/bash
  4. #keepalived 监控端口脚本
  5. #使用方法:
  6. #在keepalived的配置文件中
  7. #vrrp_script check_port {#创建一个vrrp_script脚本,检查配置
  8. # script "/etc/keepalived/check_port.sh 6379" #配置监听的端口
  9. # interval 2 #检查脚本的频率,单位(秒)
  10. #}
  11. CHK_PORT=$1
  12. if [ -n "$CHK_PORT" ];then
  13. PORT_PROCESS=`ss -lnt|grep $CHK_PORT|wc -l`
  14. if [ $PORT_PROCESS -eq 0 ];then
  15. echo "Port $CHK_PORT Is Not Used,End."
  16. exit 1
  17. fi
  18. else
  19. echo "Check Port Cant Be Empty!"
  20. fi
  21. ###########################################
  22. 配置文件
  23. keepalived 主:
  24. [root@k8s-20 conf.d]# vi /etc/keepalived/keepalived.conf
  25. ! Configuration File for keepalived
  26. global_defs {
  27. router_id 192.168.10.20
  28. }
  29. vrrp_script chk_nginx {
  30. script "/etc/keepalived/check_port.sh 7443"
  31. interval 2
  32. weight -20
  33. }
  34. vrrp_instance VI_1 {
  35. state MASTER
  36. interface ens33
  37. virtual_router_id 251
  38. priority 100
  39. advert_int 1
  40. mcast_src_ip 192.168.10.20
  41. nopreempt
  42. authentication {
  43. auth_type PASS
  44. auth_pass 11111111
  45. }
  46. track_script {
  47. chk_nginx
  48. }
  49. virtual_ipaddress {
  50. 192.168.10.10
  51. }
  52. }
  53. keepalived 从:
  54. [root@k8s-30 conf.d]# vi /etc/keepalived/keepalived.conf
  55. ! Configuration File for keepalived
  56. global_defs {
  57. router_id 192.168.10.30
  58. }
  59. vrrp_script chk_nginx {
  60. script "/etc/keepalived/check_port.sh 7443"
  61. interval 2
  62. weight -20
  63. }
  64. vrrp_instance VI_1 {
  65. state BACKUP
  66. interface ens33
  67. virtual_router_id 251
  68. mcast_src_ip 192.168.10.30
  69. priority 90
  70. advert_int 1
  71. authentication {
  72. auth_type PASS
  73. auth_pass 11111111
  74. }
  75. track_script {
  76. chk_nginx
  77. }
  78. virtual_ipaddress {
  79. 192.168.10.10
  80. }
  81. }
  82. ####################
  83. nopreempt:非抢占式

3.3.3.启动代理并检查

  1. systemctl start nginx keepalived
  2. systemctl enable nginx keepalived
  3. netstat -lntup|grep nginx
  4. ip addr
  5. 模拟测试
  6. 如果搭建成功,此时VIP地址10.4.7.10/32是在K8s-11服务器上,
  7. 先把k8s-11keepalive先停了,看VIP地址是否会漂移到k8s-12上,
  8. VIP地址漂移到k8s-12后,再重启k8s-11keepalive,看VIP地址是否会漂回来
  9. 若是,则配置成功。

3.4.部署controller-manager

3.4.1.集群架构

主机名 角色 IP地址
k8s-40.host.com controller-manager 192.168.10.40
k8s-50.host.com controller-manager 192.168.10.50

部署方法以k8s-40.host.com为例

3.4.2.创建启动脚本

k8s-40.host.com上

  1. [root@k8s-40 bin]# vi /opt/kubernetes/server/bin/kube-controller-manager.sh
  2. #!/bin/sh
  3. ./kube-controller-manager \
  4. --cluster-cidr 172.7.0.0/16 \
  5. --leader-elect true \
  6. --log-dir /data/logs/kubernetes/kube-controller-manager \
  7. --master http://127.0.0.1:8080 \
  8. --service-account-private-key-file ./cert/ca-key.pem \
  9. --service-cluster-ip-range 10.96.0.0/22 \
  10. --root-ca-file ./cert/ca.pem \
  11. --v 2

3.4.3.授权文件权限,创建目录

  1. [root@k8s-40 bin]# chmod +x /opt/kubernetes/server/bin/kube-controller-manager.sh
  2. [root@k8s-40 bin]# mkdir -p /data/logs/kubernetes/kube-controller-manager

3.4.4.创建supervisor配置

  1. [root@k8s-40 bin]# vi /etc/supervisord.d/kube-conntroller-manager.ini
  2. [program:kube-controller-manager-10-40]
  3. command=/opt/kubernetes/server/bin/kube-controller-manager.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)

3.4.5.启动服务并检查

  1. [root@k8s-40 bin]# supervisorctl update
  2. [root@k8s-40 bin]# supervisorctl status

3.4.6.部署启动所有集群

不同的地方

  1. /etc/supervisord.d/kube-conntroller-manager.ini
  2. [program:kube-controller-manager-10-40]

3.5.部署kube-scheduler

3.5.1.集群架构

主机名 角色 IP地址
k8s-21.host.com kube-scheduler 192.168.10.40
k8s-22.host.com kube-scheduler 192.168.10.50

部署方法以k8s-40.host.com为例

3.5.2.创建启动脚本

k8s-40.host.com上

  1. [root@k8s-40 bin]# vi /opt/kubernetes/server/bin/kube-scheduler.sh
  2. #!/bin/sh
  3. ./kube-scheduler \
  4. --leader-elect \
  5. --log-dir /data/logs/kubernetes/kube-scheduler \
  6. --master http://127.0.0.1:8080 \
  7. --v 2

3.5.3.授权文件权限,创建目录

  1. [root@k8s-40 bin]# chmod +x /opt/kubernetes/server/bin/kube-scheduler.sh
  2. [root@k8s-40 bin]# mkdir -p /data/logs/kubernetes/kube-scheduler

3.5.4.创建supervisor配置

  1. [root@k8s-40 bin]# vi /etc/supervisord.d/kube-scheduler.ini
  2. [program:kube-scheduler-10-40]
  3. command=/opt/kubernetes/server/bin/kube-scheduler.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-scheduler/scheduler.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)

3.5.5.启动服务并检查

  1. [root@k8s-40 bin]# supervisorctl update
  2. [root@k8s-40 bin]# supervisorctl status

3.5.6.部署启动所有集群

不同的地方

  1. /etc/supervisord.d/kube-scheduler.ini
  2. [program:kube-scheduler-10-40]

3.6.检查master节点

3.6.1.建立kubectl软链接

  1. [root@k8s-40 bin]# ln -s /opt/kubernetes/server/bin/kubectl /usr/bin/kubectl

3.6.2.检查master节点

  1. [root@k8s-40 bin]# kubectl get cs

4.部署node节点

4.1.部署kubelet

4.1.1.集群架构

主机名 角色 IP地址
k8s-40.host.com kubelet 192.168.10.40
k8s-50.host.com kubelet 192.168.10.50

部署方法以k8s-40.host.com为例

4.1.2.签发kubelet证书

k8s-60.host.com上

1、创建生成证书csr的json配置文件

  1. [root@k8s-60 certs]# vi kubelet-csr.json
  2. {
  3. "CN": "k8s-kubelet",
  4. "hosts": [
  5. "127.0.0.1",
  6. "192.168.10.10",
  7. "192.168.10.40",
  8. "192.168.10.50",
  9. "192.168.10.51",
  10. "192.168.10.52",
  11. "192.168.10.53",
  12. "192.168.10.54",
  13. "192.168.10.55",
  14. "192.168.10.56"
  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. }

2、生成kubelet证书文件

  1. [root@k8s-60 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server kubelet-csr.json | cfssl-json -bare kubelet

3、检查生成的证书文件

  1. [root@k8s-60 certs]# ll
  2. kubelet.csr
  3. kubelet-csr.json
  4. kubelet-key.pem
  5. kubelet.pem

4.1.3.拷贝证书文件至各节点,并创建配置

k8s-40.host.com上

1、拷贝证书文件

  1. [root@k8s-40 cert]# scp k8s-60:/opt/certs/kubelet.pem .
  2. [root@k8s-40 cert]# scp k8s-60:/opt/certs/kubelet-key.pem .

2、创建配置

(1)、set-cluster

  1. [root@k8s-40 ~]# cd /opt/kubernetes/server/bin/conf
  2. [root@k8s-40 conf]# kubectl config set-cluster myk8s \
  3. --certificate-authority=/opt/kubernetes/server/bin/cert/ca.pem \
  4. --embed-certs=true \
  5. --server=https://192.168.10.10:7443 \
  6. --kubeconfig=kubelet.kubeconfig

(2)、set-credentials

  1. [root@k8s-40 conf]# kubectl config set-credentials k8s-node \
  2. --client-certificate=/opt/kubernetes/server/bin/cert/client.pem \
  3. --client-key=/opt/kubernetes/server/bin/cert/client-key.pem \
  4. --embed-certs=true \
  5. --kubeconfig=kubelet.kubeconfig

(3)、set-context

  1. [root@k8s-40 conf]# kubectl config set-context myk8s-context \
  2. --cluster=myk8s \
  3. --user=k8s-node \
  4. --kubeconfig=kubelet.kubeconfig

(4)、use-context

  1. [root@k8s-40 conf]# kubectl config use-context myk8s-context --kubeconfig=kubelet.kubeconfig

(5)、查看生成的kubelet.kubeconfig

  1. [root@k8s-40 conf]# ll
  2. kubelet.kubeconfig

(6)、k8s-node.yaml

(1)创建配置文件

  1. [root@k8s-40 conf]# vi 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

(2)应用资源配置

  1. [root@k8s-40 conf]# kubectl create -f k8s-node.yaml

(3)查看集群角色和角色属性

  1. [root@k8s-40 conf]# kubectl get clusterrolebinding k8s-node
  2. NAME AGE
  3. k8s-node 2m
  4. [root@k8s-40 conf]# kubectl get clusterrolebinding k8s-node -o yaml
  5. 出现如下:
  6. apiVersion: rbac.authorization.k8s.io/v1
  7. kind: ClusterRoleBinding
  8. metadata:
  9. creationTimestamp: "2020-06-18T06:17:28Z"
  10. name: k8s-node
  11. resourceVersion: "5673"
  12. selfLink: /apis/rbac.authorization.k8s.io/v1/clusterrolebindings/k8s-node
  13. uid: a2d8f40b-1f1d-457b-b72e-b5a69ebcaaa0
  14. roleRef:
  15. apiGroup: rbac.authorization.k8s.io
  16. kind: ClusterRole
  17. name: system:node
  18. subjects:
  19. - apiGroup: rbac.authorization.k8s.io
  20. kind: User
  21. name: k8s-node

(4)拷贝kubelet.kubeconfig 到k8s-50.host.com上

  1. [root@k8s-50 ~]# cd /opt/kubernetes/server/bin/conf
  2. [root@k8s-50 conf]# scp k8s-40:/opt/kubernetes/server/bin/conf/kubelet.kubeconfig .

4.1.4.准备pause基础镜像

k8s-60.host.com上

1、下载pause镜像

  1. [root@k8s-60 ~]# docker pull kubernetes/pause

2、上传到docker私有仓库harbor中

(1)、给镜像打tag

  1. [root@k8s-60 ~]# docker images | grep kubernetes
  2. [root@k8s-60 ~]# docker tag f9d5de079539 harbor.od.com/public/pause:latest
  3. [root@k8s-60 ~]# docker images | grep pause

(2)、上传到harbor上

  1. [root@k8s-60 ~]# docker push harbor.od.com/public/pause:latest

4.1.5.创建kubelet启动脚本

k8s-40.host.com上

  1. [root@k8s-40 conf]# vi /opt/kubernetes/server/bin/kubelet.sh
  2. #!/bin/sh
  3. ./kubelet \
  4. --anonymous-auth=false \
  5. --cgroup-driver systemd \
  6. --cluster-dns 10.96.0.2 \
  7. --cluster-domain cluster.local \
  8. --runtime-cgroups=/systemd/system.slice \
  9. --kubelet-cgroups=/systemd/system.slice \
  10. --fail-swap-on="false" \
  11. --client-ca-file ./cert/ca.pem \
  12. --tls-cert-file ./cert/kubelet.pem \
  13. --tls-private-key-file ./cert/kubelet-key.pem \
  14. --hostname-override k8s-40.host.com \
  15. --image-gc-high-threshold 20 \
  16. --image-gc-low-threshold 10 \
  17. --kubeconfig ./conf/kubelet.kubeconfig \
  18. --log-dir /data/logs/kubernetes/kube-kubelet \
  19. --pod-infra-container-image harbor.od.com/public/pause:latest \
  20. --root-dir /data/kubelet

4.1.6.授权,创建目录

k8s-40.host.com上

  1. [root@k8s-40 conf]# chmod +x /opt/kubernetes/server/bin/kubelet.sh
  2. [root@k8s-40 conf]# mkdir -p /data/logs/kubernetes/kube-kubelet /data/kubelet

4.1.7.创建supervisor配置

  1. [root@k8s-40 conf]# vi /etc/supervisord.d/kube-kubelet.ini
  2. [program:kube-kubelet-10-40]
  3. command=/opt/kubernetes/server/bin/kubelet.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-kubelet/kubelet.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)

4.1.8.启动服务并检查

  1. [root@k8s-40 conf]# supervisorctl update
  2. [root@k8s-40 conf]# supervisorctl status

4.1.9.部署所有节点

不同的地方

  1. /opt/kubernetes/server/bin/kubelet.sh
  2. --hostname-override
  3. ##########
  4. /etc/supervisord.d/kube-kubelet.ini
  5. [program:kube-kubelet-10-40]

4.1.10.检查所有节点并给节点打上标签

  1. [root@k8s-40 ~]# kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-40.host.com Ready <none> 2m9s v1.15.10
  4. k8s-50.host.com Ready <none> 32s v1.15.10
  5. [root@k8s-40 bin]# kubectl label node k8s-40.host.com node-role.kubernetes.io/master=
  6. [root@k8s-40 bin]# kubectl label node k8s-40.host.com node-role.kubernetes.io/node=
  7. ##k8s-50上操作注意修改主机名,再查看
  8. [root@k8s-50 ~]# kubectl get nodes
  9. NAME STATUS ROLES AGE VERSION
  10. k8s-40.host.com Ready master,node 4m6s v1.15.10
  11. k8s-50.host.com Ready master,node 2m29s v1.15.10

4.2.部署kube-proxy

4.2.1.集群架构

主机名 角色 IP地址
k8s-40.host.com kube-proxy 192.168.10.40
k8s-50.host.com kube-proxy 192.168.10.50

部署方法以k8s-40.host.com为例

4.2.2.签发kube-proxy证书

k8s-60.host.com上

1、创建生成证书csr的json配置文件

  1. [root@k8s-60 certs]# vi kube-proxy-csr.json
  2. {
  3. "CN": "system:kube-proxy",
  4. "key": {
  5. "algo": "rsa",
  6. "size": 2048
  7. },
  8. "names": [
  9. {
  10. "C": "CN",
  11. "ST": "beijing",
  12. "L": "beijing",
  13. "O": "od",
  14. "OU": "ops"
  15. }
  16. ]
  17. }

2、生成kube-proxy证书文件

  1. [root@k8s-60 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

3、检查生成的证书文件

  1. [root@k8s-60 certs]# ll
  2. kube-proxy-client.csr
  3. kube-proxy-client-key.pem
  4. kube-proxy-client.pem
  5. kube-proxy-csr.json

4.2.3.拷贝证书文件至各节点,并创建配置

k8s-40.host.com上

1、拷贝证书文件

  1. [root@k8s-40 cert]# scp k8s-200:/opt/certs/kube-proxy-client.pem .
  2. [root@k8s-40 cert]# scp k8s-200:/opt/certs/kube-proxy-client-key.pem .
  3. [root@k8s-60 certs]# scp kube-proxy-client.pem kube-proxy-client-key.pem k8s-40:/opt/kubernetes/server/bin/cert

2、创建配置

(1)、set-cluster

  1. [root@k8s-40 ~]# cd /opt/kubernetes/server/bin/conf
  2. [root@k8s-40 conf]# kubectl config set-cluster myk8s \
  3. --certificate-authority=/opt/kubernetes/server/bin/cert/ca.pem \
  4. --embed-certs=true \
  5. --server=https://192.168.10.10:7443 \
  6. --kubeconfig=kube-proxy.kubeconfig

(2)、set-credentials

  1. [root@k8s-40 conf]# kubectl config set-credentials kube-proxy \
  2. --client-certificate=/opt/kubernetes/server/bin/cert/kube-proxy-client.pem \
  3. --client-key=/opt/kubernetes/server/bin/cert/kube-proxy-client-key.pem \
  4. --embed-certs=true \
  5. --kubeconfig=kube-proxy.kubeconfig

(3)、set-context

  1. [root@k8s-40 conf]# kubectl config set-context myk8s-context \
  2. --cluster=myk8s \
  3. --user=kube-proxy \
  4. --kubeconfig=kube-proxy.kubeconfig

(4)、use-context

  1. [root@k8s-40 conf]# kubectl config use-context myk8s-context --kubeconfig=kube-proxy.kubeconfig

(5)、拷贝kube-proxy.kubeconfig 到 k8s-22.host.com的conf目录下

  1. [root@k8s-50 conf]# scp k8s-40:/opt/kubernetes/server/bin/conf/kube-proxy.kubeconfig .

4.2.4.创建kube-proxy启动脚本

k8s-21.host.com上

1、加载ipvs模块

  1. [root@k8s-40 bin]# lsmod |grep ip_vs
  2. [root@k8s-40 bin]# vi /root/ipvs.sh
  3. #!/bin/bash
  4. ipvs_mods_dir="/usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs"
  5. for i in $(ls $ipvs_mods_dir|grep -o "^[^.]*")
  6. do
  7. /sbin/modinfo -F filename $i &>/dev/null
  8. if [ $? -eq 0 ];then
  9. /sbin/modprobe $i
  10. fi
  11. done
  12. [root@k8s-40 bin]# chmod +x /root/ipvs.sh
  13. [root@k8s-40 bin]# sh /root/ipvs.sh
  14. [root@k8s-40 bin]# lsmod |grep ip_vs

2、创建启动脚本

  1. [root@k8s-40 bin]# vi /opt/kubernetes/server/bin/kube-proxy.sh
  2. #!/bin/sh
  3. ./kube-proxy \
  4. --cluster-cidr 172.7.0.0/16 \
  5. --hostname-override k8s-40.host.com \
  6. --proxy-mode=ipvs \
  7. --ipvs-scheduler=nq \
  8. --kubeconfig ./conf/kube-proxy.kubeconfig

4.2.5.授权,创建目录

  1. [root@k8s-50 bin]# ls -l /opt/kubernetes/server/bin/conf/|grep kube-proxy
  2. [root@k8s-50 bin]# chmod +x /opt/kubernetes/server/bin/kube-proxy.sh
  3. [root@k8s-50 bin]# mkdir -p /data/logs/kubernetes/kube-proxy

4.2.6.创建supervisor配置

  1. [root@k8s-40 bin]# vi /etc/supervisord.d/kube-proxy.ini
  2. [program:kube-proxy-10-40]
  3. command=/opt/kubernetes/server/bin/kube-proxy.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-proxy/proxy.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)

4.2.7.启动服务并检查

  1. [root@k8s-40 bin]# supervisorctl update
  2. [root@k8s-40 bin]# supervisorctl status
  3. [root@k8s-40 bin]# yum install ipvsadm -y
  4. [root@k8s-40 conf]# ipvsadm -Ln
  5. IP Virtual Server version 1.2.1 (size=4096)
  6. Prot LocalAddress:Port Scheduler Flags
  7. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  8. TCP 10.96.0.1:443 nq
  9. -> 192.168.10.40:6443 Masq 1 0 0
  10. -> 192.168.10.50:6443 Masq 1 0 0
  11. [root@k8s-40 conf]# kubectl get svc
  12. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  13. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 56m

4.2.8.部署所有节点

不同的地方

  1. /opt/kubernetes/server/bin/kube-proxy.sh
  2. --hostname-override
  3. ##########
  4. /etc/supervisord.d/kube-proxy.ini
  5. [program:kube-proxy-10-40]

5.验证kubernetes集群

5.1.在任意一个节点上创建一个资源配置清单

k8s-40.host.com上

  1. [root@k8s-40 ~]# vi /root/nginx-ds.yaml
  2. apiVersion: extensions/v1beta1
  3. kind: DaemonSet
  4. metadata:
  5. name: nginx-ds
  6. spec:
  7. template:
  8. metadata:
  9. labels:
  10. app: nginx-ds
  11. spec:
  12. containers:
  13. - name: my-nginx
  14. image: harbor.od.com/public/nginx:v1.7.9
  15. ports:
  16. - containerPort: 80

5.2.应用资源配置,并检查

5.2.1.k8s-40.host.com上
  1. 要拉取harbor仓库的镜像,得先登录harbor仓库
  2. docker login harbor.od.com
  3. # 如果报错登录不上,查看/etc/docker/daemon.json,加上参数
  4. "insecure-registries": ["harbor.od.com"],
  5. 还不行到/etc/hosts 加上 10.4.7.60 harbor.od.com
  6. [root@k8s-40 ~]# kubectl create -f /root/nginx-ds.yaml
  7. daemonset.extensions/nginx-ds created
  8. [root@k8s-40 conf]# kubectl get pods # 还在创建中
  9. NAME READY STATUS RESTARTS AGE
  10. nginx-ds-blb2x 0/1 ContainerCreating 0 16s
  11. nginx-ds-kngn7 0/1 ContainerCreating 0 16s
  12. [root@k8s-40 conf]# kubectl get pods
  13. NAME READY STATUS RESTARTS AGE
  14. nginx-ds-blb2x 1/1 Running 0 64s
  15. nginx-ds-kngn7 1/1 Running 0 64s
  16. [root@k8s-40 conf]# kubectl get pods -o wide
  17. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  18. nginx-ds-blb2x 1/1 Running 0 69s 172.7.40.2 k8s-40.host.com <none> <none>
  19. nginx-ds-kngn7 1/1 Running 0 69s 172.7.50.2 k8s-50.host.com <none> <none>
  20. [root@k8s-40 conf]# curl 172.7.40.2 #能连通
  21. [root@k8s-40 conf]# curl 172.7.50.2 #不通,是因为还不能跨主机通信

5.2.2.k8s-22.host.com上
  1. [root@k8s-50 conf]# kubectl get pods -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. nginx-ds-blb2x 1/1 Running 0 3m50s 172.7.40.2 k8s-40.host.com <none> <none>
  4. nginx-ds-kngn7 1/1 Running 0 3m50s 172.7.50.2 k8s-50.host.com <none> <none>
  5. [root@k8s-50 conf]# curl 172.7.50.2 #能通

5.2.3.查看kubernetes是否搭建好

各组件安装

  1. [root@k8s-30 ~]# supervisorctl status
  2. etcd-server-10-30 RUNNING pid 9037, uptime 2:41:25
  3. [root@k8s-40 ~]# supervisorctl status
  4. etcd-server-10-40 RUNNING pid 9007, uptime 2:39:32
  5. kube-apiserver-10-40 RUNNING pid 9153, uptime 1:03:26
  6. kube-controller-manager-10-40 RUNNING pid 9187, uptime 0:38:37
  7. kube-kubelet-10-40 RUNNING pid 9294, uptime 0:20:28
  8. kube-proxy-10-40 RUNNING pid 11566, uptime 0:07:56
  9. kube-scheduler-10-40 RUNNING pid 9205, uptime 0:33:34
  10. [root@k8s-50 ~]# supervisorctl status
  11. etcd-server-10-50 RUNNING pid 9034, uptime 2:38:36
  12. kube-apiserver-10-50 RUNNING pid 9177, uptime 1:02:52
  13. kube-controller-manager-10-50 RUNNING pid 9210, uptime 0:38:43
  14. kube-kubelet-10-50 RUNNING pid 9292, uptime 0:18:04
  15. kube-proxy-10-50 RUNNING pid 11149, uptime 0:07:57
  16. kube-scheduler-10-50 RUNNING pid 9230, uptime 0:33:41

cs状态

  1. [root@k8s-40 ~]# kubectl get cs
  2. NAME STATUS MESSAGE ERROR
  3. scheduler Healthy ok
  4. etcd-0 Healthy {"health": "true"}
  5. etcd-2 Healthy {"health": "true"}
  6. etcd-1 Healthy {"health": "true"}
  7. controller-manager Healthy ok

node状态

  1. [root@k8s-40 ~]# kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-40.host.com Ready master,node 21m v1.15.10
  4. k8s-50.host.com Ready master,node 19m v1.15.10

pod状态

  1. [root@k8s-40 ~]# kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. nginx-ds-blb2x 1/1 Running 0 6m26s
  4. nginx-ds-kngn7 1/1 Running 0 6m26s

查看日记

  1. # kubectl describe pod nginx-ds-kngn7
  2. # kubectl describe pod nginx-ds-dwhn4 -n 域名空间

node节点在创建yarm的时候没有提前登陆过镜像仓库,导致镜像没拉去下来

注意要先登陆镜像仓库

搭建完成,服务器做快照

出现错误

node状态为NotReady

查看日记kubelet.stdout.log

发现是vip_IP没有起来

重启主keepalived,问题解决