Ubuntu部署k8s
Ubuntu部署k8s
Ubuntu 4核8G
1.1 net-tools安装
$sudo apt install net-tools
1.2 修改节点名和设置静态ip
1.2.1 修改节点名字
$sudo vim /etc/hostname
#修改为k8s节点名称
$sudo cat /etc/hostname
#查看修改名称生效
1.2.2 设置静态ip
$sudo vim /etc/network/interfaces
#vim打开文件编辑
若vim不可用则使用sudo apt install vim
自行安装vim
#编辑文件添加(按键i进入insert模式,输入下面内容,按键esc后再:wq 保存退出)
auto lo
iface lo inet loopback
auto ens33 #网卡名通过ifconfig查询
iface ens33 inet static #设置为静态
address 192.168.100.5 #自行设置IP,可在vm网络编辑器中查找最大最小IP地址,在其中选择
gateway 192.168.100.1 #自行设置
netmask 255.255.255.0
dns-nameserver 114.114.114.114
dns-nameserver 8.8.8.8
1.3 禁用selinux,关闭防火墙、交换空间
1.3.1 禁用selinux
$sudo vim /etc/selinux/config
#禁用selinux
SELINUX=disabled
#在文件中添加
1.3.2 禁用防火墙
apt install selinux-utils
#安装插件
$setenforce 0
#禁用防火墙
$sudo ufw disable
$getenforce
#验证
1.3.3 关闭交换空间
$swapoff -a
#关闭交换空间
$sudo vim /etc/fstab
#注释掉swap那一整行
1.4 安装docker
(1) step 1: 安装必要的一些系统工具
更新索引包
$sudo apt-get update
获取HTTPS支持
$sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
(2)step 2: 安装GPG证书
$curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
(3)step 3: 写入软件源信息
$ sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
(4)Step 4: 更新并安装 Docker-CE
$sudo apt-get -y update
$sudo apt-get -y install docker-ce
1.5 修改Linux内核
$sudo vim /etc/sysctl.d/k8s.conf
# 在该文件中添加如下内容
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
$sysctl -p
#从配置文件“/etc/sysctl.conf”加载内核参数设置
1.6 修改Docker 镜像源
$sudo mkdir -p /etc/docker
$sudo tee /etc/docker/daemon.conf <<-'EOF'
>{
> "registry-mirrors": ["https://lzyjejsb.mirror.aliyuncs.com"] #阿里云平台获取镜像源
>}
>EOF
$sudo systemctl daemon-reload
$sudo systemctl restart docker
$ifconfig
(查看docker是否安装上)
1.7 安装kubernetes
1.7.1 添加kubernetes软件源,安装kubeadm,kubelet,kubectl,启动kubelet服务并设置开机自动启动
#设置国内源
$ curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
#添加gpg
$ cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
#添加源
> deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
> EOF
$ apt-get update
安装最新版本
$ apt-get install -y kubelet kubeadm kubectl
或者安装指定版本:
$ apt-get install -y kubelet=1.23.0-00 kubeadm=1.23.0-00 kubectl=1.23.0-00
#出现提示[Y/N]的时候后面写Y
$systemctl enable kubelet && systemctl start kubelet
#启动kubelet服务设置开机自启动
1.7.2 拉取镜像并tag为所需镜像
(1)查看所需镜像
$kubeadm config images list
(2)按顺序拉取下列镜像(需要什么拉取什么)
docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.13
docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.13
docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.13
docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.23.13
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将拉取的镜像改为所需镜像 (docker images查看已下载的镜像)
$docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.23.13 k8s.gcr.io/kube-proxy:v1.23.13
$docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.13 k8s.gcr.io/kube-scheduler:v1.23.13
$docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.13 k8s.gcr.io/kube-apiserver:v1.23.13
$docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.13 k8s.gcr.io/kube-controller-manager:v1.23.13
$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/coredns:v1.8.6 k8s.gcr.io/coredns:v1.8.6
1.7.3 初始化master节点
首先vim /etc/docker/daemon.json
添加以下内容
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
],
"registry-mirrors": ["https://tdhp06eh.mirror.aliyuncs.com"]
}
然后重新启动systemctl daemon-reload
systemctl restart docker
然后再初始化
$kubeadm init --image-repository registry.aliyuncs.com/google_containers --ignore-preflight-errors=Swap
成功会显示以下内容:
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
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/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.100.6:6443 --token 4sfkac.8w487zotq1w0nv2o \
--discovery-token-ca-cert-hash sha256:2f969422e7e6abe5dd7960633a26c155bbfaf1506896e963dfcc921c561df63d
成功后把这个标记记录下来,在子节点要用
- 如果出现问题
[root@master:~] rm -rf /etc/containerd/config.toml
[root@master:~] systemctl restart containerd
- 如果是
vim /etc/docker/daemon.json
添加以下内容
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
],
"registry-mirrors": ["https://tdhp06eh.mirror.aliyuncs.com"]
}
然后systemctl daemon-reload
systemctl restart docker
然后再初始化
- 如果出现问题是
解决方法
方法一:首先去查看下你的: /etc/systemd/system/kubelet.service.d
路径下有没有一个文件:10-kubeadm.conf.有的话加入
Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true --fail-swap-on=false"
再重新启动kubelet.service
systemctl daemon reload
systemctl restart kubelet.service
方法二 :.也可能是之前的取消swap:swapoff -a和注释/etc/fstab中swap的行的步骤没做好都重新检查后重新初始化
1.7.4 将kubectl命令添加到普通用户
#使用普通用户执行如下命令(root权限下同时按键ctrl+d,退出到普通用户):
$mkdir -p $HOME/.kube
$sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$sudo chown $(id -u):$(id -g) $HOME/.kube/config
1.7.5 部署weave和flannel
-
安装weave
$kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
或者使用$ kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
进行安装 -
安装flannel(非必要)
$kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
-
查看是否安装成功
$ kubectl get ns
$ kubectl get pods -n kube-system
#是否正常运行
若出错kubectl describe pod coredns-6d8c4cb4d-x58v9 -n kube-system
查看pod日志信息
如果出现问题:没有正常运行coredns,(即前两个节点coredns的ready状态是0/1的话)
解决方法:
首先修改kubectl配置 kubectl edit cm coredns -n kube-system
再将loop删除或者注释掉,如图
再删除coredns pods
kubectl delete pods coredns-加自己的数字
kubectl delete pod coredns-加自己的数字
最后再$ kubectl get pods -n kube-system
看一下是否是正常状态
1.7.6 添加node节点
(1)在node节点上执行环境准备,安装docker,修改镜像源,安装kubeadm和kubelet,注意需要拉取kube-proxy、pause、coredns三个镜像,否则weave容器等无法在该节点上正常运行。(即node节点上执行从开始到1.7.3.初始化master节点前的操作)
(2)在node节点上执行
首先vim /etc/docker/daemon.json
添加以下内容
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
],
"registry-mirrors": ["https://tdhp06eh.mirror.aliyuncs.com"]
}
然后systemctl daemon-reload
systemctl restart docker
然后加入集群
kubeadm join 192.168.100.5:6443 --token 3zz6gm.hcarpvopsh6pxibq \ --discovery-token-ca-cert-hash sha256:5755a966c9d0d0301a32d738388a8c2149583594d71e73acb8d3efb674db2ca
(3)在master节点上执行:kubectl get nodes
查看节点是否正常。正常状态为Ready,若不正常查看pods解决。
如果slave状态为NotReady,查看pod是否全部启动kubectl get pods -n kube-system -o wide
,如果有没启动的,找到pod没启动的原因kubectl describe pod weave-net-tsqgc -n kube-system
,大概率是slave节点的docker镜像没下载下来,手动拉取镜像,先查找对应的镜像 docker search weave-npc
,然后拉取docker pull huangyj/weave-npc
。
k8s删除pod时,如果直接kubectl delete pods xxx删除对应的pod,提示删除成功,但是立马又回生成一个。需要删除对应的deployment,查看deployment kubectl get deployment -n nginx1
。
更多推荐
所有评论(0)