k8s-centos7+docker+kubernetes1.14.1 安装

最近想学习下k8s,无奈自己爱瞎捣鼓,想学习下。那就先安装个环境吧!中间遇到很多个问题,也都一一解决了,把自己遇到的坑分享给跟我一样爱瞎捣鼓的人,减少很多弯路,不折腾就难受。参考了很多网上的资料

由于我电脑配置不高,虚拟机就开了两个节点,master+node1.我自己用的是kubeadm方式安装的。

192.168.254.100 k8s-master
192.168.254.101 k8s-node1

首先是centos7系统配置检查,我每台虚拟机都是分配的是2核+2G.这也是官方要求,具体安装虚拟机的过程就不赘述。

以下操作两个节点都要做并且是root用户安装

环境准备

1、关闭防火墙

 systemctl stop firewalld & systemctl disable firewalld

2、关闭selinux

sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

3、关闭swap:

swapoff -a #临时关闭
vim /etc/fstab #注释掉swap即可永久关闭

Linux的Swap内存交换机制是一定要关闭的,否则会因为内存交换而影响性能以及稳定性。

编辑/etc/fstab,注释掉包含swap的那一行即可,重启后可永久关闭

(或直接执行:sed -i '/ swap / s/^/#/' /etc/fstab 永久关闭)

4、添加主机名与IP对应关系:

5、配置阿里云yum源、配置docker仓库、配置K8S的yum源

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=1
 
repo_gpgcheck=1
 
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
 
        http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
 
EOF
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache

安装docker

执行以下命令,安装17.03 Docker:有些博客写的是 kubeadm 支持的最新版本docker就是1.7,为了安全起见,我也安装的是1.7

yum install -y --setopt=obsoletes=0 docker-ce-17.03.2.ce-1.el7.centos.x86_64 docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch

运行docker --version,可以看到安装了1.7.版本:

启动Docker服务并激活开机启动:

systemctl start docker & systemctl enable docker

安装k8s的组件

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

我现在安装的是 1.14.1版本

启动kubelet并设置kubelet开机自启

systemctl enable kubelet&& systemctl start kubelet

运行下面的命令可以查看 初始化kubernets需要的docker镜像

kubeadm config images list

意思是这些镜像我们都要pull到docker里面去,还好这些在阿里云的镜像源里都有,只是需要更改tag名称。

下载k8s需要的镜像

1、创建/etc/sysctl.d/k8s.conf文件,添加如下内容:

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

执行下命令使修改生效。

modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

2、修改docker镜像加速地址

vim /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}

【1】:阿里云docker仓库 https://dev.aliyun.com/search.html

【2】:进去注册帐号后,点击自己的管理中心。

【3】:在管理中心点击加速器,右边面板会有你的加速地址,右边面板下面有详细设置步骤。

https://xxxxx.mirror.aliyuncs.com是我个人的阿里云docker加速地址,配置成自己的就好。

并重启docker生效

systemctl daemon-reload

systemctl restart docker.service

3、在线下载镜像文件

新建 k8s-docker.sh,内容如下,如果懒的话就直接下载我做好的 k8s-docker.sh

echo ""
echo "=========================================================="
echo "Pull Kubernetes v1.14.1 Images from aliyuncs.com ......"
echo "=========================================================="
echo ""

MY_REGISTRY=registry.cn-hangzhou.aliyuncs.com/openthings

# 基本组件
docker pull ${MY_REGISTRY}/k8s-gcr-io-kube-apiserver:v1.14.1
docker pull ${MY_REGISTRY}/k8s-gcr-io-kube-controller-manager:v1.14.1
docker pull ${MY_REGISTRY}/k8s-gcr-io-kube-scheduler:v1.14.1
docker pull ${MY_REGISTRY}/k8s-gcr-io-kube-proxy:v1.14.1
docker pull ${MY_REGISTRY}/k8s-gcr-io-etcd:3.3.10
docker pull ${MY_REGISTRY}/k8s-gcr-io-pause:3.1
docker pull ${MY_REGISTRY}/k8s-gcr-io-coredns:1.3.1

