ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

ubuntu16.04 k8s 1.15.2集群

2022-01-18 10:33:31  阅读:150  来源: 互联网

标签:ubuntu16.04 k8s kubernetes sudo apt 1.15 master kube


一、概述

搭建k8s集群时,需要访问google,下载相关镜像以及安装软件,非常麻烦。

正好阿里云提供了k8s的更新源,国内用户就可以直接使用了。

二、环境介绍

操作系统主机名IP地址功能配置

  • ubuntu-16.04.5-server-amd64k8s-master192.168.91.128主节点2核4G
  • ubuntu-16.04.5-server-amd64k8s-node1192.168.91.129从节点2核4G
  • ubuntu-16.04.5-server-amd64k8s-node2192.168.91.131从节点2核4G

三、安装前准备

主机名

确保3台主机的 /etc/hostname 已经修改为正确的主机名,修改后,请重启系统。

时间

务必保证3台服务器的时区是一样的,强制更改时区为上海,执行以下命令

ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
bash -c "echo 'Asia/Shanghai' > /etc/timezone"

安装ntpdate

apt-get install -y ntpdate

如果出现以下错误

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
执行2个命令解决
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

使用阿里云的时间服务器更新

ntpdate ntp1.aliyun.com

3台服务器都执行一下,确保时间一致!

请确保防火墙都关闭了!

四、正式开始

禁用swap
所有主机
sudo sed -i '/swap/ s/^/#/' /etc/fstab
sudo swapoff -a

安装Docker

更新apt源,并添加https支持(所有主机)

sudo apt-get update && sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y

使用utc源添加GPG Key (所有主机)

curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add 

添加Docker-ce稳定版源地址(所有主机)

sudo add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

安装docker-ce(所有主机)

sudo apt-get update
sudo apt install docker-ce=18.06.1~ce~3-0~ubuntu

安装kubelet,kubeadm,kubectl

添加apt key以及源(所有主机)

sudo apt update && sudo apt install -y apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" >>/etc/apt/sources.list.d/kubernetes.list

安装(所有主机)

sudo apt update
sudo apt install -y kubelet=1.15.2-00 kubeadm=1.15.2-00 kubectl=1.15.2-00
sudo apt-mark hold kubelet=1.15.2-00 kubeadm=1.15.2-00 kubectl=1.15.2-00

安装kubernetes集群(仅master)

—image-repository 指定镜像源,指定为阿里云的源,这样就会避免在拉取镜像超时,如果没问题,过几分钟就能看到成功的日志输入

sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.15.2 --pod-network-cidr=192.169.0.0/16

输出:

[init] Using Kubernetes version: v1.15.2

[preflight] Running pre-flight checks

