跳转至

2. k8s集群安装

1. 克隆虚拟机,修改内存和处理器

#内存——4G
#处理器——2C

1.1 创建快照1!

2. 同步时间!!!

#曾经因为这个问题,导致从节点加不上,卡住我一星期!!!
yum install ntpdate -y
ntpdate -u ntp.aliyun.com

3. 设置host解析

#1.修改hostname
hostnamectl set-hostname k8s-master

# 在slave-1节点
hostnamectl set-hostname k8s-slave1

#2.修改hosts文件
cat >>/etc/hosts<<EOF
192.168.178.151 k8s-master
192.168.178.152 k8s-slave1
EOF

4. 设置iptables

iptables -P FORWARD ACCEPT

5. 关闭swap

swapoff -a
# 防止开机自动挂载 swap 分区
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

6. 关闭selinux和防火墙

sed -ri 's#(SELINUX=).*#\1disabled#' /etc/selinux/config
setenforce 0
systemctl disable firewalld && systemctl stop firewalld

7. 修改内核参数

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
vm.max_map_count=262144
EOF
#查看
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

8. 设置yum源

#此条可以不执行
curl -o /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo

#添加docker-ce的阿里源
curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

#添加k8s的阿里源
cat <<EOF > /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
EOF

#生成缓存
yum clean all && yum makecache

9. 安装docker

操作节点: 所有节点

## 查看所有的可用版本
yum list docker-ce --showduplicates | sort -r

##  安装旧版本 
#   yum install docker-ce-cli-18.09.9-3.el7  docker-ce-18.09.9-3.el7

## 安装源里最新版本(速度比较慢!)
yum install docker-ce-20.10.6 -y

## 配置docker加速
mkdir -p /etc/docker

vi /etc/docker/daemon.json
{
  "insecure-registries": [    
    "192.168.178.151:5000" 
  ],                          
  "registry-mirrors" : [
    "https://8xpk5wnt.mirror.aliyuncs.com"
  ]
}

## 启动docker
systemctl enable docker && systemctl start docker

9.1 创建快照2!!!

10. 安装 kubeadm, kubelet 和 kubectl

操作节点: 所有的master和slave节点(k8s-master,k8s-slave) 需要执行

#下载目前稳定的版本
yum install -y kubelet-1.19.8 kubeadm-1.19.8 kubectl-1.19.8 --disableexcludes=kubernetes

## 查看kubeadm 版本
kubeadm version

## 设置kubelet开机启动
systemctl enable kubelet 

11. 初始化配置文件

操作节点: 只在master节点(k8s-master)执行

#master
#1.生成kubeadm配置文件
kubeadm config print init-defaults > kubeadm.yaml

#2.查看
cat kubeadm.yaml

#3.修改配置文件中的4处
vim kubeadm.yaml
  advertiseAddress: 192.168.178.151  # apiserver地址,因为单master,所以配置master的节点内网IP
imageRepository: registry.aliyuncs.com/google_containers  # 修改成阿里镜像源
kubernetesVersion: v1.19.8 #版本改为1.19.8
#添加如下一行
  podSubnet: 10.244.0.0/16  # Pod 网段,flannel插件需要使用这个网段
[root@k8s-master ~]# kubeadm config print init-defaults > kubeadm.yaml
W0626 11:21:45.855039   24480 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[root@k8s-master ~]#
[root@k8s-master ~]# ls
anaconda-ks.cfg  kubeadm.yaml
[root@k8s-master ~]# cat kubeadm.yaml
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 1.2.3.4
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: k8s-master
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: v1.19.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
scheduler: {}
vim kubeadm.yaml

apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 192.168.178.151  # apiserver地址,因为单master,所以配置master的节点内网IP
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: k8s-master
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers  # 修改成阿里镜像源
kind: ClusterConfiguration
kubernetesVersion: v1.19.8  #版本
networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16  # Pod 网段,flannel插件需要使用这个网段
  serviceSubnet: 10.96.0.0/12
scheduler: {}

12. 提前下载镜像

操作节点:只在master节点(k8s-master)执行

#master
# 查看需要使用的镜像列表,若无问题,将得到如下列表
kubeadm config images list --config kubeadm.yaml

# 提前下载镜像到本地
kubeadm config images pull --config kubeadm.yaml

13. 初始化master节点

操作节点:只在master节点(k8s-master)执行

#master
kubeadm init --config kubeadm.yaml

若初始化成功后,最后会提示如下信息:

...
Your Kubernetes master 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.178.151:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:1c4305f032f4bf534f628c32f5039084f4b103c922ff71b12a5f0f98d1ca9a4f

