美文网首页
k8s-环境搭建-基于ubuntu server18.10

k8s-环境搭建-基于ubuntu server18.10

作者: 西海岸虎皮猫大人 | 来源:发表于2020-07-17 22:18 被阅读0次

    0.环境:

    ubuntu: server 18.10
    k8s: 1.13.1
    docker: ce 18.06.1
    虚拟机: VMware Workstation 15 Pro
    相关文件: https://pan.baidu.com/s/19b1bGwRA_COg5r65LrpjYA
    提取码:jyyz

    1.环境准备

    # 开启root用户
    su passwd root
    # 允许root远程登录
    vi /etc/ssh/sshd_config
    PermitRootLogin yes
    
    service ssh restart
    
    # 配置ubuntu阿里云镜像
    cp /etc/apt/sources.list /etc/apt/sources.list.bak
    vi /etc/apt/sources.list
    
    deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    
    apt-get update
    
    # 安装相关包
    apt-get install -y curl telnet wget man apt-transport-https ca-certificates software-properties-common vim
    

    2.安装docker

    # 安装docker(离线)
    # 下载地址
    https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/docker-ce_18.06.1~ce~3-0~ubuntu_amd64.deb
    
    # 安装依赖
    apt-get install -y libltdl7
    # 安装
    dpkg -i docker-ce_18.06.1~ce~3-0~ubuntu_amd64.deb
    # 启动docker并配置开机启动
    systemctl enable docker
    systemctl start docker
    # 查看镜像
    docker ps
    # 启动docker容器
    # -it 回显字符 --rm关闭后删除 sh进入shell
    docker run -it --rm alpine:latest sh
    # 当前用户添加docker权限
    sudo groupadd docker
    sudo usermod -aG docker $USER
    
    # 配置docker阿里云镜像
    vi /etc/docker/daemon.json
    
    {
                "registry-mirrors": ["https://2obyxj55.mirror.aliyuncs.com"]
    }
    
    # 重载配置
    systemctl daemon-reload
    # 重启docker
    systemctl restart docker
    

    3.k8s配置安装

    # 配置k8s国内源
    vi /etc/apt/sources.list.d/kubernetes.list
    deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main
    
    # 报错提示
    be verified because the public key is not available: NO_PUBKEY 6A030B21BA07F4FB
    
    # 添加证书,copy后8位
    gpg --keyserver keyserver.ubuntu.com --recv-keys BA07F4FB
    gpg --export --armor BA07F4FB | apt-key add -
    
    apt update
    
    # 禁用防火墙
    ufw disable
    # 关闭swap
    swapoff -a
    vi /etc/fstab
    # 注掉该行
    # /swap.img     none    swap    sw      0       0
    
    # 关闭selinux
    apt install -y selinux-utils
    setenforce 0
    # 重启
    shutdown -r now
    
    # k8s网络配置
    vi /etc/sysctl.d/k8s.conf
    
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    vm.swappiness = 0
    
    modprobe br_netfilter
    sysctl -p /etc/sysctl.d/k8s.conf
    
    # 安装k8s
    # kubelet 后台进程
    # kubeadm 更高权限操作 初始化环境使用
    # kubectl 用户客户端
    apt-get install -y kubelet=1.13.1-00 kubernetes-cni=0.6.0-00 kubeadm=1.13.1-00 kubectl=1.13.1-00
    
    # 启动并配置开机启动
    systemctl enable kubelet && systemctl start kubelet
    # 测试启动
    kubectl get nodes
    

    4.k8s集群配置

    # 克隆两台虚拟机 us-02 us-03
    # 分别修改主机名
    vi /ets/hostname
    vi /etc/cloud/cloud.cfg
    # 将preserve_hostname后面设置为true
    
    
    # 分别配置ip
    vi /etc/netplan/50-cloud-init.yaml
    # 3台分别配置hosts
    vi /etc/hosts
    
    192.168.68.141 us-01
    192.168.68.142 us-02
    192.168.68.143 us-03
    
    --------------------------
    # us-01配置工作目录
    mkdir working
    cd working
    # 生成配置
    kubeadm config print init-defaults ClusterConfiguration > kubeadm.conf
    # 修改配置
    vi kubeadm.conf
    
    # 修改ip
    localAPIEndpoint:
      advertiseAddress: 192.168.68.141
    # 修改镜像
    imageRepository: registry.aliyuncs.com/google_containers
    # 修改版本号
    kubernetesVersion: v1.13.1
    # 配置子网
    networking:
      dnsDomain: cluster.local
      podSubnet: "10.244.0.0/16"
    
    # 拉取
    kubeadm config images pull --config ./kubeadm.conf
    
    # 说明
    # kube-apiserver: 集群对外提供接口
    # kube-scheduler: 内部调度器
    # kube-proxy: 负载均衡
    # etcd: 容器数据一致性
    
    # 启动k8s
    kubeadm init --config ./kubeadm.conf
    
    # 日志中node加入集群的指令(从节点执行,master不必执行)
    # kubeadm join 192.168.68.141:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:15e488c84e53b855c0dd37f3125f23510e838143994479d0821d21504ab7e79f
    
    # 配置
    mkdir $HOME/.kube
    cp -i /etc/kubenetes/admin.conf $HOME/.kube/config
    chown $(id -u):$(id -g) $HOME/.kube/config
    # 启动
    systemctl enable kubelet
    systemctl start kubelet
    
    # master和node配置网络环境
    # 配置内部通信网络
    cd $HOME/working
    wget https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/
    kube-flannel.yml
    # 加载配置
    kubectl apply -f kube-flannel.yml 
    
    # us-02 us-03开机启动kubelet
    systemctl enable kubelet
    systemctl start kubelet
    
    # us-01配置分发到us-02 us-03
    scp /etc/kubernetes/admin.conf root@us-02:/root
    scp /etc/kubernetes/admin.conf root@us-03:/root
    
    # us-02 us-03分别配置
    mkdir $HOME/.kube
    cp -i $HOME/admin.conf $HOME/.kube/config
    chown $(id -u):$(id -g) $HOME/.kube/config
    
    kubeadm join 192.168.68.141:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:15e488c84e53b855c0dd37f3125f23510e838143994479d0821d21504ab7e79f
    
    kubectl get nodes
    
    # us-01 flannel配置分发到us-02 us-03
    scp $HOME/working/kube-flannel.yml root@us-02:/root
    scp $HOME/working/kube-flannel.yml root@us-03:/root
    
    # us-02 us-03加载配置
    kubectl apply -f kube-flannel.yml
    

    5.创建tomcat实例

    # 创建tomcat实例
    # 描述文件
    vi myweb-rc.yaml
    -----------------------
    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: myweb
    spec:
      replicas: 5
    #Pod副本期待数量为5
      selector:
        app: myweb
      template:
        metadata:
          labels:
            app: myweb
        spec:
          containers:
          - name: myweb
            image: docker.io/kubeguide/tomcat-app:v1
            ports:
            - containerPort: 8080
            env:
            - name: MYSQL_SERVICE_HOST
              value: "mysql"
            - name: MYSQL_SERVICE_PORT
              value: "3306"
    --------------------------
    # 加载描述文件
    kubectl create -f myweb-rc.yaml
    
    # 创建服务描述文件
    vi myweb-svc.yaml
    -----------------------
    apiVersion: v1
    kind: Service
    metadata:
      name: myweb
    spec:
      type: NodePort
      ports:
        - port: 8080
          nodePort: 30001
      selector:
        app: myweb
    -------------------------
    # 部署服务
    kubectl create -f myweb-svc.yaml
    # 查看pods 
    kubectl get pods
    # ***异常 7.1
    kubectl describe pods 
    # 查看服务
    kubectl get service 
    kubectl describe service
    # 浏览器访问: http://192.168.68.141:30001/
    

    6.创建mysql实例

    # 创建mysql实例
    vi mysql-rc.yaml
    
    # 描述文件
    -----------------------------------------
    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: mysql
    spec:
      # 实例个数,如配置多个,k8s会自动做集群
      replicas: 1
      selector:
        app: mysql
      template:
        metadata:
          labels:
            app: mysql
        spec:
          containers:
          - name: mysql
            image: hub.c.163.com/library/mysql
            ports:
            - containerPort: 3306
            env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
    -------------------------------------
    # 发布
    kubectl create -f mysql-rc.yaml
    
    参考:

    两小时Kubernetes(K8S)从懵圈到熟练——大型分布式集群环境捷径部署搭建-bilibili
    文档链接:https://pan.baidu.com/s/1sZuuMTEehr6galLdiyJByA 提取码:xtks
    从零开始在ubuntu上安装和使用k8s集群及报错解决
    Ubuntu 18.04 LTS Server服务器安装图文教程
    ubuntu-18.10-live-server设置静态IP

    相关文章

      网友评论

          本文标题:k8s-环境搭建-基于ubuntu server18.10

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