kubernetes二进制部署(HA)
Kubernetes 企业级部署实战
环境
角色 | 主机名 | 内网IP | 集群IP | 操作系统 | 服务 |
---|---|---|---|---|---|
BIND&&Proxy | HDSS7-11 | 10.0.0.11 | - | CentOS7.6 | DNS Nginx |
etcd-node&&Proxy | HDSS7-12 | 10.0.0.12 | - | CentOS7.6 | etcd nginx |
k8s-node | HDSS7-21 | 10.0.0.21 | - | CentOS7.6 | etcd,k8s-master k8s-node |
k8s-node | HDSS7-22 | 10.0.0.22 | - | CentOS7.6 | etcd,k8s-master k8s-node |
registry&&Storge | HDSS7-200 | 10.0.0.200 | - | CentOS7.6 | harbor NFS CA |
- 全部服务器关闭 firewalld 和 selinux,禁用 swap,部署机(master200)可免密 ssh 登陆其他服务器
- 软件版本
- CentOS: 7.6
- Bind: 9
- etcd: 3.1.20
- docker-ce: 19.03
- harbor: 1.9.1
- flannel: 0.11.0
- kubernetes: 1.15.2
- k8s牵扯到多个网段,这里说明下
- 10.0.0.0/24 该网段是服务器物理网卡 IP 地址段,通过该地址访问互联网
- 10.254.0.0/16 该网段是杜撰的,分配至 k8s service 的 clusterIP
- 172.7.0.0/16 该网段是杜撰的,是 k8s pod 的 IP 地址区间,用于区别流量来源
yum install wget net-tools telnet tree nmap sysstat lrzsz dos2unix bind-utils -y
部署DNS服务
-
在hdss7-11上安装Bind、
[root@hdss7-11 ~]# yum install -y bind
-
配置bind
[root@hdss7-11 ~]# vi /etc/named.conf # BIND进程的工作属性,区域的定义 13 listen-on port 53 { 10.0.0.11; }; # 监听本机IP 14 listen-on-v6 port 53 { ::1; }; # 删除,不监听IPV6 20 allow-query { any; }; # 允许所有主机查看 21 forwarders { 10.0.0.254; }; # 办公网上一级的DNS 33 recursion yes; # dns采用递归的查询 35 dnssec-enable no; # 关闭,节省资源(生产可能不需要关闭) 36 dnssec-validation no; # 关闭,节省资源,不做互联网认证
-
检查配置文件是否正确
[root@hdss7-11 ~]# named-checkconf
-
配置区域配置文件
```
[root@hdss7-11 ~]# vim /etc/named.rfc1912.zones
zone "host.com" IN {type master; file "host.com.zone"; allow-update { 10.0.0.11; };
};
zone "od.com" {
type master;
file "od.com.zone";
allow-update { 10.0.0.11; };
};
* 配置区域数据文件
[root@hdss7-11 ~]# vim /var/named/host.com.zone
```
$ORIGIN host.com.
$TTL 600 ; 10 minutes
@ IN SOA dns.host.com. dnsadmin.host.com. (
2019121001 ; serial
10800 ; refresh (3 hours)
900 ; retry (15 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
NS dns.host.com.
$TTL 60 ; 1 minute
dns A 10.0.0.11
HDSS7-11 A 10.0.0.11
HDSS7-12 A 10.0.0.12
HDSS7-21 A 10.0.0.21
HDSS7-22 A 10.0.0.22
HDSS7-200 A 10.0.0.200
[root@hdss7-11 ~]# vim /var/named/od.com.zone
$ORIGIN od.com.
$TTL 600 ; 10 minutes
@ IN SOA dns.od.com. dnsadmin.od.com. (
2019121001 ; serial
10800 ; refresh (3 hours)
900 ; retry (15 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
NS dns.od.com.
$TTL 60 ; 1 minute
dns A 10.0.0.11
- 验证解析
[root@hdss7-11 ~]# dig hdss7-21.host.com +short 10.0.0.21
- 更改客户端dns
[root@hdss7-11 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 DNS1=10.0.0.11
- 更改所有主机的DNS,重启网卡
# vim /etc/sysconfig/network-scripts/ifcfg-ens33 DNS1=10.0.0.11
建议不在网卡配置中声明DNS1 重启网络服务会影响resolv.conf文件中某些配置
- 添加主机域search host.com使用短域名与DNS服务器地址
[root@hdss7-200 ~]# cat /etc/resolv.conf search host.com nameserver 10.0.0.11
准备签发证书环境
运维主机HDSS7-200.host.coms上
-
安装CFSSL
-
证数签发工具CFSSL:R1.2
- cfssl下载地址:https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -O /usr/bin/cfssl
- cfsslhson下载地址:https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -O /usr/bin/cfssl-json
- cfssl-certinfo下载地址:https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -O /usr/bin/cfssl-certinfo
-
-
下载文件到本地主机上
[root@hdss7-200 ~]# wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -O /usr/bin/cfssl [root@hdss7-200 ~]# wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -O /usr/bin/cfssl-json [root@hdss7-200 ~]# wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -O /usr/bin/cfssl-certinfo [root@hdss7-200 ~]# chmod +x /usr/bin/cfssl* [root@hdss7-200 ~]# which cfssl-certinfo /usr/bin/cfssl-certinfo
- 签发证书
[root@hdss7-200 ~]# cd /opt/ [root@hdss7-200 opt]# mkdir certs [root@hdss7-200 opt]# cd certs/ [root@hdss7-200 certs]# vi /opt/certs/ca-csr.json { "CN": "OldboyEdu", # 机构名称,浏览器使用该字段验证网站是否合法,一般写的是域名,非常重要,浏览器使用该字段验证网站是否合法 "hosts": [ ], "key": { "algo": "rsa", # 算法 "size": 2048 # 长度 }, "names": [ { "C": "CN", # C,国家 "ST": "beijing", # ST 州,省 "L": "beijing", # L 地区 城市 "O": "od", # O 组织名称,公司名称 "OU": "ops" # OU 组织单位名称,公司部门 } ], "ca": { "expiry": "175200h" # expiry 过期时间,任何证书都有过期时间.20年 } }
- 签发承载式证书
[root@hdss7-200 certs]# cfssl gencert -initca ca-csr.json | cfssl-json -bare ca [root@hdss7-200 certs]# ll 总用量 16 -rw-r--r-- 1 root root 993 12月 10 11:54 ca.csr -rw-r--r-- 1 root root 328 12月 10 11:53 ca-csr.json -rw------- 1 root root 1679 12月 10 11:54 ca-key.pem # 根证书的私钥 -rw-r--r-- 1 root root 1346 12月 10 11:54 ca.pem # 根证书
部署docker环境
在node主机与运维主机上:21、22、200
[root@hdss7-200 ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
[root@hdss7-200 ~]# mkdir -p /etc/docker
[root@hdss7-200 ~]# mkdir -p /data/docker
[root@hdss7-200 ]# vi /etc/docker/daemon.json
{
"graph": "/data/docker",
"storage-driver": "overlay2",
"insecure-registries": ["registry.access.redhat.com","quay.io","harbor.od.com"],
"registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
"bip": "172.7.200.1/24", # 定义k8s主机上k8s pod的ip地址网段
"exec-opts": ["native.cgroupdriver=systemd"],
"live-restore": true
}
[root@hdss7-200 ~]# systemctl start docker
[root@hdss7-200 ~]# systemctl enable docker
[root@hdss7-21 ~]# vim /etc/docker/daemon.json
{
"graph": "/data/docker",
"storage-driver": "overlay2",
"insecure-registries": ["registry.access.redhat.com","quay.io","harbor.od.com"],
"registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
"bip": "172.7.21.1/24",
"exec-opts": ["native.cgroupdriver=systemd"],
"live-restore": true
}
[root@hdss7-21 ~]# systemctl start docker
[root@hdss7-21 ~]# systemctl enable docker
[root@hdss7-22 ~]# vim /etc/docker/daemon.json
{
"graph": "/data/docker",
"storage-driver": "overlay2",
"insecure-registries": ["registry.access.redhat.com","quay.io","harbor.od.com"],
"registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
"bip": "172.7.22.1/24",
"exec-opts": ["native.cgroupdriver=systemd"],
"live-restore": true
}
[root@hdss7-22 ~]# systemctl start docker
[root@hdss7-22 ~]# systemctl enable docker
Harbor 搭建
安装1.7.6以上版本
[root@hdss7-200 ~]# cd /opt/
[root@hdss7-200 opt]# mkdir src
[root@hdss7-200 opt]# cd src/
[root@hdss7-200 src]# ls
harbor-offline-installer-v1.9.1.tgz
[root@hdss7-200 src]# tar -xf harbor-offline-installer-v1.9.1.tgz -C /opt/
- 把软件包做版本标识,创建一个软连接,便于后期的升级
[root@hdss7-200 src]# cd /opt [root@hdss7-200 opt]# mv harbor/ harbor-v1.9.1 [root@hdss7-200 opt]# ln harbor-v1.9.1/ /opt/harbor ln: ‘harbor-v1.9.1/’: hard link not allowed for directory [root@hdss7-200 opt]# ln -s harbor-v1.9.1/ /opt/harbor [root@hdss7-200 opt]# ll total 0 drwxr-xr-x. 2 root root 71 Dec 12 16:21 certs drwx--x--x. 4 root root 28 Dec 12 16:56 containerd lrwxrwxrwx. 1 root root 14 Dec 12 17:20 harbor -> harbor-v1.9.1/ drwxr-xr-x. 2 root root 100 Dec 12 17:16 harbor-v1.9.1 drwxr-xr-x. 2 root root 49 Dec 12 17:14 src
-
编辑harbor文件
[root@hdss7-200 opt]# cd harbor [root@hdss7-200 harbor]# vim harbor.yml 5 hostname: harbor.od.com 10 port: 180 27 harbor_admin_password: Harbor12345 40 data_volume: /data/harbor 87 location: /data/harbor/logs # 更改日志存储路径 [root@hdss7-200 harbor]# mkdir -p /data/harbor/logs
-
docker编排工具
[root@hdss7-200 harbor]# yum install -y docker-compose [root@hdss7-200 harbor]# rpm -qa docker-compose docker-compose-1.18.0-4.el7.noarch [root@hdss7-200 harbor]# ./install.sh [root@hdss7-200 harbor]# docker-compose ps #每次docker重启需要执行 [root@hdss7-200 harbor]# docker-compose up -d #安装nginx做反向代理 [root@hdss7-200 harbor]# yum install -y nginx [root@hdss7-200 harbor]# vim /etc/nginx/conf.d/harbor.od.com.conf server { listen 80; server_name harbor.od.com; client_max_body_size 1000m; location / { proxy_pass http://127.0.0.1:180; } } # 检测配置文件 [root@hdss7-200 harbor]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@hdss7-200 harbor]# systemctl start nginx [root@hdss7-200 harbor]# systemctl enable nginx
- DNS服务器增加解析
[root@hdss7-11 ~]# vim /var/named/od.com.zone $ORIGIN od.com. $TTL 600 ; 10 minutes @ IN SOA dns.od.com. dnsadmin.od.com. ( 2019121002 ; serial 10800 ; refresh (3 hours) 900 ; retry (15 minutes) 604800 ; expire (1 week) 86400 ; minimum (1 day) ) NS dns.od.com. $TTL 60 ; 1 minute dns A 10.0.0.11 harbor A 10.0.0.200 [root@hdss7-11 ~]# systemctl restart named # 验证解析 [root@hdss7-11 ~]# dig harbor.od.com +short 10.0.0.200
-
访问http://harbor.od.com
创建一个public项目,访问级别为公开 -
测试上传镜像
[root@hdss7-200 harbor]# docker pull nginx:1.15 1.15: Pulling from library/nginx 743f2d6c1f65: Pull complete 6bfc4ec4420a: Pull complete 688a776db95f: Pull complete Digest: sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68 Status: Downloaded newer image for nginx:1.15 docker.io/library/nginx:1.15 [root@hdss7-200 harbor]# docker tag nginx:1.15 harbor.od.com/public/nginx:1.15 [root@hdss7-200 harbor]# docker login harbor.od.com Username: admin Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded [root@hdss7-200 harbor]# docker push harbor.od.com/public/nginx:1.15 The push refers to repository [harbor.od.com/public/nginx] 332fa54c5886: Pushed 6ba094226eea: Pushed 6270adb5794c: Pushed 1.15: digest: sha256:e770165fef9e36b990882a4083d8ccf5e29e469a8609bb6b2e3b47d9510e2c8d size: 948
部署etcd集群
集群规划
主机名 | 角色 | IP |
---|---|---|
HDSS7-12.host.com | etcd lead | 10.0.0.12 |
HDSS7-21.host.com | etcd follow | 10.0.0.21 |
HDSS7-21.host.com | etcd follow | 10.0.0.22 |
注: 这里部署文档以HDSS7-12.host.com主机为例,另外一台运算节点安装部署方法类似
- 在HDss7-200上创建基于根证书的config配置文件 -- 此文件夹内有,直接上传,不需粘贴复制
[root@hdss7-200 certs]# vim /opt/certs/ca-config.json { "signing": { "default": { "expiry": "175200h" }, "profiles": { "server": { "expiry": "175200h", "usages": [ "signing", "key encipherment", "server auth" ] }, "client": { "expiry": "175200h", "usages": [ "signing", "key encipherment", "client auth" ] }, "peer": { "expiry": "175200h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } } }
- IP地址根据实际情况填写,IP地址为有可能装ETCD的主机,多一个IP为预备,-- 此文件夹内有,直接上传,无需粘贴复制
[root@hdss7-200 certs]# vim /opt/certs/etcd-peer-csr.json
{
"CN": "k8s-etcd",
"hosts": [
"10.0.0.11",
"10.0.0.12",
"10.0.0.21",
"10.0.0.22"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "beijing",
"L": "beijing",
"O": "od",
"OU": "ops"
}
]
}
- 签发证书
[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
- 在etcd主机上创建而etcd用户
[root@hdss7-12 ~]# useradd -s /sbin/nologin -M etcd [root@hdss7-12 ~]# mkdir /opt/src [root@hdss7-12 ~]# cd /opt/src/
- 下载etcd软件包,建议使用不超过3.3的版本
[root@hdss7-12 src]# wget https://github.com/etcd-io/etcd/releases/download/v3.1.20/etcd-v3.1.20-linux-amd64.tar.gz [root@hdss7-12 src]# ls etcd-v3.1.20-linux-amd64.tar.gz [root@hdss7-12 src]# tar xfv etcd-v3.1.20-linux-amd64.tar.gz -C /opt/ [root@hdss7-12 opt]# mv etcd-v3.1.20-linux-amd64/ etcd-v3.1.20
- 创建软连接便于后期版本更新
[root@hdss7-12 opt]# ln -s /opt/etcd-v3.1.20 /opt/etcd [root@hdss7-12 opt]# ll total 0 lrwxrwxrwx. 1 root root 17 Dec 12 20:05 etcd -> /opt/etcd-v3.1.20 drwxr-xr-x. 3 478493 89939 123 Oct 11 2018 etcd-v3.1.20 drwxr-xr-x. 2 root root 45 Dec 12 20:04 src
- 创建目录,拷贝证书、私钥
[root@hdss7-12 opt]# mkdir -p /opt/etcd/certs /data/etcd /data/logs/etcd-server
- 将运维主机上生成的ca.pem etcd-peer-key.pem etcd-peer.pem 拷贝到opt/etcd/certs目录中,私钥文件权限为600
[root@hdss7-12 opt]# cd /opt/etcd/certs/ [root@hdss7-12 certs]# scp hdss7-200:/opt/certs/ca.pem . [root@hdss7-12 certs]# scp hdss7-200:/opt/certs/etcd-peer-key.pem . [root@hdss7-12 certs]# scp hdss7-200:/opt/certs/etcd-peer.pem .
- 更改属主组
[root@hdss7-12 certs]# chown -R etcd:etcd /opt/etcd/certs /data/etcd /data/logs/etcd-server [root@hdss7-12 certs]# ll total 12 -rw-r--r--. 1 etcd etcd 1346 Dec 12 20:11 ca.pem -rw-------. 1 etcd etcd 1675 Dec 12 20:18 etcd-peer-key.pem -rw-r--r--. 1 etcd etcd 1428 Dec 12 20:18 etcd-peer.pem
- 创建etcd服务启动脚本IP地址改为本机IP
[root@hdss7-12 certs]# vim /opt/etcd/etcd-server-startup.sh #!/bin/sh ./etcd --name etcd-server-7-12 \ --data-dir /data/etcd/etcd-server \ --listen-peer-urls https://10.0.0.12:2380 \ --listen-client-urls https://10.0.0.12:2379,http://127.0.0.1:2379 \ --quota-backend-bytes 8000000000 \ --initial-advertise-peer-urls https://10.0.0.12:2380 \ --advertise-client-urls https://10.0.0.12:2379,http://127.0.0.1:2379 \ --initial-cluster etcd-server-7-12=https://10.0.0.12:2380,etcd-server-7-21=https://10.0.0.21:2380,etcd-server-7-22=https://10.0.0.22:2380 \ --ca-file ./certs/ca.pem \ --cert-file ./certs/etcd-peer.pem \ --key-file ./certs/etcd-peer-key.pem \ --client-cert-auth \ --trusted-ca-file ./certs/ca.pem \ --peer-ca-file ./certs/ca.pem \ --peer-cert-file ./certs/etcd-peer.pem \ --peer-key-file ./certs/etcd-peer-key.pem \ --peer-client-cert-auth \ --peer-trusted-ca-file ./certs/ca.pem \ --log-output stdout
- 赋予启动脚本执行权限
[root@hdss7-12 certs]# chmod +x /opt/etcd/etcd-server-startup.sh [root@hdss7-12 certs]# chown -R etcd:etcd /opt/etcd-v3.1.20/ /data/etcd/ /data/logs/etcd-server/
- 设置etcd后端运行
[root@hdss7-12 certs]# yum install supervisor -y [root@hdss7-12 certs]# systemctl start supervisord [root@hdss7-12 certs]# systemctl enable supervisord
- 更改supervisor配置文件:[program:etcd-server-7-12]名字根据实际修改
[root@hdss7-12 certs]# vim /etc/supervisord.d/etcd-server.ini [program:etcd-server-7-12] command=/opt/etcd/etcd-server-startup.sh ; the program (relative uses PATH, can take args) numprocs=1 ; number of processes copies to start (def 1) directory=/opt/etcd ; directory to cwd to before exec (def no cwd) autostart=true ; start at supervisord start (default: true) autorestart=true ; retstart at unexpected quit (default: true) startsecs=30 ; number of secs prog must stay running (def. 1) startretries=3 ; max # of serial start failures (default 3) exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) stopsignal=QUIT ; signal used to kill process (default TERM) stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) user=etcd ; setuid to this UNIX account to run the program redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile=/data/logs/etcd-server/etcd.stdout.log ; stdout log path, NONE for none; default AUTO stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB) stdout_logfile_backups=4 ; # of stdout logfile backups (default 10) stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) stdout_events_enabled=false ; emit events on stdout writes (default false) killallgroup=true stopgroup=true
- 创建后端启动etcd
[root@hdss7-12 logs]# supervisorctl update [root@hdss7-12 etcd]# supervisorctl status etcd-server-7-12 STARTING [root@hdss7-12 etcd]# supervisorctl status etcd-server-7-12 RUNNING pid 30907, uptime 0:03:23
[root@hdss7-12 etcd]# netstat -luntp | grep etcd tcp 0 0 10.0.0.12:2379 0.0.0.0:* LISTEN 30908/./etcd tcp 0 0 127.0.0.1:2379 0.0.0.0:* LISTEN 30908/./etcd tcp 0 0 10.0.0.12:2380 0.0.0.0:* LISTEN 30908/./etcd
- 查看日志
[root@hdss7-12 etcd]# tail -n 100 /data/logs/etcd-server/etcd.stdout.log
- 三个etcd节点都起来后
[root@hdss7-12 etcd]# ./etcdctl cluster-health member 6cbdd801d2c800d9 is healthy: got healthy result from http://127.0.0.1:2379 member 74538ef5dc383e39 is healthy: got healthy result from http://127.0.0.1:2379 member f7a9c20602b8532e is healthy: got healthy result from http://127.0.0.1:2379 cluster is healthy [root@hdss7-12 etcd]# ./etcdctl member list 6cbdd801d2c800d9: name=etcd-server-7-21 peerURLs=https://10.0.0.21:2380 clientURLs=http://127.0.0.1:2379,https://10.0.0.21:2379 isLeader=false 74538ef5dc383e39: name=etcd-server-7-22 peerURLs=https://10.0.0.22:2380 clientURLs=http://127.0.0.1:2379,https://10.0.0.22:2379 isLeader=false f7a9c20602b8532e: name=etcd-server-7-12 peerURLs=https://10.0.0.12:2380 clientURLs=http://127.0.0.1:2379,https://10.0.0.12:2379 isLeader=true
安装部署主控节点服务 -- apiserver
部署kube-apiserver集群
集群规划
主机名 | 角色 | ip |
---|---|---|
HDSS7-21.host.com | kube-apiserver | 10.0.0.21 |
HDSS7-22.host.com | kube-apiserver | 10.0.0.22 |
HDSS7-11.host.com | 4层负载均衡 | 10.0.0.11 |
HDSS7-12.host.com | 4层负载均衡 | 10.0.0.12 |
注:这里192.168.153.11和192.168.153.12使用nginx做4层负载均衡器,用keepalive跑一个vip:192.168.153.10,代理两个kube-apiserver,实现高可用
注: 这里部署文档以HDSS7-21.host.com主机为例,另外一台运算节点安装部署方法类似
- 下载软件,解压,创建软连接,安装k8s-v1.15.2
[root@hdss7-21 src]# cd /opt/src/ [root@hdss7-21 src]# ll -rw-r--r--. 1 root root 443770238 Aug 6 15:31 kubernetes-server-linux-amd64-v1.15.2.tar.gz [root@hdss7-21 src]# tar xf kubernetes-server-linux-amd64-v1.15.2.tar.gz -C /opt/ [root@hdss7-21 src]# cd /opt/ [root@hdss7-21 opt]# mv kubernetes/ kubernetes-v1.15.2 [root@hdss7-21 opt]# ln -s kubernetes-v1.15.2/ kubernetes [root@hdss7-21 opt]# ll total 0 drwx--x--x. 4 root root 28 Dec 12 16:46 containerd lrwxrwxrwx. 1 root root 13 Dec 12 21:06 etcd -> etcd-v3.1.20/ drwxr-xr-x. 4 etcd etcd 166 Dec 12 21:44 etcd-v3.1.20 lrwxrwxrwx. 1 root root 19 Dec 12 22:33 kubernetes -> kubernetes-v1.15.2/ drwxr-xr-x. 4 root root 79 Aug 5 18:01 kubernetes-v1.15.2 drwxr-xr-x. 2 root root 97 Dec 12 22:30 src [root@hdss7-21 opt]# cd kubernetes [root@hdss7-21 kubernetes]# ls addons kubernetes-src.tar.gz LICENSES server
- 删除源码包
[root@hdss7-21 kubernetes]# rm -rf kubernetes-src.tar.gz
- 删除无用的文件docker镜像等
[root@hdss7-21 kubernetes]# cd server/bin/ [root@hdss7-21 bin]# rm -rf *.tar [root@hdss7-21 bin]# rm -rf *_tag
- 剩余的可执行文件
[root@hdss7-21 bin]# ll total 884636 -rwxr-xr-x. 1 root root 43534816 Aug 5 18:01 apiextensions-apiserver -rwxr-xr-x. 1 root root 100548640 Aug 5 18:01 cloud-controller-manager -rwxr-xr-x. 1 root root 200648416 Aug 5 18:01 hyperkube -rwxr-xr-x. 1 root root 40182208 Aug 5 18:01 kubeadm -rwxr-xr-x. 1 root root 164501920 Aug 5 18:01 kube-apiserver -rwxr-xr-x. 1 root root 116397088 Aug 5 18:01 kube-controller-manager -rwxr-xr-x. 1 root root 42985504 Aug 5 18:01 kubectl -rwxr-xr-x. 1 root root 119616640 Aug 5 18:01 kubelet -rwxr-xr-x. 1 root root 36987488 Aug 5 18:01 kube-proxy -rwxr-xr-x. 1 root root 38786144 Aug 5 18:01 kube-scheduler -rwxr-xr-x. 1 root root 1648224 Aug 5 18:01 mounter
- 签发apiserver-client证书:apiserver与etc通信的证书。apiserver是客户端,etcd是服务端
运维主机HDSS-200.host.com上
- 创建生成证书签名请求(csr)的JSON配置文件
[root@hdss7-200 certs]# vim client-csr.json [root@hdss7-200 certs]# vim /opt/certs/client-csr.json { "CN": "k8s-node", "hosts": [ ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "beijing", "L": "beijing", "O": "od", "OU": "ops" } ] } [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
-
创建签名请求(csr)的还送配置文件,apiserver,server端证书
[root@hdss7-200 certs]# vim apiserver-csr.json { "CN": "k8s-apiserver", "hosts": [ "127.0.0.1", "10.254.0.1", "kubernetes.default", "kubernetes.default.svc", "kubernetes.default.svc.cluster", "kubernetes.default.svc.cluster.local", "10.0.0.10", "10.0.0.21", "10.0.0.22", "10.0.0.23" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "beijing", "L": "beijing", "O": "od", "OU": "ops" } ] } [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
-
拷贝证书
[root@hdss7-21 bin]# mkdir cert [root@hdss7-21 bin]# cd cert/ 最后有个点,表示拷贝到当前目录 [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/ca.pem . [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/ca-key.pem . [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/client.pem . [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/client-key.pem . [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/apiserver.pem . [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/apiserver-key.pem .
-
创建启动配置脚本 可直接上传
[root@hdss7-21 cert]# cd .. [root@hdss7-21 bin]# mkdir conf [root@hdss7-21 bin]# cd conf/ [root@hdss7-21 config]# vi audit.yaml
apiVersion: audit.k8s.io/v1beta1 # This is required. kind: Policy # Don't generate audit events for all requests in RequestReceived stage. omitStages: - "RequestReceived" rules: # Log pod changes at RequestResponse level - level: RequestResponse resources: - group: "" # Resource "pods" doesn't match requests to any subresource of pods, # which is consistent with the RBAC policy. resources: ["pods"] # Log "pods/log", "pods/status" at Metadata level - level: Metadata resources: - group: "" resources: ["pods/log", "pods/status"] # Don't log requests to a configmap called "controller-leader" - level: None resources: - group: "" resources: ["configmaps"] resourceNames: ["controller-leader"] # Don't log watch requests by the "system:kube-proxy" on endpoints or services - level: None users: ["system:kube-proxy"] verbs: ["watch"] resources: - group: "" # core API group resources: ["endpoints", "services"] # Don't log authenticated requests to certain non-resource URL paths. - level: None userGroups: ["system:authenticated"] nonResourceURLs: - "/api*" # Wildcard matching. - "/version" # Log the request body of configmap changes in kube-system. - level: Request resources: - group: "" # core API group resources: ["configmaps"] # This rule only applies to resources in the "kube-system" namespace. # The empty string "" can be used to select non-namespaced resources. namespaces: ["kube-system"] # Log configmap and secret changes in all other namespaces at the Metadata level. - level: Metadata resources: - group: "" # core API group resources: ["secrets", "configmaps"] # Log all other resources in core and extensions at the Request level. - level: Request resources: - group: "" # core API group - group: "extensions" # Version of group should NOT be included. # A catch-all rule to log all other requests at the Metadata level. - level: Metadata # Long-running requests like watches that fall under this rule will not # generate an audit event in RequestReceived. omitStages: - "RequestReceived"
- 查看帮助命令,查看每行的意思
[root@hdss7-21 bin]# ./kube-apiserver --help|grep -A 5 target-ram-mb
-
编写启动脚本
[root@hdss7-21 conf]# vi /opt/kubernetes/server/bin/kube-apiserver.sh
#!/bin/bash ./kube-apiserver \ --apiserver-count 2 \ --audit-log-path /data/logs/kubernetes/kube-apiserver/audit-log \ --audit-policy-file /opt/kubernetes/server/bin/conf/audit.yaml \ --authorization-mode RBAC \ --client-ca-file ./cert/ca.pem \ --requestheader-client-ca-file ./cert/ca.pem \ --enable-admission-plugins NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota \ --etcd-cafile ./cert/ca.pem \ --etcd-certfile ./cert/client.pem \ --etcd-keyfile ./cert/client-key.pem \ --etcd-servers https://10.0.0.12:2379,https://10.0.0.21:2379,https://10.0.0.22:2379 \ --service-account-key-file ./cert/ca-key.pem \ --service-cluster-ip-range 10.254.0.0/16 \ --service-node-port-range 3000-29999 \ --target-ram-mb=1024 \ --kubelet-client-certificate ./cert/client.pem \ --kubelet-client-key ./cert/client-key.pem \ --log-dir /data/logs/kubernetes/kube-apiserver \ --tls-cert-file ./cert/apiserver.pem \ --tls-private-key-file ./cert/apiserver-key.pem \ --v 2
-
查看帮助
[root@hdss7-21 bin]# ./kube-apiserver --help|grep -A 5 target-ram-mb
- 添加执行权限
[root@hdss7-21 bin]# chmod +x kube-apiserver.sh
-
创建后台启动
[root@hdss7-21 bin]# vim /etc/supervisord.d/kube-apiserver.ini [program:kube-apiserver-7-21] # 21根据实际IP地址更改 command=/opt/kubernetes/server/bin/kube-apiserver.sh ; the program (relative uses PATH, can take args) numprocs=1 ; number of processes copies to start (def 1) directory=/opt/kubernetes/server/bin ; directory to cwd to before exec (def no cwd) autostart=true ; start at supervisord start (default: true) autorestart=true ; retstart at unexpected quit (default: true) startsecs=30 ; number of secs prog must stay running (def. 1) startretries=3 ; max # of serial start failures (default 3) exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) stopsignal=QUIT ; signal used to kill process (default TERM) stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) user=root ; setuid to this UNIX account to run the program redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile=/data/logs/kubernetes/kube-apiserver/apiserver.stdout.log ; stderr log path, NONE for none; default AUTO stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB) stdout_logfile_backups=4 ; # of stdout logfile backups (default 10) stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) stdout_events_enabled=false ; emit events on stdout writes (default false) killallgroup=true stopgroup=true
[root@hdss7-21 bin]# mkdir -p /data/logs/kubernetes/kube-apiserver
[root@hdss7-21 bin]# supervisorctl update
kube-apiserver-7-21: added process group [root@hdss7-21 bin]# netstat -luntp | grep kube-api tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 33042/./kube-apiser tcp6 0 0 :::6443 :::* LISTEN 33042/./kube-apiser
L4反向代理apiserver保证高可用
-
hdss7-11 hdss7-12主机上部署nginx
[root@hdss7-11 ~]# yum install -y nginx [root@hdss7-11 ~]# vim /etc/nginx/nginx.conf
# 末尾追加 stream { upstream kube-apiserver { server 10.0.0.21:6443 max_fails=3 fail_timeout=30s; server 10.0.0.22:6443 max_fails=3 fail_timeout=30s; } server { listen 7443; proxy_connect_timeout 2s; proxy_timeout 900s; proxy_pass kube-apiserver; } }
[root@hdss7-11 ~]# systemctl start nginx [root@hdss7-11 ~]# systemctl enable nginx
- hdss7-11 hdss7-12主机上部署keeyalived
[root@hdss7-11 ~]# yum install keepalived -y
- 编写监听脚本
[root@hdss7-11 ~]# vi /etc/keepalived/check_port.sh
#!/bin/bash #keepalived 监控端口脚本 #使用方法: #在keepalived的配置文件中 #vrrp_script check_port {#创建一个vrrp_script脚本,检查配置 # script "/etc/keepalived/check_port.sh 6379" #配置监听的端口 # interval 2 #检查脚本的频率,单位(秒) #} CHK_PORT=$1 if [ -n "$CHK_PORT" ];then PORT_PROCESS=`ss -lnt|grep $CHK_PORT|wc -l` if [ $PORT_PROCESS -eq 0 ];then echo "Port $CHK_PORT Is Not Used,End." exit 1 fi else echo "Check Port Cant Be Empty!" fi
- 配置keepalived
keepalived主
! Configuration File for keepalived
global_defs {
router_id 10.0.0.11
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_port.sh 7443"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33 # 根据实际网卡更改
virtual_router_id 251
priority 100
advert_int 1
mcast_src_ip 10.0.0.11
nopreempt
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.0.0.10
}
}
[root@hdss7-11 ~]# systemctl enable keepalived.service
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@hdss7-11 ~]# systemctl start keepalived.service
keepalived从:
! Configuration File for keepalived
global_defs {
router_id 10.0.0.12
script_user root
enable_script_security
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_port.sh 7443"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens33 # 根据实际网卡更改
virtual_router_id 251
mcast_src_ip 10.0.0.12
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.0.0.10
}
}
[root@hdss7-12 ~]# systemctl start keepalived.service
[root@hdss7-12 ~]# systemctl enable keepalived.service
生产使用nopreempt参数指定非主占模式避免vip漂移到未完全恢复服务的主机,若vip出现变动,主keepalived恢复后,一定要确认主keepalived端口起来, 服务搞好,重启keepalived,使vip变回主keepalived
[root@hdss7-11 ~]# netstat -luntp | grep 7443
tcp 0 0 0.0.0.0:7443 0.0.0.0:* LISTEN 38902/nginx: master
安装部署主控节点控制器与调度服务
集群规划
主机名 | 角色 | ip |
---|---|---|
HDSS7-21.host.com | controller-manager schedule | 10.0.0.21 |
HDSS7-22.host.com | controller-manager schedule | 10.0.0.22 |
注: 这里部署文档以HDSS7-21.host.com主机为例,另外一台运算节点安装部署方法类似
部署kube-controller-manager
-
创建启动脚本:
[root@hdss7-21 bin]# vi /opt/kubernetes/server/bin/kube-controller-manager.sh
#!/bin/sh ./kube-controller-manager \ --cluster-cidr 172.7.0.0/16 \ --leader-elect true \ --log-dir /data/logs/kubernetes/kube-controller-manager \ --master http://127.0.0.1:8080 \ --service-account-private-key-file ./cert/ca-key.pem \ --service-cluster-ip-range 10.254.0.0/16 \ --root-ca-file ./cert/ca.pem \ --v 2
[root@hdss7-21 bin]# mkdir /data/logs/kubernetes/kube-controller-manager [root@hdss7-21 bin]# chmod +x kube-controller-manager.sh
-
添加后台启动
[root@hdss7-21 bin]# vi /etc/supervisord.d/kube-controller-manager.ini
[program:kube-controller-manager-7-21] command=/opt/kubernetes/server/bin/kube-controller-manager.sh ; the program (relative uses PATH, can take args) numprocs=1 ; number of processes copies to start (def 1) directory=/opt/kubernetes/server/bin ; directory to cwd to before exec (def no cwd) autostart=true ; start at supervisord start (default: true) autorestart=true ; retstart at unexpected quit (default: true) startsecs=30 ; number of secs prog must stay running (def. 1) startretries=3 ; max # of serial start failures (default 3) exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) stopsignal=QUIT ; signal used to kill process (default TERM) stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) user=root ; setuid to this UNIX account to run the program redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile=/data/logs/kubernetes/kube-controller-manager/controller.stdout.log ; stderr log path, NONE for none; default AUTO stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB) stdout_logfile_backups=4 ; # of stdout logfile backups (default 10) stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) stdout_events_enabled=false ; emit events on stdout writes (default false) killallgroup=true stopgroup=true
[root@hdss7-21 bin]# supervisorctl update kube-controller-manager-7-21: added process group
部署kube-scheduler
-
创建启动脚本
[root@hdss7-21 bin]# vim /opt/kubernetes/server/bin/kube-scheduler.sh
#!/bin/sh ./kube-scheduler \ --leader-elect \ --log-dir /data/logs/kubernetes/kube-scheduler \ --master http://127.0.0.1:8080 \ --v 2
[root@hdss7-21 bin]# chmod +x /opt/kubernetes/server/bin/kube-scheduler.sh [root@hdss7-21 bin]# mkdir -p /data/logs/kubernetes/kube-scheduler
-
添加后台启动
[root@hdss7-21 bin]# vi /etc/supervisord.d/kube-scheduler.ini
[program:kube-scheduler-7-21]
command=/opt/kubernetes/server/bin/kube-scheduler.sh ; the program (relative uses PATH, can take args)
numprocs=1 ; number of processes copies to start (def 1)
directory=/opt/kubernetes/server/bin ; directory to cwd to before exec (def no cwd)
autostart=true ; start at supervisord start (default: true)
autorestart=true ; retstart at unexpected quit (default: true)
startsecs=30 ; number of secs prog must stay running (def. 1)
startretries=3 ; max # of serial start failures (default 3)
exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT ; signal used to kill process (default TERM)
stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
user=root ; setuid to this UNIX account to run the program
redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/data/logs/kubernetes/kube-scheduler/scheduler.stdout.log ; stderr log path, NONE for none; default AUTO
stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=4 ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false ; emit events on stdout writes (default false)
killallgroup=true
stopgroup=true
[root@hdss7-21 bin]# supervisorctl update
kube-scheduler-7-21: added process group
[root@hdss7-22 config]# supervisorctl status
etcd-server-7-22 RUNNING pid 30591, uptime 18:59:07
kube-apiserver-7-22 RUNNING pid 31033, uptime 16:42:23
kube-controller-manager-7-22 RUNNING pid 33196, uptime 0:13:50
kube-scheduler-7-22 STARTING
- 查看集群节点状态
[root@hdss7-21 bin]# ln -s /opt/kubernetes/server/bin/kubectl /usr/bin/kubectl [root@hdss7-21 bin]# which kubectl /usr/bin/kubectl [root@hdss7-21 bin]# kubectl get cs NAME STATUS MESSAGE ERROR scheduler Healthy ok controller-manager Healthy ok etcd-2 Healthy {"health": "true"} etcd-1 Healthy {"health": "true"} etcd-0 Healthy {"health": "true"}
部署Node节点服务
部署kubelet
集群规划
主机名 | 角色 | ip |
---|---|---|
HDSS7-21.host.com | kubelet | 10.0.0.21 |
HDSS7-22.host.com | kubelet | 10.0.0.22 |
注: 这里部署文档以HDSS7-21.host.com主机为例,另外一台运算节点安装部署方法类似
运维主机HDSS7-200.host.com
签发证书
-
创建生成证书签名请求(csr)的JSON配置文件
[root@hdss7-200 ~]# cd /opt/certs/ [root@hdss7-200 certs]# vi kubelet-csr.json # 添加node节点IP,预先配置上可有能安装使用的IP,如果新node的ip不在证书内,需要重新编写证书,拷贝至所有主
{ "CN": "k8s-kubelet", "hosts": [ "127.0.0.1", "10.0.0.10", "10.0.0.21", "10.0.0.22", "10.0.0.23", "10.0.0.24", "10.0.0.25", "10.0.0.26", "10.0.0.27", "10.0.0.28" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "beijing", "L": "beijing", "O": "od", "OU": "ops" } ] }
-
生成证书
[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
[root@hdss7-200 certs]# ll -rw-r--r--. 1 root root 1115 Dec 13 22:03 kubelet.csr -rw-r--r--. 1 root root 452 Dec 13 22:02 kubelet-csr.json -rw-------. 1 root root 1679 Dec 13 22:03 kubelet-key.pem -rw-r--r--. 1 root root 1468 Dec 13 22:03 kubelet.pem # 注意私钥文件权限属性600
-
分发证书到hdss7-21,hdss7-22
#拷贝证书、私钥,拷贝至:/opt/kubernetes/server/bin/cert/ [root@hdss7-21 ~]# cd /opt/kubernetes/server/bin/cert/ [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/kubelet.pem . root@hdss7-200's password: kubelet.pem 100% 1468 1.2MB/s 00:00 [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/kubelet-key.pem .
- 创建配置——set-cluster
只做一次,最后生成的 kubelet.kubeconfig 拷贝至其他节点,在conf目录下
[root@hdss7-21 cert]# cd /opt/kubernetes/server/bin/conf
[root@hdss7-21 conf]# kubectl config set-cluster myk8s \
--certificate-authority=/opt/kubernetes/server/bin/cert/ca.pem \
--embed-certs=true \
--server=https://10.0.0.10:7443 \
--kubeconfig=kubelet.kubeconfig
Cluster "myk8s" set.
- 创建配置——set-credentials
[root@hdss7-21 conf]# kubectl config set-credentials k8s-node \ --client-certificate=/opt/kubernetes/server/bin/cert/client.pem \ --client-key=/opt/kubernetes/server/bin/cert/client-key.pem \ --embed-certs=true \ --kubeconfig=kubelet.kubeconfig User "k8s-node" set.
- 创建配置——set-context
[root@hdss7-21 conf]# kubectl config set-context myk8s-context \ --cluster=myk8s \ --user=k8s-node \ --kubeconfig=kubelet.kubeconfig Context "myk8s-context" created.
- 创建配置——use-context
[root@hdss7-21 conf]# kubectl config use-context myk8s-context --kubeconfig=kubelet.kubeconfig Switched to context "myk8s-context".
- k8s-node.yaml 授予权限,角色绑定
[root@hdss7-21 conf]# vi k8s-node.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: k8s-node
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:node
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: k8s-node
[root@hdss7-21 conf]# kubectl create -f k8s-node.yaml
clusterrolebinding.rbac.authorization.k8s.io/k8s-node created
[root@hdss7-21 conf]# kubectl get clusterrolebinding k8s-node
[root@hdss7-21 conf]# kubectl get clusterrolebinding k8s-node -o yaml
HDSS7-22上:
- HDSS-21上最后生成的 kubelet.kubeconfig 拷贝至其他节点
[root@hdss7-22 conf]# scp hdss7-21:/opt/kubernetes/server/bin/conf/kubelet.kubeconfig .
其它计算节点安装部署方法类似安装其他节点,需要先拷贝已安装节点的kubelet.kubeconfig
准备pause基础镜像 --sidecar模式
运维主机hdss7-200.host.com上:
- 下载
[root@hdss7-200 ~]# docker pull kubernetes/pause Using default tag: latest latest: Pulling from kubernetes/pause 4f4fb700ef54: Pull complete b9c8ec465f6b: Pull complete Digest: sha256:b31bfb4d0213f254d361e0079deaaebefa4f82ba7aa76ef82e90b4935ad5b105 Status: Downloaded newer image for kubernetes/pause:latest docker.io/kubernetes/pause:latest
- 提交至私有仓库(harbor)中
[root@hdss7-200 ~]# docker images | grep pause kubernetes/pause latest f9d5de079539 5 years ago 240kB [root@hdss7-200 ~]# docker tag f9d5de079539 harbor.od.com/public/pause:latest [root@hdss7-200 ~]# docker push harbor.od.com/public/pause:latest
- 编写kubelet启动脚本 -- 注意更改主机名
[root@hdss7-21 conf]# vi /opt/kubernetes/server/bin/kubelet.sh
#!/bin/sh ./kubelet \ --anonymous-auth=false \ --cgroup-driver systemd \ --cluster-dns 10.254.0.2 \ --cluster-domain cluster.local \ --runtime-cgroups=/systemd/system.slice \ --kubelet-cgroups=/systemd/system.slice \ --fail-swap-on="false" \ --client-ca-file ./cert/ca.pem \ --tls-cert-file ./cert/kubelet.pem \ --tls-private-key-file ./cert/kubelet-key.pem \ --hostname-override hdss7-21.host.com \ --image-gc-high-threshold 20 \ --image-gc-low-threshold 10 \ --kubeconfig ./conf/kubelet.kubeconfig \ --log-dir /data/logs/kubernetes/kube-kubelet \ --pod-infra-container-image harbor.od.com/public/pause:latest \ --root-dir /data/kubelet
[root@hdss7-21 conf]# mkdir -p /data/logs/kubernetes/kube-kubelet /data/kubelet [root@hdss7-21 conf]# chmod +x /opt/kubernetes/server/bin/kubelet.sh
-
添加后台启动
[root@hdss7-21 conf]# vi /etc/supervisord.d/kubelet.ini
[program:kube-kubelet-7-21] command=/opt/kubernetes/server/bin/kubelet.sh ; the program (relative uses PATH, can take args) numprocs=1 ; number of processes copies to start (def 1) directory=/opt/kubernetes/server/bin ; directory to cwd to before exec (def no cwd) autostart=true ; start at supervisord start (default: true) autorestart=true ; retstart at unexpected quit (default: true) startsecs=30 ; number of secs prog must stay running (def. 1) startretries=3 ; max # of serial start failures (default 3) exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) stopsignal=QUIT ; signal used to kill process (default TERM) stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) user=root ; setuid to this UNIX account to run the program redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile=/data/logs/kubernetes/kube-kubelet/kubelet.stdout.log ; stderr log path, NONE for none; default AUTO stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB) stdout_logfile_backups=4 ; # of stdout logfile backups (default 10) stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) stdout_events_enabled=false ; emit events on stdout writes (default false) killallgroup=true stopgroup=true
-
查看运行状态
[root@hdss7-22 conf]# supervisorctl update [root@hdss7-22 conf]# supervisorctl status etcd-server-7-22 RUNNING pid 43905, uptime 0:11:26 kube-apiserver-7-22 RUNNING pid 44498, uptime 0:09:30 kube-controller-manager-7-22 RUNNING pid 44769, uptime 0:08:11 kube-kubelet-7-22 RUNNING pid 47185, uptime 0:01:43 kube-scheduler-7-22 RUNNING pid 44982, uptime 0:07:09
-
ROlES添加标签,设定节点角色,可同时加两个标签
[root@hdss7-21 cert]# kubectl label node hdss7-21.host.com node-role.kubernetes.io/master= [root@hdss7-21 cert]# kubectl label node hdss7-21.host.com node-role.kubernetes.io/node=
[root@hdss7-21 cert]# kubectl get nodes NAME STATUS ROLES AGE VERSION hdss7-21.host.com Ready master,node 53m v1.15.2 hdss7-22.host.com Ready master,node 6m27s v1.15.2
部署kube-proxy
集群规划
主机名 | 角色 | ip |
---|---|---|
HDSS7-21.host.com | kube-proxy | 10.0.0.21 |
HDSS7-22.host.com | kube-proxy | 10.0.0.22 |
注: 这里部署文档以HDSS7-21.host.com主机为例,另外一台运算节点安装部署方法类似
签发kube-proxy证书
运维主机HDSS7-200.host.com上
- 创建生政证书签名要求(csr)的JSON配置文件
[root@hdss7-200 certs]# vim /opt/certs/kube-proxy-csr.json
{ "CN": "system:kube-proxy", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "beijing", "L": "beijing", "O": "od", "OU": "ops" } ] }
- 生成证书
[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 [root@hdss7-200 certs]# ll -rw-r--r--. 1 root root 1005 Dec 14 16:43 kube-proxy-client.csr -rw-------. 1 root root 1679 Dec 14 16:43 kube-proxy-client-key.pem -rw-r--r--. 1 root root 1375 Dec 14 16:43 kube-proxy-client.pem -rw-r--r--. 1 root root 267 Dec 14 16:36 kube-proxy-csr.json
-
分发证书,将证书拷贝到node节点
[root@hdss7-21 ~]# cd /opt/kubernetes/server/bin/cert/ [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/kube-proxy-client.pem . [root@hdss7-21 cert]# scp hdss7-200:/opt/certs/kube-proxy-client-key.pem .
-
在conf文件夹下创建配置 -- 只做一次,然后将kube-proxy.kubeconfig拷贝至各个node节点
[root@hdss7-21 conf]# kubectl config set-cluster myk8s \ --certificate-authority=/opt/kubernetes/server/bin/cert/ca.pem \ --embed-certs=true \ --server=https://10.0.0.10:7443 \ --kubeconfig=kube-proxy.kubeconfig Cluster "myk8s" set.
[root@hdss7-21 conf]# ls audit.yaml k8s-node.yaml kubelet.kubeconfig kube-proxy.kubeconfig
[root@hdss7-21 conf]# kubectl config set-credentials kube-proxy \ --client-certificate=/opt/kubernetes/server/bin/cert/kube-proxy-client.pem \ --client-key=/opt/kubernetes/server/bin/cert/kube-proxy-client-key.pem \ --embed-certs=true \ --kubeconfig=kube-proxy.kubeconfig User "kube-proxy" set.
[root@hdss7-21 conf]# kubectl config set-context myk8s-context \ --cluster=myk8s \ --user=kube-proxy \ --kubeconfig=kube-proxy.kubeconfig Context "myk8s-context" created.
[root@hdss7-21 conf]# kubectl config use-context myk8s-context --kubeconfig=kube-proxy.kubeconfig Switched to context "myk8s-context".
-
第一台node节点部署完成后,将生成的配置文件拷贝至各个Node节点
[root@hdss7-22 ~]# cd /opt/kubernetes/server/bin/conf/ [root@hdss7-22 conf]# scp hdss7-21:/opt/kubernetes/server/bin/conf/kube-proxy.kubeconfig .
-
加载ipvs模块
[root@hdss7-21 conf]# cd [root@hdss7-21 ~]# lsmod | grep ip_vs [root@hdss7-21 ~]# vi ipvs.sh
#!/bin/bash ipvs_mods_dir="/usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs" for i in $(ls $ipvs_mods_dir|grep -o "^[^.]*") do /sbin/modinfo -F filename $i &>/dev/null if [ $? -eq 0 ];then /sbin/modprobe $i fi done
[root@hdss7-21 ~]# chmod +x ipvs.sh [root@hdss7-21 ~]# ./ipvs.sh [root@hdss7-21 ~]# lsmod | grep ip_vs ip_vs_wrr 12697 0 ip_vs_wlc 12519 0 ip_vs_sh 12688 0 ip_vs_sed 12519 0 ip_vs_rr 12600 0 ip_vs_pe_sip 12740 0 nf_conntrack_sip 33860 1 ip_vs_pe_sip ip_vs_nq 12516 0 ip_vs_lc 12516 0 ip_vs_lblcr 12922 0 ip_vs_lblc 12819 0 ip_vs_ftp 13079 0 ip_vs_dh 12688 0 ip_vs 145497 24 ip_vs_dh,ip_vs_lc,ip_vs_nq,ip_vs_rr,ip_vs_sh,ip_vs_ftp,ip_vs_sed,ip_vs_wlc,ip_vs_wrr,ip_vs_pe_sip,ip_vs_lblcr,ip_vs_lblc nf_nat 26787 3 ip_vs_ftp,nf_nat_ipv4,nf_nat_masquerade_ipv4 nf_conntrack 133095 8 ip_vs,nf_nat,nf_nat_ipv4,xt_conntrack,nf_nat_masquerade_ipv4,nf_conntrack_netlink,nf_conntrack_sip,nf_conntrack_ipv4 libcrc32c 12644 4 xfs,ip_vs,nf_nat,nf_conntrack
-
创建kube-proxy启动脚本
[root@hdss7-21 ~]# vi /opt/kubernetes/server/bin/kube-proxy.sh
#!/bin/sh ./kube-proxy \ --cluster-cidr 172.7.0.0/16 \ --hostname-override hdss7-21.host.com \ --proxy-mode=ipvs \ --ipvs-scheduler=nq \ --kubeconfig ./conf/kube-proxy.kubeconfig
[root@hdss7-21 ~]# chmod +x /opt/kubernetes/server/bin/kube-proxy.sh [root@hdss7-21 ~]# mkdir /data/logs/kubernetes/kube-proxy [root@hdss7-21 ~]# vi /etc/supervisord.d/kube-proxy.ini
[program:kube-proxy-7-21] command=/opt/kubernetes/server/bin/kube-proxy.sh ; the program (relative uses PATH, can take args) numprocs=1 ; number of processes copies to start (def 1) directory=/opt/kubernetes/server/bin ; directory to cwd to before exec (def no cwd) autostart=true ; start at supervisord start (default: true) autorestart=true ; retstart at unexpected quit (default: true) startsecs=30 ; number of secs prog must stay running (def. 1) startretries=3 ; max # of serial start failures (default 3) exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) stopsignal=QUIT ; signal used to kill process (default TERM) stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) user=root ; setuid to this UNIX account to run the program redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile=/data/logs/kubernetes/kube-proxy/proxy.stdout.log ; stderr log path, NONE for none; default AUTO stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB) stdout_logfile_backups=4 ; # of stdout logfile backups (default 10) stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) stdout_events_enabled=false ; emit events on stdout writes (default false) killallgroup=true stopgroup=true
[root@hdss7-21 ~]# supervisorctl update kube-proxy-7-21: added process group [root@hdss7-21 ~]# supervisorctl status kube-proxy-7-21 RUNNING pid 110734, uptime 0:01:33
- 查看ipvs是否生效
[root@hdss7-21 ~]# yum install ipvsadm -y
[root@hdss7-21 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.254.0.1:443 nq
-> 10.0.0.21:6443 Masq 1 0 0
-> 10.0.0.22:6443 Masq 1 0 0
[root@hdss7-21 ~]# cat /data/logs/kubernetes/kube-proxy/proxy.stdout.log | more
验证kubernetes集群
- 在任意一个运算节点,创建一个资源配置清单 这里我们选择HDSS7-21.host.com
[root@hdss7-21 ~]# vi nginx-ds.yaml
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: nginx-ds
spec:
template:
metadata:
labels:
app: nginx-ds
spec:
containers:
- name: my-nginx
image: harbor.od.com/public/nginx:v1.15
ports:
- containerPort: 80
[root@hdss7-22 ~]# kubectl apply -f nginx-ds.yaml
daemonset.extensions/nginx-ds created
[root@hdss7-21 ~]# kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-1 Healthy {"health": "true"}
etcd-0 Healthy {"health": "true"}
etcd-2 Healthy {"health": "true"}
[root@hdss7-21 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
hdss7-21.host.com Ready master,node 17h v1.15.2
hdss7-22.host.com Ready master,node 16h v1.15.2
[root@hdss7-21 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-ds-jw5c5 1/1 Running 0 116s
nginx-ds-nsfrf 1/1 Running 0 116s
- 测试完删除
[root@hdss7-22 ~]# kubectl apply -f nginx-ds.yaml daemonset.extensions/nginx-ds created
kubectl 命令补全
[root@hdss7-21 src]# source <(kubectl completion bash)
[root@hdss7-21 src]# echo "source <(kubectl completion bash)" >> ~/.bashrc
实现一整套的K8S生态的搭建,并实战交付一套dubbo(java)微服务,我们要一步步实现以下工呢
- 持续集成
- 配置中心
- 监控意向图
- 日志收集分析系统
- 自动化运维平台(最终实现基于K8S的开源Paas平台)
资源要求
2c/2g/50g x 3 + 4c/4g/50g x 2