集群组件
角色 | 组成 |
---|---|
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.设置主机名
hostnamectl set-hostname k8s-xx.bolou.com
主机名=短域名+后缀
主机名后缀跟等会配置的DNS主域名一样,这样后面可以使用短域名
出现问题:
我原先主机名是:k8s-xx.locathost
每次重启服务器,/etc/resolv.conf文件的search都会自动配置主机名,导致DNS设置不生效,使用不了短域名访问别的服务器
search配置的主机名要跟DNS中一样
所以要主机名后缀设置成DNS域名
2.1.2.关闭防火墙和selinux**
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
2.1.3.设置网卡
cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.10.xx
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
DNS1=114.114.114.114
2.1.4.设置yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
2.1.5.安装常用工具
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
yum install bind -y
2.2.2.配置bind 9
vi /etc/named.conf
listen-on port 53 { 192.168.10.20; };
allow-query { any; };
forwarders { 192.168.10.254; };
recursion yes;
dnssec-enable no;
dnssec-validation no;
##########
named-checkconf
vi /etc/named.rfc1912.zones
zone "bolou.com" IN {
type master;
file "bolou.com.zone";
allow-update { 192.168.10.20; };
};
zone "daa.com" IN {
type master;
file "daa.com.zone";
allow-update { 192.168.10.20; };
};
##########
vi /var/named/bolou.com.zone
$ORIGIN bolou.com.
$TTL 600 ; 10 minutes
@ IN SOA dns.bolou.com. dnsadmin.bolou.com. (
2020032001 ; serial
10800 ; refresh (3 hours)
900 ; retry (15 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
NS dns.bolou.com.
$TTL 60 ; 1 minute
dns A 192.168.10.20
k8s-20 A 192.168.10.20
k8s-30 A 192.168.10.30
k8s-40 A 192.168.10.40
k8s-50 A 192.168.10.50
k8s-60 A 192.168.10.60
##########
vi /var/named/daa.com.zone
$ORIGIN daa.com.
$TTL 600 ; 10 minutes
@ IN SOA dns.daa.com. dnsadmin.daa.com. (
2020032001 ; serial
10800 ; refresh (3 hours)
900 ; retry (15 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
NS dns.daa.com.
$TTL 60 ; 1 minute
dns A 192.168.10.20
2.2.3.检查配置并启动bind 9
named-checkconf
systemctl start named
systemctl enable named
netstat -lntup|grep 53
2.2.4.检查
安装命令:dig、nslookup
[root@k8s-20 ~]# yum -y install bind-utils
[root@k8s-20 ~]# dig -t A k8s-20.bolou.com @192.168.10.20 +short
192.168.10.20
[root@k8s-20 ~]# dig -t A k8s-30.bolou.com @192.168.10.20 +short
192.168.10.30
[root@k8s-20 ~]# dig -t A k8s-40.bolou.com @192.168.10.20 +short
192.168.10.40
[root@k8s-20 ~]# dig -t A k8s-50.bolou.com @192.168.10.20 +short
192.168.10.50
[root@k8s-20 ~]# dig -t A k8s-60.bolou.com @192.168.10.20 +short
192.168.10.60
2.2.5.配置DNS客户端
Linux所有主机
vi /etc/sysconfig/network-scripts/ifcfg-ens33
GATEWAY=192.168.10.1
DNS1=192.168.10.20
##########
vi /etc/resolv.conf
search host.com
nameserver 192.168.10.20
##########
systemctl restart network
Windows主机
wmnet2网卡更改DNS:192.168.10.20
2.2.6.检查
Linux
ping www.baidu.com
ping k8s-100
主机域和业务域要分开用
比如:www.baidu.com 是百度的业务,用的业务域
主机域是内部使用,假的域,随便定义
比如,目前使用的主机域,host,也是主机的意思,好记
Windows
ping k8s-100.bolou.com
2.3.准备签发证书环境
k8s-60.bolou.com 上
2.3.1.安装cfssl
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -O /usr/bin/cfssl
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -O /usr/bin/cfssl-json
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -O /usr/bin/cfssl-certinfo
chmod +x /usr/bin/cfssl*
wget https://github.com/cloudflare/cfssl/releases/download/v1.6.0/cfssljson_1.6.0_linux_amd64 -O /usr/bin/cfssl-json
wget https://github.com/cloudflare/cfssl/releases/download/v1.6.0/cfssl_1.6.0_linux_amd64 -O /usr/bin/cfssl
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配置文件
mkdir /opt/certs
vi /opt/certs/ca-csr.json
{
"CN": "Xingqiwu",
"hosts": [
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "beijing",
"L": "beijing",
"O": "daa",
"OU": "ops"
}
],
"ca": {
"expiry": "175200h" # 证书到期时间
}
}
2.3.3.生成ca证书文件
cd /opt/certs
cfssl gencert -initca ca-csr.json | cfssl-json -bare ca
ll
ca.csr
ca-csr.json
ca-key.pem
ca.pem
2.4.部署docker
k8s-40.bolou.com,k8s-50.bolou.com,k8s-100.bolou.com上
2.4.1.安装
[root@k8s-40 ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
2.4.2.配置
mkdir /etc/docker
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://9h2e4dkd.mirror.aliyuncs.com"], # 换自己阿里云镜像加速
"bip": "172.7.40.1/24",
"exec-opts": ["native.cgroupdriver=systemd"],
"live-restore": true
}
##########
bip要根据宿主机ip变化
注意:k8s-40.bolou.com bip 172.7.40.1/24
k8s-50.bolou.com bip 172.7.50.1/24
k8s-100.bolou.com bip 172.7.100.1/24
2.4.3.启动
mkdir -p /data/docker
systemctl start docker
systemctl enable docker
docker --version
2.5.部署docker镜像私有仓库harbor
k8s-100.bolou.com 上
2.5.1.下载软件并解压
harbor官网github地址
https://github.com/goharbor/harbor
用1.7.5以上的版本,1.7.5以下有漏洞
wget https://github.com/goharbor/harbor/releases/download/v2.0.0/harbor-offline-installer-v2.0.0.tgz
[root@k8s-100 src]# tar -xvf harbor-offline-installer-v2.0.0.tgz -C /opt/
[root@k8s-100 src]# cd /opt/
[root@k8s-100 opt]# mv harbor harbor-v2.0.0
[root@k8s-100 opt]# ln -s harbor-v2.0.0 harbor
2.5.2.配置
[root@k8s-100 opt]# cd harbor
[root@k8s-100 harbor]# cp harbor.yml.tmpl harbor.yml
[root@k8s-100 opt]# vi /opt/harbor/harbor.yml
hostname: harbor.daa.com
http:
port: 180
# 没用https,注销
# https:
# https port for harbor, default is 443
# port: 443
# The path of cert and key files for nginx
# certificate: /your/certificate/path
# private_key: /your/private/key/path
harbor_admin_password:12345
data_volume: /data/harbor
log:
level: info
rotate_count: 50
rotate_size:200M
location: /data/harbor/logs
[root@k8s-100 opt]# mkdir -p /data/harbor/logs
2.5.3.安装docker-compose
[root@k8s-100 opt]# yum install docker-compose -y
2.5.4.安装harbor
[root@k8s-100 harbor]# ./install.sh
2.5.5.检查harbor启动情况
[root@k8s-100 harbor]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
harbor-core /harbor/entrypoint.sh Up (healthy)
harbor-db /docker-entrypoint.sh Up (healthy) 5432/tcp
harbor-jobservice /harbor/entrypoint.sh Up (healthy)
harbor-log /bin/sh -c /usr/local/bin/ ... Up (healthy) 127.0.0.1:1514->10514/tcp
harbor-portal nginx -g daemon off; Up (healthy) 8080/tcp
nginx nginx -g daemon off; Up (healthy) 0.0.0.0:180->8080/tcp
redis redis-server /etc/redis.conf Up (healthy) 6379/tcp
registry /home/harbor/entrypoint.sh Up (healthy) 5000/tcp
registryctl /home/harbor/start.sh Up (healthy)
2.5.6.配置harbor的dns内网解析
[root@k8s-20 ~]# vi /var/named/daa.com.zone
2020032002 ; serial
harbor A 192.168.10.100
[root@k8s-20 ~]# systemctl restart named
[root@k8s-20 ~]# dig -t A harbor.daa.com +short
192.168.10.100
dig -t A harbor.daa.com @192.168.10.10 +short
2.5.7.安装NGINX并配置
[root@k8s-100 harbor]# yum install nginx -y
[root@k8s-100 harbor]# vi /etc/nginx/conf.d/harbor.daa.com.conf
server {
listen 80;
server_name harbor.daa.com;
client_max_body_size 1000m;
location / {
proxy_pass http://127.0.0.1:180;
}
}
[root@k8s-100 harbor]# nginx -t
[root@k8s-100 harbor]# systemctl start nginx
[root@k8s-100 harbor]# systemctl enable nginx
2.5.8.浏览器打开harbor.daa.com并测试
[root@k8s-20 ~]# curl harbor.daa.com
1、浏览器输入:harbor.daa.com 用户名:admin 密码:12345
2、新建项目:public 访问级别:公开
3、下载镜像并给镜像打tag
[root@k8s-100 harbor]# docker pull nginx:1.7.9
[root@k8s-100 harbor]# docker images |grep 1.7.9
[root@k8s-100 harbor]# docker tag 84581e99d807 harbor.daa.com/public/nginx:v1.7.9
4、登录harbor并上传到仓库
[root@k8s-100 harbor]# docker login harbor.daa.com
[root@k8s-100 harbor]# docker push harbor.daa.com/public/nginx:v1.7.9
## 登录harbor报错
Error response from daemon: Get https://harbor.daa.com/v2/: dial tcp 192.168.10.100:443: connect: connection refused
解决方法:
/etc/docker/daemon.json中添加
"insecure-registries": ["registry.access.redhat.com","quay.io","harbor.daa.com"],
报错:
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
原因:这是180端口不通
在harbor本机 curl ip:180 或telnet ip 180 是通的,到别的服务器就不通了
解决方法:把原先harbor的所有容器全删除,再重新./install.sh 起服务
2.5.9.检查
可以看到NGINX镜像已经上传到public下
设置harbor开机自启
[root@k8s-100 ~]# vim /etc/rc.d/rc.local
cd /opt/harbor
/usr/bin/docker-compose stop
/usr/bin/docker-compose start
[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上
[root@k8s-100 ~]# vi /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"
]
}
}
}
}
3.1.3.创建生成自签发证书的csr的json配置文件
[root@k8s-100 ~]# vi /opt/certs/etcd-peer-csr.json
{
"CN": "k8s-etcd",
"hosts": [
"192.168.10.20",
"192.168.10.30",
"192.168.10.40",
"192.168.10.50"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "beijing",
"L": "beijing",
"O": "daa",
"OU": "ops"
}
]
}
3.1.4.生成etcd证书文件
[root@k8s-100 ~]# cd /opt/certs/
[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.检查生成的证书文件
[root@k8s-100 certs]# ll etcd-peer*
etcd-peer.csr
etcd-peer-csr.json
etcd-peer-key.pem
etcd-peer.pem
3.1.6.创建etcd用户
k8s-30、k8s-40、k8s-50上
[root@k8s-30 opt]# useradd -s /sbin/nologin -M etcd
3.1.7.下载软件,解压,做软连接
https://github.com/etcd-io/etcd/tags
wget https://github.com/etcd-io/etcd/releases/download/v3.3.20/etcd-v3.3.20-linux-amd64.tar.gz
[root@k8s-30 src]# tar -xvf etcd-v3.4.7-linux-amd64.tar.gz -C /opt/
[root@k8s-30 src]# cd /opt/
[root@k8s-30 opt]# ln -s etcd-v3.4.7-linux-amd64/ etcd
3.1.8.创建目录,拷贝证书文件
创建证书目录、数据目录、日志目录
[root@k8s-30 opt]# mkdir -p /opt/etcd/certs /data/etcd /data/etcd/etcd-server /data/logs/etcd-server
拷贝生成的证书文件
免密:
ssh-keygen
ssh-copy-id 目的主机ip
到运维主机上去拷贝
[root@k8s-60 certs]# scp ca.pem etcd-peer.pem etcd-peer-key.pem k8s-30:/opt/etcd/certs
注意私钥文件权限600
[root@k8s-30 certs]# chmod 600 etcd-peer-key.pem
3.1.9.创建etcd服务启动脚本
[root@k8s-30 ~]# vi /opt/etcd/etcd-server-startup.sh
#!/bin/sh
./etcd --name etcd-server-10-30 \
--data-dir /data/etcd/etcd-server \
--listen-peer-urls https://192.168.10.30:2380 \
--listen-client-urls https://192.168.10.30:2379,http://127.0.0.1:2379 \
--quota-backend-bytes 8000000000 \
--initial-advertise-peer-urls https://192.168.10.30:2380 \
--advertise-client-urls https://192.168.10.30:2379,http://127.0.0.1:2379 \
--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 \
--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@k8s-30 ~]# chmod +x /opt/etcd/etcd-server-startup.sh
高版本的etcd已经没有--ca-file、--peer-ca-file,需要删掉这两个配置
3.1.10.授权目录权限
[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软件
[root@k8s-30 ~]# yum install supervisor -y
[root@k8s-30 ~]# systemctl start supervisord
[root@k8s-30 ~]# systemctl enable supervisord
3.1.12.创建supervisor配置
[root@k8s-30 ~]# vi /etc/supervisord.d/etcd-server.ini
[program:etcd-server-10-30]
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)
3.1.13.启动etcd服务并检查
[root@k8s-30 ~]# supervisorctl update
[root@k8s-30 ~]# supervisorctl status
[root@k8s-30 ~]# netstat -lntup|grep etcd
排错:
# 查看某进程错误日志
supervisorctl tail program_name
3.1.14.部署启动所有集群
不同的地方
/opt/etcd/etcd-server-startup.sh
--name
--listen-peer-urls
--listen-client-urls
--initial-advertise-peer-urls
--advertise-client-urls
##########
/etc/supervisord.d/etcd-server.ini
[program:etcd-server-10-30]
3.1.15.检查集群状态
[root@k8s-30 etcd]# vim ~/.bashrc # 加入下面一行内容,另外两台etcd节点服务也加
export PATH=$PATH:/opt/etcd
[root@k8s-30 etcd]# source ~/.bashrc # 加载生效
[root@k8s-30 ~]# etcdctl cluster-health
[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
https://github.com/kubernetes/kubernetes/releases/tag/v1.15.10
CHANGELOG-1.15.md--→server binaries--→kubernetes-server-linux-amd64.tar.gz
[root@k8s-40 src]# tar -xf kubernetes-server-linux-amd64-1.15.10.tar.gz -C /opt/
[root@k8s-40 opt]# mv kubernetes/ kubernetes-v1.15.10
[root@k8s-40 opt]# ln -s /opt/kubernetes-v1.15.10/ /opt/kubernetes
[root@k8s-40 opt]# cd kubernetes
[root@k8s-40 kubernetes]# rm -rf kubernetes-src.tar.gz # go语言写的kubernetes源码
[root@k8s-40 kubernetes]# cd server/bin
[root@k8s-40 bin]# rm -f *.tar # kubernetes镜像包
[root@k8s-40 bin]# rm -f *_tag
3.2.3.签发client证书
k8s-100.bolou.com上
1、创建生成证书csr的json配置文件
[root@k8s-100 certs]# vi /opt/certs/client-csr.json
{
"CN": "k8s-node",
"hosts": [
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "beijing",
"L": "beijing",
"O": "daa",
"OU": "ops"
}
]
}
2、生成client证书文件
[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、检查生成的证书文件
[root@k8s-100 certs]# ll
client.csr
client-csr.json
client-key.pem
client.pem
3.2.4.签发kube-apiserver证书
k8s-100.bolou.com上
1、创建生成证书csr的json配置文件
[root@k8s-100 certs]# vi /opt/certs/apiserver-csr.json
{
"CN": "k8s-apiserver",
"hosts": [
"127.0.0.1",
"10.96.0.1", # Service ClusterIP
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster",
"kubernetes.default.svc.cluster.local",
"192.168.10.10",
"192.168.10.40",
"192.168.10.50",
"192.168.10.51"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "beijing",
"L": "beijing",
"O": "daa",
"OU": "ops"
}
]
}
2、生成kube-apiserver证书文件
[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、检查生成的证书文件
[root@k8s-100 certs]# ll
apiserver.csr
apiserver-csr.json
apiserver-key.pem
apiserver.pem
3.2.5.拷贝证书文件至各节点,并创建配置
1、拷贝证书文件到/opt/kubernetes/server/bin/cert目录下
两套master节点创建证书目录
[root@k8s-40 bin]# mkdir cert
[root@k8s-50 bin]# mkdir cert
[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、创建配置
[root@k8s-40 bin]# mkdir conf
[root@k8s-40 bin]# cd conf
[root@k8s-40 conf]# vi audit.yaml
apiVersion: audit.k8s.io/v1beta1 # This is required.
kind: Policy
omitStages:
- "RequestReceived"
rules:
- level: RequestResponse
resources:
- group: ""
resources: ["pods"]
- level: Metadata
resources:
- group: ""
resources: ["pods/log", "pods/status"]
- level: None
resources:
- group: ""
resources: ["configmaps"]
resourceNames: ["controller-leader"]
- level: None
users: ["system:kube-proxy"]
verbs: ["watch"]
resources:
- group: "" # core API group
resources: ["endpoints", "services"]
- level: None
userGroups: ["system:authenticated"]
nonResourceURLs:
- "/api*" # Wildcard matching.
- "/version"
- level: Request
resources:
- group: "" # core API group
resources: ["configmaps"]
namespaces: ["kube-system"]
- level: Metadata
resources:
- group: "" # core API group
resources: ["secrets", "configmaps"]
- level: Request
resources:
- group: "" # core API group
- group: "extensions" # Version of group should NOT be included.
- level: Metadata
mitStages:
- "RequestReceived"
3.2.6.创建apiserver启动脚本
[root@k8s-40 bin]# 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 ./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://192.168.10.30:2379,https://192.168.10.40:2379,https://192.168.10.50:2379 \
--service-account-key-file ./cert/ca-key.pem \
--service-cluster-ip-range 10.96.0.0/22 \
--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 \
--service-account-signing-key-file ./cert/ca-key.pem \
--service-account-issuer https://kubernetes.default.svc.cluster.local \
--v 2
### 说明1.20版本之后,为了启用令牌请求投射,你必须为 kube-apiserver 设置以下命令行参数:
--service-account-issuer
--service-account-key-file
--service-account-signing-key-file
--api-audiences
官方文档: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.授权和创建目录
[root@k8s-40 bin]# cd /opt/kubernetes/server/bin
[root@k8s-40 bin]# chmod +x kube-apiserver.sh
[root@k8s-40 bin]# mkdir -p /data/logs/kubernetes/kube-apiserver
3.2.8.创建supervisor配置
[root@k8s-40 bin]# vi /etc/supervisord.d/kube-apiserver.ini
[program:kube-apiserver-10-40]
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)
3.2.9.启动服务并检查
[root@k8s-40 bin]# supervisorctl update
[root@k8s-40 bin]# supervisorctl status
[root@k8s-40 bin]# netstat -nltup|grep kube-api
3.2.10.部署启动所有集群
不同的地方
/etc/supervisord.d/kube-apiserver.ini
[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
[root@k8s-20 etcd]# yum install nginx keepalived -y
2、k8s-20.host.com和k8s-30.host.com配置NGINX
[root@k8s-20 conf.d]# vi /etc/nginx/nginx.conf #以下内容添加到http之外
stream {
upstream kube-apiserver {
server 192.168.10.40:6443 max_fails=3 fail_timeout=30s;
server 192.168.10.50:6443 max_fails=3 fail_timeout=30s;
}
server {
listen 7443;
proxy_connect_timeout 2s;
proxy_timeout 900s;
proxy_pass kube-apiserver;
}
}
[root@k8s-20 etcd]# nginx -t
3、k8s-20.host.com和k8s-30.host.com配置keepalived
检查脚本
[root@k8s-20 ~]# 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 主:
[root@k8s-20 conf.d]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id 192.168.10.20
}
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 192.168.10.20
nopreempt
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.10.10
}
}
keepalived 从:
[root@k8s-30 conf.d]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id 192.168.10.30
}
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 192.168.10.30
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.10.10
}
}
####################
nopreempt:非抢占式
3.3.3.启动代理并检查
systemctl start nginx keepalived
systemctl enable nginx keepalived
netstat -lntup|grep nginx
ip addr
模拟测试
如果搭建成功,此时VIP地址10.4.7.10/32是在K8s-11服务器上,
先把k8s-11的keepalive先停了,看VIP地址是否会漂移到k8s-12上,
VIP地址漂移到k8s-12后,再重启k8s-11的keepalive,看VIP地址是否会漂回来
若是,则配置成功。
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上
[root@k8s-40 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.96.0.0/22 \
--root-ca-file ./cert/ca.pem \
--v 2
3.4.3.授权文件权限,创建目录
[root@k8s-40 bin]# chmod +x /opt/kubernetes/server/bin/kube-controller-manager.sh
[root@k8s-40 bin]# mkdir -p /data/logs/kubernetes/kube-controller-manager
3.4.4.创建supervisor配置
[root@k8s-40 bin]# vi /etc/supervisord.d/kube-conntroller-manager.ini
[program:kube-controller-manager-10-40]
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)
3.4.5.启动服务并检查
[root@k8s-40 bin]# supervisorctl update
[root@k8s-40 bin]# supervisorctl status
3.4.6.部署启动所有集群
不同的地方
/etc/supervisord.d/kube-conntroller-manager.ini
[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上
[root@k8s-40 bin]# vi /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
3.5.3.授权文件权限,创建目录
[root@k8s-40 bin]# chmod +x /opt/kubernetes/server/bin/kube-scheduler.sh
[root@k8s-40 bin]# mkdir -p /data/logs/kubernetes/kube-scheduler
3.5.4.创建supervisor配置
[root@k8s-40 bin]# vi /etc/supervisord.d/kube-scheduler.ini
[program:kube-scheduler-10-40]
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)
3.5.5.启动服务并检查
[root@k8s-40 bin]# supervisorctl update
[root@k8s-40 bin]# supervisorctl status
3.5.6.部署启动所有集群
不同的地方
/etc/supervisord.d/kube-scheduler.ini
[program:kube-scheduler-10-40]
3.6.检查master节点
3.6.1.建立kubectl软链接
[root@k8s-40 bin]# ln -s /opt/kubernetes/server/bin/kubectl /usr/bin/kubectl
3.6.2.检查master节点
[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配置文件
[root@k8s-60 certs]# vi kubelet-csr.json
{
"CN": "k8s-kubelet",
"hosts": [
"127.0.0.1",
"192.168.10.10",
"192.168.10.40",
"192.168.10.50",
"192.168.10.51",
"192.168.10.52",
"192.168.10.53",
"192.168.10.54",
"192.168.10.55",
"192.168.10.56"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "beijing",
"L": "beijing",
"O": "od",
"OU": "ops"
}
]
}
2、生成kubelet证书文件
[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、检查生成的证书文件
[root@k8s-60 certs]# ll
kubelet.csr
kubelet-csr.json
kubelet-key.pem
kubelet.pem
4.1.3.拷贝证书文件至各节点,并创建配置
k8s-40.host.com上
1、拷贝证书文件
[root@k8s-40 cert]# scp k8s-60:/opt/certs/kubelet.pem .
[root@k8s-40 cert]# scp k8s-60:/opt/certs/kubelet-key.pem .
2、创建配置
(1)、set-cluster
[root@k8s-40 ~]# cd /opt/kubernetes/server/bin/conf
[root@k8s-40 conf]# kubectl config set-cluster myk8s \
--certificate-authority=/opt/kubernetes/server/bin/cert/ca.pem \
--embed-certs=true \
--server=https://192.168.10.10:7443 \
--kubeconfig=kubelet.kubeconfig
(2)、set-credentials
[root@k8s-40 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
(3)、set-context
[root@k8s-40 conf]# kubectl config set-context myk8s-context \
--cluster=myk8s \
--user=k8s-node \
--kubeconfig=kubelet.kubeconfig
(4)、use-context
[root@k8s-40 conf]# kubectl config use-context myk8s-context --kubeconfig=kubelet.kubeconfig
(5)、查看生成的kubelet.kubeconfig
[root@k8s-40 conf]# ll
kubelet.kubeconfig
(6)、k8s-node.yaml
(1)创建配置文件
[root@k8s-40 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
(2)应用资源配置
[root@k8s-40 conf]# kubectl create -f k8s-node.yaml
(3)查看集群角色和角色属性
[root@k8s-40 conf]# kubectl get clusterrolebinding k8s-node
NAME AGE
k8s-node 2m
[root@k8s-40 conf]# kubectl get clusterrolebinding k8s-node -o yaml
出现如下:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
creationTimestamp: "2020-06-18T06:17:28Z"
name: k8s-node
resourceVersion: "5673"
selfLink: /apis/rbac.authorization.k8s.io/v1/clusterrolebindings/k8s-node
uid: a2d8f40b-1f1d-457b-b72e-b5a69ebcaaa0
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:node
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: k8s-node
(4)拷贝kubelet.kubeconfig 到k8s-50.host.com上
[root@k8s-50 ~]# cd /opt/kubernetes/server/bin/conf
[root@k8s-50 conf]# scp k8s-40:/opt/kubernetes/server/bin/conf/kubelet.kubeconfig .
4.1.4.准备pause基础镜像
k8s-60.host.com上
1、下载pause镜像
[root@k8s-60 ~]# docker pull kubernetes/pause
2、上传到docker私有仓库harbor中
(1)、给镜像打tag
[root@k8s-60 ~]# docker images | grep kubernetes
[root@k8s-60 ~]# docker tag f9d5de079539 harbor.od.com/public/pause:latest
[root@k8s-60 ~]# docker images | grep pause
(2)、上传到harbor上
[root@k8s-60 ~]# docker push harbor.od.com/public/pause:latest
4.1.5.创建kubelet启动脚本
k8s-40.host.com上
[root@k8s-40 conf]# vi /opt/kubernetes/server/bin/kubelet.sh
#!/bin/sh
./kubelet \
--anonymous-auth=false \
--cgroup-driver systemd \
--cluster-dns 10.96.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 k8s-40.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
4.1.6.授权,创建目录
k8s-40.host.com上
[root@k8s-40 conf]# chmod +x /opt/kubernetes/server/bin/kubelet.sh
[root@k8s-40 conf]# mkdir -p /data/logs/kubernetes/kube-kubelet /data/kubelet
4.1.7.创建supervisor配置
[root@k8s-40 conf]# vi /etc/supervisord.d/kube-kubelet.ini
[program:kube-kubelet-10-40]
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)
4.1.8.启动服务并检查
[root@k8s-40 conf]# supervisorctl update
[root@k8s-40 conf]# supervisorctl status
4.1.9.部署所有节点
不同的地方
/opt/kubernetes/server/bin/kubelet.sh
--hostname-override
##########
/etc/supervisord.d/kube-kubelet.ini
[program:kube-kubelet-10-40]
4.1.10.检查所有节点并给节点打上标签
[root@k8s-40 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-40.host.com Ready <none> 2m9s v1.15.10
k8s-50.host.com Ready <none> 32s v1.15.10
[root@k8s-40 bin]# kubectl label node k8s-40.host.com node-role.kubernetes.io/master=
[root@k8s-40 bin]# kubectl label node k8s-40.host.com node-role.kubernetes.io/node=
##k8s-50上操作注意修改主机名,再查看
[root@k8s-50 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-40.host.com Ready master,node 4m6s v1.15.10
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配置文件
[root@k8s-60 certs]# vi kube-proxy-csr.json
{
"CN": "system:kube-proxy",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "beijing",
"L": "beijing",
"O": "od",
"OU": "ops"
}
]
}
2、生成kube-proxy证书文件
[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、检查生成的证书文件
[root@k8s-60 certs]# ll
kube-proxy-client.csr
kube-proxy-client-key.pem
kube-proxy-client.pem
kube-proxy-csr.json
4.2.3.拷贝证书文件至各节点,并创建配置
k8s-40.host.com上
1、拷贝证书文件
[root@k8s-40 cert]# scp k8s-200:/opt/certs/kube-proxy-client.pem .
[root@k8s-40 cert]# scp k8s-200:/opt/certs/kube-proxy-client-key.pem .
[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
[root@k8s-40 ~]# cd /opt/kubernetes/server/bin/conf
[root@k8s-40 conf]# kubectl config set-cluster myk8s \
--certificate-authority=/opt/kubernetes/server/bin/cert/ca.pem \
--embed-certs=true \
--server=https://192.168.10.10:7443 \
--kubeconfig=kube-proxy.kubeconfig
(2)、set-credentials
[root@k8s-40 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
(3)、set-context
[root@k8s-40 conf]# kubectl config set-context myk8s-context \
--cluster=myk8s \
--user=kube-proxy \
--kubeconfig=kube-proxy.kubeconfig
(4)、use-context
[root@k8s-40 conf]# kubectl config use-context myk8s-context --kubeconfig=kube-proxy.kubeconfig
(5)、拷贝kube-proxy.kubeconfig 到 k8s-22.host.com的conf目录下
[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模块
[root@k8s-40 bin]# lsmod |grep ip_vs
[root@k8s-40 bin]# vi /root/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@k8s-40 bin]# chmod +x /root/ipvs.sh
[root@k8s-40 bin]# sh /root/ipvs.sh
[root@k8s-40 bin]# lsmod |grep ip_vs
2、创建启动脚本
[root@k8s-40 bin]# vi /opt/kubernetes/server/bin/kube-proxy.sh
#!/bin/sh
./kube-proxy \
--cluster-cidr 172.7.0.0/16 \
--hostname-override k8s-40.host.com \
--proxy-mode=ipvs \
--ipvs-scheduler=nq \
--kubeconfig ./conf/kube-proxy.kubeconfig
4.2.5.授权,创建目录
[root@k8s-50 bin]# ls -l /opt/kubernetes/server/bin/conf/|grep kube-proxy
[root@k8s-50 bin]# chmod +x /opt/kubernetes/server/bin/kube-proxy.sh
[root@k8s-50 bin]# mkdir -p /data/logs/kubernetes/kube-proxy
4.2.6.创建supervisor配置
[root@k8s-40 bin]# vi /etc/supervisord.d/kube-proxy.ini
[program:kube-proxy-10-40]
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)
4.2.7.启动服务并检查
[root@k8s-40 bin]# supervisorctl update
[root@k8s-40 bin]# supervisorctl status
[root@k8s-40 bin]# yum install ipvsadm -y
[root@k8s-40 conf]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.96.0.1:443 nq
-> 192.168.10.40:6443 Masq 1 0 0
-> 192.168.10.50:6443 Masq 1 0 0
[root@k8s-40 conf]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 56m
4.2.8.部署所有节点
不同的地方
/opt/kubernetes/server/bin/kube-proxy.sh
--hostname-override
##########
/etc/supervisord.d/kube-proxy.ini
[program:kube-proxy-10-40]
5.验证kubernetes集群
5.1.在任意一个节点上创建一个资源配置清单
k8s-40.host.com上
[root@k8s-40 ~]# vi /root/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.7.9
ports:
- containerPort: 80
5.2.应用资源配置,并检查
5.2.1.k8s-40.host.com上
要拉取harbor仓库的镜像,得先登录harbor仓库
docker login harbor.od.com
# 如果报错登录不上,查看/etc/docker/daemon.json,加上参数
"insecure-registries": ["harbor.od.com"],
还不行到/etc/hosts 加上 10.4.7.60 harbor.od.com
[root@k8s-40 ~]# kubectl create -f /root/nginx-ds.yaml
daemonset.extensions/nginx-ds created
[root@k8s-40 conf]# kubectl get pods # 还在创建中
NAME READY STATUS RESTARTS AGE
nginx-ds-blb2x 0/1 ContainerCreating 0 16s
nginx-ds-kngn7 0/1 ContainerCreating 0 16s
[root@k8s-40 conf]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-ds-blb2x 1/1 Running 0 64s
nginx-ds-kngn7 1/1 Running 0 64s
[root@k8s-40 conf]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ds-blb2x 1/1 Running 0 69s 172.7.40.2 k8s-40.host.com <none> <none>
nginx-ds-kngn7 1/1 Running 0 69s 172.7.50.2 k8s-50.host.com <none> <none>
[root@k8s-40 conf]# curl 172.7.40.2 #能连通
[root@k8s-40 conf]# curl 172.7.50.2 #不通,是因为还不能跨主机通信
5.2.2.k8s-22.host.com上
[root@k8s-50 conf]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ds-blb2x 1/1 Running 0 3m50s 172.7.40.2 k8s-40.host.com <none> <none>
nginx-ds-kngn7 1/1 Running 0 3m50s 172.7.50.2 k8s-50.host.com <none> <none>
[root@k8s-50 conf]# curl 172.7.50.2 #能通
5.2.3.查看kubernetes是否搭建好
各组件安装
[root@k8s-30 ~]# supervisorctl status
etcd-server-10-30 RUNNING pid 9037, uptime 2:41:25
[root@k8s-40 ~]# supervisorctl status
etcd-server-10-40 RUNNING pid 9007, uptime 2:39:32
kube-apiserver-10-40 RUNNING pid 9153, uptime 1:03:26
kube-controller-manager-10-40 RUNNING pid 9187, uptime 0:38:37
kube-kubelet-10-40 RUNNING pid 9294, uptime 0:20:28
kube-proxy-10-40 RUNNING pid 11566, uptime 0:07:56
kube-scheduler-10-40 RUNNING pid 9205, uptime 0:33:34
[root@k8s-50 ~]# supervisorctl status
etcd-server-10-50 RUNNING pid 9034, uptime 2:38:36
kube-apiserver-10-50 RUNNING pid 9177, uptime 1:02:52
kube-controller-manager-10-50 RUNNING pid 9210, uptime 0:38:43
kube-kubelet-10-50 RUNNING pid 9292, uptime 0:18:04
kube-proxy-10-50 RUNNING pid 11149, uptime 0:07:57
kube-scheduler-10-50 RUNNING pid 9230, uptime 0:33:41
cs状态
[root@k8s-40 ~]# kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
etcd-0 Healthy {"health": "true"}
etcd-2 Healthy {"health": "true"}
etcd-1 Healthy {"health": "true"}
controller-manager Healthy ok
node状态
[root@k8s-40 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-40.host.com Ready master,node 21m v1.15.10
k8s-50.host.com Ready master,node 19m v1.15.10
pod状态
[root@k8s-40 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-ds-blb2x 1/1 Running 0 6m26s
nginx-ds-kngn7 1/1 Running 0 6m26s
查看日记
# kubectl describe pod nginx-ds-kngn7
# kubectl describe pod nginx-ds-dwhn4 -n 域名空间
node节点在创建yarm的时候没有提前登陆过镜像仓库,导致镜像没拉去下来
注意要先登陆镜像仓库
搭建完成,服务器做快照
出现错误
node状态为NotReady
查看日记kubelet.stdout.log
发现是vip_IP没有起来
重启主keepalived,问题解决