k8s的二进制部署
k8s的二进制部署的实验:
源码包部署
k8s的master01:192.168.233.91
组件:kube-apiserver kube-controller-manager kube-scheduler etcd
k8s的master02:192.168.233.92
组件:kube-apiserver kube-controller-manager kube-scheduler
node节点01:192.168.233.93 kubelet kube-proxy etcd
node节点02:192.168.233.94 kubelet kube-proxy etcd
负载均衡:nginx+keepalived:master:192.168.233.95
backup:192.168.233.96
etcd:192.168.233.91
192.168.233.93
192.168.233.94
91、93、94主机:

清空iptables的所有策略

关闭交换分区
swapoff -a

k8s在设计时,为了提升性能,默认是不使用swap交换分区的,kubenetes在初始化时,会检测swap是否关闭
91:

93:

94:

91、93、94:



#开启网桥模式
net.bridge.brideg-nf-call-ip6tables=1
net.bridge.brideg-nf-call-iptables=1
#网桥的流量传给iptables链,实现地址映射
#关闭ipv6的流量(可关也可以不关)
net.ipv6.conf.all.disable_ipv6=1
#根据工作中的实际情况,自定
net.ipv4.ip_forward=1


时间同步
yum install ntpdate -y
ntpdate ntp.aliyun.com


date

部署 docker引擎
91、93、94:
yum install -y yum-utils device-mapper-persistent-data lvm2

yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker.service
systemctl enable docker.service

部署 etcd 集群
(部署第一个组件,存储k8s的集群信息和用户配置组件)
(etcd是一个高可用----分布式的键值存储数据库,采用raft算法保证节点的信息一致性。etcd是go语言写的)
(etcd的端口:2379:api接口,对外为客户端提供通信
2380:内部服务的通信端口
etcd一般都是集群部署,etcd也有选举leader的机制,至少要三台,或者奇数台)
k8s的内部通信依靠证书认证,密钥认证:证书的签发环境
91:
把三个证书拖进来

sfssl:证书签发的命令工具
cfssl-certinfo:查看证书信息的工具
cfssljson:把证书的格式转化成json格式,变成文件的承载式证书
移到bin目录下面
mv cfssl-certinfo cfssljson cfssl /usr/local/bin/

chmod 777 /usr/local/bin/cfssl*

cd到opt,然后创建一个mkdir k8s的目录
mkdir /opt/k8s


拖两个证书包到k8s

vim etcd-cert.sh

记得改文件里的IP地址

赋权
chmod 777 etcd-cert.sh etcd.sh

mkdir /opt/k8s/etcd-cert

mv etcd-cert.sh etcd-cert

cd etcd-cert/
./etcd-cert.sh

ca-config.json:配置了证书生成策略,定义了默认过期时间和一个名为 "www" 的配置模板
ca-csr.json:用于生成根证书和私钥的签名请求文件。包括了Common Name(CN),密钥算法和一些组织信息
ca.pem:根证书文件
server-csr:服务器证书签名请求文件
server-key.pem:生成etcd服务器证书和私钥
server.pem:etcd服务器的证书文件,用于加密和认证etcd节点之间的通信


tar zxvf etcd-v3.4.9-linux-amd64.tar.gz






cd /opt/k8s/etcd-cert/
cp *.pem /opt/etcd/ssl/



./etcd.sh etcd01 192.168.233.31 etcd02=https://192.168.233.33:2380,etcd03=https://192.168.233.34:2380

再开一台91的终端:
传参到93和94的主机
scp -r /opt/etcd/ root@192.168.233.93:/opt/

scp -r /opt/etcd/ root@192.168.233.94:/opt/

scp /usr/lib/systemd/system/etcd.service root@192.168.233.93:/usr/lib/systemd/system/

scp /usr/lib/systemd/system/etcd.service root@192.168.233.94:/usr/lib/systemd/system/

93:



94:



91、93、94:
(谁先启动谁是主)


91:
检查etcd群集状态
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.233.91:2379,https://192.168.233.93:2379,https://192.168.233.94:2379" endpoint health --write-out=table

master节点的部署:
91:
上传 master.zip 和 k8s-cert.sh 到 /opt/k8s 目录中,解压 master.zip 压缩包


91是主节点
92是从节点
95和96是nginx的IP地址

unzip master.zip


