概述
二进制软件包下载地址:
软件名称 | 下载地址 | 备注 |
---|---|---|
cfssl | http://pkg.cfssl.org/R1.2/cfssl_linux-amd64 http://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 http://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 | 证书签发工具 |
kubernetes-server | https://dl.k8s.io/1.18.3/kubernetes-server-linux-amd64.tar.gz | |
ETCD | https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz | Kubernetes数据库 |
cni | https://github.com/containernetworking/plugins/releases/download/v0.8.6/cni-plugins-linux-amd64-v0.8.6.tgz | 网络环境 |
Flannel | https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml |
二进制包:
从github下载发行版的二进制包,手动部署每个组件,组成Kubernetes集群。
Kubeadm降低部署门槛,但屏蔽了很多细节,遇到问题很难排查。如果想更容易可控,推荐使用二进制包部署Kubernetes集群,虽然手动部署麻烦点,期间可以学习很多工作原理,也利于后期维护。
实验环境:
角色 | IP | 组件 | 推荐配置 |
---|---|---|---|
k8s-master01 | 172.16.3.225 | Kube-apiserver , Kube-controller-manager , Kube-scheduler , etcd | CPU 2核+ 2G内存+ |
k8s-node01 | 172.16.3.226 | Kubelet , Kube-proxy , Docker , Flannel , etcd | CPU 2核+ 2G内存+ |
k8s-node02 | 172.16.3.227 | Kubelet , Kube-proxy , Docker , Flannel , etcd | CPU 2核+ 2G内存+ |
实验步骤:
关闭三台主机的Selinux,firewalld,Swap并修改主机名:
[root@bogon ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@bogon ~]# systemctl stop firewalld
[root@bogon ~]# systemctl disable firewalld
[root@bogon ~]# iptables -vnL # 查看一下防火墙规则确保内容是空
[root@bogon ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab
[root@bogon ~]# swapoff -a
[root@bogon ~]# hostnamectl set-hostname k8s-master01 #(1)上的操作
[root@bogon ~]# hostnamectl set-hostname k8s-node01 #(2)上的操作
[root@bogon ~]# hostnamectl set-hostname k8s-node02 #(3)上的操作
[root@bogon ~]# cat >> /etc/hosts << EOF
172.16.3.225 k8s-master01
172.16.3.226 k8s-node01
172.16.3.227 k8s-node02
EOF
将桥接的IPv4流量传递到iptables的链:
[root@bogon ~]# cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
[root@bogon ~]# sysctl --system # 生效
# 如果服务器时间不一致需要同步一下时间
[root@bogon ~]# yum install ntpdate -y
[root@bogon ~]# ntpdate time.windows.com
[root@bogon ~]# shutdown -r now#(1,2,3)上的操作
在Master主机上安装cfssl用于生成证书:
Etcd 是一个分布式键值存储系统,Kubernetes使用Etcd进行数据存储,所以先准备一个Etcd数据库,为解决Etcd单点故障,应采用集群方式部署,这里使用3台组建集群,可容忍1台机器故障,当然,你也可以使用5台组建集群,可容忍2台机器故障
节点名称 | IP |
---|---|
etcd-1 | 172.16.3.225 |
etcd-2 | 172.16.3.226 |
etcd-3 | 172.16.3.227 |
注:为了节省机器,这里与K8s节点机器复用。也可以独立于k8s集群之外部署,只要apiserver能连接到就行。
cfssl是一个开源的证书管理工具,使用json文件生成证书,相比openssl更方便使用。
[root@k8s-master01 ~]# wget http://pkg.cfssl.org/R1.2/cfssl_linux-amd64
[root@k8s-master01 ~]# wget http://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
[root@k8s-master01 ~]# wget http://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
[root@k8s-master01 ~]# chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
[root@k8s-master01 ~]# mv cfssl_linux-amd64 /usr/local/bin/cfssl
[root@k8s-master01 ~]# mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
[root@k8s-master01 ~]# mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
在Master生成etcd证书:
[root@k8s-master01 ~]# mkdir ~/k8s/{k8s-cert,etcd-cert} -p # 用于存储Etcd,K8s证书目录
[root@k8s-master01 ~]# cd ~/k8s/etcd-cert/
自签证书颁发机构(CA):
[root@k8s-master01 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@k8s-master01 etcd-cert]# cat > ca-csr.json << EOF
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
[root@k8smaster ssl]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
使用自签CA签发Etcd HTTPS证书:
[root@k8s-master01 etcd-cert]# cat > server-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"172.16.3.225",
"172.16.3.226",
"172.16.3.227"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
注释:上述文件中写到的IP是自己etcd的ip地址
[root@k8s-master01 etcd-cert]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server # 生成etcd证书
[root@k8s-master01 etcd-cert]# ls | grep -v pem | xargs -i rm {} # 删除不需要的证书
在Master部署etcd:
ETCD Github地址:https://github.com/etcd-io/etcd/releases
以下在节点1上操作,为简化操作,待会将节点1生成的所有文件拷贝到节点2和节点3.
[root@k8s-master01 etcd-cert]# mkdir ~/Kubernetes-tar # 创建一个目录用于存放二进制软件包
[root@k8s-master01 etcd-cert]# cd ~/Kubernetes-tar/
[root@k8smaster Kubernetes-tar]# wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
[root@k8smaster Kubernetes-tar]# mkdir /opt/etcd/{bin,cfg,ssl} -p # 方便我们后期管理将etcd存放到此目录
[root@k8smaster Kubernetes-tar]# tar zxf etcd-v3.4.9-linux-amd64.tar.gz
[root@k8smaster Kubernetes-tar]# cd etcd-v3.4.9-linux-amd64
[root@k8s-master01 etcd-v3.4.9-linux-amd64]# mv etcd etcdctl /opt/etcd/bin/
创建etcd的配置文件:
[root@k8s-master01 etcd-v3.4.9-linux-amd64]# cat > /opt/etcd/cfg/etcd.conf << EOF
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://172.16.3.225:2380"
ETCD_LISTEN_CLIENT_URLS="https://172.16.3.225:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://172.16.3.225:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://172.16.3.225:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.31.71:2380,etcd-2=https://192.168.31.72:2380,etcd-3=https://192.168.31.73:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF
- ETCD_NAME:节点名称,集群中唯一
- ETCD_DATA_DIR:数据目录
- ETCD_LISTEN_PEER_URLS:集群通信监听地址
- ETCD_LISTEN_CLIENT_URLS:客户端访问监听地址
- ETCD_INITIAL_ADVERTISE_PEER_URLS:集群通告地址
- ETCD_ADVERTISE_CLIENT_URLS:客户端通告地址
- ETCD_INITIAL_CLUSTER:集群节点地址
- ETCD_INITIAL_CLUSTER_TOKEN:集群Token
- ETCD_INITIAL_CLUSTER_STATE:加入集群的当前状态,new是新集群,existing表示加入已有集群
用于systemd管理我们etcd:
[root@k8s-master01 etcd-v3.4.9-linux-amd64]# cat > /usr/lib/systemd/system/etcd.service << EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd.conf
ExecStart=/opt/etcd/bin/etcd
--cert-file=/opt/etcd/ssl/server.pem
--key-file=/opt/etcd/ssl/server-key.pem
--peer-cert-file=/opt/etcd/ssl/server.pem
--peer-key-file=/opt/etcd/ssl/server-key.pem
--trusted-ca-file=/opt/etcd/ssl/ca.pem
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem
--logger=zap
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
[root@k8s-master01 Kubernetes-tar]# cp -rf ~/k8s/etcd-cert/* /opt/etcd/ssl/ # 把刚才生成的证书拷贝到配置文件中的路径
[root@k8s-master01 Kubernetes-tar]# systemctl daemon-reload
[root@k8s-master01 Kubernetes-tar]# cd
[root@k8s-master01 ~]# scp -r /opt/etcd/ root@k8s-node01:/opt/etcd # 将etcd目录拷贝到node01上
[root@k8s-master01 ~]# scp -r /opt/etcd/ root@k8s-node02:/opt/etcd # 将etcd目录拷贝到node02上
[root@k8s-master01 ~]# scp /usr/lib/systemd/system/etcd.service root@k8s-node01:/usr/lib/systemd/system/ # 将启动文件拷贝到node01上
[root@k8s-master01 ~]# scp /usr/lib/systemd/system/etcd.service root@k8s-node02:/usr/lib/systemd/system/ # 将启动文件拷贝到node02上
在Node 上分别修改etcd.conf配置文件中的节点名称和当前服务器IP:
[root@k8s-node01 ~]# vim /opt/etcd/cfg/etcd.conf
ETCD_NAME="etcd-2" # 修改此处,节点2改为etcd-2,节点3改为etcd-3
ETCD_LISTEN_PEER_URLS="https://172.16.3.226:2380" # 修改成自己本机IP
ETCD_LISTEN_CLIENT_URLS="https://172.16.3.226:2379" # 修改成自己本机IP
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://172.16.3.226:2380" # 修改成自己本机IP
ETCD_ADVERTISE_CLIENT_URLS="https://172.16.3.226:2379" # 修改成自己本机IP
[root@k8s-node01 ~]# systemctl daemon-reload
三台主机启动Etcd:
[root@k8s-master01 ~]# systemctl start etcd
[root@k8s-master01 ~]# systemctl enable etcd
[root@k8s-master01 ~]# systemctl status etcd # 查看一下状态是否启动
在Master主机上查看一下etcd的集群状态:
[root@k8s-master01 ~]# ETCDCTL_API=3 /opt/etcd/bin/etcdctl --cacert=/opt/etcd/ssl/ca.pem --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --endpoints="https://172.16.3.225:2379,https://172.16.3.226:2379,https://172.16.3.227:2379" endpoint health
https://172.16.3.227:2379 is healthy: successfully committed proposal: took = 32.411317ms
https://172.16.3.226:2379 is healthy: successfully committed proposal: took = 26.987082ms
https://172.16.3.225:2379 is healthy: successfully committed proposal: took = 33.140215ms
注:出现以上效果说明集群状态没有问题(healthy)
在Node主机上安装Docker环境:
[root@k8snode1 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@k8snode1 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@k8snode1 ~]# yum makecache fast
[root@k8snode1 ~]# yum -y install docker-ce
[root@k8snode1 ~]# vim /usr/lib/systemd/system/docker.service # 修改一下docker存储路径
14 ExecStart=/usr/bin/dockerd --graph=/home/docker
[root@k8snode1 ~]# mkdir /home/docker
[root@k8snode1 ~]# systemctl daemon-reload
[root@k8snode1 ~]# systemctl start docker
[root@k8snode1 ~]# docker info | grep 'Docker Root Dir' # 验证一下docker存储路径是否修改成功
Docker Root Dir: /home/docker
[root@k8snode1 ~]# systemctl enable docker
接下来我们来部署Master组件:
1、 生成kube-apiserver证书:
[root@k8s-master01 ~]# cd ~/k8s/k8s-cert/
[root@k8s-master01 k8s-cert]# cat > ca-config.json << EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
[root@k8s-master01 k8s-cert]# cat > ca-csr.json << EOF
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
[root@k8s-master01 k8s-cert]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca - # 生成证书
使用自签CA签发kube-apiserver HTTPS证书:
[root@k8s-master01 k8s-cert]# cat > server-csr.json << EOF
{
"CN": "kubernetes",
"hosts": [
"10.0.0.1",
"127.0.0.1",
"172.16.3.225",
"172.16.3.226",
"172.16.3.227",
"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
[root@k8s-master01 k8s-cert]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
Github二进制下载地址: https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.18.md#v1183
2、解压二进制包
[root@k8s-master01 k8s-cert]# cd ~/Kubernetes-tar/
[root@k8s-master01 Kubernetes-tar]# wget https://dl.k8s.io/1.18.3/kubernetes-server-linux-amd64.tar.gz
[root@k8s-master01 Kubernetes-tar]# tar zxf kubernetes-server-linux-amd64.tar.gz
[root@k8s-master01 Kubernetes-tar]# cd kubernetes/server/bin/
[root@k8s-master01 bin]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}
[root@k8s-master01 bin]# cp kube-apiserver kube-scheduler kube-controller-manager /opt/kubernetes/bin
[root@k8s-master01 bin]# cp kubectl /usr/bin/
3、部署kube-apiserver
[root@k8s-master01 bin]# cat > /opt/kubernetes/cfg/kube-apiserver.conf << EOF
KUBE_APISERVER_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/kubernetes/logs \
--etcd-servers=https://172.16.3.225:2379,https://172.16.3.226:2379,https://172.16.3.227:2379 \
--bind-address=172.16.3.225 \
--secure-port=6443 \
--advertise-address=172.16.3.225 \
--allow-privileged=true \
--service-cluster-ip-range=10.0.0.0/24 \
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \
--authorization-mode=RBAC,Node \
--enable-bootstrap-token-auth=true \
--token-auth-file=/opt/kubernetes/cfg/token.csv \
--service-node-port-range=30000-32767 \
--kubelet-client-certificate=/opt/kubernetes/ssl/server.pem \
--kubelet-client-key=/opt/kubernetes/ssl/server-key.pem \
--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 \
--audit-log-maxage=30 \
--audit-log-maxbackup=3 \
--audit-log-maxsize=100 \
--audit-log-path=/opt/kubernetes/logs/k8s-audit.log"
EOF
注:上面两个 第一个是转义符,第二个是换行符,使用转义符是为了使用EOF保留换行符。
- --logtostderr:启用日志,如果为true默认将日志存放在messages里,false会将日志存放在指定位置
- --v:日志级别,数字越低生成的日志越多
- --log-dir:日志目录
- --etcd-servers:etcd集群地址
- --bind-address:监听地址
- --secure-port:https安全端口
- --advertise-address:集群通告地址
- --allow-privileged:启用授权
- --service-cluster-ip-range:Service虚拟IP地址段
- --enable-admission-plugins:准入控制模块
- --authorization-mode:认证授权,启用RBAC授权和节点自管理
- --enable-bootstrap-token-auth:启用TLS bootstrap机制
- --token-auth-file:bootstrap token文件
- --service-node-port-range:Service nodeport类型默认分配端口范围
- --kubelet-client-xxx:apiserver访问kubelet客户端证书
- --tls-xxx-file:apiserver https证书
- --etcd-xxxfile:连接Etcd集群证书
- --audit-log-path:审计日志
4、拷贝刚才生成的证书
[root@k8s-master01 bin]# cp -rf ~/k8s/k8s-cert/* /opt/kubernetes/ssl/
5、启用 TLS Bootstrapping 机制
TLS Bootstraping:Master apiserver启用TLS认证后,Node节点kubelet和kube-proxy要与kube-apiserver进行通信,必须使用CA签发的有效证书才可以,当Node节点很多时,这种客户端证书颁发需要大量工作,同样也会增加集群扩展复杂度。为了简化流程,Kubernetes引入了TLS bootstraping机制来自动颁发客户端证书,kubelet会以一个低权限用户自动向apiserver申请证书,kubelet的证书由apiserver动态签署。所以强烈建议在Node上使用这种方式,目前主要用于kubelet,kube-proxy还是由我们统一颁发一个证书。
TLS bootstraping 工作流程:
6、创建上述配置文件中token文件:
[root@k8s-master01 bin]# cat > /opt/kubernetes/cfg/token.csv << EOF
c47ffb939f5ca36231d9e3121a252940,kubelet-bootstrap,10001,"system:node-bootstrapper"
EOF
格式:token,用户名,UID,用户组
token也可自行生成替换:
head -c 16 /dev/urandom | od -An -t x | tr -d ' '
7、通过systemd来管理ApiServer:
[root@k8s-master01 bin]# cat > /usr/lib/systemd/system/kube-apiserver.service << EOF
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-apiserver.conf
ExecStart=/opt/kubernetes/bin/kube-apiserver $KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
[root@k8s-master01 bin]# systemctl daemon-reload
[root@k8s-master01 bin]# systemctl start kube-apiserver
[root@k8s-master01 bin]# systemctl enable kube-apiserver
8、授权kubelet-bootstrap用户允许请求证书
[root@k8s-master01 bin]# kubectl create clusterrolebinding kubelet-bootstrap
--clusterrole=system:node-bootstrapper
--user=kubelet-bootstrap
部署kube-controller-manager:
1、创建kube-controller-manager配置文件:
[root@k8s-master01 bin]# cat > /opt/kubernetes/cfg/kube-controller-manager.conf << EOF
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/kubernetes/logs \
--leader-elect=true \
--master=127.0.0.1:8080 \
--bind-address=127.0.0.1 \
--allocate-node-cidrs=true \
--cluster-cidr=10.244.0.0/16 \
--service-cluster-ip-range=10.0.0.0/24 \
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \
--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem \
--root-ca-file=/opt/kubernetes/ssl/ca.pem \
--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem \
--experimental-cluster-signing-duration=87600h0m0s"
EOF
- --master:通过本地非安全本地端口8080连接apiserver。
- --leader-elect:当该组件启动多个时,自动选举(HA)
- --cluster-signing-cert-file/–cluster-signing-key-file:自动为kubelet颁发证书的CA,与apiserver保持一致
2、systemd管理controller-manager
[root@k8s-master01 bin]# cat > /usr/lib/systemd/system/kube-controller-manager.service << EOF
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-controller-manager.conf
ExecStart=/opt/kubernetes/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
[root@k8s-master01 bin]# systemctl daemon-reload
[root@k8s-master01 bin]# systemctl start kube-controller-manager
[root@k8s-master01 bin]# systemctl enable kube-controller-manager
部署kube-scheduler
[root@k8s-master01 bin]# cat > /opt/kubernetes/cfg/kube-scheduler.conf << EOF
KUBE_SCHEDULER_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/kubernetes/logs \
--leader-elect \
--master=127.0.0.1:8080 \
--bind-address=127.0.0.1"
EOF
- --master:通过本地非安全本地端口8080连接apiserver。
- --leader-elect:当该组件启动多个时,自动选举(HA)
2、systemd管理scheduler
[root@k8s-master01 bin]# cat > /usr/lib/systemd/system/kube-scheduler.service << EOF
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-scheduler.conf
ExecStart=/opt/kubernetes/bin/kube-scheduler $KUBE_SCHEDULER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
[root@k8s-master01 bin]# systemctl daemon-reload
[root@k8s-master01 bin]# systemctl start kube-scheduler
[root@k8s-master01 bin]# systemctl enable kube-scheduler
查看Master集群状态:
所有组件都已经启动成功,通过kubectl工具查看当前集群组件状态:
[root@k8s-master01 bin]# kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-2 Healthy {"health":"true"}
etcd-0 Healthy {"health":"true"}
etcd-1 Healthy {"health":"true"}
如上输出说明Master节点组件运行正常。
[root@k8s-master01 cfg]# kubectl api-resources # 查看所有缩写命令,因为有的命令太长了不方便记住,所以可以通过一些简写的方式查看到自己想要的效果
部署Node1组件:
1、在所有Node节点上创建工作目录:
[root@k8s-node01 ~]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}
2、master节点拷贝node需要执行的二进制文件:
为了方便我们先操作Node1然后在吧Node1生成的内容发送给Node2.
[root@k8s-master01 bin]# cd ~/Kubernetes-tar/kubernetes/server/bin/
[root@k8s-master01 bin]# scp kubelet kube-proxy root@k8s-node01:/opt/kubernetes/bin/
[root@k8s-master01 bin]# cd ~/k8s/k8s-cert/
[root@k8s-master01 k8s-cert]# scp ca* root@k8s-node01:/opt/kubernetes/ssl/
3、Node1部署Kubelet
[root@k8s-node01 ~]# cat > /opt/kubernetes/cfg/kubelet.conf << EOF
KUBELET_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/kubernetes/logs \
--hostname-override=k8s-node01 \
--network-plugin=cni \
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \
--config=/opt/kubernetes/cfg/kubelet-config.yml \
--cert-dir=/opt/kubernetes/ssl \
--pod-infra-container-image=lizhenliang/pause-amd64:3.0"
EOF
- --hostname-override:显示名称,集群中唯一
- --network-plugin:启用CNI
- --kubeconfig:空路径,会自动生成,后面用于连接apiserver
- --bootstrap-kubeconfig:首次启动向apiserver申请证书
- --config:配置参数文件
- --cert-dir:kubelet证书生成目录
- --pod-infra-container-image:管理Pod网络容器的镜像
4、配置参数文件:
[root@k8s-node01 ~]# cat > /opt/kubernetes/cfg/kubelet-config.yml << EOF
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 0.0.0.0
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS:
- 10.0.0.2
clusterDomain: cluster.local
failSwapOn: false
authentication:
anonymous:
enabled: false
webhook:
cacheTTL: 2m0s
enabled: true
x509:
clientCAFile: /opt/kubernetes/ssl/ca.pem
authorization:
mode: Webhook
webhook:
cacheAuthorizedTTL: 5m0s
cacheUnauthorizedTTL: 30s
evictionHard:
imagefs.available: 15%
memory.available: 100Mi
nodefs.available: 10%
nodefs.inodesFree: 5%
maxOpenFiles: 1000000
maxPods: 110
EOF
5、在Master生成bootstrap.kubeconfig文件:
KUBE_APISERVER="https://172.16.3.225:6443" # apiserver IP:PORT
TOKEN="c47ffb939f5ca36231d9e3121a252940" # 与token.csv里保持一致
kubectl config set-cluster kubernetes
--certificate-authority=/opt/kubernetes/ssl/ca.pem
--embed-certs=true
--server=${KUBE_APISERVER}
--kubeconfig=bootstrap.kubeconfig
kubectl config set-credentials "kubelet-bootstrap"
--token=${TOKEN}
--kubeconfig=bootstrap.kubeconfig
kubectl config set-context default
--cluster=kubernetes
--user="kubelet-bootstrap"
--kubeconfig=bootstrap.kubeconfig
kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
将Master生成的bootstrap.kubeconfig拷贝到Node01:
[root@k8s-master01 bin]# scp bootstrap.kubeconfig root@k8s-node01:/opt/kubernetes/cfg/
6、Systemd管理kubelet:
[root@k8s-node01 ~]# cat > /usr/lib/systemd/system/kubelet.service << EOF
[Unit]
Description=Kubernetes Kubelet
After=docker.service
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet.conf
ExecStart=/opt/kubernetes/bin/kubelet $KUBELET_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
[root@k8s-node01 ~]# systemctl daemon-reload
[root@k8s-node01 ~]# systemctl start kubelet
[root@k8s-node01 ~]# systemctl enable kubelet
在Master上批准kubelet证书申请并加入集群
# 查看kubelet证书请求
[root@k8s-master01 k8s-cert]# kubectl get csr
NAME AGE SIGNERNAME REQUESTOR CONDITION
node-csr-WiJJfVJOYoNiJyXnu9NXrvhKXHCVGHSyx-IZfUVJJ8k 7s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
# 批准申请
[root@k8s-master01 k8s-cert]# kubectl certificate approve node-csr-WiJJfVJOYoNiJyXnu9NXrvhKXHCVGHSyx-IZfUVJJ8k
# 查看节点
[root@k8s-master01 k8s-cert]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-node01 NotReady <none> 17s v1.18.3
注:由于网络插件还没有部署,节点会没有准备就绪 NotReady
在Node1部署Kube-Proxy
1、创建配置文件:
[root@k8s-node01 ~]# cat > /opt/kubernetes/cfg/kube-proxy.conf << EOF
KUBE_PROXY_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/kubernetes/logs \
--config=/opt/kubernetes/cfg/kube-proxy-config.yml"
EOF
2、设置配置参数文件:
[root@k8s-node01 ~]# cat > /opt/kubernetes/cfg/kube-proxy-config.yml << EOF
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
metricsBindAddress: 0.0.0.0:10249
clientConnection:
kubeconfig: /opt/kubernetes/cfg/kube-proxy.kubeconfig
hostnameOverride: k8s-node01
clusterCIDR: 10.0.0.0/24
EOF
3、在Master上生成Kube-Proxy证书:
[root@k8s-master01 k8s-cert]# cd ~/k8s/k8s-cert/
[root@k8s-master01 k8s-cert]# 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
[root@k8s-master01 k8s-cert]# 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@k8s-master01 k8s-cert]# ls kube-proxy*pem
kube-proxy-key.pem kube-proxy.pem
4、生成kubeconfig文件:
KUBE_APISERVER="https://172.16.3.225:6443"
kubectl config set-cluster kubernetes
--certificate-authority=/opt/kubernetes/ssl/ca.pem
--embed-certs=true
--server=${KUBE_APISERVER}
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-credentials kube-proxy
--client-certificate=./kube-proxy.pem
--client-key=./kube-proxy-key.pem
--embed-certs=true
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-context default
--cluster=kubernetes
--user=kube-proxy
--kubeconfig=kube-proxy.kubeconfig
kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
将生成的kube-proxy.kubeconfig文件拷贝到配置文件指定路径:
[root@k8s-master01 k8s-cert]# scp kube-proxy.kubeconfig root@k8s-node01:/opt/kubernetes/cfg/
5、systemd管理kube-proxy
[root@k8s-node01 ~]# cat > /usr/lib/systemd/system/kube-proxy.service << EOF
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-proxy.conf
ExecStart=/opt/kubernetes/bin/kube-proxy $KUBE_PROXY_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
[root@k8s-node01 ~]# systemctl daemon-reload
[root@k8s-node01 ~]# systemctl start kube-proxy
[root@k8s-node01 ~]# systemctl enable kube-proxy
部署CNI网络
先准备好CNI二进制文件:
下载地址:https://github.com/containernetworking/plugins/releases/download/v0.8.6/cni-plugins-linux-amd64-v0.8.6.tgz
在Node01解压二进制包并移动到默认工作目录:
[root@k8s-node01 ~]# mkdir /opt/cni/bin -p
[root@k8s-node01 ~]# tar zxf cni-plugins-linux-amd64-v0.8.6.tgz -C /opt/cni/bin/
在Master部署CNI网络:
[root@k8s-master01 ~]# wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml # 注意如果wget不下来添加一下hosts
[root@k8s-master01 ~]# cat >> /etc/hosts << EOF
199.232.68.133 raw.githubusercontent.com
EOF
[root@k8s-master01 Kubernetes-tar]# kubectl apply -f kube-flannel.yml
授权apiserver访问kubelet
[root@k8s-master01 Kubernetes-tar]# cat > apiserver-to-kubelet-rbac.yaml << EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:kube-apiserver-to-kubelet
rules:
- apiGroups:
- ""
resources:
- nodes/proxy
- nodes/stats
- nodes/log
- nodes/spec
- nodes/metrics
- pods/log
verbs:
- "*"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: system:kube-apiserver
namespace: ""
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:kube-apiserver-to-kubelet
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: kubernetes
EOF
[root@k8s-master01 Kubernetes-tar]# kubectl apply -f apiserver-to-kubelet-rbac.yaml
部署好Node准备就绪:
[root@k8s-master01 ~]# kubectl get pods,node -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system pod/kube-flannel-ds-p4k7m 1/1 Running 0 4m39s
NAMESPACE NAME STATUS ROLES AGE VERSION
node/k8s-node01 Ready <none> 29m v1.18.3
将Node1的目录拷贝到Node2:
[root@k8s-node01 ~]# scp -r /opt/kubernetes/ root@k8s-node02:/opt/
[root@k8s-node01 ~]# scp -r /opt/cni/ root@k8s-node02:/opt/
[root@k8s-node01 ~]# scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@k8s-node02:/usr/lib/systemd/system/
Node2上删除kubelet证书和kubeconfig文件
[root@k8s-node02 ~]# rm -f /opt/kubernetes/cfg/kubelet.kubeconfig
[root@k8s-node02 ~]# rm -f /opt/kubernetes/ssl/kubelet*
注:这几个文件是证书申请审批后自动生成的,每个Node不同,必须删除重新生成。
修改主机名
[root@k8s-node02 ~]# vim /opt/kubernetes/cfg/kubelet.conf
--hostname-override=k8s-node02
[root@k8s-node02 ~]# vim /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: k8s-node02
[root@k8s-node02 ~]# systemctl daemon-reload
[root@k8s-node02 ~]# systemctl start kubelet
[root@k8s-node02 ~]# systemctl enable kubelet
[root@k8s-node02 ~]# systemctl start kube-proxy
[root@k8s-node02 ~]# systemctl enable kube-proxy
在Master上批准新Node kubelet证书申请
[root@k8s-master01 ~]# kubectl get csr
node-csr-ywqpEKog_klHZOEf32nQfxdEzzNlcT0xhm6bJFImB8I 54s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
[root@k8s-master01 ~]# kubectl certificate approve node-csr-ywqpEKog_klHZOEf32nQfxdEzzNlcT0xhm6bJFImB8I
查看Node状态:
[root@k8s-master01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-node01 Ready <none> 78m v1.18.3
k8s-node02 Ready <none> 21s v1.18.3
部署CoreDNS
CoreDNS用于集群内部Service名称解析。
[root@k8s-master01 Kubernetes-tar]# kubectl apply -f coredns.yaml
[root@k8s-master01 Kubernetes-tar]# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-5ffbfd976d-gl79l 1/1 Running 0 35s
kube-system kube-flannel-ds-p4k7m 1/1 Running 0 54m
kube-system kube-flannel-ds-qvgpd 1/1 Running 0 112s
DNS解析测试:
[root@k8s-master01 Kubernetes-tar]# kubectl run -it --rm dns-test --image=busybox:1.28.4 sh
If you don't see a command prompt, try pressing enter.
/ # nslookup kubernetes
Server: 10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local
Name: kubernetes
Address 1: 10.0.0.1 kubernetes.default.svc.cluster.local
解析没问题。
最后
以上就是勤奋香水为你收集整理的Kubernetes 1.18单节点二进制部署实验环境:实验步骤:的全部内容,希望文章能够帮你解决Kubernetes 1.18单节点二进制部署实验环境:实验步骤:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复