念念不忘
必会回响

搭建k8s集群,并安装Kubernetes-Dashboard

环境介绍

基本环境

  • CentOS Linux release 7.5.1804 (Core)
  • JDK1.8.0_161
  • Kubernetes v1.5.2
  • yum源:清华大学

部署规划

Master:

  • ip: 10.10.202.158
  • hostname: apm-slave-02
  • 安装节点
    • docker
    • etcd
    • flannel
    • kube-apiserver
    • kube-scheduler
    • kube-controller-manager

Node:

  • ip: 10.10.202.159
  • hostname: apm-slave-03
  • 安装节点
    • docker
    • flannel
    • kubelet
    • kube-proxy

防火墙

systemctl disable firewalld.service
systemctl stop firewalld.service

部署Master节点

安装Docker

yum install docker

启动docker,并加入开机启动

systemctl start docker
systemctl enable docker

安装etcd

yum install etcd -y

配置etcd,编辑 /etc/ectd/etcd.conf

vim /etc/etcd/etcd.conf
- ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379"
+ ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
- ETCD_NAME="default"
+ ETCD_NAME="master"
- ETCD_ADVERTISE_CLIENT_URLS="http://127.0.0.1:2379"
+ ETCD_ADVERTISE_CLIENT_URLS="http://apm-slave02:2379,http://apm-slave02:4001"

启动ectd

systemctl start etcd

查看服务是否启动

systemctl is-active etcd

active

获取etcd的健康指标

etcdctl -C http://apm-slave02:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://apm-slave02:2379
cluster is healthy

加入开机启动

systemctl enable etcd

安装kubernetes

yum install kubernetes

配置kubernetes,编辑 /etc/kubernetes/下面的apiserverconfigscheduler配置文件

apiserver

vim /etc/kubernetes/apiserver
- KUBE_API_ADDRESS="--address=127.0.0.1"
+ KUBE_API_ADDRESS="--address=0.0.0.0"
- KUBE_ETCD_SERVERS="--etcd-servers=http://localhost:2379"
+ KUBE_ETCD_SERVERS="--etcd-servers=http://10.10.202.158:2379"
- KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,ServiceAccount,SecurityContextDeny,ResourceQuota"
+ KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

config

vim /etc/kubernetes/config
- KUBE_MASTER="--master=http://127.0.0.1:8080"
+ KUBE_MASTER="--master=http://10.10.202.158:8080"

启动Master组件

systemctl start kube-apiserver.service
systemctl start kube-controller-manager.service
systemctl start kube-scheduler.service

加入开机启动

systemctl enable kube-apiserver.service
systemctl enable kube-controller-manager.service
systemctl enable kube-scheduler.service

检查

systemctl list-unit-files |grep kube
kube-apiserver.service                        enabled 
kube-controller-manager.service               enabled 
kube-proxy.service                            disabled
kube-scheduler.service                        enabled 
kubelet.service                               disabled

安装flannel

yum install flannel

配置flannel

vim /etc/sysconfig/flanneld
-FLANNEL_ETCD_ENDPOINTS="http://127.0.0.1:2379"
+FLANNEL_ETCD_ENDPOINTS="http://10.10.202.158:2379"

配置etcd中flannel的key

etcdctl mk /atomic.io/network/config '{ "Network": "10.0.0.0/16" }'

启动flannel

systemctl start flanneld.service

设置开机启动

systemctl enable flanneld.service

检查服务:

systemctl is-active  kube-apiserver.service kube-controller-manager.service kube-scheduler.service etcd flanneld.service

active
active
active
active
active

注意启动顺序 etcd —> kubernetes

部署Node节点

安装Docker

yum install docker

启动docker,并加入开机启动

systemctl start docker
systemctl enable docker

安装flannel

yum install flannel

配置flannel

