官网地址:https://github.com/kubernetes/kubernetes/releases?after=v1.13.1
Master1:192.168.200.10/24 kube-apiserver kube-controller-manager kube-scheduler etcd
Node01:192.168.200.40/24 kubelet kube-proxy docker flannel etcd
Node02:192.168.200.60/24 kubelet kube-proxy docker flannel etcd
master1上进行操作
[root@master1 ~]# mkdir k8s
[root@master1 ~]# cd k8s/
[root@master1 k8s]# ls #将以下两个脚本文件上传到该目录下
etcd-cert.sh etcd.sh #etcd-cert.sh为etcd证书的脚本,etcd.sh为etcd服务的脚本
[root@master1 k8s]# mkdir etcd-cert
[root@master1 k8s]# ls
etcd-cert etcd-cert.sh etcd.sh
[root@master1 k8s]# mv etcd-cert.sh etcd-cert
[root@master1 k8s]# vim cfssl.sh
curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl
curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson
curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo
chmod +x /usr/local/bin/cfssl /usr/local/bin/cfssljson /usr/local/bin/cfssl-certinfo
[root@master1 k8s]# bash cfssl.sh
[root@master1 k8s]# ls /usr/local/bin/
cfssl cfssl-certinfo cfssljson
//cfssl 生成证书工具 , cfssljson通过传入json文件生成证书,cfssl-certinfo查看证书信息
#定义ca证书
[root@master1 ~]# cd /root/k8s/etcd-cert
cat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
#实现证书签名
[root@master1 ~]# cd /root/k8s/etcd-cert
cat > ca-csr.json <<EOF
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
#生产证书,生成ca-key.pem ca.pem
[root@master1 etcd-cert]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
2021/04/10 22:53:30 [INFO] generating a new CA key and certificate from CSR
2021/04/10 22:53:30 [INFO] generate received request
2021/04/10 22:53:30 [INFO] received CSR
2021/04/10 22:53:30 [INFO] generating key: rsa-2048
2021/04/10 22:53:30 [INFO] encoded CSR
2021/04/10 22:53:30 [INFO] signed certificate with serial number 397125830926114737701706075410049927608256756699
[root@master1 ~]# cd /root/k8s/etcd-cert
cat > server-csr.json <<EOF
{
"CN": "etcd",
"hosts": [
"192.168.200.10",
"192.168.200.40",
"192.168.200.60"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
server-key.pem server.pem
[root@master1 ~]# cd /root/k8s/etcd-cert
[root@master1 etcd-cert]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
2021/04/10 22:57:45 [INFO] generate received request
2021/04/10 22:57:45 [INFO] received CSR
2021/04/10 22:57:45 [INFO] generating key: rsa-2048
2021/04/10 22:57:45 [INFO] encoded CSR
2021/04/10 22:57:45 [INFO] signed certificate with serial number 219342381748991728851284184667512408378336584859
2021/04/10 22:57:45 [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@master1 etcd-cert]# ls
ca-config.json ca-key.pem server.csr server.pem
ca.csr ca.pem server-csr.json
ca-csr.json etcd-cert.sh server-key.pem
#ETCD 二进制包地址
https://github.com/etcd-io/etcd/releases
[root@master1 etcd-cert]# cd ~/k8s/
[root@master1 k8s]# ls
cfssl.sh etcd.sh flannel-v0.10.0-linux-amd64.tar.gz
etcd-cert etcd-v3.3.10-linux-amd64.tar.gz kubernetes-server-linux-amd64.tar.gz
[root@master1 k8s]# tar zxvf etcd-v3.3.10-linux-amd64.tar.gz
[root@master1 k8s]# ls etcd-v3.3.10-linux-amd64
Documentation etcdctl README.md
etcd README-etcdctl.md READMEv2-etcdctl.md
[root@master1 k8s]# mkdir /opt/etcd/{cfg,bin,ssl} -p //cfg:配置文件,bin:命令文件,ssl:证书
[root@master1 k8s]# mv etcd-v3.3.10-linux-amd64/etcd etcd-v3.3.10-linux-amd64/etcdctl /opt/etcd/bin/
[root@master1 k8s]# ls /opt/etcd/bin/
etcd etcdctl
[root@master1 k8s]# cp etcd-cert/*.pem /opt/etcd/ssl/
[root@master1 k8s]# ls /opt/etcd/ssl/
ca-key.pem ca.pem server-key.pem server.pem
[root@master1 k8s]# bash etcd.sh etcd01 192.168.200.10 etcd02=https://192.168.200.40:2380,etcd03=https://192.168.200.60:2380
[root@localhost ~]# ps -ef | grep etcd
[root@master1 k8s]# scp -r /opt/etcd/ root@192.168.200.40:/opt/
[root@master1 k8s]# scp -r /opt/etcd/ root@192.168.200.60:/opt/
[root@master1 ~]# scp /usr/lib/systemd/system/etcd.service root@192.168.200.40:/usr/lib/systemd/system/
[root@master1 ~]# scp /usr/lib/systemd/system/etcd.service root@192.168.200.60:/usr/lib/systemd/system/
###在node1节点修改
[root@node1 system]# cd /opt/etcd/cfg/
[root@node1 cfg]# vim etcd
#[Member]
ETCD_NAME="etcd02"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.200.40:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.200.40:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.200.40:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.200.40:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.200.10:2380,etcd02=https://192.168.200.40:2380,etcd03=https://192.168.200.60:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
###在node2节点修改
[root@node2 system]# cd /opt/etcd/cfg/
[root@node2 cfg]# vim etcd
#[Member]
ETCD_NAME="etcd03"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.200.60:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.200.60:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.200.60:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.200.60:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.200.10:2380,etcd02=https://192.168.200.40:2380,etcd03=https://192.168.200.60:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
#首先在master1节点上进行启动
[root@master1 ~]# cd /root/k8s/
[root@master1 k8s]# bash etcd.sh etcd01 192.168.200.10 etcd02=https://192.168.200.40:2380,etcd03=https://192.168.200.60:2380
#接着在node1和node2节点分别进行启动
[root@node1 cfg]# systemctl start etcd.service
[root@node2 cfg]# systemctl start etcd.service
[root@master1 ~]# cd /root/k8s/etcd-cert/
[root@master1 etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.200.10:2379,https://192.168.200.40:2379,https://192.168.200.60:2379" cluster-health
member 995575c0c650e89b is healthy: got healthy result from https://192.168.200.40:2379
member 9abd0b70bb7a8d9a is healthy: got healthy result from https://192.168.200.10:2379
member acca2e49c9c76117 is healthy: got healthy result from https://192.168.200.60:2379
cluster is healthy
所有node节点部署docker引擎
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce docker-ce-cli containerd.io
systemctl start docker #启动docker
systemctl enable docker #设置开机自启动
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://p3pqujwc.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p
service network restart
systemctl restart docker
----master1上进行操作-----
[root@master1 ~]# cd k8s/etcd-cert/
[root@master1 etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.200.10:2379,https://192.168.200.40:2379,https://192.168.200.60:2379" set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}
#查看写入的信息
[root@master1 etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.200.10:2379,https://192.168.200.40:2379,https://192.168.200.60:2379" get /coreos.com/network/config
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}
[root@master1 ~]# cd k8s/
[root@master1 k8s]# ls
cfssl.sh
etcd-cert
etcd.sh
etcd-v3.3.10-linux-amd64
etcd-v3.3.10-linux-amd64.tar.gz
flannel-v0.10.0-linux-amd64.tar.gz
kubernetes-server-linux-amd64.tar.gz
[root@master1 k8s]# scp flannel-v0.10.0-linux-amd64.tar.gz root@192.168.200.40:/root
[root@master1 k8s]# scp flannel-v0.10.0-linux-amd64.tar.gz root@192.168.200.60:/root
tar zxvf flannel-v0.10.0-linux-amd64.tar.gz
mkdir /opt/kubernetes/{cfg,bin,ssl} -p
mv mk-docker-opts.sh flanneld /opt/kubernetes/bin/
[root@localhost ~]# vim flannel.sh
#!/bin/bash
ETCD_ENDPOINTS=${1:-"http://127.0.0.1:2379"}
cat <<EOF >/opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} \
-etcd-cafile=/opt/etcd/ssl/ca.pem \
-etcd-certfile=/opt/etcd/ssl/server.pem \
-etcd-keyfile=/opt/etcd/ssl/server-key.pem"
EOF
cat <<EOF >/usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service
[Service]
Type=notify
EnvironmentFile=/opt/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq \$FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable flanneld
systemctl restart flanneld
[root@localhost ~]# bash flannel.sh https://192.168.200.10:2379,https://192.168.200.40:2379,https://192.168.200.60:2379
[root@localhost ~]# vim /usr/lib/systemd/system/docker.service
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
EnvironmentFile=/run/flannel/subnet.env #添加此行
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS -H fd:// --containerd=/run/containerd/containerd.sock #修改此行
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
[root@localhost ~]# cat /run/flannel/subnet.env
DOCKER_OPT_BIP="--bip=172.17.42.1/24"
DOCKER_OPT_IPMASQ="--ip-masq=false"
DOCKER_OPT_MTU="--mtu=1450"
//说明:bip指定启动时的子网
DOCKER_NETWORK_OPTIONS=" --bip=172.17.42.1/24 --ip-masq=false --mtu=1450"
systemctl daemon-reload
systemctl restart docker
[root@node1 ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.42.1 netmask 255.255.255.0 broadcast 172.17.42.255
ether 02:42:92:10:6f:24 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.42.0 netmask 255.255.255.255 broadcast 0.0.0.0
inet6 fe80::3833:cff:fe69:56f6 prefixlen 64 scopeid 0x20<link>
ether 3a:33:0c:69:56:f6 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 26 overruns 0 carrier 0 collisions 0
[root@node2 ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.70.1 netmask 255.255.255.0 broadcast 172.17.70.255
ether 02:42:75:18:1e:44 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.70.0 netmask 255.255.255.255 broadcast 0.0.0.0
inet6 fe80::8cd8:34ff:fec1:1dbb prefixlen 64 scopeid 0x20<link>
ether 8e:d8:34:c1:1d:bb txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 26 overruns 0 carrier 0 collisions 0
[root@node1 ~]# ping 172.17.70.1
PING 172.17.70.1 (172.17.70.1) 56(84) bytes of data.
64 bytes from 172.17.70.1: icmp_seq=1 ttl=64 time=0.211 ms
64 bytes from 172.17.70.1: icmp_seq=2 ttl=64 time=0.257 ms
64 bytes from 172.17.70.1: icmp_seq=3 ttl=64 time=0.228 ms
64 bytes from 172.17.70.1: icmp_seq=4 ttl=64 time=0.171 ms
[root@node1 ~]# docker run -it centos:7 /bin/bash #node1和node2都运行该命令
[root@7c733f24ebeb /]# yum install net-tools -y #node1和node2都运行该命令
[root@7c733f24ebeb /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.42.2 netmask 255.255.255.0 broadcast 172.17.42.255
ether 02:42:ac:11:2a:02 txqueuelen 0 (Ethernet)
RX packets 20185 bytes 15437540 (14.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9598 bytes 521494 (509.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@f4ed072201aa /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.70.2 netmask 255.255.255.0 broadcast 172.17.70.255
ether 02:42:ac:11:46:02 txqueuelen 0 (Ethernet)
RX packets 20233 bytes 15440288 (14.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9303 bytes 505678 (493.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
//再次测试ping通两个node中的centos:7容器
[root@7c733f24ebeb /]# ping 172.17.70.2
PING 172.17.70.2 (172.17.70.2) 56(84) bytes of data.
64 bytes from 172.17.70.2: icmp_seq=1 ttl=62 time=0.364 ms
64 bytes from 172.17.70.2: icmp_seq=2 ttl=62 time=0.245 ms
64 bytes from 172.17.70.2: icmp_seq=3 ttl=62 time=0.236 ms
###注意:如果ping时卡住,可以先退出容器,然后重启flanneld、network和docker,然后重新进入容器,就可以ping通了。(所有节点都要进行重启)
#在master上操作
[root@master1 ~]# cd k8s/
#将 master.zip上传到该目录下进行解压
[root@master1 k8s]# unzip master.zip
[root@master1 k8s]# chmod +x controller-manager.sh
[root@master1 k8s]# mkdir /opt/kubernetes/{cfg,bin,ssl} -p
[root@master1 k8s]# mkdir k8s-cert #创建k8s证书存放目录
[root@master1 k8s]# cd k8s-cert/
[root@master1 k8s-cert]# ls #讲写好的k8s-cert.sh脚本上传到该目录下
k8s-cert.sh
#查看并修改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.200.10", //master1
"192.168.200.20", //master2
"192.168.200.100", //vip
"192.168.200.70", //lb (master)
"192.168.200.80", //lb (backup)
"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@master1 k8s-cert]# bash k8s-cert.sh
[root@master1 k8s-cert]# ls *.pem #
admin-key.pem ca.pem server-key.pem
admin.pem kube-proxy-key.pem server.pem
ca-key.pem kube-proxy.pem
[root@master1 k8s-cert]# cp ca*pem server*pem /opt/kubernetes/ssl/
[root@localhost k8s-cert]# cd ..
[root@master1 k8s]# tar zxvf kubernetes-server-linux-amd64.tar.gz
[root@master1 k8s]# cd /root/k8s/kubernetes/server/bin
[root@master1 bin]# cp kube-apiserver kubectl kube-controller-manager kube-scheduler /opt/kubernetes/bin/
[root@master1 bin]# cd /root/k8s/
[root@master1 k8s]# head -c 16 /dev/urandom | od -An -t x | tr -d ' '
849b37696c2aefc70a979562a2bab824
#使用 head -c 16 /dev/urandom | od -An -t x | tr -d ' ' 可以随机生成序列号
[root@master1 k8s]# vim /opt/kubernetes/cfg/token.csv
849b37696c2aefc70a979562a2bab824,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
[root@master1 k8s]# bash apiserver.sh 192.168.200.10 https://192.168.200.10:2379,https://192.168.200.40:2379,https://192.168.200.60:2379
#检查进程是否启动成功
[root@master1 k8s]# ps aux | grep kube
root 55228 0.0 0.0 112676 984 pts/2 R+ 17:20 0:00 grep --color=auto kube
[root@master1 k8s]# cat /opt/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.200.10:2379,https://192.168.200.40:2379,https://192.168.200.60:2379 \
--bind-address=192.168.200.10 \
--secure-port=6443 \
--advertise-address=192.168.200.10 \
--allow-privileged=true \
--service-cluster-ip-range=10.0.0.0/24 \
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \
--authorization-mode=RBAC,Node \
--kubelet-https=true \
--enable-bootstrap-token-auth \
--token-auth-file=/opt/kubernetes/cfg/token.csv \
--service-node-port-range=30000-50000 \
--tls-cert-file=/opt/kubernetes/ssl/server.pem \
--tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \
--client-ca-file=/opt/kubernetes/ssl/ca.pem \
--service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \
--etcd-cafile=/opt/etcd/ssl/ca.pem \
--etcd-certfile=/opt/etcd/ssl/server.pem \
--etcd-keyfile=/opt/etcd/ssl/server-key.pem"
#监听的https端口
[root@master1 k8s]# netstat -ntap | grep 6443
tcp 0 0 192.168.200.10:6443 0.0.0.0:* LISTEN 55290/kube-apiserve
tcp 0 0 192.168.200.10:54566 192.168.200.10:6443 ESTABLISHED 55290/kube-apiserve
tcp 0 0 192.168.200.10:6443 192.168.200.10:54566 ESTABLISHED 55290/kube-apiserve
[root@master1 k8s]# ./scheduler.sh 127.0.0.1
[root@master1 k8s]# ps aux | grep ku
[root@master1 k8s]# ./controller-manager.sh 127.0.0.1
[root@master1 k8s]# /opt/kubernetes/bin/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"}
#master上操作
[root@master1 ~]# cd /root/k8s/kubernetes/server/bin
[root@master1 bin]# scp kubelet kube-proxy root@192.168.200.40:/opt/kubernetes/bin/
root@192.168.200.40's password:
kubelet 100% 168MB 97.0MB/s 00:01
kube-proxy 100% 48MB 93.9MB/s 00:00
[root@master1 bin]# scp kubelet kube-proxy root@192.168.200.60:/opt/kubernetes/bin/
root@192.168.200.60's password:
kubelet 100% 168MB 96.5MB/s 00:01
kube-proxy 100% 48MB 102.6MB/s 00:00
#nod01节点操作
[root@node1 ~]# ls #将node.zip上传到/root目录下
anaconda-ks.cfg node.zip 视频 音乐
flannel.sh README.md 图片 桌面
flannel-v0.10.0-linux-amd64.tar.gz 公共 文档
initial-setup-ks.cfg 模板 下载
#解压node.zip,获得kubelet.sh proxy.sh
[root@node1 ~]# unzip node.zip
#在master上操作
[root@master1 k8s]# mkdir kubeconfig
[root@master1 k8s]# cd kubeconfig/
#拷贝kubeconfig.sh文件到该目录下,并进行重命名
[root@master1 kubeconfig]# mv kubeconfig.sh kubeconfig
[root@master1 kubeconfig]# vim kubeconfig
----------------删除以下部分----------------------------------------------------------------------
# 创建 TLS Bootstrapping Token
#BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ' ')
BOOTSTRAP_TOKEN=0fb61c46f8991b718eb38d27b605b008
cat > token.csv <<EOF
${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
EOF
[root@master1 kubeconfig]# cat /opt/kubernetes/cfg/token.csv
849b37696c2aefc70a979562a2bab824,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
[root@master1 kubeconfig]# vim kubeconfig
kubectl config set-credentials kubelet-bootstrap \
--token=849b37696c2aefc70a979562a2bab824 \ #修改此处
--kubeconfig=bootstrap.kubeconfig
[root@master1 kubeconfig]# vim /etc/profile
export PATH=$PATH:/opt/kubernetes/bin/ #将此行添加在结尾处
[root@master1 kubeconfig]# source /etc/profile
[root@master1 kubeconfig]# kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
etcd-1 Healthy {"health":"true"}
etcd-2 Healthy {"health":"true"}
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
[root@master1 kubeconfig]# bash kubeconfig 192.168.200.10 /root/k8s/k8s-cert/
[root@master1 kubeconfig]# ls
bootstrap.kubeconfig kubeconfig kube-proxy.kubeconfig
root@master1 kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.200.40:/opt/kubernetes/cfg/
root@192.168.200.40's password:
bootstrap.kubeconfig 100% 2168 2.3MB/s 00:00
kube-proxy.kubeconfig 100% 6270 7.4MB/s 00:00
[root@master1 kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.200.60:/opt/kubernetes/cfg/
root@192.168.200.60's password:
bootstrap.kubeconfig 100% 2168 1.8MB/s 00:00
kube-proxy.kubeconfig 100% 6270 6.9MB/s 00:00
[root@master1 kubeconfig]# kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
#在node01节点上操作
[root@node1 ~]# bash kubelet.sh 192.168.200.40
[root@node1 ~]# ps aux | grep kube
root 80970 1.0 2.2 369632 42436 ? Ssl 19:24 0:00 /opt/kubernetes/bin/kubelet --logtostderr=true --v=4 --hostname-override=192.168.200.40 --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 80988 0.0 0.0 112676 984 pts/2 R+ 19:25 0:00 grep --color=auto kube
#master上操作
[root@master1 kubeconfig]# kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-ROXvPI34y36KB5tjPw2YWHro-g7s9u-9AYDkt7kd_fQ 98s kubelet-bootstrap Pending
#Pending(等待集群给该节点颁发证书)
[root@master1 kubeconfig]# kubectl certificate approve node-csr-ROXvPI34y36KB5tjPw2YWHro-g7s9u-9AYDkt7kd_fQ #此处的序列号为上面使用kubectl get csr显示的NAME
[root@master1 kubeconfig]# kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-ROXvPI34y36KB5tjPw2YWHro-g7s9u-9AYDkt7kd_fQ 4m57s kubelet-bootstrap Approved,Issued
#Approved,Issued(已经被允许加入群集)
[root@master1 kubeconfig]# kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.200.40 Ready <none> 67s v1.12.3
#在node01节点操作
[root@node1 ~]# bash proxy.sh 192.168.200.40
root@node1 ~]# systemctl status kube-proxy.service #查看是否启动成功
#node02节点部署
#把现成的/opt/kubernetes目录复制到其他节点进行修改即可
[root@node1 ~]# scp -r /opt/kubernetes/ root@192.168.200.60:/opt/
[root@node1 ~]# scp /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@192.168.200.60:/usr/lib/systemd/system/
#在node02上操作,进行修改
[root@node2 ~]# cd /opt/kubernetes/ssl/
[root@node2 ssl]# ls
kubelet-client-2021-04-13-19-29-15.pem kubelet.crt
kubelet-client-current.pem kubelet.key
[root@node2 ssl]# rm -rf *
[root@node2 ssl]# cd ../cfg/
[root@node2 cfg]# vim kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.200.60 \ #修改为node2自己的IP
--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@node2 cfg]# vim kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.200.60 #修改为自己的IP
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS:
- 10.0.0.2
clusterDomain: cluster.local.
failSwapOn: false
authentication:
anonymous:
enabled: true
[root@node2 cfg]# vim kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.200.60 \ #修改为自己的IP
--cluster-cidr=10.0.0.0/24 \
--proxy-mode=ipvs \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"
[root@node2 cfg]# systemctl start kubelet.service
[root@node2 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@node2 cfg]# systemctl start kube-proxy.service
[root@node2 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@master1 kubeconfig]# kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-ROXvPI34y36KB5tjPw2YWHro-g7s9u-9AYDkt7kd_fQ 16m kubelet-bootstrap Approved,Issued
node-csr-WpTFLoUV_yPjqF6SvuiX4OwChJWqirvgHtLDoI7ZMVI 68s kubelet-bootstrap Pending
[root@master1 kubeconfig]# kubectl certificate approve node-csr-WpTFLoUV_yPjqF6SvuiX4OwChJWqirvgHtLDoI7ZMVI #注意修改序列号
[root@master1 kubeconfig]# kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.200.40 Ready <none> 12m v1.12.3
192.168.200.60 Ready <none> 18s v1.12.3
建立完整TLS加密通信,需要有一个CA认证机构,会向客户端下发根证书、服务端证书以及签名私钥给客户端。ca.pem & ca-key.pem & ca.csr组成了一个自签名的CA机构。
证书名称 | 作用 |
---|---|
ca.pem | CA根证书文件 |
ca-key.pem | 服务端私钥,用于对客户端请求的解密和签名 |
ca.csr | 证书签名请求,用于交叉签名或重新签名 |
该文件为一个用户的描述文件,基本格式为 Token,用户名,UID,用户组;这个文件在 apiserver 启动时被 apiserver 加载,然后就相当于在集群内创建了一个这个用户;接下来就可以用 RBAC 给他授权
该文件中内置了 token.csv 中用户的 Token,以及 apiserver CA 证书;kubelet 首次启动会加载此文件,使用 apiserver CA 证书建立与 apiserver 的 TLS 通讯,使用其中的用户 Token 作为身份标识向apiserver 发起 CSR 请求
kubectl只是个go编写的可执行程序,只要为kubectl配置合适的kubeconfig,就可以在集群中的任意节点使用 。kubectl的权限为admin,具有访问kubernetes所有api的权限。
证书名称 | 作用 |
---|---|
ca.pem | CA根证书 |
admin.pem | kubectl的TLS认证证书,具有admin权限 |
admin-key.pem | kubectl的TLS认证私钥 |
证书名称 | 作用 |
---|---|
ca.pem | CA根证书 |
kubelet-client.crt | kubectl的TLS认证证书 |
kubelet-client.key | kubectl的TLS认证私钥 |
kubelet.crt | 独立于 apiserver CA 的自签 CA |
kubelet.key | 独立于 apiserver CA 的私钥 |
当成功签发证书后,目标节点的 kubelet 会将证书写入到 --cert-dir= 选项指定的目录中;此时如果不做其他设置应当生成上述除ca.pem以外的4个文件
kube-apiserver是在部署kubernetes集群是最需要先启动的组件,也是和集群交互的核心组件。
使用的证书 | 证书作用 |
---|---|
ca.pem | CA根证书 |
ca-key.pem | CA端私钥 |
Server.pem | kube-apiserver的tls认证证书 |
Server-key.pem | kube-apiserver的tls认证私钥 |
kubelet 发起的 CSR 请求都是由 kube-controller-manager 来做实际签署的,所有使用的证书都是根证书的密钥对 。由于kube-controller-manager是和kube-apiserver部署在同一节点上,且使用非安全端口通信,故不需要证书
使用的证书 | 证书作用 |
---|---|
ca.pem | CA根证书 |
ca-key.pem | kube-apiserver的tls认证私钥 |
kube-scheduler是和kube-apiserver一般部署在同一节点上,且使用非安全端口通信,故启动参参数中没有指定证书的参数可选 。 若分离部署,可在kubeconfig文件中指定证书,使用kubeconfig认证,kube-proxy类似