指向apiserver的ip地址





chmod 777 *.sh

mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}

mkdir /opt/k8s/k8s-cert

mv /opt/k8s/k8s-cert.sh /opt/k8s/k8s-cert

cd /opt/k8s/k8s-cert/
./k8s-cert.sh
cp ca*pem apiserver*pem /opt/kubernetes/ssl/

cd /opt/k8s/

拖软件包

tar zxvf kubernetes-server-linux-amd64.tar.gz

cp kube-apiserver kubectl kube-controller-manager kube-scheduler /opt/kubernetes/bin/

ln -s /opt/kubernetes/bin/* /usr/local/bin/


#!/bin/bash
#获取随机数前16个字节内容,以十六进制格式输出,并删除其中空格
BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ' ')
#生成 token.csv 文件,按照 Token序列号,用户名,UID,用户组 的格式生成
cat > /opt/kubernetes/cfg/token.csv <<EOF
${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
EOF

chmod 777 token.sh

./token.sh

cat /opt/kubernetes/cfg/token.csv

二进制文件、token、证书都准备好后,开启 apiserver 服务
cd /opt/k8s/
./apiserver.sh 192.168.233.91 https://192.168.233.91:2379,https://192.168.233.93:2379,https://192.168.233.94:2379

netstat -antp | grep 6443

./scheduler.sh

./controller-manager.sh

./admin.sh

通过kubectl工具查看当前集群组件状态
kubectl get cs


node节点的部署:
93、94:
mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}


拖软件包

unzip node.zip

91:

scp kubelet kube-proxy root@192.168.233.93:/opt/kubernetes/bin/

scp kubelet kube-proxy root@192.168.233.94:/opt/kubernetes/bin/

91:

拖软件包




chmod 777 kubeconfig.sh



scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.233.93:/opt/kubernetes/cfg/

scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.233.94:/opt/kubernetes/cfg/

93:

94:

91:
RBAC授权,生成和赋权用户,使用户 kubelet-bootstrap 能够有权限发起 CSR 请求证书
kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap

若执行失败,可先给kubectl绑定默认cluster-admin管理员集群角色,授权对整个集群的管理员权限
kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous

93:
chmod 777 kubelet.sh

./kubelet.sh 192.168.233.93

91:




94:




91:




kube-proxy节点上的网络代理部署:
93:
cd /opt

for i in (ls /usr/lib/modules/(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done

chmod 777 proxy.sh

./proxy.sh 192.168.233.93

ps aux | grep kube-proxy

94:
cd /opt

for i in (ls /usr/lib/modules/(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done


安装flannel:
93、94:
把软件包拖到opt目录





91:
cd /opt/k8s
传 kube-flannel.yml 文件到 /opt/k8s 目录中,部署 CNI 网络



93、94:
ifconfig查看一下有没有flannel.1
部署 CoreDNS
CoreDNS:可以为集群中的 service 资源创建一个域名与 IP 的对应关系解析。
service是对外提供访问的地址,现在我们加入DNS机制之后,可以直接访问服务名
在所有 node 节点上操作
93、94:
#上传 coredns.tar 到 /opt 目录中
cd /opt
docker load -i coredns.tar

//在 master01 节点上操作
91:
#上传 coredns.yaml 文件到 /opt/k8s 目录中,部署 CoreDNS
cd /opt/k8s
kubectl apply -f coredns.yaml

kubectl get pods -n kube-system

#DNS 解析测试
kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous

kubectl run -it --rm dns-test --image=busybox:1.28.4 sh

/ # 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

exit
master02 节点部署
92:
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X

#在master添加hosts
cat >> /etc/hosts << EOF
192.168.233.91 master01
192.168.233.93 node01
192.168.233.94 node02
EOF

91:


92:


93:
如上同步
94:
如上同步
91、92、93、94同步操作:



91:
从 master01 节点上拷贝证书文件、各master组件的配置文件和服务管理文件到 master02 节点
scp -r /opt/etcd/ root@192.168.233.92:/opt/
scp -r /opt/kubernetes/ root@192.168.233.92:/opt
scp -r /root/.kube root@192.168.233.92:/root
scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}.service root@192.168.233.92:/usr/lib/systemd/system/
92:
修改配置文件kube-apiserver中的IP
vim /opt/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.233.91:2379,https://192.168.233.93:2379,https://192.168.233.94:2379 \
--bind-address=192.168.233.92 \ #修改
--secure-port=6443 \
--advertise-address=192.168.233.92 \ #修改
在 master02 节点上启动各服务并设置开机自启
systemctl start kube-apiserver.service
systemctl enable kube-apiserver.service
systemctl start kube-controller-manager.service
systemctl enable kube-controller-manager.service
systemctl start kube-scheduler.service
systemctl enable kube-scheduler.service

查看node节点状态
ln -s /opt/kubernetes/bin/* /usr/local/bin/
kubectl get nodes

弄完之后查看一下node状态 以及pod

负载均衡部署 :
35、36同步操作:

配置nginx的官方在线yum源,配置本地nginx的yum源
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
EOF

yum install nginx -y

修改nginx配置文件,配置四层反向代理负载均衡,指定k8s群集2台master的节点ip和6443端口
vim /etc/nginx/nginx.conf

(以下内容可以直接把配置文件里的东西全部删掉,然后使用)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
#添加
stream {
log_format main '$remote_addr upstream_addr - \[time_local] $status $upstream_bytes_sent';
#日志记录格式
#$remote_addr: 客户端的 IP 地址。
#$upstream_addr: 上游服务器的地址。
#[$time_local]: 访问时间,使用本地时间。
#$status: HTTP 响应状态码。
#$upstream_bytes_sent: 从上游服务器发送到客户端的字节数。
access_log /var/log/nginx/k8s-access.log main;
upstream k8s-apiserver {
server 192.168.233.31:6443;
server 192.168.233.32:6443;
}
server {
listen 6443;
proxy_pass k8s-apiserver;
}
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - remote_user \[time_local] "$request" '
'$status body_bytes_sent "http_referer" '
'"http_user_agent" "http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}

重启和开启自启nginx

部署keepalived服务
yum install keepalived -y

! Configuration File for keepalived
global_defs {
接收邮件地址
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
邮件发送地址
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_MASTER
#lb01节点的为 NGINX_MASTER,lb02节点的为 NGINX_BACKUP
#vrrp_strict #注释掉
}
#添加一个周期性执行的脚本
vrrp_script check_nginx {
script "/etc/nginx/check_nginx.sh"
#指定检查nginx存活的脚本路径
}
vrrp_instance VI_1 {
state MASTER
#lb01节点的为 MASTER,lb02节点的为 BACKUP
interface ens33
#指定网卡名称 ens33
virtual_router_id 51
#指定vrid,两个节点要一致
priority 10 0
#lb01节点的为 100,lb02节点的为 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.233.100/24 #指定 VIP
}
track_script {
check_nginx #指定vrrp_script配置的脚本
}
}




创建nginx状态检查脚本
vim /etc/nginx/check_nginx.sh

#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop
systemctl stop keepalived
fi

chmod +x /etc/nginx/check_nginx.sh

先启动nginx,再启动keepalived

ip a查看虚拟ip地址

可以停主的nginx 看看ip有没有飘到备上

93、94同步操作:
修改node节点上的bootstrap.kubeconfig,kubelet.kubeconfig配置文件为VIP
cd /opt/kubernetes/cfg/
vim bootstrap.kubeconfig

vim kubelet.kubeconfig
server: https://192.168.233.100:6443
vim kube-proxy.kubeconfig
server: https://192.168.233.100:6443

重启kubelet和kube-proxy服务
systemctl restart kubelet.service
systemctl restart kube-proxy.service

然后用主的有虚拟ip的主机查看端口号6443

##### 在 master01 节点上操作 #####
//测试创建pod
kubectl run nginx --image=nginx
//查看Pod的状态信息
kubectl get pods
部署 Dashboard :
Dashboard:仪表盘,kubernetes的可视化界面,在这个可视化界面上,可以对k8s集群进行管理
91:
在 master01 节点上操作
#上传 recommended.yaml 文件到 /opt/k8s 目录中

kubectl apply -f recommended.yaml

创建service account并绑定默认cluster-admin管理员集群角色
kubectl create serviceaccount dashboard-admin -n kube-system

kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin

获取token值
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

然后用浏览器登录 (有的浏览器用不了)

点击高级


这一步的密码就是上面几步中获取token值的密码


进入到这里算完成创建

补齐的命令部署:

在最后一行

