目录
基础环境的配置
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 4d v1.18.2
node Ready <none> 4d v1.18.2
部署MySQL
[root@master mysql]# cat mysql.yaml
apiVersion: v1
kind: ReplicationController
metadata:
labels:
app: mysql
name: mysql
spec:
replicas: 1
selector:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql
name: mysql
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
部署MySQL service
[root@master mysql]# cat mysqls.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
selector:
app: mysql
ports:
- port: 3306
运行容器
[root@master mysql]# kubectl apply -f mysql.yaml
replicationcontroller/mysql created
[root@master mysql]# kubectl apply -f mysqls.yaml
service/mysql created
MySQL运行成功
[root@master mysql]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d23h
mysql ClusterIP 10.97.2.190 <none> 3306/TCP 20s
[root@master mysql]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-vzgzp 1/1 Running 0 49s
部署Tmocat
[root@master myweb]# cat tomcat.yaml
apiVersion: v1 //描述对象的版本
kind: ReplicationController //要声明的对象名
metadata: //对RC对象描述信息
name: myweb
labels: //标签名用于selector来引用
app: myweb
spec: //对RC对象的具体描述
replicas: 1 //副本数
selector: //引用了labels创建的标签用于连接
app: myweb
template:
metadata:
labels:
app: myweb
spec:
containers:
- image: tomcat //拉取镜像
name: tomcat
ports:
- containerPort: 8080 //容器端口
env: //给容器设置环境变量,与mysql连接
- name: MYSQL_SERVCE_HOST
value: "mysql"
创建Tomcat service
[root@master myweb]# cat tomcats.yaml
apiVersion: v1
kind: Service
metadata:
name: myweb
spec:
selector:
app: myweb
ports:
- port: 8080
name: myweb
nodePort: 30001
type: NodePort
运行Tomcat容器
[root@master myweb]# kubectl apply -f tomcat.yaml
replicationcontroller/myweb created
[root@master myweb]# kubectl apply -f tomcats.yaml
service/myweb created
查看是否运行成功(拉取镜像需要等一会)
[root@master myweb]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-vzgzp 1/1 Running 0 20m
myweb-snmv8 1/1 Running 0 30s
查看映射的口号
[root@master myweb]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d23h
mysql ClusterIP 10.97.2.190 <none> 3306/TCP 19m
myweb NodePort 10.103.232.170 <none> 8080:30001/TCP 35s
浏览器访问 http://主机ip:30001