vim /etc/sysconfig/flanneld
-FLANNEL_ETCD_ENDPOINTS="http://127.0.0.1:2379"
+FLANNEL_ETCD_ENDPOINTS="http://10.10.202.158:2379"

配置etcd中flannel的key

etcdctl mk /atomic.io/network/config '{ "Network": "10.0.0.0/16" }'

启动flannel

systemctl start flanneld.service

设置开机启动

systemctl enable flanneld.service

安装kubernetes

yum install kubernetes

node节点需要运行如下组件:

  • kubelet
  • kubernets-proxy

编辑/etc/kubernetes/config

-KUBE_MASTER="--master=http://127.0.0.1:8080"
+KUBE_MASTER="--master=http://10.10.202.158:8080"

编辑/etc/kubernetes/kubelet

###
# kubernetes kubelet (minion) config

# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"

# The port for the info server to serve on
# KUBELET_PORT="--port=10250"

# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override=apm-slave03"

# location of the api-server
KUBELET_API_SERVER="--api-servers=http://10.10.202.158:8080"

# pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"

# Add your own!
KUBELET_ARGS=""

注意此处的KUBELET_POD_INFRA_CONTAINER,它的值是registry.access.redhat.com/rhel7/pod-infrastructure:latest,如果没有安装rhsm会出错,具体请见《解决k8s出现pod服务一直处于ContainerCreating状态的问题》 。

启动kubernetes服务

systemctl start kubelet.service
systemctl start kube-proxy.service

加入开机启动

systemctl enable kubelet.service
systemctl enable kube-proxy.service

检测Node节点的服务

systemctl is-active kube-proxy.service kubelet.service flanneld.service

active
active
active

在Master(10.10.202.158)上执行如下命令:

kubectl get endpoints
NAME         ENDPOINTS            AGE
kubernetes   10.10.202.158:6443   2d
kubectl get nodes
NAME          STATUS    AGE
apm-slave03   Ready     22h

此时,k8s集群就已经安装完成。

安装Kubernetes-Dashboard

在Master节点的机器上创建两个文件dashboard-controller.yamldashboard-service.yaml,其具体内容为:

dashboard-controller.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: kubernetes-dashboard
  namespace: kube-system
  labels:
    k8s-app: kubernetes-dashboard
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ''
    spec:
      serviceAccountName: dashboard
      containers:
      - name: kubernetes-dashboard
        image: registry.cn-hangzhou.aliyuncs.com/google-containers/kubernetes-dashboard-amd64:v1.4.2
        resources:
          limits:
            cpu: 100m
            memory: 50Mi
          requests:
            cpu: 100m
            memory: 50Mi
        ports:
        - containerPort: 9090
        args:
        - --apiserver-host=http://10.10.202.158:8080
        livenessProbe:
          httpGet:
            path: /
            port: 9090
          initialDelaySeconds: 30
          timeoutSeconds: 30

注意- --apiserver-host=http://10.10.202.158:8080,请修改成自己的地址。
dashboard-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: kubernetes-dashboard
  namespace: kube-system
  labels:
    k8s-app: kubernetes-dashboard
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  type: NodePort
  selector:
    k8s-app: kubernetes-dashboard
  ports:
  - port: 80
    targetPort: 9090

执行如下命令

kubectl create -f .

显示如下日志:

deployment "kubernetes-dashboard" created
service "kubernetes-dashboard" created

查看部署

kubectl get deployments --all-namespaces                             
NAMESPACE     NAME                   DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kube-system   kubernetes-dashboard   1         1         1            1           5s

看到可用节点为1个。

查看pod

kubectl get pods -n kube-system
NAME                                    READY     STATUS    RESTARTS   AGE
kubernetes-dashboard-2620295069-12qfj   1/1       Running   0          3h

访问http://10.10.202.158:8080/ui/即可打开Dashboard

赞(0) 打赏
未经允许不得转载:堆上小栈 » 搭建k8s集群,并安装Kubernetes-Dashboard

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册