centos7搭建单机版kubernetes+kubernetes_dashboard

  • 由于需要工作中使用k8s,所以自己先尝试着搭建一个单机版跑个工程应用试试,下面咱们开始安装:

环境准备

准备一台服务器

  • 系统:centos7.6
  • cpu:2核
  • 内存:4G

1.关闭防火墙

systemctl disable firewalld
systemctl stop firewalld

(说实话自己用,这一步我进行了省略)

2.修改hostname

hostnamectl set-hostname k8s

3.安装etcd、kubernetes软件包(docker、kube-apiserver、kube-controller-manager、kube-scheduler、kubelet、kube-proxy)

yum install -y etcd kubernetes

4.修改配置文件
(把/etc/kubernetes/apiserver此文件其中的两行修改成如下)

vim /etc/kubernetes/apiserver
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
vim /etc/kubernetes/kubelet
KUBELET_ARGS="--cluster-dns=10.0.0.110 --cluster-domain=cluster.local"

5.安装rhsm依赖

mkdir -p /opt/soft
cd /opt/soft
yum -y install *rhsm*
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem

6.按顺序启动服务

systemctl start etcd docker kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy

7.下载docker镜像

docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest

8.搭建kubernetes-dashboard

mkdir -p /opt/k8s_project/dashboard
cd /opt/k8s_project/dashboard
touch kubernetes-dashboard.yaml
vim kubernetes-dashboard.yaml
添加下面内容(注意看注释内容)
kind: Deployment 
apiVersion: extensions/v1beta1 
metadata: 
  labels: 
    app: kubernetes-dashboard 
  name: kubernetes-dashboard 
  namespace: kube-system 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      app: kubernetes-dashboard 
  template: 
    metadata: 
      labels: 
        app: kubernetes-dashboard 
      # Comment the following annotation if Dashboard must not be deployed on master 
      annotations: 
        scheduler.alpha.kubernetes.io/tolerations: | 
          [ 
            { 
              "key": "dedicated", 
              "operator": "Equal", 
              "value": "master", 
              "effect": "NoSchedule" 
            } 
          ] 
    spec: 
      containers: 
      - name: kubernetes-dashboard 
        image: docker.io/siriuszg/kubernetes-dashboard-amd64:v1.5.1 
        imagePullPolicy: Always 
        ports: 
        - containerPort: 9090 
          protocol: TCP 
        args: 
          # Uncomment the following line to manually specify Kubernetes API server Host 
          # If not specified, Dashboard will attempt to auto discover the API server and connect 
          # to it. Uncomment only if the default does not work. 
          - --apiserver-host=http://172.17.0.1:8080 #与服务apiserver相连
        livenessProbe: 
          httpGet: 
            path: / 
            port: 9090 
          initialDelaySeconds: 30 
          timeoutSeconds: 30 
--- 
kind: Service 
apiVersion: v1 
metadata: 
  labels: 
    app: kubernetes-dashboard 
  name: kubernetes-dashboard 
  namespace: kube-system 
spec:
  type: NodePort #开启对外端口
  ports: 
  - port: 80 #为内部CLUSTER-IP对应端口80
    targetPort: 9090 #为容器内部所对应端口9090
    nodePort: 30000 #对外端口为30000
  selector: 
    app: kubernetes-dashboard
下载kubernetes-dashboard镜像
docker pull docker.io/siriuszg/kubernetes-dashboard-amd64:v1.5.1
创建kubernetes-dashboard命令
kubectl create -f kubernetes-dashboard.yaml
查看pods命令
kubectl get pods --all-namespaces -o wide
查看service命令
kubectl get svc --all-namespaces -o wide
创建好之后访问页面地址:http://服务器外网ip:30000

8.如果网外不能访问

开启防火墙
systemctl start firewalld
iptable开通端口
iptables -P FORWARD ACCEPT

这次肯定行了,亲测!!!!!!!!!

  • 下面是我自己创建的几个例子

创建nginx应用

mkdir -p /opt/k8s_project/nginx
touch nginx-rc.yaml
touch nginx-service.yaml

vim nginx-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-controller
spec:
  replicas: 1
  selector:
    name: nginx
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80

vim nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  ports:
    - port: 8001 #内部CLUSTER-IP端口8001
      targetPort: 80 #容器内部端口80
      nodePort: 30000 #外网访问ip30001
  selector:
    name: nginx

创建命令

kubectl create -f nginx-rc.yaml
kubectl create -f nginx-service.yaml
创建好之后访问页面地址:http://服务器外网ip:30001

举一反三,都是一样的原理,可以进行自己build镜像,放上自己的工程服务,只是测试一下,升入研究请搭建集群版,多学多练,谢谢大家支持

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