searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

ubuntu18.04部署多master的k8s集群

2023-04-25 17:06:08
100
0

ubuntu k8s安装

一、  环境准备

主机名

IP

备注

k8s-master-test01

192.168.0.10

配置16核32G500G硬盘

操作系统 Ubuntu18.04 docker Docker version 20.10.21

k8s-master-test02

192.168.0.11

k8s-master-test03

192.168.0.12

k8s-master-node01

192.168.0.13

k8s-vip

192.168.0.100

vip

以下1、2、3、4、5、6、7、8、9是在所有主机上执行

10、11是在master节点上执行

1、关闭防火墙

使用ufw status命令查看当前的防火墙状态:inactive状态是防火墙关闭状态 active是开启状态

关闭防火墙

ufw disable

2、修改时间

timedatectl set-timezone Asia/Shanghai

3、修改主机名

hostnamectl set-hostname k8s-master-test01

添加hosts文件

cat <<EOF >> /etc/hosts

192.168.0.10 k8s-master-test01 192.168.0.11 k8s-master-test02 192.168.0.12 k8s-master-test03

192.168.0.100 k8s-vip

EOF

4、关闭swap分区

swapoff -a  # 临时

sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久

5、docker安装

1、更新apt软件包索引并安装软件包

apt-get install apt-transport-https ca-certificates curl gnupg lsb-release software-properties-common -y

2、添加 Docker 官方的 GPG 密钥

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

3、设置稳定版本的apt仓库地址

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

4、安装最新docker版本

apt-get install docker-ce docker-ce-cli containerd.io -y

5、配置 docker 配置文件,配置进行加速和 systemd

mkdir -p /data/lib/docker

修改docker镜像存放位置

cat << EOF > /etc/docker/daemon.json
{
"registry-mirrors": ["https://b1cx9cn7.mirror.aliyuncs.com"],
"data-root":"/data/lib/docker",
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF

启动 Docker

systemctl daemon-reload && sudo systemctl restart docker && sudo systemctl enable docker

6、添加阿里云apt软件源

1、curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -

2、

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list

deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main

EOF

3、apt-get update

7、安装kubectl kubelet kubeadm

apt-get install kubelet=1.23.1-00 kubeadm=1.23.1-00 kubectl=1.23.1-00 -y

systemctl start kubelet && systemctl enable kubelet

8、修改内核系统参数

1、修改内核参数

(首先确认你的系统已经加载了 br_netfilter 模块,默认是没有该模块的,需要你先安装 bridge-utils)

apt-get install -y bridge-utils

modprobe br_netfilter

lsmod | grep br_netfilter

2、添加加载内核模块

tee /etc/modules-load.d/containerd.conf<<EOF

overlay

br_netfilter

EOF

3、加载内核模块

modprobe overlay

modprobe br_netfilter

4、配置内核参数

tee /etc/sysctl.d/kubernetes.conf<<EOF

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

net.ipv4.ip_forward = 1

EOF

修改fileter参数为 1 默认为 2(如果为2calico网络模式不支持)

cat /etc/sysctl.d/10-network-security.conf

net.ipv4.conf.default.rp_filter=1

net.ipv4.conf.all.rp_filter=1

sysctl --system

9、安装keepalived(在三个master节点上)

1、  安装

sudo apt-get install -y libssl-dev openssl libpopt-dev keepalived

2、配置文件

cat <<EOF > /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
script_user root
enable_script_security
router_id LVS_DEVEL
}
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 3
weight -2
fall 10
rise 2
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 200
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.0.100
}
track_script {
check_haproxy
}
}
EOF

其余的两个节点为BACKUP ,并修改priority,该值越大越优先

启动keepalived并设置开机启动

systemctl enable keepalived

systemctl start keepalived

systemctl status keepalived

10、安装haproxy

1、  安装

apt-get install -y haproxy

2、配置文件

cat <<EOF > /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
pidfile /var/run/haproxy.pid
maxconn 4000
# user haproxy
# group haproxy
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

