Kubernetes集羣組件:node
節點 | IP地址 |
---|---|
master | 10.10.10.14 |
node1 | 10.10.10.15 |
node2 | 10.10.10.16 |
更改Hostname爲 master、node一、node2,配置全部測試機的/etc/hosts文件nginx
[root@master ~]# cat /etc/hosts 10.10.10.14 master etcd node14 10.10.10.15 node1 node15 10.10.10.16 node2 node16
關閉CentOS7自帶的防火牆服務docker
系統初始化安裝(全部主機)-選擇【最小化安裝】,而後yum update,升級到最新版本
yum -y install epel-release
yum updatejson
[root@master ~]#vim
yum install -y etcd kubernetes-master ntp flannel
[root@node1 ~]#centos
yum install -y kubernetes-node ntp flannel docker
時間校對
全部主機api
systemctl start ntpd;systemctl enable ntpd ntpdate ntp1.aliyun.com hwclock -w
[root@master ~]# grep -v '^#' /etc/etcd/etcd.conf bash
ETCD_NAME=default ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://10.10.10.14:2379" ETCD_ADVERTISE_CLIENT_URLS="http://10.10.10.14:2379"
啓動服務服務器
systemctl start etcd;systemctl enable etcd
檢查etcd cluster狀態網絡
[root@master ~]# etcdctl cluster-health member 8e9e05c52164694d is healthy: got healthy result from http://10.10.10.14:2379 cluster is healthy
檢查etcd集羣成員列表,這裏只有一臺
[root@master ~]# etcdctl member list 8e9e05c52164694d: name=default peerURLs=http://localhost:2380 clientURLs=http://10.10.10.14:2379 isLeader=true
[root@master ~]# grep -v '^#' /etc/kubernetes/config
KUBE_LOGTOSTDERR="--logtostderr=true" KUBE_LOG_LEVEL="--v=0" KUBE_ALLOW_PRIV="--allow-privileged=false" KUBE_MASTER="--master=http://10.10.10.14:8080"
[root@master ~]# grep -v '^#' /etc/kubernetes/apiserver
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0" KUBE_ETCD_SERVERS="--etcd-servers=http://10.10.10.14:2379" KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" KUBE_ADMISSION_CONTROL="--admission-control=AlwaysAdmit" KUBE_API_ARGS=""
[root@master ~]# grep -v '^#' /etc/kubernetes/controller-manager
KUBE_CONTROLLER_MANAGER_ARGS=""
[root@master ~]# grep -v '^#' /etc/kubernetes/scheduler
KUBE_SCHEDULER_ARGS="--address=0.0.0.0"
for i in kube-apiserver kube-controller-manager kube-scheduler;do systemctl restart $i; systemctl enable $i;done
[root@master ~]# etcdctl set /atomic.io/network/config '{"Network": "172.16.0.0/16"}' {"Network": "172.16.0.0/16"}
[root@node1 ~]# grep -v '^#' /etc/sysconfig/flanneld FLANNEL_ETCD_ENDPOINTS="http://10.10.10.14:2379" FLANNEL_ETCD_PREFIX="/atomic.io/network" FLANNEL_OPTIONS=""
查看驗證網絡信息
[root@master ~]# etcdctl get /atomic.io/network/config { "Network": "172.16.0.0/16" } [root@master ~]# etcdctl ls /atomic.io/network/subnets /atomic.io/network/subnets/172.16.69.0-24 /atomic.io/network/subnets/172.16.6.0-24 [root@master ~]# etcdctl get /atomic.io/network/subnets/172.16.6.0-24 {"PublicIP":"10.10.10.15"} [root@master ~]# etcdctl get /atomic.io/network/subnets/172.16.69.0-24 {"PublicIP":"10.10.10.16"}
[root@node1 ~]# grep -v '^#' /etc/kubernetes/config KUBE_LOGTOSTDERR="--logtostderr=true" KUBE_LOG_LEVEL="--v=0" KUBE_ALLOW_PRIV="--allow-privileged=false" KUBE_MASTER="--master=http://10.10.10.14:8080"
[root@node1 ~]# grep -v '^#' /etc/kubernetes/proxy KUBE_PROXY_ARGS="--bind=address=0.0.0.0" [root@node1 ~]#
[root@node1 ~]# grep -v '^#' /etc/kubernetes/kubelet KUBELET_ADDRESS="--address=127.0.0.1" KUBELET_HOSTNAME="--hostname-override=10.10.10.15" KUBELET_API_SERVER="--api-servers=http://10.10.10.14:8080" KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest" KUBELET_ARGS=""
for i in flanneld kube-proxy kubelet docker;do systemctl restart $i;systemctl enable $i;systemctl status $i ;done
node2與node1配置基本一致,除下面一處例外
[root@node2 ~]# vi /etc/kubernetes/kubelet KUBELET_HOSTNAME="--hostname-override=10.10.10.16"
[root@master ~]# kubectl get nodes NAME STATUS AGE 10.10.10.15 Ready 18h 10.10.10.16 Ready 13h
k8s支持2種方式,一種是直接經過命令參數的方式,另外一種是經過配置文件的方式,配置文件的話支持json和yaml
kubectl run nginx --image=nginx --port=80 --replicas=2
建立成功可是kubectl get pods 沒有結果
提示信息:no API token found for service account default
解決辦法:編輯/etc/kubernetes/apiserver 去除 KUBE_ADMISSION_CONTROL中的SecurityContextDeny,ServiceAccount,並重啓kube-apiserver.service服務
pod-infrastructure:latest鏡像下載失敗
報錯信息:image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request.
解決方案:yum install *rhsm* -y
登錄容器報錯
[root@node14 ~]# kubectl exec -it nginx-bl7lc /bin/bash
Error from server: error dialing backend: dial tcp 10.10.10.16:10250: getsockopt: connection refused
解決方法:
10250是kubelet的端口。
在Node上檢查/etc/kubernetes/kubelet
KUBELET_ADDRESS須要修改成node ip
[root@master log]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-3449338310-h6l9d 1/1 Running 0 6m nginx-3449338310-n4grl 1/1 Running 0 6m [root@master log]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx 2 2 2 2 13m
[root@node14 log]# kubectl expose deployment nginx --port=80 --type=LoadBalancer service "nginx" exposed
expose命令將會建立一個service,將本地(某個節點上)的一個隨機端口關聯到容器中的80端口。
可使用如下命令來查service:
[root@node14 log]# kubectl get service NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 21h nginx 10.254.160.227 <pending> 80:30255/TCP 7s
外網訪問:
[root@node16 ~]# elinks --dump http://10.10.10.16:30255 Welcome to nginx!
[root@node14 log]# kubectl delete deployment nginx deployment "nginx" deleted [root@node14 log]# kubectl delete service nginx service "nginx" deleted
[root@node14 ~]# vim nginx-pod.yaml
apiVersion: v1 kind: Pod metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx imagePullPolicy: IfNotPresent ports: - containerPort: 80 restartPolicy: Always
[root@node14 ~]# kubectl create -f nginx-pod.yaml pod "nginx" created
[root@node14 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 16s
[root@node14 ~]# vim nginx-svc.yaml
apiVersion: v1 kind: Service metadata: name: nginx-service spec: type: NodePort sessionAffinity: ClientIP selector: app: nginx ports: - port: 80 nodePort: 30080
[root@node14 ~]# kubectl create -f nginx-svc.yaml service "nginx-service" created
[root@node14 ~]# kubectl get service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 23h nginx-service 10.254.154.111 <nodes> 80:30080/TCP 20s
[root@node16 log]# elinks --dump http://10.10.10.16:30080 Welcome to nginx!