Kubernetes安装记

首先安装3台虚拟机

  • 使用virtualbox
  • 使用centos minimal安装iso
  • 每台虚拟机能够连接google
  • 每台虚拟机两个网卡

设置http和https的代理:

export http_proxy=http://192.168.99.1:1087;export https_proxy=http://192.168.99.1:1087;
export no_proxy="127.0.0.1,192.168.99.0/24,localhost,10.96.0.0/12,10.244.0.0/16"

取消的话使用:

unset http_proxy;
unset https_proxy;

在virtualbox里面,选择创建虚拟机,linux->redhat 64
内存设置为4096
硬盘位动态分配,20G

创建完成之后,进入该虚拟机的设置,选择network,打开第二张网卡,设置为hostonly adapter
选择display,将Graphics Controller设置为VBoxVGA。不然进入安装系统界面的时候看不到鼠标。

启动虚拟机,安装centos系统:

  • 时区设置,中国上海
  • 分区设置
  • 网络设置,设置节点名,打开两个网卡

安装完成之后,设置静态ip:

BOOTPROTO="static"         # 使用静态IP地址,默认为dhcp
IPADDR="192.168.241.100"   # 设置的静态IP地址
NETMASK="255.255.255.0"    # 子网掩码
GATEWAY="192.168.241.2"    # 网关地址

设置时区:

timedatectl set-timezone Asia/Shanghai

安装kubernetes:

  • 禁用机器的swap:
sudo swapoff -a 
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

yum update 进行更新

关闭selinux

* vi /etc/selinux/config
* SELINUX=disabled
* reboot
* 使用/usr/sbin/sestatus -v查看selinux状态

关闭firewalld

systemctl stop firewalld.service
systemctl disable firewalld.service
firewall-cmd --state

安装docker和containerd

根据官方文档安装docker daemon

sudo systemctl start docker
sudo systemctl enable docker

为docker增加http proxy:

sudo mkdir -p /etc/systemd/system/docker.service.d
vi /etc/systemd/system/docker.service.d/http-proxy.conf

[Service]    
Environment="HTTP_PROXY=http://192.168.99.1:1087/" "HTTPS_PROXY=http://192.168.99.1:1087/"  "NO_PROXY=localhost,127.0.0.1,192.168.99.0/24,.corp,10.96.0.0/12,10.244.0.0/16"

sudo systemctl daemon-reload
sudo systemctl restart docker

在所有机器上安装:

  • kubeadm
  • kubelet
  • kubectl

首先增加kubernetes的yum repo:

cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF

使用yum安装:

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet

iptable设置:

cat <  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

使用kubeadm init进行安装:

kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.99.110

kubeadm输出:


