首页 > 其他分享 >23. K8S安装过程笔记

23. K8S安装过程笔记

时间:2023-03-19 23:14:47浏览次数:52  
标签:K8S kubernetes 23 -- 笔记 v1.16 kubelet docker kube

23. K8S安装过程笔记

 发表于 2019-11-27   更新于 2022-08-17

以下所有操作均基于Cent OS 7操作系统。

视频教程连接:Bilibili Youtube

基本环境配置

  1. 关闭selinux
    1
    2
    setenforce 0
    sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  2. 关闭swap分区或禁用swap文件
    1
    2
    3
    4
    swapoff -a
    # 注释掉关于swap分区的行
    yes | cp /etc/fstab /etc/fstab_bak
    cat /etc/fstab_bak |grep -v swap > /etc/fstab
  3. 修改网卡配置
    1
    2
    3
    4
    5
    $ vim /etc/sysctl.conf
    net.ipv4.ip_forward = 1
    net.bridge.bridge-nf-call-iptables = 1
    net.bridge.bridge-nf-call-ip6tables = 1
    $ sysctl -p
  4. 启用内核模块
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $ modprobe -- ip_vs
    $ modprobe -- ip_vs_rr
    $ modprobe -- ip_vs_wrr
    $ modprobe -- ip_vs_sh
    $ modprobe -- nf_conntrack_ipv4
    $ cut -f1 -d " " /proc/modules | grep -e ip_vs -e nf_conntrack_ipv4
    $ vim /etc/sysconfig/modules/ipvs.modules
    modprobe -- ip_vs
    modprobe -- ip_vs_rr
    modprobe -- ip_vs_wrr
    modprobe -- ip_vs_sh
    modprobe -- nf_conntrack_ipv4
  5. 关闭防火墙
    1
    2
    $ systemctl stop firewalld
    $ systemctl disable firewalld
  6. 配置hosts

kubectl、kubeadm、kubelet的安装

添加Kubernetes的yum源

此处使用alibaba的镜像源

1
2
3
4
5
6
7
8
9
$vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

 

安装kubelet、kubeadm、kubectl

1
$ yum install -y kubelet kubeadm kubectl

启动kubelet服务

1
2
$ systemctl enable kubelet
$ systemctl start kubelet

此时执行systemctl status kubelet查看服务状态,服务状态应为Error(255), 如果是其他错误可使用journalctl -xe查看错误信息。

Docker安装和配置

Docker安装