#---------------------------------------------------------------------
# kubernetes apiserver frontend which proxys to the backends
#---------------------------------------------------------------------
frontend apiserver
mode tcp
option tcplog
bind *:16443
default_backend apiserver


#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend apiserver
mode tcp
balance roundrobin
server k8s-master1 192.168.0.10:6443 check
server k8s-master2 192.168.0.11:6443 check
server k8s-master3 192.168.0.12:6443 check
EOF

启动haproxy并设置开机启动

systemctl enable haproxy

systemctl start haproxy

systemctl status haproxy

11、安装k8s master节点

1、查看需要的安装镜像

kubeadm config images list

2、  通过国内镜像下载

因默认安装的镜像源需要翻墙才能下载,解决办法是可以先从阿里源下载相同版本的镜像,在修改标签。

docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.1

docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.1

docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.1

docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.23.1

docker pull registry.aliyuncs.com/google_containers/pause:3.6

docker pull registry.aliyuncs.com/google_containers/etcd:3.5.1-0

docker pull registry.aliyuncs.com/google_containers/coredns:v1.8.6

3、修改标签

docker tag registry.aliyuncs.com/google_containers/coredns:v1.8.6

k8s.gcr.io/coredns/coredns:v1.8.6

docker tag registry.aliyuncs.com/google_containers/pause:3.6 k8s.gcr.io/pause:3.6

docker tag registry.aliyuncs.com/google_containers/etcd:3.5.1-0 k8s.gcr.io/etcd:3.5.1-0

docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.23.1 k8s.gcr.io/kube-proxy:v1.23.1

docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.1 k8s.gcr.io/kube-scheduler:v1.23.1

docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.1 k8s.gcr.io/kube-controller-manager:v1.23.1

docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.1 k8s.gcr.io/kube-apiserver:v1.23.1

4、安装

kubeadm init --kubernetes-version 1.23.1 --control-plane-endpoint "k8s-vip:16443" --pod-network-cidr=10.200.0.0/16 --upload-certs

显示如下表示集群初始化成功

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of the control-plane node running the following command on each as root:

kubeadm join k8s-vip:16443 --token j9vowq.9x4kei16aecy1f17 \
--discovery-token-ca-cert-hash sha256:585506f443aa307d4c39135fa46c1624d6a16d59b9f965e061cd32a2cf9f84ee \
--control-plane --certificate-key 892deed5616426df4947b8735c8fca0295888fc64f628497ca5fbf171596d59b

Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join k8s-vip:16443 --token j9vowq.9x4kei16aecy1f17 \
--discovery-token-ca-cert-hash sha256:585506f443aa307d4c39135fa46c1624d6a16d59b9f965e061cd32a2cf9f84ee

5、获取操作k8s集群权限

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

12、node节点安装

在node节点上执行1、2、3、4、5、6、7、8步骤之后执行如下加入集群:

kubeadm join k8s-vip:16443 --token j9vowq.9x4kei16aecy1f17 \

--discovery-token-ca-cert-hash sha256:585506f443aa307d4c39135fa46c1624d6a16d59b9f965e061cd32a2cf9f84ee

执行kubectl get node查看node节点状态

13、网络插件calico安装

以上步骤安装完后,机器搭建起来了,但状态还是NotReady状态,需要安装网络插件

kubectl apply -f https://docs.projectcalico.org/v3.21/manifests/calico.yaml

安装之后节点状态显示Ready

执行kubectl get node查看集群各个节点状态

14、其它master节点安装

在另外2台master节点上执行1、2、3、4、5、6、7、8、9、10 步骤之后执行如下加入集群:

kubeadm join k8s-vip:16443 --token j9vowq.9x4kei16aecy1f17 \

--discovery-token-ca-cert-hash sha256:585506f443aa307d4c39135fa46c1624d6a16d59b9f965e061cd32a2cf9f84ee \

--control-plane --certificate-key 892deed5616426df4947b8735c8fca0295888fc64f628497ca5fbf171596d59b

执行kubectl get node查看集群各个节点状态

0条评论
0 / 1000
周****雷
2文章数
0粉丝数
周****雷
2 文章 | 0 粉丝
周****雷
2文章数
0粉丝数
周****雷
2 文章 | 0 粉丝
原创