# 修改tag
docker tag ${MY_REGISTRY}/k8s-gcr-io-kube-apiserver:v1.14.1 k8s.gcr.io/kube-apiserver:v1.14.1
docker tag ${MY_REGISTRY}/k8s-gcr-io-kube-scheduler:v1.14.1 k8s.gcr.io/kube-scheduler:v1.14.1
docker tag ${MY_REGISTRY}/k8s-gcr-io-kube-controller-manager:v1.14.1 k8s.gcr.io/kube-controller-manager:v1.14.1
docker tag ${MY_REGISTRY}/k8s-gcr-io-kube-proxy:v1.14.1 k8s.gcr.io/kube-proxy:v1.14.1
docker tag ${MY_REGISTRY}/k8s-gcr-io-etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
docker tag ${MY_REGISTRY}/k8s-gcr-io-pause:3.1 k8s.gcr.io/pause:3.1
docker tag ${MY_REGISTRY}/k8s-gcr-io-coredns:1.3.1 k8s.gcr.io/coredns:1.3.1

## 删除镜像
docker rmi ${MY_REGISTRY}/k8s-gcr-io-kube-apiserver:v1.14.1
docker rmi ${MY_REGISTRY}/k8s-gcr-io-kube-controller-manager:v1.14.1
docker rmi ${MY_REGISTRY}/k8s-gcr-io-kube-scheduler:v1.14.1
docker rmi ${MY_REGISTRY}/k8s-gcr-io-kube-proxy:v1.14.1
docker rmi ${MY_REGISTRY}/k8s-gcr-io-etcd:3.3.10
docker rmi ${MY_REGISTRY}/k8s-gcr-io-pause:3.1
docker rmi ${MY_REGISTRY}/k8s-gcr-io-coredns:1.3.1

echo ""
echo "=========================================================="
echo "Pull Kubernetes v1.14.1 Images FINISHED."
echo "           by https://blog.csdn.net/yulong_1988"
echo "=========================================================="

echo ""
sh k8s-docker.sh

运行安装,安装完后查看docker images 是否下载成功

以上操作在两个节点上都要进行,一下操作只在master上进行

初始化kubernetes

【1】运行安装命令

kubeadm init --kubernetes-version=1.14.1 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.254.100
[init] Using Kubernetes version: v1.14.1
[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/
[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-kubelet-client" 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.254.100]
[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.254.100 127.0.0.1 ::1]
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.254.100 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-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 19.004589 seconds
[upload-config] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.14" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --experimental-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: eehowx.9lau2dfgsga63qgb
[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 192.168.254.100:6443 --token eehowx.9lau2dfgsga63qgb \
    --discovery-token-ca-cert-hash sha256:eeabf48bb9043f9517d9d4430dd4ea24f111f865e9d0383e24008b3510934214 

【2】为kubectl准备Kubeconfig文件。

#kubectl默认会在执行的用户家目录下面的.kube目录下寻找config文件。这里是将在初始化时[kubeconfig]步骤生成的admin.conf拷贝到.kube/config。

#在该配置文件中,记录了API Server的访问地址,所以后面直接执行kubectl命令就可以正常连接到API Server中。

mkdir -p $HOME/.kube

cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

chown root.root /root/.kube/config

这个时候可以让node1节点加入进来了

切换到node1,按照上面的提示执行join命令

kubeadm join 192.168.254.100:6443 --token eehowx.9lau2dfgsga63qgb --discovery-token-ca-cert-hash sha256:eeabf48bb9043f9517d9d4430dd4ea24f111f865e9d0383e24008b3510934214

[root@centos75 ~]# kubectl get node 获取node信息表示命令使用正常

安装配置 flannel 网络

一开始我下载的是0.10.0版本的镜像,安装了好几次都失败了,后来重新安装的 11.0就好了,建议大家安装11.0

去docker-hub先随便找个flannel 0.11.0的镜像,pull下来。

docker pull jmgao1983/flannel:v0.11.0-amd64

下载 kube-flannel.yml 配置文件

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

替换kube-flannel.yml中的镜像名称

sed -i 's#quay.io/coreos/flannel:v0.11.0-amd64#jmgao1983/flannel:v0.11.0-amd64#g' kube-flannel.yml

如果懒的话也可以下载我替换好的 kube-flannel.yml

运行下面命令安装flannel

kubectl apply -f kube-flannel.yml

 

再次查看node和 Pod状态,全部为Running

先这样吧,回忆着写的,如果有写的错的地方请大家指出,下一篇文章再安装 kubernetes dashboard,界面管理k8s

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值