Kubernetes搭建

1.修改Hostname并设置Hosts

2.关闭swap

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

3.关闭防火墙

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

4.修改内核参数

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

5.设置YUM源

curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
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

6.所有节点安装docker

7.所有节点安装

yum install -y kubelet-1.16.2 kubeadm-1.16.2 kubectl-1.16.2 --disableexcludes=kubernetes
systemctl enable kubelet

8.在Master初始化配置文件

kubeadm config print init-defaults > kubeadm.yaml
cat kubeadm.yaml
###修改###
advertiseAddress: 192.168.188.8
imageRepository: registry.aliyuncs.com/google_containers
###添加podSubnet###
networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16  # Pod 网段,flannel插件需要使用这个网段
  serviceSubnet: 10.96.0.0/12
scheduler: {}

9.在Master提前下载镜像

kubeadm config images pull --config kubeadm.yaml

10.初始化Master节点

kubeadm init --config kubeadm.yaml

11.配置kubectl客户端的认证

 mkdir -p $HOME/.kube
 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
 sudo chown $(id -u):$(id -g) $HOME/.kube/config

12.添加Slave节点

###第十步打印出的###
kubeadm join 192.168.188.8:6443 --token ***

13.安装Flannel插件

下载配置文件

wget https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/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=ens33   #修改此行
        resources:
          requests:
            cpu: "100m"

安装flannel

docker pull quay.io/coreos/flannel:v0.11.0-amd64
kubectl create -f kube-flannel.yml

14.部署Dashboard

下载配置文件

wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta5/aio/deploy/recommended.yaml

修改配置文件

vi recommended.yaml
...
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 443
      targetPort: 8443
  selector:
    k8s-app: kubernetes-dashboard
  type: NodePort  # 加上type=NodePort变成NodePort类型的服务

安装

kubectl create -f recommended.yaml
kubectl -n kubernetes-dashboard get svc   #查看访问地址

创建ServiceAccount进行访问

vi admin.conf
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: admin
  annotations:
    rbac.authorization.kubernetes.io/autoupdate: "true"
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: admin
  namespace: kubernetes-dashboard

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin
  namespace: kubernetes-dashboard
kubectl create -f admin.conf
kubectl -n kubernetes-dashboard get secret |grep admin-token
kubectl -n kubernetes-dashboard get secret admin-token-***** -o jsonpath={.data.token}|base64 -d

15.使用火狐浏览器访问Dashboard

16.查看节点状态

发表评论