ubuntu18.04部署多master的k8s集群

2023-04-25 17:06:08
100
0

ubuntu k8s安装

一、  环境准备

主机名

IP

备注

k8s-master-test01

192.168.0.10

配置16核32G500G硬盘

操作系统 Ubuntu18.04 docker Docker version 20.10.21

k8s-master-test02

192.168.0.11

k8s-master-test03

192.168.0.12

k8s-master-node01

192.168.0.13

k8s-vip

192.168.0.100

vip

以下1、2、3、4、5、6、7、8、9是在所有主机上执行

10、11是在master节点上执行

1、关闭防火墙

使用ufw status命令查看当前的防火墙状态:inactive状态是防火墙关闭状态 active是开启状态

关闭防火墙

ufw disable

2、修改时间

timedatectl set-timezone Asia/Shanghai

3、修改主机名

hostnamectl set-hostname k8s-master-test01

添加hosts文件

cat <<EOF >> /etc/hosts

192.168.0.10 k8s-master-test01 192.168.0.11 k8s-master-test02 192.168.0.12 k8s-master-test03

192.168.0.100 k8s-vip

EOF

4、关闭swap分区

swapoff -a  # 临时

sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久

5、docker安装

1、更新apt软件包索引并安装软件包

apt-get install apt-transport-https ca-certificates curl gnupg lsb-release software-properties-common -y

2、添加 Docker 官方的 GPG 密钥

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

3、设置稳定版本的apt仓库地址

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

4、安装最新docker版本

apt-get install docker-ce docker-ce-cli containerd.io -y

5、配置 docker 配置文件,配置进行加速和 systemd

mkdir -p /data/lib/docker

修改docker镜像存放位置

cat << EOF > /etc/docker/daemon.json
{
"registry-mirrors": ["https://b1cx9cn7.mirror.aliyuncs.com"],
"data-root":"/data/lib/docker",
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF

启动 Docker

systemctl daemon-reload && sudo systemctl restart docker && sudo systemctl enable docker

6、添加阿里云apt软件源

1、curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -

2、

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list

deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main

EOF

3、apt-get update

7、安装kubectl kubelet kubeadm

apt-get install kubelet=1.23.1-00 kubeadm=1.23.1-00 kubectl=1.23.1-00 -y

systemctl start kubelet && systemctl enable kubelet

8、修改内核系统参数

1、修改内核参数

(首先确认你的系统已经加载了 br_netfilter 模块,默认是没有该模块的,需要你先安装 bridge-utils)

apt-get install -y bridge-utils

modprobe br_netfilter

lsmod | grep br_netfilter

2、添加加载内核模块

tee /etc/modules-load.d/containerd.conf<<EOF

overlay

br_netfilter

EOF

3、加载内核模块

modprobe overlay

modprobe br_netfilter

4、配置内核参数

tee /etc/sysctl.d/kubernetes.conf<<EOF

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

net.ipv4.ip_forward = 1

EOF

修改fileter参数为 1 默认为 2(如果为2calico网络模式不支持)

cat /etc/sysctl.d/10-network-security.conf

net.ipv4.conf.default.rp_filter=1

net.ipv4.conf.all.rp_filter=1

sysctl --system

9、安装keepalived(在三个master节点上)

1、  安装

sudo apt-get install -y libssl-dev openssl libpopt-dev keepalived

2、配置文件

cat <<EOF > /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
script_user root
enable_script_security
router_id LVS_DEVEL
}
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 3
weight -2
fall 10
rise 2
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 200
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.0.100
}
track_script {
check_haproxy
}
}
EOF

其余的两个节点为BACKUP ,并修改priority,该值越大越优先

启动keepalived并设置开机启动

systemctl enable keepalived

systemctl start keepalived

systemctl status keepalived

10、安装haproxy

1、  安装

apt-get install -y haproxy

2、配置文件

cat <<EOF > /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
pidfile /var/run/haproxy.pid
maxconn 4000
# user haproxy
# group haproxy
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

