單節點k8s的一個小例子 webapp+mysql

安裝kubernetes
準備一臺centos7
1) 關閉firewalld 和 selinux

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

2)安裝etcd和kubernetes

yum install -y etcd kubernetes

3)修改配置文件

vi /etc/sysconfig/docker

將--selinux-enabled 改成 --selinux-enabled=false --insecure-registry gcr.io

vi /etc/kubernetes/apiserver

把--admission_control參數中的ServiceAccount刪除

4)準備工做

yum install python-rhsm-certificates
若是提示python-rhsm-certificates-1.19.10-1.el7_4.x86_64 被已安裝的 subscription-manager-rhsm-certificates-
1.20.11-1.el7.centos.x86_64 取代
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-
1.el7_4.x86_64.rpm
rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm |cpio -iv --to-stdout ./etc/r hsm/ca/redhat-
uep.pem > /etc/r hsm/ca/redhat-uep.pem

配置docker加速器

vi /etc/docker/daemon.json//加入以下內容
{
"registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}

5)按順序啓動全部服務

for s in etcd docker kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy
do
systemctl start $s
done

6)建立一個rc文件html

vim mysql-rc.yaml node

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"

  

docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
docker pull mysql:5.6

kubectl create -f mysql-rc.yaml
kubectl get rc
kubectl get pods python


7)建立一個svc文件 mysql


 vim mysql-svc.yaml linux

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
    - port: 3306
  selector:
    app: mysql

  


kubectl create -f mysql-svc.yaml
kubectl get svc

8)建立web rc文件

vim web-rc.yaml web

kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 1
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
        - name: myweb
          image: kubeguide/tomcat-app:v1
          ports:
          - containerPort: 8080
          env:
          - name: MYSQL_SERVICE_HOST
            value: '10.254.170.174' #這裏的IP須要經過kubectl get svc 查看mysql的cluster ip
          - name: MYSQL_SERVICE_PORT
            value: '3306'

  

[root@weifeng04 ~]# kubectl get svc
NAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   10.254.0.1       <none>        443/TCP          3d
mysql        10.254.170.174   <none>        3306/TCP         3d

  


kubectl create -f web-rc.yaml

9)建立web svc文件

vim web-svc.yamly sql

kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30001
  selector:
    app: myweb

  

kubectl create -f web-svc.yaml

10)訪問
iptables -P FORWARD ACCEPT docker


curl 本機ip:30001/demo/ 或瀏覽器json

[root@weifeng04 ~]# curl 192.168.20.99:30001/demo/

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HPE University Docker&Kubernetes Learning</title>
</head>
<body  align="center">

	
      <h2>Congratulations!!</h2>
     <br></br>
	 <input type="button" value="Add..." onclick="location.href='input.html'" >
	     <br></br>
      <TABLE align="center"  border="1" width="600px">
   <TR>

  

相關文章
相關標籤/搜索