美文网首页K8
centos8搭建单机k8s

centos8搭建单机k8s

作者: 孙广宁 | 来源:发表于2022-05-11 22:11 被阅读0次

    一、单机安装k8s

    • 配置yum源 /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
    enabled=1
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    
    • 关闭防火墙
    systemctl stop firewalld
    systemctl disable firewalld
    
    • 关闭selinux
    setenforce 0
    
    • 修改文件/etc/selinux/config
    vim /etc/selinux/config
    SELINUX=disabled
    SELINUXTYPE=targeted
    
    • 禁用swap
    swapoff -a
    
    • 修改文件/etc/sysctl.d/k8s.conf
    cat > /etc/sysctl.d/k8s.conf <<EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
    sysctl --system
    
    • 安装docker
    yum -y install yum-utils device-mapper-persistent-data lvm2
    yum-config-manager -y --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    yum -y install docker-ce-18.06.3.ce-3.el7 docker-ce-cli-18.06.3.ce-3.el7 containerd.io
    systemctl start docker
    systemctl enable docker
    
    • 安装kubelet
    yum -y install kubelet-1.13* kubeadm-1.13* kubectl-1.13*
    systemctl start kubelet
    systemctl enable kubelet
    
    • 拉取k8s镜像
    docker pull mirrorgooglecontainers/kube-apiserver:v1.13.3
    docker pull mirrorgooglecontainers/kube-controller-manager:v1.13.3
    docker pull mirrorgooglecontainers/kube-scheduler:v1.13.3
    docker pull mirrorgooglecontainers/kube-proxy:v1.13.3
    docker pull mirrorgooglecontainers/pause:3.1
    docker pull mirrorgooglecontainers/etcd:3.2.24
    docker pull coredns/coredns:1.2.6
    
    • 重新打tag
    docker tag mirrorgooglecontainers/kube-apiserver:v1.13.3 k8s.gcr.io/kube-apiserver:v1.13.3
    docker tag mirrorgooglecontainers/kube-controller-manager:v1.13.3 k8s.gcr.io/kube-controller-manager:v1.13.3
    docker tag mirrorgooglecontainers/kube-scheduler:v1.13.3 k8s.gcr.io/kube-scheduler:v1.13.3
    docker tag mirrorgooglecontainers/kube-proxy:v1.13.3 k8s.gcr.io/kube-proxy:v1.13.3
    docker tag mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
    docker tag mirrorgooglecontainers/etcd:3.2.24 k8s.gcr.io/etcd:3.2.24
    docker tag coredns/coredns:1.2.6 k8s.gcr.io/coredns:1.2.6
    
    • 初始化 k8s
    kubeadm init --kubernetes-version=v1.13.3
    
    • 初始化配置
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    kubectl apply -f https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')
    
    • 查看节点状态
    [root@apollo-db ~]# kubectl get nodes
    NAME        STATUS   ROLES    AGE   VERSION
    apollo-db   Ready    master   2d    v1.13.12
    
    • 查看是否创建成功
    [root@apollo-db ~]# kubectl get pods --all-namespaces
    NAMESPACE     NAME                                READY   STATUS    RESTARTS   AGE
    kube-system   coredns-86c58d9df4-5nxv9            1/1     Running   0          2d
    kube-system   coredns-86c58d9df4-grjwn            1/1     Running   0          2d
    kube-system   etcd-apollo-db                      1/1     Running   0          2d
    kube-system   kube-apiserver-apollo-db            1/1     Running   0          2d
    kube-system   kube-controller-manager-apollo-db   1/1     Running   0          2d
    kube-system   kube-proxy-x9dd8                    1/1     Running   0          2d
    kube-system   kube-scheduler-apollo-db            1/1     Running   0          2d
    kube-system   weave-net-mpmzq                     2/2     Running   2          2d
    [root@apollo-db ~]#
    
    • 允许master创建资源
    kubectl taint nodes --all node-role.kubernetes.io/master-
    

    二、 helm测试

    • 下载并安装helm工具
    wget https://storage.googleapis.com/kubernetes-helm/helm-v2.11.0-linux-amd64.tar.gz
    tar -zxvf helm-v2.11.0-linux-amd64.tar.gz
    cd linux-amd64/
    cp helm /usr/local/bin/
    
    • 添加测试的helm repo仓库
    helm repo add apphub https://apphub.aliyuncs.com
    
    • 查看helm repo仓库
    [root@apollo-db ~]# helm repo list
    NAME    URL
    apphub  https://apphub.aliyuncs.com
    
    • 删除和更新helm repo仓库
    helm repo remove aliyun  
    helm repo update
    
    • 安装nginx测试
    [root@apollo-db ~]# helm search repo nginx
    NAME                            CHART VERSION   APP VERSION             DESCRIPTION
    apphub/nginx                    5.1.5           1.16.1                  Chart for the nginx server
    apphub/nginx-ingress            1.30.3          0.28.0                  An nginx Ingress controller that uses ConfigMap...
    apphub/nginx-ingress-controller 5.3.4           0.29.0                  Chart for the nginx Ingress controller
    apphub/nginx-lego               0.3.1                                   Chart for nginx-ingress-controller and kube-lego
    apphub/nginx-php                1.0.0           nginx-1.10.3_php-7.0    Chart for the nginx php server
    
    • helm安装nginx
    [root@apollo-db ~]# helm install appnginx apphub/nginx
    NAME: appnginx
    LAST DEPLOYED: Wed May 11 22:08:20 2022
    NAMESPACE: default
    STATUS: deployed
    REVISION: 1
    TEST SUITE: None
    NOTES:
    Get the NGINX URL:
    
      NOTE: It may take a few minutes for the LoadBalancer IP to be available.
            Watch the status with: 'kubectl get svc --namespace default -w appnginx'
    
      export SERVICE_IP=$(kubectl get svc --namespace default appnginx --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")
      echo "NGINX URL: http://$SERVICE_IP/"
    [root@apollo-db ~]#
    
    • 查看安装结果
    [root@apollo-db ~]# helm list
    NAME        NAMESPACE   REVISION    UPDATED                                 STATUS      CHART       APP VERSION
    appnginx    default     1           2022-05-11 22:08:20.248320947 +0800 CST deployed    nginx-5.1.5 1.16.1
    [root@apollo-db ~]#
    
    • 查看pod service deploy
    [root@apollo-db ~]# kubectl get pod
    NAME                        READY   STATUS    RESTARTS   AGE
    appnginx-65cbf4d8d8-c2vqr   1/1     Running   0          66s
    [root@apollo-db ~]# kubectl get svc
    NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
    appnginx     LoadBalancer   10.108.252.26   <pending>     80:31380/TCP,443:31180/TCP   69s
    kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP                      2d
    [root@apollo-db ~]# kubectl get deploy
    NAME       READY   UP-TO-DATE   AVAILABLE   AGE
    appnginx   1/1     1            1           103s
    [root@apollo-db ~]#
    
    • 卸载安装
    [root@apollo-db ~]# helm uninstall appnginx
    release "appnginx" uninstalled
    [root@apollo-db ~]#
    

    相关文章

      网友评论

        本文标题:centos8搭建单机k8s

        本文链接:https://www.haomeiwen.com/subject/jpedurtx.html