#---------------------------------------------------------------------
# kubernetes apiserver frontend which proxys to the backends
#---------------------------------------------------------------------
frontend apiserver
mode tcp
option tcplog
bind *:16443
default_backend apiserver


#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend apiserver
mode tcp
balance roundrobin
server k8s-master1 192.168.0.10:6443 check
server k8s-master2 192.168.0.11:6443 check
server k8s-master3 192.168.0.12:6443 check
EOF

启动haproxy并设置开机启动

systemctl enable haproxy

systemctl start haproxy

systemctl status haproxy

11、安装k8s master节点

1、查看需要的安装镜像

kubeadm config images list

2、  通过国内镜像下载

因默认安装的镜像源需要翻墙才能下载,解决办法是可以先从阿里源下载相同版本的镜像,在修改标签。

docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.1

docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.1

docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.1

docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.23.1

docker pull registry.aliyuncs.com/google_containers/pause:3.6

docker pull registry.aliyuncs.com/google_containers/etcd:3.5.1-0

docker pull registry.aliyuncs.com/google_containers/coredns:v1.8.6

3、修改标签

docker tag registry.aliyuncs.com/google_containers/coredns:v1.8.6

k8s.gcr.io/coredns/coredns:v1.8.6

docker tag registry.aliyuncs.com/google_containers/pause:3.6 k8s.gcr.io/pause:3.6

docker tag registry.aliyuncs.com/google_containers/etcd:3.5.1-0 k8s.gcr.io/etcd:3.5.1-0

docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.23.1 k8s.gcr.io/kube-proxy:v1.23.1

docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.1 k8s.gcr.io/kube-scheduler:v1.23.1

docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.1 k8s.gcr.io/kube-controller-manager:v1.23.1

docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.1 k8s.gcr.io/kube-apiserver:v1.23.1

4、安装

kubeadm init --kubernetes-version 1.23.1 --control-plane-endpoint "k8s-vip:16443" --pod-network-cidr=10.200.0.0/16 --upload-certs

显示如下表示集群初始化成功

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of the control-plane node running the following command on each as root:

kubeadm join k8s-vip:16443 --token j9vowq.9x4kei16aecy1f17 \
--discovery-token-ca-cert-hash sha256:585506f443aa307d4c39135fa46c1624d6a16d59b9f965e061cd32a2cf9f84ee \
--control-plane --certificate-key 892deed5616426df4947b8735c8fca0295888fc64f628497ca5fbf171596d59b

Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join k8s-vip:16443 --token j9vowq.9x4kei16aecy1f17 \
--discovery-token-ca-cert-hash sha256:585506f443aa307d4c39135fa46c1624d6a16d59b9f965e061cd32a2cf9f84ee

5、获取操作k8s集群权限

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

12、node节点安装

在node节点上执行1、2、3、4、5、6、7、8步骤之后执行如下加入集群:

kubeadm join k8s-vip:16443 --token j9vowq.9x4kei16aecy1f17 \

--discovery-token-ca-cert-hash sha256:585506f443aa307d4c39135fa46c1624d6a16d59b9f965e061cd32a2cf9f84ee

执行kubectl get node查看node节点状态

13、网络插件calico安装

以上步骤安装完后,机器搭建起来了,但状态还是NotReady状态,需要安装网络插件

kubectl apply -f https://docs.projectcalico.org/v3.21/manifests/calico.yaml

安装之后节点状态显示Ready

执行kubectl get node查看集群各个节点状态

14、其它master节点安装

在另外2台master节点上执行1、2、3、4、5、6、7、8、9、10 步骤之后执行如下加入集群:

kubeadm join k8s-vip:16443 --token j9vowq.9x4kei16aecy1f17 \

--discovery-token-ca-cert-hash sha256:585506f443aa307d4c39135fa46c1624d6a16d59b9f965e061cd32a2cf9f84ee \

--control-plane --certificate-key 892deed5616426df4947b8735c8fca0295888fc64f628497ca5fbf171596d59b

执行kubectl get node查看集群各个节点状态

文章来自个人专栏
文章 | 订阅
0条评论
0 / 1000
请输入你的评论
0
0