Kubernetes 基础环境安装

程序员 2022-11-17 PM 180℃ 0条
相关术语
  • 主机Master:k8s里面的master指的是集群控制节点。每个k8s集群都需要一个master节点来负责整个集群的管理和控制,基本上k8s所有的控制命令都是发
  • 节点Node:负责执行请求和所在分配任务的计算机,由k8s主机负责对节点进行控制;Node可以是一台物理主机,也可以是一台虚拟机
  • Pod:Pod就是一组容器的集合,在Pod里面的容器共享网络/存储,所以他们可以通过localhost进行内部通信。虽然网络和存储是共享的,但是cpu和memery不是共享,多容器之间可以有属于自己的CGroup,也就说可以单独对pod中的容器做资源限制
环境准备

0、linux镜像地址:http://mirror.nsc.liu.se/centos-store/7.5.1804/isos/x86_64/

1、关闭防火墙以及开机自启动

#systemctl stop firewalld && systemctl disable firewalld
防火墙导致端口不通

2、关闭selinux 以及开机自启

#sestatus // 查看selinux状态
#setenforce 0 // 临时关闭selinux,重启后被打开
#vim /etc/selinux/config
selinux=disabled  //重启后关闭
//可能导致错误,selinux已经被flannel网络组件弃用
https://github.com/flannel-io/flannel/issues/709

3、关闭内存swap

# swapoff -a
# free -m // 查看是否关闭
              total        used        free      shared  buff/cache   available
Mem:           4460         961        2660          10         839        3430
Swap:             0           0           0
# vim /etc/fstab  // 永久关闭,重启生效,注释掉最后一行
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

4、加载ipvs模块,kube-proxy使用ipvs模式

cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
//可能出现的问题
[ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
# echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables  // 设置错误

5、配置yum源

cat>>/etc/yum.repos.d/kubrenetes.repo<<EOF
[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
EOF
安装

三台虚拟机

ip角色主机名
192.168.42.179master主节点k8s-master
192.168.47.139node1从节点k8s-node1
  • 设置主机名称
hostnamectl --static set-hostname k8s-master
hostnamectl --static set-hostname k8s-node1
  • 加入/etc/hosts
10.0.0.6  k8s-master
10.0.0.10  k8s-node1
10.0.0.9  k8s-node2
  • yum 安装软件
- kubeadm:用来初始化集群的指令
- kubelet:在集群中的每个节点用来启动pod和container等
- kubectl:用来与集群通信的命令行工具
yum install -y kubelet kubeadm kubectl
kubelet --version //查看版本
systemctl enable kubelet // 开机启动
  • 启动master节点
[root@k8s-master ~]# kubeadm init --kubernetes-version=v1.20.4 \
> --image-repository registry.aliyuncs.com/google_containers \
> --apiserver-advertise-address=10.0.0.6 \
> --service-cidr=10.1.0.0/16 \
> --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.20.4
[preflight] Running pre-flight checks
[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'
[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 [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.1.0.1 192.168.42.179]
[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 [k8s-master localhost] and IPs [192.168.42.179 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.42.179 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
[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] Starting the kubelet
[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
[kubelet-check] Initial timeout of 40s passed.
[apiclient] All control plane components are healthy after 59.502340 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.20" 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 k8s-master as control-plane by adding the labels "node-role.kubernetes.io/master=''" and "node-role.kubernetes.io/control-plane='' (deprecated)"
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 08tizs.47bj2g1lc3l7vopr
[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.42.179:6443 --token 08tizs.47bj2g1lc3l7vopr \
    --discovery-token-ca-cert-hash sha256:351888c88e02470dd36753391c34f646a31bf1c1bcdbc7c308b7c024efba44d0 
    
// 启动成功之后,执行

--kubernetes-version:指定版本

--apiserver-advertise-address:用户指定master的那个监听地址

--pod-network-cidr:指定pod的网络范围,该参数依赖于使用的网络方案,使用经典的flannel网络

  • 查看节点信息
[root@k8s-master ~]# kubectl get node
NAME         STATUS   ROLES                  AGE   VERSION
k8s-master   NotReady    control-plane,master   78m   v1.20.4
  • 安装flannel网络组件
[root@k8s-master ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
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
[root@k8s-master ~]# kubectl get pod -n kube-system  // 查看组件状态
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-7f89b7bc75-lrjcf             1/1     Running   0          91m
coredns-7f89b7bc75-ssm2t             1/1     Running   0          91m
etcd-k8s-master                      1/1     Running   0          92m
kube-apiserver-k8s-master            1/1     Running   0          92m
kube-controller-manager-k8s-master   1/1     Running   0          92m
kube-flannel-ds-vwzhq                1/1     Running   0          23s    // flannel网络组件
kube-proxy-8bssr                     1/1     Running   0          91m
kube-scheduler-k8s-master            1/1     Running   0          92m
  • 加入从节点node1,master节点启动成功,可以看到
kubeadm join 192.168.42.179:6443 --token 08tizs.47bj2g1lc3l7vopr \
    --discovery-token-ca-cert-hash sha256:351888c88e02470dd36753391c34f646a31bf1c1bcdbc7c308b7c024efba44d0 

在master节点上,查看状态

[root@k8s-master ~]# kubectl get node
NAME         STATUS     ROLES                  AGE   VERSION
k8s-master   Ready      control-plane,master   94m   v1.20.4
k8s-node2    NotReady   <none>                 7s    v1.20.4
[root@k8s-master ~]# kubectl get pod -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-7f89b7bc75-lrjcf             1/1     Running   0          94m
coredns-7f89b7bc75-ssm2t             1/1     Running   0          94m
etcd-k8s-master                      1/1     Running   0          95m
kube-apiserver-k8s-master            1/1     Running   0          95m
kube-controller-manager-k8s-master   1/1     Running   0          95m
kube-flannel-ds-vwzhq                1/1     Running   0          3m23s
kube-flannel-ds-xv8fm                1/1     Running   0          35s
kube-proxy-8bssr                     1/1     Running   0          94m
kube-proxy-cjrlf                     1/1     Running   0          35s
kube-scheduler-k8s-master            1/1     Running   0          95m
标签: k8s

非特殊说明,本博所有文章均为博主原创。

评论啦~