1,创建文件index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>helloworld</title>
</head>
<body>
Hello World!
</body>
</html>
2,创建configmap,里面的index.html内容设为步骤1的index.html
kubectl create configmap index-html --from-file index.html
3,查看configmap index-html的内容
kubectl describe configmap index-html
内容如下
Name: index-html
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
index.html:
----
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>helloworld</title>
</head>
<body>
Hello World!
</body>
</html>
Events: <none>
4,创建deployment和service的yaml文件,sample-deploy-svc.yaml,内容如下
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: sample-volume
volumes:
- name: sample-volume
configMap:
name: index-html
---
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx-svc
namespace: default
spec:
ports:
- nodePort: 30080
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
type: NodePort
5,创建deployment和service
apply -f sample-deploy-svc.yaml
6,查看pod,service,deployment,replicaset。
kubectl get all
查看结果
NAME READY STATUS RESTARTS AGE
pod/nginx-deploy-8577cb6-2l4k8 1/1 Running 0 2m7s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-svc NodePort 10.96.253.239 <none> 80:30080/TCP 2m7s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deploy 1 1 1 1 2m9s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deploy-8577cb6 1 1 1 2m9s
7,访问nginx的index.html
curl http://<NODE_IP>:30080/index.html
结果为
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>helloworld</title>
</head>
<body>
Hello World!
</body>
</html>
说明index.html通过configmap成功发布到nginx容器里了。