[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/

[preflight] Pulling images required for setting up a Kubernetes cluster

[preflight] This might take a minute or two, depending on the speed of your internet connection

[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'

[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"

[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"

[kubelet-start] Activating the kubelet service

[certs] Using certificateDir folder "/etc/kubernetes/pki"

[certs] Generating "ca" certificate and key

[certs] Generating "apiserver" certificate and key

[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.10.104]

[certs] Generating "apiserver-kubelet-client" certificate and key

[certs] Generating "front-proxy-ca" certificate and key

[certs] Generating "front-proxy-client" certificate and key

[certs] Generating "etcd/ca" certificate and key

[certs] Generating "etcd/server" certificate and key

[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.10.104 127.0.0.1 ::1]

[certs] Generating "etcd/peer" certificate and key

[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.10.104 127.0.0.1 ::1]

[certs] Generating "etcd/healthcheck-client" certificate and key

[certs] Generating "apiserver-etcd-client" certificate and key

[certs] Generating "sa" key and public key

[kubeconfig] Using kubeconfig folder "/etc/kubernetes"

[kubeconfig] Writing "admin.conf" kubeconfig file

[kubeconfig] Writing "kubelet.conf" kubeconfig file

[kubeconfig] Writing "controller-manager.conf" kubeconfig file

[kubeconfig] Writing "scheduler.conf" kubeconfig file

[control-plane] Using manifest folder "/etc/kubernetes/manifests"

[control-plane] Creating static Pod manifest for "kube-apiserver"

[control-plane] Creating static Pod manifest for "kube-controller-manager"

[control-plane] Creating static Pod manifest for "kube-scheduler"

[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"

[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s

[kubelet-check] Initial timeout of 40s passed.

[apiclient] All control plane components are healthy after 41.503569 seconds

[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace

[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster

[upload-certs] Skipping phase. Please see --upload-certs

[mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"

[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]

[bootstrap-token] Using token: 767b6y.incfuyom78fl6j88

[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles

[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials

[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token

[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster

[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace

[addons] Applied essential addon: CoreDNS

[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.

Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:

https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.10.104:6443 --token 767b6y.incfuyom78fl6j88 \

--discovery-token-ca-cert-hash sha256:941807715378bcbd5bd1cbe244c4bdbf00dee4e45c3b0ff3555eea746607a672

View Code

注意:出现以下警告信息:

[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/

可以忽略,解决方法:

#修改或者创建

vim /etc/docker/daemon.json
#添加如下内容 --这行就不要添加了这只是个注释(^_^)------qingfeng
{
  "exec-opts": ["native.cgroupdriver=systemd"]
}

拷贝kubeconfig文件到家目录的.kube目录 (仅master)

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

安装网络插件,让pod之间通信(仅master)

https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
kubectl apply -f  rbac-kdd.yaml
kubectl apply -f  calico.yaml

查看kube-system命名空间下的pod状态(仅master)

kubectl get pod -n kube-system

等待1分钟,效果如下:

kubectl  get pods -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
calico-node-8w22s                    2/2     Running   0          26m
calico-node-r7bzc                    2/2     Running   0          36m
calico-node-zds6x                    2/2     Running   0          26m
coredns-bccdc95cf-d8vcl              1/1     Running   0          107m
coredns-bccdc95cf-kzjt5              1/1     Running   0          107m
etcd-k8s-master                      1/1     Running   0          106m
kube-apiserver-k8s-master            1/1     Running   0          106m
kube-controller-manager-k8s-master   1/1     Running   0          106m
kube-proxy-db49l                     1/1     Running   0          107m
kube-proxy-tthjs                     1/1     Running   0          26m
kube-proxy-vdtj8                     1/1     Running   0          26m
kube-scheduler-k8s-master            1/1     Running   0          106m

加入node节点 (仅node)

拷贝kubeadm init输入的加入节点命令,在每个node上执行

kubeadm join 192.168.10.104:6443 --token 767b6y.incfuyom78fl6j88     --discovery-token-ca-cert-hash sha256:941807715378bcbd5bd1cbe244c4bdbf00dee4e45c3b0ff3555eea746607a672

查看集群状态(仅master)

root@k8s-master:~# kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   44m   v1.15.2
k8s-node1    Ready    <none>   42m   v1.15.2
k8s-node2    Ready    <none>   42m   v1.15.2

命令补全

apt-get install bash-completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
source  ~/.bashrc

四、部署应用

这里以flask为例:

vim flask.yaml

内容如下:

apiVersion: extensions/v1beta1
kind: Deployment 
metadata: 
  name: flaskapp-1
spec: 
  replicas: 1
  template: 
    metadata: 
      labels: 
        name: flaskapp-1
    spec: 
      containers: 
        - name: flaskapp-1
          image: jcdemo/flaskapp
          ports:
          - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: flaskapp-1
  labels:
    name: flaskapp-1
spec:
  type: NodePort
  ports:
  - port: 5000
    name: flaskapp-port
    targetPort: 5000
    protocol: TCP
    nodePort: 30005
  selector:
    name: flaskapp-1

启动应用

kubectl apply -f flask.yaml

查看应用状态

root@k8s-master:~# kubectl get pods -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
flaskapp-1-59698bc97d-ssqv8   1/1     Running   0          43m   192.169.2.3   k8s-node2   <none>           <none>

以上信息可以说明,这个pod运行在k8s-node2这台主机上面

使用curl访问pod ip的服务

root@k8s-master:~# curl 192.169.2.3:5000
<html><head><title>Docker + Flask Demo</title></head><body><table><tr><td> Start Time </td> <td>2019-Aug-07 12:30:38</td> </tr><tr><td> Hostname </td> <td>flaskapp-1-59698bc97d-ssqv8</td> </tr><tr><td> Local Address </td> <td>192.169.2.3</td> </tr><tr><td> Remote Address </td> <td>192.168.10.120</td> </tr><tr><td> Server Hit </td> <td>3</td> </tr></table></body></html>root@k8s-master:~# 

查看svc端口

root@k8s-master:~# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
flaskapp-1   NodePort    10.107.108.46   <none>        5000:30005/TCP   45m
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          53m

直接网页访问k8s-node2的30005端口

http://192.168.10.119:30005/

效果如下:

五、部署dashboard可视化插件

概述

在 Kubernetes Dashboard 中可以查看集群中应用的运行状态,也能够创建和修改各种 Kubernetes 资源,比如 Deployment、Job、DaemonSet 等。用户可以 Scale Up/Down Deployment、执行 Rolling Update、重启某个 Pod 或者通过向导部署新的应用。Dashboard 能显示集群中各种资源的状态以及日志信息。

可以说,Kubernetes Dashboard 提供了 kubectl 的绝大部分功能,大家可以根据情况进行选择。

github地址:

https://github.com/kubernetes/dashboard

安装

Kubernetes 默认没有部署 Dashboard,可通过如下命令安装:

kubectl apply -f http://mirror.faasx.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

查看service

root@k8s-master:~# kubectl --namespace=kube-system get deployment kubernetes-dashboard
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-dashboard   1/1     1            1           5m23s
root@k8s-master:~# kubectl --namespace=kube-system get service kubernetes-dashboard
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes-dashboard   ClusterIP   10.100.111.103   <none>        443/TCP   5m28s

查看pod

确保状态是Running

root@k8s-master:~# kubectl get pod --namespace=kube-system -o wide | grep dashboard
kubernetes-dashboard-8594bd9565-t78bj   1/1     Running   0          8m41s   192.169.2.7      k8s-node2    <none>           <none>

允许外部访问

注意:会占用终端

kubectl proxy --address='0.0.0.0'  --accept-hosts='^*$'

通过浏览器访问

注意:192.168.10.104为master ip
http://192.168.10.104:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/.

效果如下:

配置登录权限

Dashboard 支持 Kubeconfig 和 Token 两种认证方式,为了简化配置,我们通过配置文件 dashboard-admin.yaml 为 Dashboard 默认用户赋予 admin 权限。

vim dashboard-admin.yml

内容如下:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard
  labels: 
     k8s-app: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: kubernetes-dashboard
  namespace: kube-system

执行kubectl apply使之生效

kubectl apply -f dashboard-admin.yml

现在直接点击登录页面的跳过,就可以进入 Dashboard 了,效果如下:

关于dashboard界面结构介绍,请参考链接:

https://www.cnblogs.com/kenken2018/p/10340157.html

标签:ubuntu16.04,k8s,kubernetes,sudo,apt,1.15,master,kube
来源: https://blog.csdn.net/ethnicitybeta/article/details/122554586

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有