您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

部署k8s

安装docker

参考菜鸟教程:Ubuntu Docker 安装 | 菜鸟教程 (runoob.com)

docker配置

1、当前用户加入"docker"用户组

sudo usermod -aG docker $USER

2、 配置 cgroup 驱动为 systemd

sudo vim /etc/docker/daemon.json
#  创建文件 /etc/docker/daemon.json ,内容如下:
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}

3、重启服务生效配置

sudo systemctl daemon-reload
sudo systemctl restart docker.service

4、检查配置是否生效

sudo docker info | grep Cgroup

# ECHO ------
Cgroup Driver: systemd

关闭 swap

swapoff -a && sudo sed -i 's/^.*swap/#&/g' /etc/fstab

安装 kubelet kubeadm kubectl

由于网络原因,直接 APT-GET 安装可能安装不了,这里需要配置一下镜像仓库。

1、配置阿里云 kubernetes 镜像仓库

$ sudo apt-get update && sudo apt-get install -y apt-transport-https

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

2、创建文件 /etc/apt/sources.list.d/kubernetes.list, 内容如下:

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

3、安装 kubelet kubectl kubeadm

$ sudo apt-get update
$ sudo apt-get install -y kubelet kubeadm kubectl

4、设置kubelet开机启动

$ sudo systemctl enable kubelet

部署kubenetes master节点

查看所需的镜像及版本

kubeadm config images list

结果显示

k8s.gcr.io/kube-apiserver:v1.21.1
k8s.gcr.io/kube-controller-manager:v1.21.1
k8s.gcr.io/kube-scheduler:v1.21.1
k8s.gcr.io/kube-proxy:v1.21.1
k8s.gcr.io/pause:3.4.1
k8s.gcr.io/etcd:3.4.13-0
k8s.gcr.io/coredns/coredns:v1.8.0 
#注意,后面如果出现某个镜像拉取不成功可以根据版本号从dockerhub中手动拉取,再打上标签即可

初始化

sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers \
--apiserver-advertise-address=192.168.209.136 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--kubernetes-version=v1.21.1

#image-repository:镜像地址,由于默认的k8s.gcr.io无法访问,所以改用阿里云的镜像地址
# --apiserver-advertise-address: 指定master服务发布的ip地址,即节点的IP地址
# --pod-network-cidr: 指定pod网络的IP地址范围
# --service-cidr: 指定service网络的IP地址范围
#kubernetes-version:版本号

成功后显示如下(这些信息后面会用到,最好保存):

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/

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

kubeadm join 192.168.209.131:6443 --token vikbfm.wm7fdjktikuce2n9 \
	--discovery-token-ca-cert-hash sha256:7a98e166b0d1c961b9adf75bcc5af8175ef0b1f8079002416a49f301e0fafca2 

在主机节点当前用户home下创建.kube, 拷贝admin配置文件,赋予权限

mkdir -p $HOME/.kube

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

执行完成之后就可以直接使用kubectl命令了,如图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MbaJZ3he-1622549061030)(C:\Users\14258\Pictures\QQ图片20210518183253.png)]

因为还没安装pod网络插件,所以状态显示为NotReady、Pending

安装pod网络插件flanne

$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  #结果
    Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
    podsecuritypolicy.policy/psp.flannel.unprivileged created
    clusterrole.rbac.authorization.k8s.io/flannel created
    clusterrolebinding.rbac.authorization.k8s.io/flannel created
    serviceaccount/flannel created
    configmap/kube-flannel-cfg created
    daemonset.apps/kube-flannel-ds created

成功后master-node节点则会显示为Ready状态,coredns也会变成Running状态

将节点加入集群

问题1:拉取coredns不成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3BRVn9EZ-1622549061033)(C:\Users\14258\Pictures\QQ图片20210518181607.png)]

解决办法

1.从其他仓库拉取该镜像

sudo docker pull zmsjianyu/coredns:v1.8.0

2.根据选择的镜像源来重新打tag

sudo docker tag zmsjianyu/coredns:v1.8.0 registry.aliyuncs.com/google_containers/coredns/coredns:v1.8.0 

3.重新执行初始化命令 kubectl init,即可成功

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/

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

kubeadm join 192.168.209.136:6443 --token h7okc5.sd0btinr99j249xi \
	--discovery-token-ca-cert-hash sha256:79c33a4b101d29a2616d61b69acbc922276a5591a2264289acf06ade37f1531d 



分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进