Centos安装 k8s
作者:互联网
### 一. 安装docker
1.安装依赖包
```shell
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
```
2.设置阿里云镜像源
```shell
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
```
### 使用docker脚本一键安装
```shell
curl -fsSL "https://get.docker.com/" | sh
systemctl enable --now docker
```
3.安装Docker-CE
# Install Docker CE
## Set up the repository
### Install required packages.
```shell
yum install yum-utils device-mapper-persistent-data lvm2
```
### Add Docker repository.
```shell
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
```
### Install Docker CE.
```shell
yum update && yum install docker-ce-18.06.2.ce
```
### Create /etc/docker directory.
```shell
mkdir /etc/docker
```
### Setup daemon.
```shell
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
mkdir -p /etc/systemd/system/docker.service.d
```
### Restart Docker
```shell
systemctl daemon-reload
systemctl restart docker
```
### 安装k8s
在Docker安装完成的基础上,完成一下步骤
#### 使用阿里云镜像源
```shell
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF
```
#### 关闭SElinux
```shell
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
```
#### 安装kubelet kubeadm kubectl
```shell
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet #
开机启动kubelet
```
#### centos7用户还需要设置路由:
```shell
yum install -y bridge-utils.x86_64
modprobe br_netfilter # 加载br_netfilter模块,使用lsmod查看开启的模块
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 重新加载所有配置文件
systemctl disable --now firewalld # 关闭防火墙
```
#### k8s要求关闭swap (qxl)
```shell
swapoff -a && sysctl -w vm.swappiness=0 # 关闭swap
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab # 取消开机挂载swap
```
#### 国外服务器
```shell
kubeadm config images pull #拉取集群所需镜像,这个需要翻墙
```
#### 国内服务器
```shell
kubeadm config images list #列出所需镜像,根据所需镜像安装
```
#(不是一定是下面的,根据实际情况来选择版本)
#建议节点中也进行此操作安装
# 根据所需镜像名字先拉取国内资源
```shell
docker pull mirrorgooglecontainers/kube-apiserver:v1.14.2
docker pull mirrorgooglecontainers/kube-controller-manager:v1.14.2
docker pull mirrorgooglecontainers/kube-scheduler:v1.14.2
docker pull mirrorgooglecontainers/kube-proxy:v1.14.2
docker pull mirrorgooglecontainers/pause:3.1
docker pull mirrorgooglecontainers/etcd:3.3.10
docker pull coredns/coredns:1.3.1 # 这个在mirrorgooglecontainers中没有
# 修改镜像tag
docker tag mirrorgooglecontainers/kube-apiserver:v1.14.2 k8s.gcr.io/kube-apiserver:v1.14.2
docker tag mirrorgooglecontainers/kube-controller-manager:v1.14.2 k8s.gcr.io/kube-controller-manager:v1.14.2
docker tag mirrorgooglecontainers/kube-scheduler:v1.14.2 k8s.gcr.io/kube-scheduler:v1.14.2
docker tag mirrorgooglecontainers/kube-proxy:v1.14.2 k8s.gcr.io/kube-proxy:v1.14.2
docker tag mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag mirrorgooglecontainers/etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
docker tag coredns/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1
# 把所需的镜像下载好,init的时候就不会再拉镜像,由于无法连接google镜像库导致出错
# 删除原来的镜像
docker rmi mirrorgooglecontainers/kube-apiserver:v1.14.2
docker rmi mirrorgooglecontainers/kube-controller-manager:v1.14.2
docker rmi mirrorgooglecontainers/kube-scheduler:v1.14.2
docker rmi mirrorgooglecontainers/kube-proxy:v1.14.2
docker rmi mirrorgooglecontainers/pause:3.1
docker rmi mirrorgooglecontainers/etcd:3.3.10
docker rmi coredns/coredns:1.3.1
```
```shell
cat >> kubeadm-config.yaml <<EOF
apiServer:
timeoutForControlPlane: 4m0s
extraArgs:
enable-admission-plugins: NamespaceLifecycle,LimitRanger,ServiceAccount,TaintNodesByCondition,Priority,DefaultTolerationSeconds,DefaultStorageClass,PersistentVolumeClaimResize,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota
runtime-config: "api/all=true"
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: stable
#下面HAPROXY_IPmaster的ip
controlPlaneEndpoint: "HAPROXY_IP:8443"
dns:
type: "CoreDNS"
networking:
dnsDomain: cluster.local
podSubnet: "10.244.0.0/16"
EOF
```
#### 初始化k8s
```shell
kubeadm reset --force #重置集群,初始化出错可以使用
#k8s初始化
#对hostname命名也有规范,报hostname不规范修改/etc/hostname,然后reboot就行了
kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs
```
#### 初始化成功会生成token
```shell
#每个机器创建的master以下部分都不同,需要自己保存好,用于添加k8s节点
kubeadm join ip:8443 --token xxx \
--discovery-token-ca-cert-hash sha256:xxxx
```
#### 普通用户设置权限
```shell
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
```
#### 安装网络calico、flannel
```bash
# use calico cni
kubectl apply -f https://docs.projectcalico.org/v3.7/manifests/canal.yaml
# use standard flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.11.0/Documentation/kube-flannel.yml
```
#### node加入集群中
- node需要安装docker和kubectl\kubecadm\kubelet,且版本与master的版本一致。
```bash
#主节点初始化生成的token,在节点中使用
kubeadm join master_ip:8443 --token xxx \
--discovery-token-ca-cert-hash sha256:xxxx
# 加入成功后查看master的nodes数目,稍等节点进行初始化,然后从notready到ready的状态。
```
标签:2docker,k8s,Centos,v1.14,mirrorgooglecontainers,docker,kube,安装 来源: https://www.cnblogs.com/dncey/p/11460151.html