| Version 7 (modified by , 2 years ago) ( diff ) |
|---|
You may continue to install Kubernetes on the vm you already installed docker. If you are installing this on a different machine make sure docker is already installed.
Installing kubeadm, kubelet, and kubectl:
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl vim git
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
Update the apt package index again:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
kubectl version --client
kubeadm version
Disable Swap Space
sudo swapoff -a
sudo sed -i.bak -r 's/(.+ swap .+)/#\1/' /etc/fstab
free -h
Install Container runtime
sudo tee /etc/modules-load.d/k8s.conf <<EOF overlay br_netfilter EOF
sudo modprobe overlay
sudo modprobe br_netfilter
sudo tee /etc/sysctl.d/kubernetes.conf<<EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF
sudo sysctl --system
sudo apt install -y containerd.io
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml
Both the container runtime and the kubelet have a property called "cgroup driver", which is essential for the management of cgroups on Linux machines.
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
systemctl status containerd
Initialize control plane
lsmod | grep br_netfilter
Output should similar to:
br_netfilter 22256 0 bridge 151336 1 br_netfilter
sudo systemctl enable kubelet
sudo kubeadm config images pull --cri-socket /run/containerd/containerd.sock
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --cri-socket /run/containerd/containerd.sock
You will see Your Kubernetes control-plane has initialized successfully!
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl cluster-info
wget https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
kubectl get pods -n kube-flannel
kubectl get nodes -o wide
kubectl get nodes
Scheduling Pods on Kubernetes Master Node
control-plane node to maximize resource usage.
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
Create a file simple-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
To create the Pod shown above, run the following command:
kubectl apply -f simple-pod.yaml
Pods are generally not created directly and are created using workload resources. See Working with Pods Links to an external site. for more information on how Pods are used with workload resources
Deploying a Simple Web Application on Kubernetes
A Deployment ensures that a specified number of pod replicas are running at any given time. Let's create a simple Deployment for a web application using the nginx image. Save the following YAML to a file named webapp-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
labels:
app: webapp
spec:
replicas: 2
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
A Service is an abstraction that defines a logical set of Pods and enables external traffic exposure, load balancing, and service discovery. For our web application, we'll use a NodePort service. Save the following YAML to a file named webapp-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: webapp-service
spec:
selector:
app: webapp
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30080
type: NodePort
Apply the Deployment and Service manifests:
kubectl apply -f webapp-deployment.yaml
kubectl apply -f webapp-service.yaml
Check the status of the Deployment and Service:
kubectl get deployments
kubectl get services
You should see your webapp-deployment with 2 replicas. Give it a time to take both replicas online.
Since we used a NodePort service, the web application should be accessible on node's IP at port 30080. If you're unsure of your node IPs, you can get them with:
kubectl get nodes -o wide
Then, in a web browser or using a tool like curl, access the web application:
curl http://<NODE_IP>:30080
You should see the default nginx welcome page, indicating that your web application is running. Part 4 Deploying WordPress and MySQL on Kubernetes
Installing dependancies: Download rancher.io/local-path storage class:
kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml
Check with kubectl get storageclass Make this storage class (local-path) the default:
kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
MySQL needs persistent storage to store its data. Save the following YAML to a file named mysql-pvc.yaml:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Apply the PVC:
kubectl apply -f mysql-pvc.yaml
Save the following YAML to a file named mysql-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "password"
- name: MYSQL_DATABASE
value: "wordpress"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
Apply the Deployment:
kubectl apply -f mysql-deployment.yaml
This will allow WordPress to communicate with MySQL. Save the following YAML to a file named mysql-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
selector:
app: mysql
ports:
- protocol: TCP
port: 3306
targetPort: 3306
Apply the Service:
kubectl apply -f mysql-service.yaml
Save the following YAML to a file named wordpress-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress:latest
env:
- name: WORDPRESS_DB_HOST
value: mysql
- name: WORDPRESS_DB_USER
value: "root"
- name: WORDPRESS_DB_PASSWORD
value: "password"
ports:
- containerPort: 80
Apply the Deployment:
kubectl apply -f wordpress-deployment.yaml
This will expose WordPress to external traffic. Save the following YAML to a file named wordpress-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
selector:
app: wordpress
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
Apply the Service:
kubectl apply -f wordpress-service.yaml
Since we used a NodePort service, WordPress should be accessible on node's IP at a dynamically allocated port above 30000. To find the NodePort assigned to WordPress:
kubectl get svc wordpress
Then, in a web browser with the ssh tunnel, access WordPress:
http://< INTERNAL-IP>:<NODE_PORT>
Part 5
Convert your Docker deployment into a Kubernetes deployment, you may compose your own service, deployment manifests as needed. Use the docker images you used previously when creating the pods/deployments.
Additional ref: https://kubebyexample.com/