从宿主机上拉到/opt/k8s目录里
[root@localhost k8s]# unzip master.zip
[root@server3 opt]# mkdir /opt/kubernetes/{cfg,bin,ssl} -p
[root@localhost k8s]# mkdir k8s-cert
[root@localhost k8s]# cd k8s-cert/
[root@server3 k8s-cert]# ls
k8s-cert.sh
[root@server3 k8s-cert]# cat k8s-cert.sh
cat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat > ca-csr.json <<EOF
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
#-----------------------
cat > server-csr.json <<EOF
{
"CN": "kubernetes",
"hosts": [
"10.0.0.1",
"127.0.0.1",
"192.168.188.30",
"192.168.188.10",
"192.168.188.100",
"192.168.188.20",
"192.168.188.60",
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster",
"kubernetes.default.svc.cluster.local"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
#-----------------------
cat > admin-csr.json <<EOF
{
"CN": "admin",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "system:masters",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
#-----------------------
cat > kube-proxy-csr.json <<EOF
{
"CN": "system:kube-proxy",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
启动脚本
[root@server3 k8s-cert]# bash k8s-cert.sh
2021/03/24 10:15:26 [INFO] generating a new CA key and certificate from CSR
2021/03/24 10:15:26 [INFO] generate received request
2021/03/24 10:15:26 [INFO] received CSR
2021/03/24 10:15:26 [INFO] generating key: rsa-2048
2021/03/24 10:15:26 [INFO] encoded CSR
2021/03/24 10:15:26 [INFO] signed certificate with serial number 120439959829303657175495288603635910631168907118
2021/03/24 10:15:26 [INFO] generate received request
2021/03/24 10:15:26 [INFO] received CSR
2021/03/24 10:15:26 [INFO] generating key: rsa-2048
2021/03/24 10:15:26 [INFO] encoded CSR
2021/03/24 10:15:26 [INFO] signed certificate with serial number 293547028150330143961732968627265826658127891196
2021/03/24 10:15:26 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
2021/03/24 10:15:26 [INFO] generate received request
2021/03/24 10:15:26 [INFO] received CSR
2021/03/24 10:15:26 [INFO] generating key: rsa-2048
2021/03/24 10:15:26 [INFO] encoded CSR
2021/03/24 10:15:26 [INFO] signed certificate with serial number 254043705608525302132370100596214027100058018467
2021/03/24 10:15:26 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
2021/03/24 10:15:26 [INFO] generate received request
2021/03/24 10:15:26 [INFO] received CSR
2021/03/24 10:15:26 [INFO] generating key: rsa-2048
2021/03/24 10:15:27 [INFO] encoded CSR
2021/03/24 10:15:27 [INFO] signed certificate with serial number 365919138943413689883243320106102594011700971042
2021/03/24 10:15:27 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
[root@server3 k8s-cert]# ll *pem
-rw-------. 1 root root 1675 3月 24 10:15 admin-key.pem
-rw-r--r--. 1 root root 1399 3月 24 10:15 admin.pem
-rw-------. 1 root root 1675 3月 24 10:15 ca-key.pem
-rw-r--r--. 1 root root 1359 3月 24 10:15 ca.pem
-rw-------. 1 root root 1675 3月 24 10:15 kube-proxy-key.pem
-rw-r--r--. 1 root root 1403 3月 24 10:15 kube-proxy.pem
-rw-------. 1 root root 1679 3月 24 10:15 server-key.pem
-rw-r--r--. 1 root root 1643 3月 24 10:15 server.pem
[root@server3 k8s-cert]# cp ca*pem server*pem /opt/kubernetes/ssl/
[root@server3 k8s-cert]# cd /opt/kubernetes/ssl/
[root@server3 ssl]# ls
ca-key.pem ca.pem server-key.pem server.pem
把kubernetes-server-linux-amd64.tar.gz压缩包放进/root/k8s/目录里
[root@server3 k8s]# tar zxvf kubernetes-server-linux-amd64.tar.gz
[root@server3 k8s]# ls
apiserver.sh etcd-v3.3.10-linux-amd64 master.zip
controller-manager.sh etcd-v3.3.10-linux-amd64.tar.gz scheduler.sh
etcd-cert kubernetes
etcd.sh kubernetes-server-linux-amd64.tar.gz
[root@server3 k8s]# cd /root/k8s/kubernetes/server/bin
[root@server3 bin]# ls
apiextensions-apiserver kube-controller-manager.tar
cloud-controller-manager kubectl
cloud-controller-manager.docker_tag kubelet
cloud-controller-manager.tar kube-proxy
hyperkube kube-proxy.docker_tag
kubeadm kube-proxy.tar
kube-apiserver kube-scheduler
kube-apiserver.docker_tag kube-scheduler.docker_tag
kube-apiserver.tar kube-scheduler.tar
kube-controller-manager mounter
kube-controller-manager.docker_tag
[root@server3 bin]# cp kube-apiserver kubectl kube-controller-manager kube-scheduler /opt/kubernetes/bin/
[root@server3 bin]# cd /root/k8s/
[root@server3 k8s]# head -c 16 /dev/urandom | od -An -t x | tr -d ' '
9c8a6669d8f96432b6ecc3007e169d59
[root@server3 k8s]# vim /opt/kubernetes/cfg/token.csv
9c8a6669d8f96432b6ecc3007e169d59,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
二进制文件,token,证书都准备好,开启apiserver
[root@server3 ~]# cd k8s/
[root@server3 k8s]# ls
apiserver.sh etcd-v3.3.10-linux-amd64.tar.gz
controller-manager.sh kubernetes
etcd-cert kubernetes-server-linux-amd64.tar.gz
etcd.sh master.zip
etcd-v3.3.10-linux-amd64 scheduler.sh
[root@server3 k8s]# chmod +x controller-manager.sh
检查进程是否启动成功
[root@server3 k8s]# ps aux | grep kube
监听的https端口
[root@server3 k8s]# netstat -anpt | grep 6443
tcp 0 0 192.168.188.30:6443 0.0.0.0:* LISTEN 54427/kube-apiserve
tcp 0 0 192.168.188.30:6443 192.168.188.30:54638 ESTABLISHED 54427/kube-apiserve
tcp 0 0 192.168.188.30:54638 192.168.188.30:6443 ESTABLISHED 54427/kube-apiserve
查看http端口是否起来
[root@server3 k8s]# netstat -ntap | grep 8080
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 54427/kube-apiserve
[root@server3 k8s]# ls
apiserver.sh etcd-v3.3.10-linux-amd64.tar.gz
controller-manager.sh kubernetes
etcd-cert kubernetes-server-linux-amd64.tar.gz
etcd.sh master.zip
etcd-v3.3.10-linux-amd64 scheduler.sh
[root@server3 k8s]# bash scheduler.sh 127.0.0.1
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-scheduler.service to /usr/lib/systemd/system/kube-scheduler.service.
[root@server3 k8s]# systemctl status kube-scheduler.service
[root@server3 k8s]# chmod +x controller-manager.sh
[root@server3 k8s]# bash controller-manager.sh 127.0.0.1
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-controller-manager.service to /usr/lib/systemd/system/kube-controller-manager.service.
查看master 节点状态
[root@server3 k8s]# /opt/kubernetes/bin/kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-2 Healthy {"health":"true"}
etcd-1 Healthy {"health":"true"}
etcd-0 Healthy {"health":"true"}
master上操作
把 kubelet、kube-proxy拷贝到node节点上去
[root@server3 k8s]# cd kubernetes/
[root@server3 kubernetes]# ls
addons kubernetes-src.tar.gz LICENSES server
[root@server3 kubernetes]# cd server/
[root@server3 server]# ls
bin
[root@server3 server]# cd bin/
[root@server3 bin]# ls
apiextensions-apiserver kube-controller-manager.tar
cloud-controller-manager kubectl
cloud-controller-manager.docker_tag kubelet
cloud-controller-manager.tar kube-proxy
hyperkube kube-proxy.docker_tag
kubeadm kube-proxy.tar
kube-apiserver kube-scheduler
kube-apiserver.docker_tag kube-scheduler.docker_tag
kube-apiserver.tar kube-scheduler.tar
kube-controller-manager mounter
kube-controller-manager.docker_tag
[root@server3 bin]# scp kubelet kube-proxy root@192.168.188.40:/opt/kubernetes/bin/
root@192.168.188.40's password:
kubelet 100% 168MB 90.3MB/s 00:01
kube-proxy 100% 48MB 88.4MB/s 00:00
[root@server3 bin]# scp kubelet kube-proxy root@192.168.188.50:/opt/kubernetes/bin/
root@192.168.188.50's password:
kubelet 100% 168MB 141.9MB/s 00:01
kube-proxy 100% 48MB 144.9MB/s 00:00
nod01节点操作
复制node.zip到/root目录下再解压
[root@node01 ~]# ls
anaconda-ks.cfg node.zip 模板 下载
flannel.sh original-ks.cfg 视频 音乐
flannel-v0.10.0-linux-amd64.tar.gz README.md 图片 桌面
initial-setup-ks.cfg 公共 文档
[root@node01 ~]# unzip node.zip
Archive: node.zip
inflating: proxy.sh
inflating: kubelet.sh
在master上操作
[root@server3 bin]# cd /root/k8s/
[root@server3 k8s]# ls
apiserver.sh etcd-v3.3.10-linux-amd64.tar.gz
controller-manager.sh kubernetes
etcd-cert kubernetes-server-linux-amd64.tar.gz
etcd.sh master.zip
etcd-v3.3.10-linux-amd64 scheduler.sh
[root@server3 k8s]# mkdir kubeconfig
[root@server3 k8s]# cd kubeconfig/
[root@server3 kubeconfig]# ls
[root@server3 kubeconfig]# rz -E
rz waiting to receive.
[root@server3 kubeconfig]# ls
kubeconfig.sh
[root@server3 kubeconfig]# mv kubeconfig.sh kubeconfig
[root@server3 kubeconfig]# ls
kubeconfig
获取token信息
[root@server3 kubeconfig]# cat /opt/kubernetes/cfg/token.csv
9c8a6669d8f96432b6ecc3007e169d59,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
配置文件修改为tokenID
设置客户端认证参数
[root@server3 kubeconfig]# vim kubeconfig
修改
kubectl config set-credentials kubelet-bootstrap \
--token=9c8a6669d8f96432b6ecc3007e169d59 \
--kubeconfig=bootstrap.kubeconfig
设置环境变量(可以写入到/etc/profile中)
[root@server3 kubeconfig]# export PATH=$PATH:/opt/kubernetes/bin/
[root@server3 kubeconfig]# kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
etcd-1 Healthy {"health":"true"}
etcd-2 Healthy {"health":"true"}
[root@server3 kubeconfig]# bash kubeconfig 192.168.188.30 /root/k8s/k8s-cert/
Cluster "kubernetes" set.
User "kubelet-bootstrap" set.
Context "default" created.
Switched to context "default".
Cluster "kubernetes" set.
User "kube-proxy" set.
Context "default" created.
Switched to context "default".
[root@master kubeconfig]# ls
bootstrap.kubeconfig kubeconfig kube-proxy.kubeconfig
拷贝配置文件到node节点
[root@master kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.188.40:/opt/kubernetes/cfg/
root@192.168.188.40's password:
bootstrap.kubeconfig 100% 2168 2.6MB/s 00:00
kube-proxy.kubeconfig 100% 6270 8.3MB/s 00:00
[root@master kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.188.50:/opt/kubernetes/cfg
root@192.168.188.50's password:
bootstrap.kubeconfig 100% 2168 1.7MB/s 00:00
kube-proxy.kubeconfig 100% 6270 7.3MB/s 00:00
创建bootstrap角色赋予权限用于连接apiserver请求签名(关键)
[root@master kubeconfig]# kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
clusterrolebinding.rbac.authorization.k8s.io/kubelet-bootstrap created
在node01节点上操作
[root@node01 ~]# bash kubelet.sh 192.168.188.40
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
检查kubelet服务启动
[root@node01 ~]# ps aux | grep kube
master上操作
检查到node01节点的请求
[root@master kubeconfig]# kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-kPSxcBFoxgDNEF0QSrqvRJyYb9AC3eIvuU75NCew08c 2m16s kubelet-bootstrap Pending
[root@master kubeconfig]# kubectl certificate approve node-csr-kPSxcBFoxgDNEF0QSrqvRJyYb9AC3eIvuU75NCew08c
certificatesigningrequest.certificates.k8s.io/node-csr-kPSxcBFoxgDNEF0QSrqvRJyYb9AC3eIvuU75NCew08c approved
继续查看证书状态
[root@master kubeconfig]# kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-kPSxcBFoxgDNEF0QSrqvRJyYb9AC3eIvuU75NCew08c 7m17s kubelet-bootstrap Approved,Issued(已经被允许加入群集)
查看群集节点,成功加入node01节点
[root@master kubeconfig]# kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.188.40 Ready <none> 2m23s v1.12.3
在node01节点操作,启动proxy服务
[root@node01 ~]# bash proxy.sh 192.168.188.40
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
[root@node01 ~]# systemctl status kube-proxy.service
● kube-proxy.service - Kubernetes Proxy
Loaded: loaded (/usr/lib/systemd/system/kube-proxy.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2021-03-24 19:49:01 CST; 8s ago
Main PID: 49173 (kube-proxy)
Memory: 8.2M
CGroup: /system.slice/kube-proxy.service
‣ 49173 /opt/kubernetes/bin/kube-proxy --logtostderr=...
3月 24 19:49:01 node01 kube-proxy[49173]: I0324 19:49:01.888286...
3月 24 19:49:02 node01 kube-proxy[49173]: I0324 19:49:02.050917...
3月 24 19:49:03 node01 kube-proxy[49173]: I0324 19:49:03.656172...
3月 24 19:49:04 node01 kube-proxy[49173]: I0324 19:49:04.058129...
3月 24 19:49:05 node01 kube-proxy[49173]: I0324 19:49:05.663194...
3月 24 19:49:06 node01 kube-proxy[49173]: I0324 19:49:06.067486...
3月 24 19:49:07 node01 kube-proxy[49173]: I0324 19:49:07.669626...
3月 24 19:49:08 node01 kube-proxy[49173]: I0324 19:49:08.073782...
3月 24 19:49:09 node01 kube-proxy[49173]: I0324 19:49:09.676159...
3月 24 19:49:10 node01 kube-proxy[49173]: I0324 19:49:10.080562...
Hint: Some lines were ellipsized, use -l to show in full.
在node01节点操作
把现成的/opt/kubernetes目录复制到其他节点进行修改即可
[root@node01 ~]# scp -r /opt/kubernetes/ root@192.168.188.50:/opt/
The authenticity of host '192.168.188.50 (192.168.188.50)' can't be established.
ECDSA key fingerprint is SHA256:wAcqk/TBiiofR+EeUwdTTPdsPerfoNlENEcXbZIGlc0.
ECDSA key fingerprint is MD5:10:d1:dc:b3:44:7f:b6:dd:b1:4b:24:5e:ed:3a:81:27.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.188.50' (ECDSA) to the list of known hosts.
root@192.168.188.50's password:
flanneld 100% 238 704.1KB/s 00:00
bootstrap.kubeconfig 100% 2168 4.7MB/s 00:00
kube-proxy.kubeconfig 100% 6270 13.4MB/s 00:00
kubelet 100% 378 636.0KB/s 00:00
kubelet.config 100% 268 503.1KB/s 00:00
kubelet.kubeconfig 100% 2297 3.5MB/s 00:00
kube-proxy 100% 190 337.2KB/s 00:00
mk-docker-opts.sh 100% 2139 4.3MB/s 00:00
scp: /opt//kubernetes/bin/flanneld: Text file busy
kubelet 100% 168MB 150.7MB/s 00:01
kube-proxy 100% 48MB 156.0MB/s 00:00
kubelet.crt 100% 2193 1.8MB/s 00:00
kubelet.key 100% 1679 2.4MB/s 00:00
kubelet-client-2021-03-24-19-45- 100% 1277 368.1KB/s 00:00
kubelet-client-current.pem 100% 1277 362.5KB/s 00:00
把kubelet,kube-proxy的service文件拷贝到node2中
[root@node01 ~]# scp /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@192.168.188.50:/usr/lib/systemd/system/
root@192.168.188.50's password:
kubelet.service 100% 264 313.9KB/s 00:00
kube-proxy.service 100% 231 243.9KB/s 00:00
在node02上操作,进行修改
首先删除复制过来的证书,等会node02会自行申请证书
[root@node02 ~]# cd /opt/kubernetes/ssl/
[root@node02 ssl]# rm -rf *
修改配置文件kubelet kubelet.config kube-proxy(三个配置文件)
[root@node02 ssl]# cd ../cfg/
[root@node02 cfg]# vim kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.188.50 \
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \
--config=/opt/kubernetes/cfg/kubelet.config \
--cert-dir=/opt/kubernetes/ssl \
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
[root@node02 cfg]# vim kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.188.50
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS:
- 10.0.0.2
clusterDomain: cluster.local.
failSwapOn: false
authentication:
anonymous:
enabled: true
[root@node02 cfg]# vim kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.188.50 \
--cluster-cidr=10.0.0.0/24 \
--proxy-mode=ipvs \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"
启动服务
[root@node02 cfg]# systemctl start kubelet.service
[root@node02 cfg]# systemctl enable kubelet.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@node02 cfg]# systemctl start kube-proxy.service
[root@node02 cfg]# systemctl enable kube-proxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
在master上操作查看请求
[root@master k8s]# kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-_hN7TuvCmeJ_oOHuED39v_65VUNMaON76np8-HRqRE4 84s kubelet-bootstrap Pending
node-csr-kPSxcBFoxgDNEF0QSrqvRJyYb9AC3eIvuU75NCew08c 27m kubelet-bootstrap Approved,Issued
授权许可加入群集
[root@master k8s]# kubectl certificate approve node-csr-_hN7TuvCmeJ_oOHuED39v_65VUNMaON76np8-HRqRE4
certificatesigningrequest.certificates.k8s.io/node-csr-_hN7TuvCmeJ_oOHuED39v_65VUNMaON76np8-HRqRE4 approved
[root@master k8s]# kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.188.40 Ready <none> 22m v1.12.3
192.168.188.50 Ready <none> 11s v1.12.3