测试用例
查看集群状态:
[root@localhost ~]# kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-2 Healthy {"health": "true"}
etcd-0 Healthy {"health": "true"}
etcd-1 Healthy {"health": "true"}
利用k8s安装nginx
其中 --image=nginx代表镜像为nginx,–replicas=3代表副本数为3
kubectl run nginx --image=nginx --replicas=3
查看运行的容器状态
[root@localhost ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-8586cf59-57z8r 1/1 Running 0 8m
nginx-8586cf59-95ld2 1/1 Running 0 8m
nginx-8586cf59-w9g6q 1/1 Running 0 8m
查看运行的容器在集群中的节点信息:
[root@localhost ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-8586cf59-57z8r 1/1 Running 0 36m 172.17.96.3 192.168.1.8
nginx-8586cf59-95ld2 1/1 Running 0 36m 172.17.41.3 192.168.1.7
nginx-8586cf59-w9g6q 1/1 Running 0 36m 172.17.96.2 192.168.1.8
将容器的端口暴露出来
kubectl expose deployment nginx --port=88 --target-port=80 --type=NodePort
查看容器的ip与端口
[root@localhost ~]# kubectl get svc nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.10.10.106 <none> 88:31721/TCP 23m
此时我们可以通过
curl 10.10.10.106:88 来访问
浏览器访问:192.168.1.7/8:31721 都能访问
如果不能访问看看自己的网络是否正常
cd /opt/kubernetes/ssl
[root@localhost ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.1.6:2379,https://192.168.1.7:2379,https://192.168.1.8:2379" ls /coreos.com/network/subnets
/coreos.com/network/subnets/172.17.98.0-24
/coreos.com/network/subnets/172.17.55.0-24
/coreos.com/network/subnets/172.17.37.0-2
部署Web UI (Dashboard)
创建三个yaml文件
mkdir /opt/kubernetes/ui
cd /opt/kubernetes/ui
创建权限模块:
[root@localhost ui]# cat dashboard-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
addonmanager.kubernetes.io/mode: Reconcile
name: kubernetes-dashboard
namespace: kube-system
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: kubernetes-dashboard-minimal
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
addonmanager.kubernetes.io/mode: Reconcile
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kube-system
创建ui控制器:
注意此处的镜像可以通过:https://dev.aliyun.com 获得,直接搜索kubernetes-dashboard-amd64 就能得到镜像地址了
[root@localhost ui]# cat dashboard-deployment.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: kubernetes-dashboard
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
spec:
selector:
matchLabels:
k8s-app: kubernetes-dashboard
template:
metadata:
labels:
k8s-app: kubernetes-dashboard
annotations:
scheduler.alpha.kubernetes.io/critical-pod: ''
spec:
serviceAccountName: kubernetes-dashboard
containers:
- name: kubernetes-dashboard
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.0
resources:
limits:
cpu: 100m
memory: 300Mi
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 9090
protocol: TCP
livenessProbe:
httpGet:
scheme: HTTP
path: /
port: 9090
initialDelaySeconds: 30
timeoutSeconds: 30
tolerations:
- key: "CriticalAddonsOnly"
operator: "Exists"
创建展示ui的配置:
[root@localhost ui]# cat dashboard-service.yaml
apiVersion: v1
kind: Service
metadata:
name: kubernetes-dashboard
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
spec:
type: NodePort
selector:
k8s-app: kubernetes-dashboard
ports:
- port: 80
targetPort: 9090
启动运行ui界面:
# kubectl create -f .
或者
# kubectl create -f dashboard-rbac.yaml
# kubectl create -f dashboard-deployment.yaml
# kubectl create -f dashboard-service.yaml
测试:
kubectl get all -n kube-system
通过最后一行我们可以观察到ip与端口
[root@master ui]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard NodePort 10.0.0.230 <none> 80:37970/TCP 83s
[root@localhost ui]# kubectl get ns
NAME STATUS AGE
default Active 5d
kube-public Active 5d
kube-system Active 5d
在页面启动:
http://192.168.1.7:37970 或者http://192.168.1.8:37970,可以看到类似的界面