docker的安装请查看官网文档(Overview of Docker editions)[https://docs.docker.com/install/overview/]

Docker配置

  1. 配置cgroup-driver为systemd
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 查看cgroup-driver
    $ docker info | grep -i cgroup
    # 追加 --exec-opt native.cgroupdriver=systemd 参数
    $ sed -i "s#^ExecStart=/usr/bin/dockerd.*#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd#g" /usr/lib/systemd/system/docker.service
    $ systemctl daemon-reload # 重新加载服务
    $ systemctl enable docker # 启用docker服务(开机自起)
    $ systemctl restart docker # 启动docker服务
    # 或者修改docker配置文件
    $ vim /etc/docker/daemon.json
    {
    "exec-opts": ["native.cgroupdriver=systemd"]
    }
  2. 预先拉取所需镜像
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # 查看kubeadm所需镜像
    $ kubeadm config images list
    k8s.gcr.io/kube-apiserver:v1.16.3
    k8s.gcr.io/kube-controller-manager:v1.16.3
    k8s.gcr.io/kube-scheduler:v1.16.3
    k8s.gcr.io/kube-proxy:v1.16.3
    k8s.gcr.io/pause:3.1
    k8s.gcr.io/etcd:3.3.15-0
    k8s.gcr.io/coredns:1.6.2
    # 拉取镜像
    $ docker pull kubeimage/kube-apiserver-amd64:v1.16.3
    $ docker pull kubeimage/kube-controller-manager-amd64:v1.16.3
    $ docker pull kubeimage/kube-scheduler-amd64:v1.16.3
    $ docker pull kubeimage/kube-proxy-amd64:v1.16.3
    $ docker pull kubeimage/pause-amd64:3.1
    $ docker pull kubeimage/etcd-amd64:3.3.15-0
    $ docker pull coredns/coredns:1.6.2
  3. 对预先拉取的镜像重新打tag
    1
    2
    3
    4
    5
    6
    7
    $ docker tag kubeimage/kube-apiserver-amd64:v1.16.3  k8s.gcr.io/kube-apiserver:v1.16.3
    $ docker tag kubeimage/kube-controller-manager-amd64:v1.16.3 k8s.gcr.io/kube-controller-manager:v1.16.3
    $ docker tag kubeimage/kube-scheduler-amd64:v1.16.3 k8s.gcr.io/kube-scheduler:v1.16.3
    $ docker tag kubeimage/kube-proxy-amd64:v1.16.3 k8s.gcr.io/kube-proxy:v1.16.3
    $ docker tag kubeimage/pause-amd64:3.1 k8s.gcr.io/pause:3.1
    $ docker tag kubeimage/etcd-amd64:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
    $ docker tag coredns/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2

Master节点的配置

以上步骤需要在node节点和master节点执行,当前步骤仅需在master节点执行。

Master节点的初始化

1
2
3
4
5
6
# 初始化master节点,
# --pod-network-cidr=192.168.0.0/16 指定使用Calico网络
# --apiserver-advertise-address=10.0.0.5 指向master节点IP,此处也可以使用hosts
$ kubeadm init --pod-network-cidr=10.244.0.0/16 \
--kubernetes-version=v1.16.3 \
--apiserver-advertise-address=10.0.0.5

执行上述命令的输出为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[init] Using Kubernetes version: v1.16.3
[preflight] Running pre-flight checks
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 19.03.4. Latest validated version: 18.09
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.0.0.5]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [master localhost] and IPs [10.0.0.5 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master localhost] and IPs [10.0.0.5 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 13.002108 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.16" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: kt58np.djd3youoqb0bnz4r
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[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
[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

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 10.0.0.5:6443 --token kt58np.djd3youoqb0bnz4r \
--discovery-token-ca-cert-hash sha256:37a3924142dc6d57eac2714e539c174ee3b0cda723746ada2464ac9e8a2091ce

 

保存输出中的kubeadm join部分内容,用于添加node节点,或者使用kubeadm token list 和kubeadm token create --print-join-command查看

1
2
$ kubeadm join 10.0.0.5:6443 --token kt58np.djd3youoqb0bnz4r \
--discovery-token-ca-cert-hash sha256:37a3924142dc6d57eac2714e539c174ee3b0cda723746ada2464ac9e8a2091ce

接下来执行剩余的初始化步骤

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

 

Calico网络插件的配置

Calico的官方文档地址为: https://docs.projectcalico.org/v3.10/getting-started/kubernetes/。 具体安装步骤:

  1. 安装Calico
    1
    $ kubectl apply -f https://docs.projectcalico.org/v3.10/manifests/calico.yaml
  2. 监听安装进度
    1
    $ watch kubectl get pods --all-namespaces
    出现以下内容时为安装成功
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    NAMESPACE    NAME                                       READY  STATUS   RESTARTS  AGE
    kube-system calico-kube-controllers-6ff88bf6d4-tgtzb 1/1 Running 0 2m45s
    kube-system calico-node-24h85 1/1 Running 0 2m43s
    kube-system coredns-846jhw23g9-9af73 1/1 Running 0 4m5s
    kube-system coredns-846jhw23g9-hmswk 1/1 Running 0 4m5s
    kube-system etcd-jbaker-1 1/1 Running 0 6m22s
    kube-system kube-apiserver-jbaker-1 1/1 Running 0 6m12s
    kube-system kube-controller-manager-jbaker-1 1/1 Running 0 6m16s
    kube-system kube-proxy-8fzp2 1/1 Running 0 5m16s
    kube-system kube-scheduler-jbaker-1 1/1 Running 0 5m41s
  3. 测试
    1
    2
    3
    $ kubectl get nodes -o wide
    NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
    kubernetes-master Ready master 4d12h v1.16.3 192.168.56.101 <none> CentOS Linux 7 (Core) 3.10.0-1062.el7.x86_64 docker://19.3.4

Node节点的初始化

  1. 登录node节点,执行加入集群的命令,完成加入集群操作
    1
    2
    $ kubeadm join 10.0.0.5:6443 --token kt58np.djd3youoqb0bnz4r \
    --discovery-token-ca-cert-hash sha256:37a3924142dc6d57eac2714e539c174ee3b0cda723746ada2464ac9e8a2091ce
  2. 在master节点上查看添加结果
    1
    2
    3
    4
    $ kubectl get nodes -o wide
    NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
    kubernetes-master Ready master 4d12h v1.16.3 192.168.56.101 <none> CentOS Linux 7 (Core) 3.10.0-1062.el7.x86_64 docker://19.3.4
    kubernetes-node-1 Ready <none> 4d12h v1.16.3 192.168.56.102 <none>

标签:K8S,kubernetes,23,--,笔记,v1.16,kubelet,docker,kube
From: https://www.cnblogs.com/yaoyangding/p/17234714.html

相关文章

  • 部署双主高可用K8S集群
    部署双主高可用K8S集群原创 王思宇 收录于合集#kubernetes8个#服务器9个#运维14个#容器5个#docker5个高可用原理:利用keepalived的vrrp协议创建vip,kubeadm......
  • 2023.3.19周学习总结
    一.本周任务进度1.线段树分裂合并学习完,并且练习了几个题2.上周的补题也补完了3.打了一把牛客和两把cf还有一把abc4.学习了斜率优化DP的凸包优化二.......
  • Spring Study-lesson14-事务-2023-03-19
    遵循ACID原则,这样保证批量事务其中一项报错,整个批量事务都不执行。案例:在spring-dao.xml中加载aop和tx注意细节:xmlns:aop(或tx)要有整个名字,另外注意>和“”的位......
  • 2023/3/19 考试总结
    其实今天没有什么好说的,四个半小时全在做第一题前两个小时在推式子,但其中一个半小时的式子是没用的。这时候突然知道正解怎么做了,发现是道水题,就花了一个半小时将代......
  • 2023-3-13
    2023-3-13练习题8.35证明\(\partialA=\overline{A}\cap(A^{\circ})^c\).根据定义,有\(\overline{A}\)与\((A^c)^{\circ}\)互为补集.所以有\(\overline{A}\c......
  • 玩转Git三剑客笔记
    推荐教程学习git系列教程:https://github.com/xirong/my-git实践练习进行对Git学习:https://learngitbranching.js.org/?locale=zh_CN第一章:Git基础(13讲)01|......
  • 【VTK学习笔记】VTK基本数据结构_3.2数据对象和数据集
    任务:把几何结构和拓扑结构加入到数据集中1.无拓扑结构1#include<vtkSmartPointer.h>2#include<vtkPoints.h>//几何结构3#include<vtkPolyData.h>//数据集......
  • 学习日记23.3.19
    今天在参考大佬前端代码时发现了一个小东西<htmllang="en">直接百度了一波:<htmllang="en">向搜索引擎表示该页面是html语言,并且语言为英文网站,其"lang"的意思就是“lan......
  • 2023/3/19 考试总结
    时间安排8.30~9.00T1一眼乱搞,写了一个随机旋转之后取相邻的点,然后发现过不去1e6.9.00~9.40想了个类平面最近点对的分治做法,大样例跑的起飞。9.40~11.20想了很久T2,还......
  • 【230320-1】设x,y是实数,则(2x+根号2倍y)/(2x^4+4y^4+9)的最大值是?
    ......