multipass + k3s 搭建本地K3s高可用集群
环境
k3s官网最新
multipass+hyperv
开始
创建虚拟网卡
外部 选择要连接的物理设备 网卡名WAN
创建虚拟机
数据库节点以及负载均衡节点
multipass launch --name k8sLB --m 1G --network WAN
master1
multipass launch --name k8sMS1 --m 1G --network WAN
master12
multipass launch --name k8sMS2 --m 1G --network WAN
node1
multipass launch --name k8sN1 --m 1G --network WAN
node2
multipass launch --name k8sN2 --m 1G --network WAN
配置k3s
k8sLB配置
安装docker
apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
配置数据库
ubuntu@k8sLB:~$ sudo docker run --name mariadb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -v /data/mariadb/data:/var/lib/mysql -d mariadb
k8s Master配置
master1 master2 使用指令启动k3s
curl -sfL https://get.k3s.io | sh -s - server \
--token=SECRET \
--datastore-endpoint="mysql://username:password@tcp(hostname:3306)/database-name"
配置负载均衡
k8sLB 编辑nginx 配置文件
我图省事 没用docker安装nginx
stream{
upstream k3sms{
server 192.168.0.105:6443;
server 192.168.0.107:6443;
}
server {
listen 6443;
proxy_pass k3sms;
}
}
节点加入master
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_URL=负载均衡地址 K3S_TOKEN=秘钥 sh -
测试一下
使用sudo kubectl get node命令查询节点
ubuntu@k8sMS1:~$ sudo kubectl get node
NAME STATUS ROLES AGE VERSION
k8sms2 Ready control-plane,master 100m v1.25.7+k3s1
k8sms1 Ready control-plane,master 102m v1.25.7+k3s1
k8sn1 Ready <none> 24m v1.25.7+k3s1
k8sn2 Ready <none> 6m10s v1.25.7+k3s1
手动宕机ms2 n2
ubuntu@k8sMS1:~$ sudo kubectl get node
NAME STATUS ROLES AGE VERSION
k8sms1 Ready control-plane,master 104m v1.25.7+k3s1
k8sn1 Ready <none> 26m v1.25.7+k3s1
k8sms2 NotReady control-plane,master 101m v1.25.7+k3s1
k8sn2 NotReady <none> 7m33s v1.25.7+k3s1
手动宕机ms1
ubuntu@k8sMS2:~$ sudo kubectl get node
NAME STATUS ROLES AGE VERSION
k8sms2 Ready control-plane,master 104m v1.25.7+k3s1
k8sn2 Ready <none> 10m v1.25.7+k3s1
k8sn1 Ready <none> 29m v1.25.7+k3s1
k8sms1 NotReady control-plane,master 106m v1.25.7+k3s1
非常好用
大功告成
装个nginx尝尝k3s是啥味儿的
先装个仪表盘 wget https://github.com/cnrancher/kube-explorer/releases/download/v0.3.1/kube-explorer-linux-amd64
写(百度)个yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
再写个service让其能在外网访问
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30080
type: NodePort
访问一下
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
结束