[init] Using Kubernetes version: v1.13.3
[preflight] Running pre-flight checks
    [WARNING Firewalld]: firewalld is active, please ensure ports [6443 10250] are open or your cluster may not function correctly
    [WARNING HTTPProxy]: Connection to "https://192.168.99.110" uses proxy "http://192.168.99.1:1087". If that is not intended, adjust your proxy settings
    [WARNING HTTPProxyCIDR]: connection to "10.96.0.0/12" uses proxy "http://192.168.99.1:1087". This may lead to malfunctional cluster setup. Make sure that Pod and Services IP ranges specified correctly as exceptions in proxy configuration
    [WARNING HTTPProxyCIDR]: connection to "10.244.0.0/16" uses proxy "http://192.168.99.1:1087". This may lead to malfunctional cluster setup. Make sure that Pod and Services IP ranges specified correctly as exceptions in proxy configuration
    [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.2. Latest validated version: 18.06
    [WARNING Hostname]: hostname "master" could not be reached
    [WARNING Hostname]: hostname "master": lookup master on 192.168.0.1:53: no such host
[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 "etcd/ca" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [master localhost] and IPs [192.168.99.110 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master localhost] and IPs [192.168.99.110 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.99.110]
[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 "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.

卡在这个地方,估计是镜像拉不下来,单独拉镜像。

kubeadm config images pull的输出:

[root@master ~]# kubeadm config images pull
[config/images] Pulled k8s.gcr.io/kube-apiserver:v1.13.3
[config/images] Pulled k8s.gcr.io/kube-controller-manager:v1.13.3
[config/images] Pulled k8s.gcr.io/kube-scheduler:v1.13.3
[config/images] Pulled k8s.gcr.io/kube-proxy:v1.13.3
[config/images] Pulled k8s.gcr.io/pause:3.1
[config/images] Pulled k8s.gcr.io/etcd:3.2.24
[config/images] Pulled k8s.gcr.io/coredns:1.2.6

成功时候的输出:

[root@master ~]# kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.99.110
[init] Using Kubernetes version: v1.13.3
[preflight] Running pre-flight checks
    [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.2. Latest validated version: 18.06
[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 "etcd/ca" certificate and key
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [master localhost] and IPs [192.168.99.110 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master localhost] and IPs [192.168.99.110 127.0.0.1 ::1]
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.99.110]
[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 "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
[apiclient] All control plane components are healthy after 18.502503 seconds
[uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.13" in namespace kube-system with the configuration for the kubelets in the cluster
[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "master" as an annotation
[mark-control-plane] Marking the node master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 77dklp.ma1npwcluyzzk6xv
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes master 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/

You can now join any number of machines by running the following on each node
as root:

  kubeadm join 192.168.99.110:6443 --token 77dklp.ma1npwcluyzzk6xv --discovery-token-ca-cert-hash sha256:d5f6bb873da3295f409ddee1289559d6205b807af98fae22e8f390f1fa213bce

作为root运行kubectl:

export KUBECONFIG=/etc/kubernetes/admin.conf

安装flannel:

sysctl net.bridge.bridge-nf-call-iptables=1
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

添加node:

kubeadm join 192.168.99.110:6443 --token 77dklp.ma1npwcluyzzk6xv --discovery-token-ca-cert-hash sha256:d5f6bb873da3295f409ddee1289559d6205b807af98fae22e8f390f1fa213bce
[preflight] Running pre-flight checks
    [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.2. Latest validated version: 18.06
    [WARNING HTTPProxy]: Connection to "https://192.168.99.110" uses proxy "http://192.168.99.1:1087". If that is not intended, adjust your proxy settings
[discovery] Trying to connect to API Server "192.168.99.110:6443"
[discovery] Created cluster-info discovery client, requesting info from "https://192.168.99.110:6443"
[discovery] Requesting info from "https://192.168.99.110:6443" again to validate TLS against the pinned public key
[discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "192.168.99.110:6443"
[discovery] Successfully established connection with API Server "192.168.99.110:6443"
[join] Reading configuration from the cluster...
[join] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet] Downloading configuration for the kubelet from the "kubelet-config-1.13" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[tlsbootstrap] Waiting for the kubelet to perform the TLS Bootstrap...
[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "node1" as an annotation

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the master to see this node join the cluster.

安装helm

  • 根据官网的知道教程,其实就是下了一个tar包,解压就可以看到helm的二进制文件了
  • 安装tiller,直接helm init

之后出现:

[root@master ~]# helm install --name my-release stable/tomcat
Error: release my-release failed: namespaces "default" is forbidden: User "system:serviceaccount:kube-system:default" cannot get resource "namespaces" in API group "" in the namespace "default"

是RBAC的权限问题。解决:
创建rbac-config.yaml:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: tiller
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: tiller
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
  - kind: ServiceAccount
    name: tiller
    namespace: kube-system

然后

$ kubectl create -f rbac-config.yaml
serviceaccount "tiller" created
clusterrolebinding "tiller" created
$ helm init --service-account tiller

发现helm install还是不行,于是:

helm init --service-account tiller --upgrade

执行完之后tiller pod重启,然后再helm install就ok了的

至此kubernetes和helm就安装好了。

安装完成之后

查看kube-system命名空间中的pod:

[root@master ~]# kubectl get pods -n kube-system -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
coredns-86c58d9df4-jpbvn         1/1     Running   12         59d   10.244.0.26      master              
coredns-86c58d9df4-jvtdb         1/1     Running   13         59d   10.244.0.27      master              
etcd-master                      1/1     Running   14         59d   192.168.99.110   master              
kube-apiserver-master            1/1     Running   14         59d   192.168.99.110   master              
kube-controller-manager-master   1/1     Running   12         59d   192.168.99.110   master              
kube-flannel-ds-amd64-6jxs4      1/1     Running   1          24h   192.168.99.111   node1               
kube-flannel-ds-amd64-l7nx9      1/1     Running   1          24h   192.168.99.110   master              
kube-flannel-ds-amd64-mk2s6      1/1     Running   1          24h   192.168.99.112   node2               
kube-proxy-hhn9w                 1/1     Running   12         59d   192.168.99.110   master              
kube-proxy-hj2mv                 1/1     Running   10         59d   192.168.99.112   node2               
kube-proxy-qqrzn                 1/1     Running   10         59d   192.168.99.111   node1               
kube-scheduler-master            1/1     Running   13         59d   192.168.99.110   master              
tiller-deploy-dbb85cb99-fr5th    1/1     Running   6          46d   10.244.1.35      node1               

根据kubernetes的文档,master节点上有:

  • kube-apiserver,
  • etcd
  • kube-scheduler
  • kube-controller-manager
  • coredns

node节点上有:

  • kubelet
  • kube-proxy
  • container runtime

可以看到除了kubelet是以进程的形式运行在节点上,其余的组件都是以pod的形式运行的。

安装kubernetes dashboard

使用命令:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml

secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-csrf created
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created

你可能感兴趣的:(Kubernetes安装记)