一、快速搭建文档
一、初始化kubernete kubeadm init --kubernetes-version=v1.14.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.10.12 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers .... 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.12:6443 --token 3s24j5.nh1fqex8kmekpmtf --discovery-token-ca-cert-hash sha256:62bcc3a657fd97cd6d5da14b293912234f0f14512bb4bf8d8be1023a69fc6f17 ========================== 二、安装flannel网络 wget https://raw.githubusercontent.com/loverying/mykubernetes/master/kubect_install/kube-flannel.yaml [root@master kubectyaml]# kubectl apply -f kube-flannel.yaml podsecuritypolicy.extensions/psp.flannel.unprivileged created clusterrole.rbac.authorization.k8s.io/flannel created clusterrolebinding.rbac.authorization.k8s.io/flannel created serviceaccount/flannel created configmap/kube-flannel-cfg created daemonset.extensions/kube-flannel-ds-amd64 created daemonset.extensions/kube-flannel-ds-arm64 created daemonset.extensions/kube-flannel-ds-arm created daemonset.extensions/kube-flannel-ds-ppc64le created daemonset.extensions/kube-flannel-ds-s390x created [root@master kubectyaml]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 2m43s v1.14.2 三、其他机器加入集群 kubeadm join 192.168.10.12:6443 --token 3s24j5.nh1fqex8kmekpmtf --discovery-token-ca-cert-hash sha256:62bcc3a657fd97cd6d5da14b293912234f0f14512bb4bf8d8be1023a69fc6f17 [root@master kubectyaml]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 4m3s v1.14.2 node01 Ready <none> 27s v1.14.2 node02 Ready <none> 22s v1.14.2 四、部署 dashboard wget https://raw.githubusercontent.com/loverying/mykubernetes/master/kubect_install/dashboard-admin_new.yaml wget https://raw.githubusercontent.com/loverying/mykubernetes/master/kubect_install/kubernetes-dashboard.yaml kubectl apply -f dashboard-admin_new.yaml kubectl apply -f kubernetes-dashboard.yaml 五、部署ingress-nginx wget https://raw.githubusercontent.com/loverying/mykubernetes/master/kubect_install/mandatory.yaml kubectl apply -f mandatory.yaml 六、部署应用 1)部署pod 提供后端服务 2)部署service 是做负载均衡 3)部署ingress-nginx 将负载均衡绑定在域名上 七、部署普罗米修斯监控(研究) 八、日志收集平台(研究) EFK平台
[root@master mykubernetes-master]# kubectl get pods -n ingress-nginx NAME READY STATUS RESTARTS AGE nginx-ingress-controller-5694ccb578-78ldg 1/1 Running 5 23d
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/service-nodeport.yaml
[root@master ~]# kubectl get svc -n ingress-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx NodePort 10.108.108.196 <none> 80:31124/TCP,443:31565/TCP 76s
二、部署应用
1.1)部署pod
apiVersion: apps/v1 kind: ReplicaSet metadata: name: myapp namespace: default labels: app: myapp spec: replicas: 2 selector: matchLabels: app: myapp template: metadata: name: myapp-pod labels: app: myapp spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80
1.2)访问效果
[root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapp-8w7bm 1/1 Running 0 10m 10.244.2.7 node02 <none> <none> myapp-n4xhs 1/1 Running 0 10m 10.244.1.8 node01 <none> <none> [root@master ~]# curl 10.244.2.7/hostname.html myapp-8w7bm [root@master ~]# curl 10.244.1.8/hostname.html myapp-n4xhs
2.1)创建service
apiVersion: v1 kind: Service metadata: name: myapp namespace: default spec: selector: app: myapp clusterIP: 10.99.99.99 type: NodePort ports: - port: 80 targetPort: 80 nodePort: 30080
2.2) 访问效果
[root@master ~]# kubectl get svc -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h10m <none> myapp NodePort 10.99.99.99 <none> 80:30080/TCP 8m15s app=myapp [root@master ~]# while true;do curl 192.168.10.12:30080/hostname.html;sleep 1;done myapp-8w7bm myapp-n4xhs myapp-n4xhs myapp-8w7bm myapp-8w7bm
vd--xxxx
[root@apaas-master0001 ~]# kubectl get svc -n enos vd-server -o yaml apiVersion: v1 kind: Service metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"creationTimestamp":"2019-07-22T08:59:19Z","labels":{"app":"vd-server","appId":"47"},"name":"vd-server","namespace":"enos","resourceVersion":"1968828","selfLink":"/api/v1/namespaces/enos/services/vd-server","uid":"fdc93ba3-ac5e-11e9-91e6-000d3aa15f2a"},"spec":{"clusterIP":"10.254.32.86","ports":[{"name":"tcp-8080","port":8080,"protocol":"TCP","targetPort":8080}],"selector":{"app":"vd-server"},"sessionAffinity":"None","type":"ClusterIP"},"status":{"loadBalancer":{}}} creationTimestamp: 2019-11-06T08:50:16Z labels: app: vd-xxxx appId: "47" name: vd-xxxx namespace: enos resourceVersion: "297122" selfLink: /api/v1/namespaces/enos/services/vd-xxxx uid: 745b1906-0072-11ea-b132-000d3aa1f771 spec: clusterIP: 10.254.32.86 ports: - name: tcp-8080 port: 8080 protocol: TCP targetPort: 8080 selector: app: vd-xxxx sessionAffinity: None type: ClusterIP status: loadBalancer: {}
域名链接
3.1)创建 ingress
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-myapp namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: master.com http: paths: - backend: serviceName: myapp servicePort: 30080
3.2 ) 访问效果测试
[root@master ~]# kubectl get ingress NAME HOSTS ADDRESS PORTS AGE ingress-myapp master.com 80 10m [root@master ~]# kubectl describe ingress ingress-myapp ....... [root@master ~]# while true;do curl master.com:30080/hostname.html;sleep 1;done myapp-8w7bm myapp-n4xhs myapp-n4xhs myapp-8w7bm
三、资源获取(metrics-server)
1)安装metrics-server
https://github.com/baishuchao/kubernetes/tree/master/metrics-server [root@ks-master k8s]# git clone https://github.com/baishuchao/kubernetes.git [root@ks-master k8s]# cd kubernetes/metrics-server [root@ks-master metrics-server]#kubectl apply -f .
2)资源获取示例
[root@master dashboard]# kubectl top pods -n kube-system NAME CPU(cores) MEMORY(bytes) coredns-d5947d4b-45r9x 5m 15Mi coredns-d5947d4b-snsjt 6m 15Mi etcd-master 53m 145Mi kube-apiserver-master 83m 276Mi kube-controller-manager-master 23m 54Mi kube-flannel-ds-amd64-x6pcr 2m 16Mi kube-flannel-ds-amd64-xntt5 4m 18Mi kube-flannel-ds-amd64-zsq6r 4m 20Mi kube-proxy-v67qd 12m 20Mi kube-proxy-zg6ws 9m 20Mi kube-proxy-zkmgh 9m 24Mi kube-scheduler-master 2m 18Mi kubernetes-dashboard-b6dcb8878-ckcz5 0m 0Mi metrics-server-7579f696d8-kv8hs 1m 14Mi