前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >k8s学习一:使用kubeadm安装k8s

k8s学习一:使用kubeadm安装k8s

作者头像
仙士可
发布2022-09-13 18:26:09
5610
发布2022-09-13 18:26:09
举报
文章被收录于专栏:仙士可博客仙士可博客

写在开头

在学习整个k8s之前,先想办法搭建个k8s出现成果,然后根据这个成果进行深入学习,才会让人有学习的动力,本文将记录自己的安装k8s教程

准备工作:

一台ubuntu服务器(虚拟机)

k8s环境配置

host配置

我们先给服务器定义好hosts,便于直接找到该服务器ip

代码语言:javascript
复制
192.168.192.9 master

注意,后面如果需要增加集群,也需要配置其他的hosts

主机名修改(非必要)

修改 /etc/hostname 改为 master

关闭防火墙

由于k8s的防火墙规则和系统的冲突,所以需要关闭系统的防火墙

代码语言:javascript
复制
sudo ufw disable
systemctl stop ufw

关闭selinux

关闭selinux以允许容器访问宿主机的文件系统  (新装的Ubuntu好像没这个东西,可以自行百度)

禁用swap

swap会在内存不足的时候使用磁盘当做内存,但是效率会非常低,导致服务器直接卡死

代码语言:javascript
复制
sudo swapoff -a
vi /etc/fstab  # 将swap一行前面增加个# 号注释掉

网络参数

代码语言:javascript
复制
vi /etc/sysctl.d/k8s.conf  #将下面的3行(去掉#号写入到此文件)

#net.bridge.bridge-nf-call-ip6tables = 1
#net.bridge.bridge-nf-call-iptables = 1
#net.ipv4.ip_forward = 1

modprobe br_netfilter  
sysctl -p /etc/sysctl.d/k8s.conf

安装docker

代码语言:javascript
复制
apt-get install docker.io -y
# 设置开机启动并启动docker  
sudo systemctl start docker
sudo systemctl enble docker

# 修改docker运行时,并且增加镜像
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "registry-mirrors": [
    "https://reg-mirror.qiniu.com/"
  ]
  "storage-driver": "overlay2"
}
EOF

配置ubuntu k8s 安装源

代码语言:javascript
复制
#使得 apt 支持 ssl 传输
apt-get update && apt-get install -y apt-transport-https
#下载 gpg 密钥
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
#添加 k8s 镜像源
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
#更新
apt-get update

安装 kubeadm、kubelet、kubectl

注意!这里最好是只安装 1.24之前的版本,新版本可能安装很多问题,导致无心学习,我这里选择的是1.23.10

代码语言:javascript
复制
apt list--all-versions package_name  ##查看版本 

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

# 阻止自动更新(apt upgrade时忽略)。所以更新的时候先unhold,更新完再hold。
apt-mark hold kubelet kubeadm kubectl

通过kubeadm 进行初始化k8s集群

代码语言:javascript
复制
kubeadm init \
--apiserver-advertise-address 192.168.192.9 \
--apiserver-bind-port 6443 \
--pod-network-cidr 10.244.0.0/16 \
--image-repository registry.aliyuncs.com/google_containers

如果初始化出问题,就需要自己百度多解决了

初始化成功之后:

代码语言:javascript
复制
o/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: mdutmg.pbecp9mqcowc4b0u
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

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.192.9:6443 --token mdutmg.pbecp9mqcowc4b0u \
        --discovery-token-ca-cert-hash sha256:61f8d9b13b94a3c7eff88e25faf1c873cfd559d1ee2f2988009ac85de11ec730

保存最后的kubeadm join 代码,后面加入集群有用

查看集群是否成功部署:

代码语言:javascript
复制
 kubectl get nodes

如果提示:The connection to the server localhost:8080 was refused - did you specify the right host or port?

是因为没有绑定好集群环境

代码语言:javascript
复制
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile
source /etc/profile

即可正常显示

仙士可博客
仙士可博客

这边可以看到,STATUS 是NotReady状态,因为还有网络插件没装

安装 Pod Network(flannel网络插件)

代码语言:javascript
复制
wget

可以查看到kube-flannel的配置项

仙士可博客
仙士可博客

自己手动拉下镜像,避免部署的时候失败:

代码语言:javascript
复制
root@test02:/home/tioncico# docker pull docker.io/rancher/mirrored-flannelcni-flannel:v0.19.2

部署flannel插件

代码语言:javascript
复制
root@test02:/home/tioncico# kubectl get pod --all-namespaces
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
kube-flannel           kube-flannel-ds-rfx4q                        1/1     Running   0          4s
kube-system            coredns-6d8c4cb4d-575sw                      1/1     Running   0          65m
kube-system            coredns-6d8c4cb4d-p4nv8                      1/1     Running   0          65m
kube-system            etcd-master                                  1/1     Running   0          66m
kube-system            kube-apiserver-master                        1/1     Running   0          66m
kube-system            kube-controller-manager-master               1/1     Running   0          66m
kube-system            kube-proxy-n2n4n                             1/1     Running   0          65m
kube-system            kube-scheduler-master                        1/1     Running   0          66m
kubernetes-dashboard   dashboard-metrics-scraper-6f669b9c9b-86hm6   1/1     Running   0          28m
kubernetes-dashboard   kubernetes-dashboard-54c5fb4776-qb2lf        1/1     Running   0          28m
root@test02:/home/tioncico#

可看到nodes,pods一切正常

代码语言:javascript
复制
root@test02:/home/tioncico# kubectl get pods --all-namespaces
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
kube-flannel           kube-flannel-ds-rfx4q                        1/1     Running   0          58s
kube-system            coredns-6d8c4cb4d-575sw                      1/1     Running   0          66m
kube-system            coredns-6d8c4cb4d-p4nv8                      1/1     Running   0          66m
kube-system            etcd-master                                  1/1     Running   0          67m
kube-system            kube-apiserver-master                        1/1     Running   0          67m
kube-system            kube-controller-manager-master               1/1     Running   0          67m
kube-system            kube-proxy-n2n4n                             1/1     Running   0          66m
kube-system            kube-scheduler-master                        1/1     Running   0          67m
kubernetes-dashboard   dashboard-metrics-scraper-6f669b9c9b-86hm6   1/1     Running   0          29m
kubernetes-dashboard   kubernetes-dashboard-54c5fb4776-qb2lf        1/1     Running   0          29m
root@test02:/home/tioncico# kubectl get nodes
NAME     STATUS   ROLES                  AGE   VERSION
master   Ready    control-plane,master   67m   v1.23.10
root@test02:/home/tioncico#

本文为仙士可原创文章,转载无需和我联系,但请注明来自仙士可博客www.php20.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 写在开头
  • k8s环境配置
    • host配置
      • 主机名修改(非必要)
        • 关闭防火墙
          • 关闭selinux
            • 禁用swap
              • 网络参数
                • 安装docker
                  • 配置ubuntu k8s 安装源
                    • 安装 kubeadm、kubelet、kubectl
                    • 通过kubeadm 进行初始化k8s集群
                    • 查看集群是否成功部署:
                    • 安装 Pod Network(flannel网络插件)
                      • 部署flannel插件
                      相关产品与服务
                      容器服务
                      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档