K8S

K8s 全称kubernetes入门

2019-11-20  本文已影响0人  小镇青年Jack

Kubernetes 简称 k8s. 是一个开源的 Linux 容器自动化运维平台,它消除了容器化应用程序在部署、 伸缩时涉及到的许多手动操作。换句话说,你可以将多台主机组合成集群来运行 Linux 容器,而 Kubernetes 可以帮助你简单高效地管理那些集群

k8s物理架构图(四个API server,Controller,Scheduler,etcd)

5W6H$HTOAW0`V987}W1DXF1.png

Node的架构图(三个Kube-proxy,kuberlet,Docker)

I})17NI((Y53ZFDP6M4AAW9.png

k8s系统架构,逻辑架构,网络架构

image.png

开始实操:

第一章 部署前操作

1.1 改名字
hostnamectl set-hostname node1
hostnamectl set-hostname node2
hostnamectl set-hostname node3

1.2 三台机时间同步(三台机)
yum install ntpdate -y
ntpdate time1.aliyun.com

1.3 查看防火墙
iptables -nL
getenforce

第二章 安装部署指定版本docker

1设置国内YUM源
cd /etc/yum.repos.d/
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2安装指定的docker版本
yum -y install docker-ce-18.09.7-3.el7 docker-ce-cli-18.09.7

3启动后台进程
systemctl enable docker && systemctl start docker

4设置cgroup驱动使用systemd
cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl restart docker

5查看docker版本
docker -v

第三章 部署kubeadm和kubelet

注意!!!所有的机器都操作

1.配置国内源
cat >/etc/yum.repos.d/kubernetes.repo<<EOF
[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
EOF

2.安装软件包 (ipvsadm就是LVS负载均衡)
yum install -y kubelet-1.15.1 kubeadm-1.15.1 kubectl-1.15.1 ipvsadm

3.配置kubelet禁止使用swap
swapoff -a
sed -i '/swap/d' /etc/fstab 

cat > /etc/sysconfig/kubelet<<EOF
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
EOF

4.设置内核参数
cat >  /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
#使上面的生效
sysctl --system

5.设置开机自启动
systemctl enable kubelet && systemctl start kubelet

6.使用IPVS进行负载均衡
cat >/etc/sysconfig/modules/ipvs.modules<<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod +x /etc/sysconfig/modules/ipvs.modules
source /etc/sysconfig/modules/ipvs.modules
lsmod | grep -e ip_vs -e nf_conntrack_ip

第四章 初始化集群部署Master

1.Master节点执行初始化操作 (新开一个窗口docker images)
kubeadm init \
--apiserver-advertise-address=10.0.1.51 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.15.1 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.2.0.0/16 \
--service-dns-domain=cluster.local \
--ignore-preflight-errors=Swap \
--ignore-preflight-errors=NumCPU

执行完成后会有输出,需要根据输出的要求配置kubectl命令来访问集群
================================================================
init 0
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 10.0.1.51:6443 --token h7o1k7.enq1bz8z9gadjyox \
    --discovery-token-ca-cert-hash sha256:9a0029f9bff257b21dca8ea67f355400a5969f8d600e1accebdf96c2aae47d3f 
================================================================    
    
2.为kubectl准备kubeconfig文件
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config

3.使用kubectl命令查看组件状态
[root@node1 ~]# kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   

4.使用kubectl获取Node信息
[root@node1 ~]# kubectl get node
NAME    STATUS     ROLES    AGE     VERSION
node1   NotReady   master   2m38s   v1.15.1


5.支持命令补全
yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)

第五章 部署网络插件

Master节点操作
1.部署canal网络插件
kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/canal/rbac.yaml
kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/canal/canal.yaml

2.查看启动的pod
kubectl get pods --all-namespaces

3.查看节点状态
kubectl get nodes

06镜像下载不了解决方法

导出镜像

docker save calico/node:v3.3.7 >calico.tar
docker save calico/cni:v3.3.7 >cni.tar
docker save registry.aliyuncs.com/google_containers/kube-proxy:v1.15.1 > kube-proxy.tar
docker save registry.aliyuncs.com/google_containers/kube-scheduler:v1.15.1 > kube-scheduler.tar
docker save registry.aliyuncs.com/google_containers/kube-controller-manager:v1.15.1 >kube-controller-manager.tar
docker save registry.aliyuncs.com/google_containers/kube-apiserver:v1.15.1 >kube-apiserver.tar
docker save registry.aliyuncs.com/google_containers/coredns:1.3.1 >coredns.tar
docker save registry.aliyuncs.com/google_containers/etcd:3.3.10 >etcd.tar
docker save registry.aliyuncs.com/google_containers/pause:3.1 >pause.tar
docker save quay.io/coreos/flannel:v0.9.1 >flannel.tar

导入镜像
docker load < calico.tar
docker load < cni.tar
docker load < flannel.tar.
docker load < coredns.tar

删除重新创建
wget  https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/canal/rbac.yaml
wget  https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/canal/canal.yaml

kubectl delete -f rbac.yaml
kubectl delete -f canal.yaml

kubectl apply -f rbac.yaml
kubectl apply -f canal.yaml

查看启动的pod
kubectl get pods --all-namespaces

查看节点状态
kubectl get nodes

第六章 部署Node节点

1.在Master节点上输出增加节点的命令
kubeadm token create --print-join-command


2.在Node2和node3节点执行 (上面的命令执行结果, 每次不一样!!)
##kubeadm join 10.0.1.51:6443 --token h7o1k7.enq1bz8z9gadjyox \
    --discovery-token-ca-cert-hash sha256:9a0029f9bff257b21dca8ea67f355400a5969f8d600e1accebdf96c2aae47d3f ##

3.node1节点上查看节点状态
kubectl get nodes

第8章测试k8s集群

1创建一个单pod的Nginx应用
kubectl create deployment nginx --image=nginx:alpine
 
2.查看启动的pod
[root@node1 ~/k8s]# kubectl get pod -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP         NODE    NOMINATED NODE   READINESS GATES
nginx-8f6959bd-wm66d   1/1     Running   0          65s   10.2.1.2   node2   <none>           <none>

3.测试访问
curl 10.2.1.2

4.测试扩容
kubectl scale deployment nginx --replicas=2

查看状态
kubectl get pod -o wide

5.为Nginx增加Service
kubectl expose deployment nginx --port=80 --type=NodePort

6.查看service
kubectl get service nginx -o wide

7.测试访问
curl 10.0.1.51:32483
上一篇下一篇

猜你喜欢

热点阅读