Centos7部署k8s

我用的是Centos7虚拟机,CPU个数为2,内存2G。(如果太小,安装过程中可能会报错)(踩了无数坑)

主机:192.168.40.134

节点:192.168.40.135

1.安装docker-ce

1.1安装依赖包

yum install -y yum-utils device-mapper-persistent-data lvm2

注:虚拟机使用yum install时候如果报错:could not retrieve mirrorlist...,需要改一下配置

vi /etc/sysconfig/network-scripts/ifcfg-ens33
#将ONBOOT改为yes

service network restart

1.2设置阿里云镜像源

sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

1.3安装docker-ce

注意:

如果kubernetes的版本为1.8-1.11,docker版本必须为1.11.2-1.13.1和docker-ce版本为17.03.x
如果kubernetes的版本从1.12开始,docker版本必须为17.06/17.09/18.06

这里我安装的是k8s的1.11.0版本,所以我选择docker版本为17.03.2

#如果安装过其他docker版本,可以移除旧版本
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine

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

1.4启动docker-ce

systemctl enable docker
systemctl start docker

1.5设置阿里云镜像加速

mkdir -p /etc/docker

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
}
EOF

systemctl daemon-reload
systemctl restart docker

2.配置kubenetes环境

2.1 安装 Kubeadm

#配置阿里云的国内源
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

#重建yum缓存
yum -y install epel-release
yum clean all
yum makecache

#安装
yum -y install kubelet-1.11.0 kubeadm-1.11.0 kubectl-1.11.0 kubernetes-cni-0.6.0

#启动
systemctl enable kubelet && systemctl start kubelet

2.2配置并拉取kubeadm所用的镜像

2.2.1新建一个shell脚本,如 xxx.sh
#!/bin/bash
images=(kube-proxy-amd64:v1.11.0 kube-scheduler-amd64:v1.11.0 kube-controller-manager-amd64:v1.11.0 kube-apiserver-amd64:v1.11.0
etcd-amd64:3.2.18 coredns:1.1.3 pause-amd64:3.1 kubernetes-dashboard-amd64:v1.8.3 k8s-dns-sidecar-amd64:1.14.9 k8s-dns-kube-dns-amd64:1.14.9
k8s-dns-dnsmasq-nanny-amd64:1.14.9 )
for imageName in ${images[@]} ; do
docker pull keveon/$imageName
docker tag keveon/$imageName k8s.gcr.io/$imageName
docker rmi keveon/$imageName
done
docker tag da86e6ba6ca1 k8s.gcr.io/pause:3.1
2.2.2脚本赋权
chmod -R 777 ./xxx.sh
2.2.3执行脚本
./xxx.sh

2.3关闭防火墙

#暂时关闭
systemctl stop firewalld
#永久关闭
systemctl disable firewalld

2.4关闭 Swap

#暂时关闭
swapoff -a
#永久关闭(注释掉最后带有swap的一行,前面加个#注释)
vi /etc/fstab

2.5关闭SELinux

sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux

setenforce 0

2.6配置转发参数

cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness=0
EOF

sysctl --system

3.主机配置kubernetes

3.0设置主机名

hostnamectl set-hostname master

#重新登录
reboot

#查看新主机名
hostnamectl

3.1初始化镜像

kubeadm init --kubernetes-version=v1.11.0 --pod-network-cidr=10.244.0.0/16

3.2配置kubectl认证信息

#临时
export KUBECONFIG=/etc/kubernetes/admin.conf
#永久【推荐】
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile

3.3安装Flannel网络

mkdir -p /etc/cni/net.d/

cat <<EOF> /etc/cni/net.d/10-flannel.conf
{
"name": "cbr0",
"type": "flannel",
"delegate": {
"isDefaultGateway": true
}
}
EOF

mkdir /usr/share/oci-umount/oci-umount.d -p

mkdir /run/flannel/

cat <<EOF> /run/flannel/subnet.env
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.1.0/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
EOF

3.4新建flannel.yml

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: flannel
rules:
  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - get
  - apiGroups:
      - ""
    resources:
      - nodes
    verbs:
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - nodes/status
    verbs:
      - patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-system
  labels:
    tier: node
    app: flannel
data:
  cni-conf.json: |
    {
      "name": "cbr0",
      "type": "flannel",
      "delegate": {
        "isDefaultGateway": true
      }
    }
  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: kube-flannel-ds
  namespace: kube-system
  labels:
    tier: node
    app: flannel
spec:
  template:
    metadata:
      labels:
        tier: node
        app: flannel
    spec:
      hostNetwork: true
      nodeSelector:
        beta.kubernetes.io/arch: amd64
      tolerations:
      - key: node-role.kubernetes.io/master
        operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni
        image: quay.io/coreos/flannel:v0.9.1-amd64
        command:
        - cp
        args:
        - -f
        - /etc/kube-flannel/cni-conf.json
        - /etc/cni/net.d/10-flannel.conf
        volumeMounts:
        - name: cni
          mountPath: /etc/cni/net.d
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      containers:
      - name: kube-flannel
        image: quay.io/coreos/flannel:v0.9.1-amd64
        command: [ "/opt/bin/flanneld", "--ip-masq", "--kube-subnet-mgr" ]
        securityContext:
          privileged: true
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        volumeMounts:
        - name: run
          mountPath: /run
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      volumes:
        - name: run
          hostPath:
            path: /run
        - name: cni
          hostPath:
            path: /etc/cni/net.d
        - name: flannel-cfg
          configMap:
            name: kube-flannel-cfg

执行:

kubectl create -f ./flannel.yml

3.5 查看节点信息

kubectl get nodes

如果输出类似于:

NAME               STATUS    ROLES     AGE       VERSION
master             Ready     master    21m       v1.11.0

说明主机配置完成。

4.节点配置kubenetes

4.1设置节点服务器名

hostnamectl set-hostname node1

#重新登录
reboot

#查看新名
hostnamectl

4.2节点服务器需要先执行第1步(安装docker-ce)和第2步(配置kubenetes环境)

#查看master上的token
kubeadm token list
#查看master上的证书
openssl x509 -in /etc/kubernetes/pki/ca.crt -noout -pubkey | openssl rsa -pubin -outform DER 2>/dev/null | sha256sum | cut -d' ' -f1

然后执行

kubeadm join 192.168.40.134:6443 --token oo43hw.iikfc8h0rr4di0q1 --discovery-token-ca-cert-hash sha256:17e256081bbac8c0f7c536212e61e5f467b5e240532a7fbc4bea47f90a46c07b

注:如果不知道主机ip,可以通过ifconfig查看,如果ifconfig命令找不到

需要安装net-tools.x86_64

yum install net-tools.x86_64

最后,在master上执行

kubectl get nodes

结果如下,说明配置成功

NAME               STATUS    ROLES     AGE       VERSION
master             Ready     master    21m       v1.11.0
node1              Ready     <none>    1m        v1.11.0

参考资料:https://www.cnblogs.com/myzony/p/9298783.html

posted @ 2020-01-17 15:23  剑小破  阅读(221)  评论(0编辑  收藏  举报