一、入门
1、Kubernetes中文社区---http://docs.kubernetes.org.cn/
2、Kubernetes集群组件:
- etcd 一个高可用的K/V键值对存储和服务发现系统
- flannel 实现夸主机的容器网络的通信
- kube-apiserver 提供kubernetes集群的API调用
- kube-controller-manager 确保集群服务
- kube-scheduler 调度容器,分配到Node
- kubelet 在Node节点上按照配置文件中定义的容器规格启动容器
- kube-proxy 提供网络代理服务
3、集群示意图
二、安装
1、先决条件
如下操作在所有机器执行
(1).确保系统已经安装epel-release源
# yum -y install epel-release
(2).关闭防火墙服务,避免与docker容器的防火墙规则冲突。
# systemctl stop firewalld # systemctl disable firewalld
(3).centos最好是centos7系统以上,不然软件源中没有k8s的安装包
(4).关闭selinux
#临时关闭selinux
setenforce 0
#永久关闭selinux
vi /etc/selinux/config
##修改enforcing为disabled
2.安装配置Kubernetes Master
如下操作在master上执行
(1).使用yum安装etcd和kubernetes-master
# yum -y install etcd kubernetes-master
(2).编辑/etc/etcd/etcd.conf文件
ETCD_NAME=default ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"
(3).编辑/etc/kubernetes/apiserver文件
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0" KUBE_API_PORT="--port=8080" KUBELET_PORT="--kubelet-port=10250" KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379" KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota" KUBE_API_ARGS=""
PS:测试时需要把KUBE_ADMISSION_CONTROL
中的SecurityContextDeny
和ServiceAccount
去掉,这是权限相关的
(4).启动etcd、kube-apiserver、kube-controller-manager、kube-scheduler等服务,并设置开机启动。
# for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do systemctl restart $SERVICES;systemctl enable $SERVICES;systemctl status $SERVICES ; done
(5).在etcd中定义flannel网络
# etcdctl mk /atomic.io/network/config '{"Network":"172.17.0.0/16"}'
PS:其中网络号172.17.0.0/16
与docker中的docker0网络一致(若不一致,可修改docker0网络或者配置上述etcd网络);atomic.io
与下面的Flannel配置中的FLANNEL_ETCD_PREFIX
对应
3.安装配置Kubernetes Node
如下操作在node1、node2、node3、node4等节点上操作
(1).使用yum安装flannel和kubernetes-node
# yum -y install flannel kubernetes-node
(2).为flannel网络指定etcd服务,修改/etc/sysconfig/flanneld文件
FLANNEL_ETCD="http://192.168.30.20:2379" FLANNEL_ETCD_KEY="/atomic.io/network"
(3).修改/etc/kubernetes/config文件
KUBE_LOGTOSTDERR="--logtostderr=true" KUBE_LOG_LEVEL="--v=0" KUBE_ALLOW_PRIV="--allow-privileged=false" KUBE_MASTER="--master=http://192.168.30.20:8080"
(4).按照如下内容修改对应node的配置文件/etc/kubernetes/kubelet
node1配置如下
KUBELET_ADDRESS="--address=0.0.0.0" KUBELET_PORT="--port=10250" KUBELET_HOSTNAME="--hostname-override=192.168.30.21" #修改成对应Node的IP KUBELET_API_SERVER="--api-servers=http://192.168.30.20:8080" #指定Master节点的API Server KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest" KUBELET_ARGS=""
(5).其他的节点按照node1节点来操作
(6).在所有Node节点上启动kube-proxy,kubelet,docker,flanneld等服务,并设置开机启动。
# for SERVICES in kube-proxy kubelet docker flanneld;do systemctl restart $SERVICES;systemctl enable $SERVICES;systemctl status $SERVICES; done
4.验证集群是否安装成功及相关命令
(1)验证是否成功
[root@master ~]# kubectl get node NAME STATUS AGE 192.168.30.21 Ready 1m 192.168.30.22 Ready 1m 192.168.30.23 Ready 1m 192.168.30.24 Ready 1m
注释:上述4个节点正常显示,状态为Ready,则说明集群搭建成功,
(2)相关命令
#创建 kubectl create -f kubernetes-dashboard.yaml --vaildate #创建应用并验证 #查看 kubectl get node kubectl get pods --all-namespaces #查看所有pod # 查看pods所在的运行节点 kubectl get pods -o wide # 查看Replication Controller信息 kubectl get rc # 查看service的信息 kubectl get svc kubectl logs pod pod名 kubectl get deployments # 删除 kubectl delete -f 文件名 kubectl delete pod pod名 kubectl delete rc rc名 kubectl delete service service名 kubectl delete pod --all #描述 显示Node的详细信息 kubectl describe nodes <node-name> 显示Pod的详细信息 kubectl describe pods/<pod-name> 显示由RC管理的Pod的信息 kubectl describe pods <rc-name>
三、安装nginx与dashboard应用
1、安装nginx
(1)在node节点上下载好nginx镜像,创建pod : nginx-pod.yaml
kubectl create -f nginx-pod.yaml
apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: name: nginx-pod spec: containers: - name: nginx image: nginx ports: - containerPort: 80
(2)创建replicationController : nginx-rc.yaml
kubectl create -f nginx-rc.yaml
apiVersion: v1 kind: ReplicationController metadata: name: nginx-rc spec: replicas: 1 selector: name: nginx-pod template: metadata: labels: name: nginx-pod spec: containers: - name: nginx-pod image: nginx ports: - containerPort: 80
kubectl get rc
(3)创建service : nginx-service.yaml
kubectl create -f nginx-service.yaml
apiVersion: v1 kind: Service metadata: name: nginx-service spec: type: NodePort ports: - port: 80 nodePort: 30001 selector: name: nginx-pod
kubectl get svc #查看服务
(4)使用其他机器的浏览器访问node1机器的30001端口
2、安装dashboard
(1).在master和nodes上都需要安装flannel
yum install flannel vi /etc/sysconfig/flanneld # Flanneld configuration options # etcd url location. Point this to the server where etcd runs FLANNEL_ETCD_ENDPOINTS="http://etcd:2379" # etcd config key. This is the configuration key that flannel queries # For address range assignment FLANNEL_ETCD_PREFIX="/atomic.io/network" # Any additional options that you want to pass #FLANNEL_OPTIONS=""
(2)在master 配置kubernetes-dashboard.yaml文件,修改一下即可;
cat >kubernetes-dashboard.yaml<<-EOF kind: Deployment apiVersion: extensions/v1beta1 metadata: labels: app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: replicas: 1 selector: matchLabels: app: kubernetes-dashboard template: metadata: labels: app: kubernetes-dashboard # Comment the following annotation if Dashboard must not be deployed on master annotations: scheduler.alpha.kubernetes.io/tolerations: | [ { "key": "dedicated", "operator": "Equal", "value": "master", "effect": "NoSchedule" } ] spec: containers: - name: kubernetes-dashboard image: bestwu/kubernetes-dashboard-amd64:v1.6.3 #可用或者找个可用的镜像 imagePullPolicy: Always ports: - containerPort: 9090 protocol: TCP args: # Uncomment the following line to manually specify Kubernetes API server Host # If not specified, Dashboard will attempt to auto discover the API server and connect # to it. Uncomment only if the default does not work. - --apiserver-host=http://10.0.10.10:8080 #注意这里是api的地址 livenessProbe: httpGet: path: / port: 9090 initialDelaySeconds: 30 timeoutSeconds: 30 --- kind: Service apiVersion: v1 metadata: labels: app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: type: NodePort ports: - port: 80 targetPort: 9090 selector: app: kubernetes-dashboard EOF
(3).在node节点上下载镜像
docker pull bestwu/kubernetes-dashboard-amd64:v1.6.3
docker images #查看镜像
(4)创建实例
[root@k8s ~]# kubectl create -f kubernetes-dashboard.yaml 查看是否成功运行: [root@k8s ~]# kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE default nginx-controller-6n19x 1/1 Running 0 9m kube-system kubernetes-dashboard-2079301569-0qzdv 1/1 Running 0 12m
四、安装的坑
1 安装前一定要关闭firewalld服务,否则需要配firewalld的规则
五、参考资料
1、http://blog.csdn.net/u013760355/article/details/68061976-----nginx安装
2、https://www.58jb.com/html/152.html----dashboard安装
3、http://blog.csdn.net/magerguo/article/details/72123259?locationNum=3&fps=1 ------k8s安装