接下来按照上述提示信息操作,配置kubectl客户端的认证

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s-master ~]# kubeadm config images list --config kubeadm.yaml
W0626 11:25:05.035633   41251 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
registry.aliyuncs.com/google_containers/kube-apiserver:v1.19.8
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.19.8
registry.aliyuncs.com/google_containers/kube-scheduler:v1.19.8
registry.aliyuncs.com/google_containers/kube-proxy:v1.19.8
registry.aliyuncs.com/google_containers/pause:3.2
registry.aliyuncs.com/google_containers/etcd:3.4.13-0
registry.aliyuncs.com/google_containers/coredns:1.7.0
[root@k8s-master ~]#
[root@k8s-master ~]# kubeadm config images pull --config kubeadm.yaml
W0626 11:25:12.551858   41333 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-apiserver:v1.19.8
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-controller-manager:v1.19.8
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-scheduler:v1.19.8
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-proxy:v1.19.8
[config/images] Pulled registry.aliyuncs.com/google_containers/pause:3.2
[config/images] Pulled registry.aliyuncs.com/google_containers/etcd:3.4.13-0
[config/images] Pulled registry.aliyuncs.com/google_containers/coredns:1.7.0
[root@k8s-master ~]# kubeadm init --config kubeadm.yaml
W0626 11:29:03.646974   42938 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.19.8
[preflight] Running pre-flight checks
        [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.6. Latest validated version: 19.03
[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.96.0.1 192.168.178.151]
[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.178.151 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.178.151 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 99.503370 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.19" 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 label "node-role.kubernetes.io/master=''"
[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: abcdef.0123456789abcdef
[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

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.178.151:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:e71cbfebfb50329638a107bcabb64ebe20fa393c62ee354094c6da4f6509df13
[root@k8s-master ~]#

14. 添加slave节点到集群中

操作节点:所有的slave节点(k8s-slave)需要执行 在每台slave节点,执行如下命令,该命令是在kubeadm init成功后提示信息中打印出来的,需要替换成实际init后打印出的命令。

kubeadm join 192.168.178.151:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:e71cbfebfb50329638a107bcabb64ebe20fa393c62ee354094c6da4f6509df13

如果忘记添加命令,可以通过如下命令生成:

kubeadm token create --print-join-command

15. 安装flannel插件

操作节点:只在master节点(k8s-master)执行

  • 下载flannel的yaml文件
#master
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  • 修改配置,指定网卡名称,大概在文件的190行,添加一行配置:
$ vi kube-flannel.yml
...      
      containers:
      - name: kube-flannel
        image: quay.io/coreos/flannel:v0.11.0-amd64
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        - --iface=eth0  # 如果机器存在多网卡的话,指定内网网卡的名称,默认不指定的话会找第一块网
        resources:
          requests:
            cpu: "100m"
...
  • 执行安装flannel网络插件
# 先拉取镜像,此过程国内速度比较慢
docker pull quay.io/coreos/flannel:v0.14.0-amd64
# 执行flannel安装
kubectl apply -f kube-flannel.yml

16. 设置master节点是否可调度(可选)

操作节点:k8s-master

默认部署成功后,master节点无法调度业务pod,如需设置master节点也可以参与pod的调度,需执行:

#master
kubectl taint node k8s-master node-role.kubernetes.io/master:NoSchedule-

课程后期会部署系统组件到master节点,因此,此处建议设置k8s-master节点为可调度

17. 设置kubectl自动补全

操作节点:k8s-master

#master
yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc

18. 验证集群

操作节点: 在master节点(k8s-master)执行

kubectl get nodes  #观察集群节点是否全部Ready
[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   14m   v1.19.8
k8s-slave1   Ready    <none>   13m   v1.19.8

创建测试nginx服务

kubectl run  test-nginx --image=nginx:alpine

查看pod是否创建成功,并访问pod ip测试是否可用

$ kubectl get po -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP           NODE         NOMINATED NODE   READINESS GATES
test-nginx-5bd8859b98-5nnnw   1/1     Running   0          9s    10.244.1.2   k8s-slave1   <none>           <none>
$ curl 10.244.1.2
...
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

19. 查看镜像

[root@k8s-master ~]# docker images
REPOSITORY                                                        TAG             IMAGE ID       CREATED         SIZE
quay.io/coreos/flannel                                            v0.14.0         8522d622299c   5 weeks ago     67.9MB
quay.io/coreos/flannel                                            v0.14.0-amd64   8522d622299c   5 weeks ago     67.9MB
registry.aliyuncs.com/google_containers/kube-apiserver            v1.19.8         9ba91a90b7d1   4 months ago    119MB
registry.aliyuncs.com/google_containers/kube-proxy                v1.19.8         ea03182b84a2   4 months ago    118MB
registry.aliyuncs.com/google_containers/kube-controller-manager   v1.19.8         213ae7795128   4 months ago    111MB
registry.aliyuncs.com/google_containers/kube-scheduler            v1.19.8         919a3f36437d   4 months ago    46.5MB
registry.aliyuncs.com/google_containers/etcd                      3.4.13-0        0369cf4303ff   10 months ago   253MB
registry.aliyuncs.com/google_containers/coredns                   1.7.0           bfe3a36ebd25   12 months ago   45.2MB
registry.aliyuncs.com/google_containers/pause                     3.2             80d28bedfe5d   16 months ago   683kB

20. 创建快照3!!!


最后更新: 2022-02-22 04:55:01