k8s 安装_Centos7上安装K8S

e06b7888def117dfea2bb3d14c9c3635.png

1.首先安装docker

参考Centos7上安装docker

2.安装k8s的kubeadm

yum install -y kubelet-1.18.0  kubeadm-1.18.0  kubectl-1.18.0 --disableexcludes=kubernetes

这是安装的1.18版本,可以选择默认安装最后的版本:

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

3.通过kubeadm config下载配置文件

kubeadm config print init-defaults > kube-default.conf

该命令会将官方默认的参数导入到kube-default.conf文件中

4.修改默认的配置文件

1)advertiseAddress 
 修改该参数为本地IP地址.
2)imageRepository
 修改默认镜像地址为阿里
 registry.cn-hangzhou.aliyuncs.com/google_containers
3)kubernetesVersion
  修改k8s的版本号为你下载的版本

5.参考配置好的文件

apiVersion

6.关闭swap

swapoff -a

7.修改为桥接网络

echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
    (
        cat <<EOF >  /etc/sysctl.d/k8s.conf
        net.bridge.bridge-nf-call-ip6tables = 1
        net.bridge.bridge-nf-call-iptables = 1
        EOF
        sysctl --system
    )

8.关闭防火墙

systemctl stop firewalld && systemctl disable firewalld

9.初始化k8s集群

kubeadm init --config kube-default.conf

如果上边没使用配置文件的形式,可以通过参数形式:

kubeadm init 
    --apiserver-advertise-address=192.168.124.42 
    --image-repository registry.aliyuncs.com/google_containers 
    --kubernetes-version v1.18.0 
    --service-cidr=10.96.0.0/12 
    --pod-network-cidr=10.244.0.0/16

10.如果显示下面信息表示成功

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.124.42:6443 --token e64cbs.9j6k4o6yanc701ko 
    --discovery-token-ca-cert-hash sha256:8698adc8199da7a211273957f9996a54025430d37926aa7f79ac8ceeaf0594da

11.检查k8s状态

kubectl get cs

从节点

1.安装工具

yum install -y kubelet-1.18.0  kubeadm-1.18.0  kubectl-1.18.0 --disableexcludes=kubernetes

从节点安装的k8s版本和master需要一致

2.复制刚才master生成的token命令,进行加入

kubeadm join 192.168.124.42:6443 --token e64cbs.9j6k4o6yanc701ko 
    --discovery-token-ca-cert-hash sha256:8698adc8199da7a211273957f9996a54025430d37926aa7f79ac8ceeaf0594da

3.如果token失效,或者没记录,可以通过以下命令获取

kubeadm token create --print-join-command

或者

kubeadm token list