Linux:CentOS Linux release 7.6.1810 (Core)
Container runtimes : docker-20.10.9
Etcd : v3.5.1
Kubernetes : v1.23.1
cfssl
cfssljson
cfssl-certinfo
CPU:2核
内存:2G
硬盘::20G
必须要有网络
服务器必要安装
yum install vim
yum install wget
yum install net-tools
虚拟机初始化
1.vi /etc/sysconfig/network-scripts/ifcfg-ens33
2.重启网络
service network restart
3.ping测试
ping www.baidu.com
##修改主机名
[root@master ~]# hostnamectl set-hostname master #master节点
[root@node1 ~]# hostnamectl set-hostname node1 #node1节点
[root@node2 ~]# hostnamectl set-hostname node2 #node2节点
##三台服务器的地址和主机名添加到三个节点的/etc/hosts文件中
##master节点执行##
[root@master ~]# cat >> /etc/hosts << EOF
192.168.10.210 master
192.168.10.211 node1
192.168.10.212 node2
EOF
##node1节点执行##
[root@node1 ~]# cat >> /etc/hosts << EOF
192.168.10.210 master
192.168.10.211 node1
192.168.10.212 node2
EOF
##node2节点执行##
[root@node2 ~]# cat >> /etc/hosts << EOF
192.168.10.210 master
192.168.10.211 node1
192.168.10.212 node2
EOF
##kubernetes要求集群中的节点时间必须精确一直,这里使用chronyd服务从网络同步时间
##启动chronyd服务,设置开机启动
##master节点执行##
[root@master ~]# systemctl start chronyd && systemctl enable chronyd
##node1节点执行##
[root@node1 ~]# systemctl start chronyd && systemctl enable chronyd
##node2节点执行##
[root@node2 ~]# systemctl start chronyd && systemctl enable chronyd
##核验时间,查看三个节点时间是否同步
##master节点执行##
[root@master ~]# date
##node1节点执行##
[root@node1 ~]# date
##node2节点执行##
[root@node2 ~]# date
##kubernetes和docker 在运行的中会产生大量的iptables规则,为了不让系统规则跟它们混淆,直接关闭系统的规则
##关闭firewalld服务
##master节点执行##
[root@master ~]# systemctl stop firewalld && systemctl disable firewalld
##node1节点执行##
[root@node1 ~]# systemctl stop firewalld && systemctl disable firewalld
##node2节点执行##
[root@node2 ~]# systemctl stop firewalld && systemctl disable firewalld
##关闭iptables服务
##master节点执行##
[root@master ~]# systemctl stop iptables && systemctl disable iptables
##node1节点执行##
[root@node1 ~]# systemctl stop iptables && systemctl disable iptables
##node2节点执行##
[root@node2 ~]# systemctl stop iptables && systemctl disable iptables
##selinux是linux系统下的一个安全服务,如果不关闭它,在安装集群中会产生各种各样的奇葩问题
##永久关闭
##master节点执行##
[root@master ~]# sed -i ‘s/enforcing/disabled/’ /etc/selinux/config
##node1节点执行##
[root@node1 ~]# sed -i ‘s/enforcing/disabled/’ /etc/selinux/config
##node2节点执行##
[root@node2 ~]# sed -i ‘s/enforcing/disabled/’ /etc/selinux/config
##临时关闭
##master节点执行##
[root@master ~]# setenforce 0
##node1节点执行##
[root@node1 ~]# setenforce 0
##node2节点执行##
[root@node2 ~]# setenforce 0
##swap分区指的是虚拟内存分区,它的作用是物理内存使用完,之后将磁盘空间虚拟成内存来使用,启用swap设备会对系统的性能产生非常负面的影响,因此kubernetes要求每个节点都要禁用swap设备,但是如果因为某些原因确实不能关闭swap分区,就需要在集群安装过程中通过明确的参数进行配置说明
##master节点执行##
[root@master ~]# vim /etc/fstab
##node1节点执行##
[root@node1 ~]# vim /etc/fstab
##node2节点执行##
[root@node2 ~]# vim /etc/fstab
##临时关闭
##master节点执行##
[root@master ~]# swapoff -a
##node1节点执行##
[root@node1 ~]# swapoff -a
##node2节点执行##
[root@node2 ~]# swapoff -a
##加载网桥过滤模块
##master节点执行##
[root@master ~]# cat <
EOF
##node1节点执行##
[root@node1 ~]# cat <
EOF
##node2节点执行##
[root@node2 ~]# cat <
EOF
##修改内核参数
##node1节点执行##
[root@master ~]# cat <
net.bridge.bridge-nf-call-iptables = 1
EOF
##node1节点执行##
[root@node1 ~]# cat <
net.bridge.bridge-nf-call-iptables = 1
EOF
##node2节点执行##
[root@node2 ~]# cat <
net.bridge.bridge-nf-call-iptables = 1
EOF
##配置生效
##master节点执行##
[root@master ~]# sysctl --system
##node1节点执行##
[root@node1 ~]# sysctl --system
##node2节点执行##
[root@node2 ~]# sysctl --system
操作对象:所有节点
##master节点执行操作##
##在master主机创建目录存放二进制软件包
[root@master ~]# mkdir -p /usr/local/k8s
[root@master ~]# cd /usr/local/k8s
##下载docker包
[root@master ~]# wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.9.tgz
##如果不想执行wget获取安装包,可以直接用下方提供的安装包
百度网盘链接:https://pan.baidu.com/s/1N2cxoDuGPw6zYr35TYxQ8w
提取码:vgcc
##解压
[root@master ~]# tar zxvf docker-20.10.9.tgz
##复制到本地运行目录
[root@master ~]# mv docker/* /usr/bin
###创建docker.service脚本
[root@master ~]# cat > /usr/lib/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
#在所有节点主机创建docker配置文件默认的目录
[root@master ~]# mkdir /etc/docker
[root@master ~]# cat > /etc/docker/daemon.json<< EOF
{
“registry-mirrors”: [“https://b9pmyelo.mirror.aliyuncs.com”],
“exec-opts”: [“native.cgroupdriver=systemd”],
“log-driver”: “json-file”,
“log-opts”: {
“max-size”: “100m”
},
“storage-driver”: “overlay2”
}
EOF
[root@master ~]# systemctl daemon-reload && systemctl start docker && systemctl enable docker
#查看docker启动状态
[root@master ~]# systemctl status docker
在master节点中将docker配置文件复制到其他节点
##复制到node1节点
[root@master ~]# scp -r /usr/bin/{containerd,containerd-shim,containerd-shim-runc-v2,ctr,docker,dockerd,docker-init,docker-proxy,runc} root@node1:/usr/bin/
[root@master ~]# scp /usr/lib/systemd/system/docker.service root@node1:/usr/lib/systemd/system/
[root@master ~]# scp -r /etc/docker/ root@node1:/etc/
##复制到node2节点
[root@master ~]# scp -r /usr/bin/{containerd,containerd-shim,containerd-shim-runc-v2,ctr,docker,dockerd,docker-init,docker-proxy,runc} root@node2:/usr/bin/
[root@master ~]# scp /usr/lib/systemd/system/docker.service root@node2:/usr/lib/systemd/system/
[root@master ~]# scp -r /etc/docker/ root@node2:/etc/
##node1节点执行操作##
[root@node1~]# cd /etc/docker
##删除从master节点拷贝过来的key.json文件,启动docker会自动生成
[root@node1~]# rm key.json
##加载配置文件、启动docker、设置开机自启
[root@node1~]# systemctl daemon-reload && systemctl start docker && systemctl enable docker
##node2节点执行操作##
[root@node2~]# cd /etc/docker
##删除从master节点拷贝过来的key.json文件,启动docker会自动生成
[root@node2~]# rm key.json
##加载配置文件、启动docker、设置开机自启
[root@node2~]# systemctl daemon-reload && systemctl start docker && systemctl enable docker
操作对象:master节点
##master节点执行操作##
[root@master ~]# cd /usr/local/k8s
##(1)创建下载配置文件
[root@master ~]# cat > cfssl << EOF
https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
EOF
##(2)下载
[root@master ~]# wget -i cfssl
##(3)赋予可执行的权限
[root@master ~]# chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
##(4)移动cfssl文件
[root@master ~]# mv cfssl_linux-amd64 /usr/local/bin/cfssl
[root@master ~]# mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
[root@master ~]# mv cfssl-certinfo_linux-amd64 /usr/local/bin/cfssl-certinfo
##如果不想执行(1)~(4)操作步骤,可将如下三个文件直接放入到usr/local/bin当中
链接:https://pan.baidu.com/s/1-PNvV4-4DI8cQWEIs7MFVw
提取码:i7qo
##创建证书生成的工作目录(后面相关证书的生成都在此目录下操作)
[root@master ~]# mkdir -p ~/TLS/{etcd,k8s}
[root@master ~]# cd ~/TLS/etcd
[root@master ~]# cat > ca-config.json << EOF
{
“signing”: {
“default”: {
“expiry”: “87600h”
},
“profiles”: {
“www”: {
“expiry”: “87600h”,
“usages”: [
“signing”,
“key encipherment”,
“server auth”,
“client auth”
]
}
}
}
}
EOF
##创建ca-csr配置文件
[root@master ~]# cat > ca-csr.json << EOF
{
“CN”: “etcd CA”,
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“L”: “Beijing”,
“ST”: “Beijing”
}
]
}
EOF
[root@master ~]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca –
##会生成ca.pem和ca-key.pem文件
[root@master ~]# ls *pem
##创建证书申请文件
[root@master ~]# cat > server-csr.json << EOF
{
“CN”: “etcd”,
“hosts”: [
“10.27.134.250”, # 修改为自己的ip地址
“10.27.134.251”, # 修改为自己的ip地址
“10.27.134.252” # 修改为自己的ip地址
],
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“L”: “BeiJing”,
“ST”: “BeiJing”
}
]
}
EOF
##生成HTTPS证书
[root@master ~]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
##会生成server.pem和server-key.pem文件
[root@master ~]# ls server*pem
##master节点操作
[root@master ~]# cd /usr/local/k8s
[root@master ~]# wget https://github.com/etcd-io/etcd/releases/download/v3.5.1/etcd-v3.5.1-linux-amd64.tar.gz
##不想wget从网上下载文件,可以直接用如下文件
链接:https://pan.baidu.com/s/1IWWjnoZhYyV4GXQIMJryEw
提取码:5sct
##创建etcd的工作目录
[root@master ~]# mkdir -p /opt/etcd/{bin,cfg,ssl}
##解压
[root@master ~]# tar zxvf etcd-v3.5.1-linux-amd64.tar.gz
##将二进制文件复制到工作目录
[root@master ~]# cp etcd-v3.5.1-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
##根据实际情况修改节点地址
[root@master ~]# 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://192.168.10.210:2380”
ETCD_LISTEN_CLIENT_URLS=“https://192.168.10.210:2379”
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS=“https://192.168.10.210:2380”
ETCD_ADVERTISE_CLIENT_URLS=“https://192.168.10.210:2379”
ETCD_INITIAL_CLUSTER=“etcd-1=https://192.168.10.210:2380,etcd-2=https://192.168.10.211:2380,etcd-3=https://192.168.10.212:2380”
ETCD_INITIAL_CLUSTER_TOKEN=“etcd-cluster”
ETCD_INITIAL_CLUSTER_STATE=“new”
ETCD_ENABLE_V2=“true”
EOF
##解释:
ETCD_NAME:节点名称,集群中唯一
ETCD_DATA_DIR:数据目录
ETCD_LISTEN_PEER_URLS:集群通信监听地址
ETCD_LISTEN_CLIENT_URLS:客户端访问监听地址
ETCD_INITIAL_ADVERTISE_PEERURLS:集群通告地址
ETCD_ADVERTISE_CLIENT_URLS:客户端通告地址
ETCD_INITIAL_CLUSTER:集群节点地址
ETCD_INITIALCLUSTER_TOKEN:集群Token
ETCD_INITIALCLUSTER_STATE:加入集群的当前状态,new是新集群,existing表示加入已有集群
##将之前生成的证书复制到etcd的工作目录
[root@master ~]# cp ~/TLS/etcd/capem ~/TLS/etcd/serverpem /opt/etcd/ssl
[root@master ~]# 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
##复制到node1
[root@master ~]# scp -r /opt/etcd/ root@node1:/opt/
[root@master ~]# scp /usr/lib/systemd/system/etcd.service root@node1:/usr/lib/systemd/system/
##复制到node2
[root@master ~]# scp -r /opt/etcd/ root@node2:/opt/
[root@master ~]# scp /usr/lib/systemd/system/etcd.service root@node2:/usr/lib/systemd/system/
##node1节点操作
##修改etcd.conf 配置文件中的节点名称和节点地址
[root@node1~]# vim /opt/etcd/cfg/etcd.conf
##node2节点操作
##修改etcd.conf 配置文件中的节点名称和节点地址
[root@node2 ~]# vim /opt/etcd/cfg/etcd.conf
##启动etcd服务并设置开机启动,若在node上启动失败,可以先启动master。
##master节点操作
[root@master~]# systemctl daemon-reload && systemctl start etcd && systemctl enable etcd
##node1节点操作
[root@node1~]# systemctl daemon-reload && systemctl start etcd && systemctl enable etcd
##node2节点操作
[root@node2~]# systemctl daemon-reload && systemctl start etcd && systemctl enable etcd
##检查集群状态
##master节点操作
##注意换成自己的IP地址
[root@master~]# 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://192.168.10.210:2379,https://192.168.10.211:2379,https://192.168.10.212:2379” endpoint health --write-out=table
##master节点执行##
(1)创建自签证书颁发机构(CA)配置
[root@master~]# cd ~/TLS/k8s
##创建ca-config配置文件
[root@master~]# cat > ca-config.json << EOF
{
“signing”: {
“default”: {
“expiry”: “87600h”
},
“profiles”: {
“kubernetes”: {
“expiry”: “87600h”,
“usages”: [
“signing”,
“key encipherment”,
“server auth”,
“client auth”
]
}
}
}
}
EOF
##创建ca-csr配置文件
[root@master~]# cat > ca-csr.json << EOF
{
“CN”: “kubernetes”,
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“L”: “Beijing”,
“ST”: “Beijing”,
“O”: “k8s”,
“OU”: “System”
}
]
}
EOF
(2) 生成证书
[root@master~]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
##会生成ca.pem和ca-key.pem文件
[root@master~]# ls *pem
(3) 使用自签证书签发kube-apiserver HTTPS证书
##创建证书申请文件
[root@master~]# cat > server-csr.json << EOF
{
“CN”: “kubernetes”,
“hosts”: [
“10.0.0.1”,
“127.0.0.1”,
“192.168.10.210”, # master节点ip
“192.168.10.211”, # node1节点ip
“192.168.10.212”, # node2节点ip
“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
(4)生成https证书
[root@master~]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
##会生成server.pem和server-key.pem文件
[root@master~]# ls server*pem
[root@master~]# cd /usr/local/k8s/
##下载
[root@master~]# wget https://dl.k8s.io/v1.23.1/kubernetes-server-linux-amd64.tar.gz
##如果不想wget下载,可以直接用下方包
链接:https://pan.baidu.com/s/1E53P-3aCb4Q48yP-0MFK2Q
提取码:kh1l
##解压
[root@master~]# tar zxvf kubernetes-server-linux-amd64.tar.gz
##创建kubernetes工作目录
[root@master~]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}
##从软件包内拷贝组件
[root@master~]# cd /usr/local/k8s/kubernetes/server/bin
[root@master~]# cp kube-apiserver kube-scheduler kube-controller-manager /opt/kubernetes/bin
[root@master~]# cp kubectl /usr/bin/
[root@master~]# echo $(head -c 16 /dev/urandom | od -An -t x | tr -d ’ '),kubelet-bootstrap,10001,“system:kubelet-bootstrap” > /opt/kubernetes/cfg/token.csv
[root@master~]# cat > /opt/kubernetes/cfg/kube-apiserver.conf << EOF
KUBE_APISERVER_OPTS=“–logtostderr=false
–v=2
–log-dir=/opt/kubernetes/logs
–etcd-servers=https://192.168.10.210:2379,https://192.168.10.211:2379,https://192.168.10.212:2379
–bind-address=192.168.10.210
–secure-port=6443
–advertise-address=192.168.10.210
–allow-privileged=true
–service-cluster-ip-range=10.0.0.0/24
–enable-admission-plugins=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
–service-account-issuer=api
–service-account-signing-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
–requestheader-client-ca-file=/opt/kubernetes/ssl/ca.pem
–proxy-client-cert-file=/opt/kubernetes/ssl/server.pem
–proxy-client-key-file=/opt/kubernetes/ssl/server-key.pem
–enable-aggregator-routing=true
–audit-log-maxage=30
–audit-log-maxbackup=3
–audit-log-maxsize=100
–audit-log-path=/opt/kubernetes/logs/k8s-audit.log”
##配置参数解释
–logtostderr:启用日志
––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-xxx:审计日志
–service-account-issuer=string 指定service account token issuer的标识符;该issuer将在iss声明中分发Token以使标识符生效;该参数的值为字符串或URL
–service-account-signing-key-file=string含有当前service account token issuer私钥的文件路径 ;该issuer将以此私钥签名发布的ID token(需开启TokenRequest特性)
[root@master~]# cp ~/TLS/k8s/capem ~/TLS/k8s/serverpem /opt/kubernetes/ssl
[root@master~]# 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@master~]# systemctl daemon-reload && systemctl start kube-apiserver && systemctl enable kube-apiserver
##查看状态
[root@master~]# systemctl status kube-apiserver
[root@master~]# cd ~/TLS/k8s
[root@master~]# cat > kube-controller-manager-csr.json << EOF
{
“CN”: “system:kube-controller-manager”,
“hosts”: [],
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“L”: “BeiJing”,
“ST”: “BeiJing”,
“O”: “system:masters”,
“OU”: “System”
}
]
}
EOF
[root@master~]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-controller-manager-csr.json | cfssljson -bare kube-controller-manager
[root@master~]# KUBE_CONFIG=“/opt/kubernetes/cfg/kube-controller-manager.kubeconfig”
[root@master~]# KUBE_APISERVER=“https://192.168.10.210:6443”
[root@master~]# kubectl config set-cluster kubernetes
–certificate-authority=/opt/kubernetes/ssl/ca.pem
–embed-certs=true
–server=KaTeX parse error: Undefined control sequence: \ at position 18: …UBE_APISERVER} \̲ ̲ --kubeconfig={KUBE_CONFIG}
[root@master~]# kubectl config set-credentials kube-controller-manager
–client-certificate=./kube-controller-manager.pem
–client-key=./kube-controller-manager-key.pem
–embed-certs=true
–kubeconfig=${KUBE_CONFIG}
[root@master~]# kubectl config set-context default
–cluster=kubernetes
–user=kube-controller-manager
–kubeconfig=${KUBE_CONFIG}
[root@master~]# kubectl config use-context default --kubeconfig=${KUBE_CONFIG}
[root@master~]# 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 \
–kubeconfig=/opt/kubernetes/cfg/kube-controller-manager.kubeconfig \
–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 \
–cluster-signing-duration=87600h0m0s”
EOF
##解释:
–master:通过本地非安全本地端口8080 连接apiserver
–leader-elect:当该组件启动多个时,自动选举(HA)
–cluster-signing-cert-file/–cluster-signing-key-file:自动为kubelet 颁发证书的CA,与apiserver 保持一致
[root@master~]# 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@master~]# systemctl daemon-reload && systemctl start kube-controller-manager && systemctl enable kube-controller-manager
[root@master~]# cd ~/TLS/k8s
[root@master~]# cat > kube-scheduler-csr.json << EOF
{
“CN”: “system:kube-scheduler”,
“hosts”: [],
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“L”: “BeiJing”,
“ST”: “BeiJing”,
“O”: “system:masters”,
“OU”: “System”
}
]
}
EOF
[root@master~]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-scheduler-csr.json | cfssljson -bare kube-scheduler
[root@master~]# KUBE_CONFIG=“/opt/kubernetes/cfg/kube-scheduler.kubeconfig”
[root@master~]# KUBE_APISERVER=“https://10.27.134.250:6443”
[root@master~]# kubectl config set-cluster kubernetes
–certificate-authority=/opt/kubernetes/ssl/ca.pem
–embed-certs=true
–server=KaTeX parse error: Undefined control sequence: \ at position 18: …UBE_APISERVER} \̲ ̲ --kubeconfig={KUBE_CONFIG}
[root@master~]# kubectl config set-credentials kube-scheduler
–client-certificate=./kube-scheduler.pem
–client-key=./kube-scheduler-key.pem
–embed-certs=true
–kubeconfig=${KUBE_CONFIG}
[root@master~]# kubectl config set-context default
–cluster=kubernetes
–user=kube-scheduler
–kubeconfig=${KUBE_CONFIG}
[root@master~]# kubectl config use-context default --kubeconfig=${KUBE_CONFIG}
[root@master~]# cat > /opt/kubernetes/cfg/kube-scheduler.conf << EOF
KUBE_SCHEDULER_OPTS=“–logtostderr=false \
–v=2 \
–log-dir=/opt/kubernetes/logs \
–leader-elect \
–kubeconfig=/opt/kubernetes/cfg/kube-scheduler.kubeconfig \
–bind-address=127.0.0.1”
EOF
##解释:
–master:通过本地非安全本地端口8080 连接apiserver。
–leader-elect:当该组件启动多个时,自动选举(HA)
[root@master~]# 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@master~]# systemctl daemon-reload && systemctl start kube-scheduler && systemctl enable kube-scheduler
##创建证书申请文件
[root@master~]# cat > admin-csr.json <
“CN”: “admin”,
“hosts”: [],
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“L”: “BeiJing”,
“ST”: “BeiJing”,
“O”: “system:masters”,
“OU”: “System”
}
]
}
EOF
[root@master~]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
##创建配置文件位置,如果提示文件夹已存在,则忽略
[root@master~]# mkdir /root/.kube
##创建配置文件(注意:此时路径应该是要在 /usr/local/k8s下)
[root@master~]# KUBE_CONFIG=“/root/.kube/config”
[root@master~]# KUBE_APISERVER=“https://10.27.134.250:6443”
[root@master~]# kubectl config set-cluster kubernetes
–certificate-authority=/opt/kubernetes/ssl/ca.pem
–embed-certs=true
–server=KaTeX parse error: Undefined control sequence: \ at position 18: …UBE_APISERVER} \̲ ̲ --kubeconfig={KUBE_CONFIG}
[root@master~]# kubectl config set-credentials cluster-admin
–client-certificate=./admin.pem
–client-key=./admin-key.pem
–embed-certs=true
–kubeconfig=${KUBE_CONFIG}
[root@master~]# kubectl config set-context default
–cluster=kubernetes
–user=cluster-admin
–kubeconfig=${KUBE_CONFIG}
[root@master~]# kubectl config use-context default --kubeconfig=${KUBE_CONFIG}
[root@master~]# kubectl get cs
[root@master~]# kubectl create clusterrolebinding kubelet-bootstrap
–clusterrole=system:node-bootstrapper
–user=kubelet-bootstrap
##master节点也部署成worker节点,一并参加调度
##在node1中节点操作,创建目录
[root@node1 ~]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}
##在node2中节点操作,创建目录
[root@node2 ~]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}
##在master中操作
##进入解压的kubernetes文件夹中
[root@master~]# cd /usr/local/k8s/kubernetes/server/bin
##将kubelet、kube-proxy组件拷贝至本地文件中
[root@master~]# cp kubelet kube-proxy /opt/kubernetes/bin
[root@master~]# cat > /opt/kubernetes/cfg/kubelet.conf << EOF
KUBELET_OPTS=“–logtostderr=false \
–v=2 \
–log-dir=/opt/kubernetes/logs \
–hostname-override=master \
–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网络容器的镜像
[root@master~]# 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: systemd
clusterDNS:
[root@master~]# KUBE_CONFIG=“/opt/kubernetes/cfg/bootstrap.kubeconfig”
[root@master~]# KUBE_APISERVER=https://192.168.10.210:6443
##TOKEN值与token.csv里保持一致
##查看token值
[root@master~]# cat /opt/kubernetes/cfg/token.csv
[root@master~]# TOKEN=“dd26e9d22897822de8f2926b338988ab”
##生成 kubelet bootstrap kubeconfig 配置文件
[root@master~]# kubectl config set-cluster kubernetes
–certificate-authority=/opt/kubernetes/ssl/ca.pem
–embed-certs=true
–server=KaTeX parse error: Undefined control sequence: \ at position 18: …UBE_APISERVER} \̲ ̲ --kubeconfig={KUBE_CONFIG}
[root@master~]# kubectl config set-credentials “kubelet-bootstrap”
–token=KaTeX parse error: Undefined control sequence: \ at position 9: {TOKEN} \̲ ̲ --kubeconfig={KUBE_CONFIG}
[root@master~]# kubectl config set-context default
–cluster=kubernetes
–user=“kubelet-bootstrap”
–kubeconfig=${KUBE_CONFIG}
[root@master~]# kubectl config use-context default --kubeconfig=${KUBE_CONFIG}
[root@master~]# 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@master~]# systemctl daemon-reload && systemctl start kubelet && systemctl enable kubelet
##注意:kubernetes官方推荐docker等使用systemd作为Cgroup Driver
##如果kubelet服务状态异常,查看日志是cgroupDriver不匹配的话
##先查看docker使用的Cgroup Driver
[root@master~]# docker info|grep “Cgroup Driver”
##再修改kubelet的yaml文件中Cgroup Driver的参数
##每个节点部署申请后都需被批准才能加入集群
(1)在master节点查看 kubelet 证书请求
[root@master~]# kubectl get csr
(2)批准申请
[root@master~]# kubectl certificate approve node-csr-31V7IbsN3zDicNmLUTyIs-s9uv9biTzpkMRE6vPrx7w
(3)查看节点
[root@master~]# kubectl get nodes
[root@master~]# cd ~/TLS/k8s
[root@master~]# 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@master~]# 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@master~]# 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
[root@master~]# 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: master
clusterCIDR: 10.244.0.0/16
EOF
[root@master~]# KUBE_CONFIG=“/opt/kubernetes/cfg/kube-proxy.kubeconfig”
[root@master~]# KUBE_APISERVER=“https://10.27.134.250:6443”
[root@master~]# kubectl config set-cluster kubernetes
–certificate-authority=/opt/kubernetes/ssl/ca.pem
–embed-certs=true
–server=KaTeX parse error: Undefined control sequence: \ at position 18: …UBE_APISERVER} \̲ ̲ --kubeconfig={KUBE_CONFIG}
[root@master~]# kubectl config set-credentials kube-proxy
–client-certificate=./kube-proxy.pem
–client-key=./kube-proxy-key.pem
–embed-certs=true
–kubeconfig=${KUBE_CONFIG}
[root@master~]# kubectl config set-context default
–cluster=kubernetes
–user=kube-proxy
–kubeconfig=KaTeX parse error: Expected 'EOF', got '#' at position 29: … [root@master~]#̲ kubectl config…{KUBE_CONFIG}
1.3.4.4.5 systemd管理kube-proxy
[root@master~]# 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@master~]# systemctl daemon-reload && systemctl start kube-proxy && systemctl enable kube-proxy
##Calico是一个纯三层的数据中心网络方案,是目前Kubernetes的主流方案
(1)下载配置文件
[root@master~]# cd /usr/local/k8s
[root@master~]# wget https://docs.projectcalico.org/v3.20/manifests/calico.yaml --no-check-certificate
##不想下载或者无法下载可以直接用提供的资源
链接:https://pan.baidu.com/s/1h0FLAXjLP1h4GmniRW3vXA
提取码:03kt
(2)应用yaml文件
[root@master~]# kubectl apply -f calico.yaml
(3)在apply或create命令无法下载镜像时通过docker pull 来拉取镜像
[root@master~]# cat calico.yaml |grep image
(1)查看部署结果(需要等待一会时间)
[root@master~]# kubectl get pods -n kube-system
(2)结果实时查看
[root@master~]# kubectl get pods -n kube-system -w
(3)查看节点状态(需要等待一会时间状态才会变)
[root@master~]# kubectl get node
应用场景:例如kubectl logs
[root@master~]# cd /usr/local/k8s/
[root@master~]# 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:
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:
##应用yaml文件
[root@master~]# kubectl apply -f apiserver-to-kubelet-rbac.yaml
##查看应用
[root@master~]# kubectl get ClusterRole
##在Master节点将Worker Node涉及的文件拷贝到新节点node1、node2
####部署Node1节点####
###master节点操作###
[root@master~]# scp -r /opt/kubernetes root@node1:/opt/
[root@master~]# scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@node1:/usr/lib/systemd/system
[root@master~]# scp /opt/kubernetes/ssl/ca.pem root@node1:/opt/kubernetes/ssl
###node1节点操作###
[root@node1~]# rm -f /opt/kubernetes/cfg/kubelet.kubeconfig
[root@node1~]# rm -f /opt/kubernetes/ssl/kubelet*
注:这几个文件是证书申请审批后自动生成的,每个Node不同,必须删除
否则master节点无法获得node节点信息
[root@node1~]# vim /opt/kubernetes/cfg/kubelet.conf
–hostname-override=node1
[root@node1~]# vim /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: node1
[root@node1~]# systemctl daemon-reload && systemctl start kubelet kube-proxy && systemctl enable kubelet kube-proxy
##查看证书请求
###master节点操作###
[root@master~]# kubectl get csr
##授权请求
[root@master~]# kubectl certificate approve node-csr-31V7IbsN3zDicNmLUTyIs-s9uv9biTzpkMRE6vPrx7w
[root@master~]# kubectl get node
####部署Node2节点####
###master节点操作###
[root@master~]# scp -r /opt/kubernetes root@node2:/opt/
[root@master~]# scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@node2:/usr/lib/systemd/system
[root@master~]# scp /opt/kubernetes/ssl/ca.pem root@node2:/opt/kubernetes/ssl
###node2节点操作###
[root@node2~]# rm -f /opt/kubernetes/cfg/kubelet.kubeconfig
[root@node2~]# rm -f /opt/kubernetes/ssl/kubelet*
注:这几个文件是证书申请审批后自动生成的,每个Node不同,必须删除
否则master节点无法获得node节点信息
[root@node2~]# vim /opt/kubernetes/cfg/kubelet.conf
–hostname-override=node2
[root@node2~]# vim /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: node2
[root@node2~]# systemctl daemon-reload && systemctl start kubelet kube-proxy && systemctl enable kubelet kube-proxy
##查看证书请求
###master节点操作###
[root@master~]# kubectl get csr
##授权请求
[root@master~]# kubectl certificate approve node-csr-31V7IbsN3zDicNmLUTyIs-s9uv9biTzpkMRE6vPrx7w
[root@master~]# kubectl get node
##其他更多节点同上。记得删除kubelet证书、配置文件、修改主机名!
在 Kubernetes 集群中创建一个 pod,验证是否正常运行【master 节点操作】:
##下载 nginx 【会联网拉取 nginx 镜像】
[root@master~]# kubectl create deployment nginx --image=nginx
##查看状态
[root@master~]# kubectl get pod
如果我们出现 Running 状态的时候,表示已经成功运行了
下面我们就需要将端口暴露出去,让其它外界能够访问
##暴露端口
[root@master~]# kubectl expose deployment nginx --port=80 --type=NodePort
##查看一下对外的端口
[root@master~]# kubectl get pod,svc
能够看到,我们已经成功暴露了 80 端口 到 30699 上
我们到我们的宿主机浏览器上,访问如下地址
http://192.168.10.210:30699/
发现我们的 nginx 已经成功启动了
##下载yml文件
###master节点操作###
[root@master~]# wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc5/aio/deploy/recommended.yaml
##不想下载或者无法下载可以直接用提供的资源
链接:https://pan.baidu.com/s/12IeNk0JGp_mSCAB1oBcWhw
提取码:nfp3
##修改yam文件,service改为NodePort类型
##创建服务
[root@master~]# kubectl create -f recommended.yaml
##查看服务对外暴露的端口号
[root@master~]# kubectl get svc -n kubernetes-dashboard
##创建admin.conf配置文件
[root@master~]# cat > /opt/kubernetes/cfg/admin.conf << EOF
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: admin
annotations:
rbac.authorization.kubernetes.io/autoupdate: “true”
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
subjects:
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin
namespace: kubernetes-dashboard
EOF
##查看命名空间内的加密信息是否有token和名称
[root@master~]# kubectl get secret -n kubernetes-dashboard
##使用该命令拿到token,替换上条命令查询的名称default-token-9xlqd,然后粘贴到浏览器中进行登录
[root@master~]# kubectl -n kubernetes-dashboard get secret default-token-fb59s -o jsonpath={.data.token}|base64 -d
##使用token登录dashboard